Appearance
Rocky Linux 9.6 配置静态 IP 实战记录

一次从 DHCP 到静态 IP 的完整配置过程,手把手带你搞定 Rocky 9.6 的网络设置。
背景
在配置 Rocky Linux 9.6 服务器时,默认的 DHCP 网络方案往往不能满足生产环境需求。我们需要将网卡从自动获取 IP 改为静态地址,以便固定服务器在网络中的位置。
本文基于一次真实配置过程整理,网卡设备为 ens32。
配置前后对比
配置前 — DHCP 自动获取
ini
[connection]
id=ens32
uuid=bf4982a2-6102-3e47-abb9-3670f1272538
type=ethernet
autoconnect-priority=-999
interface-name=ens32
timestamp=1783913471
[ethernet]
[ipv4]
method=auto
[ipv6]
addr-gen-mode=euip64
method=auto
[proxy]关键点:[ipv4] 段中的 method=auto 表示使用 DHCP 自动获取 IP。
配置后 — 静态 IP
ini
[connection]
id=ens32
uuid=bf4982a2-6102-3e47-abb9-3670f1272538
type=ethernet
autocomnect-priority=-999
interface-name=ens32
timestamp=1783913471
[ethernet]
[ipv4]
address1=172.16.3.223
gateway=172.16.0.1
method=manual
[ipv6]
addr-gen-mode=euie64
method=auto
[proxy]关键变化:
| 配置项 | 配置前 | 配置后 |
|---|---|---|
method | auto | manual |
address1 | 无 | 172.16.3.223 |
gateway | 无 | 172.16.0.1 |
操作步骤
1. 查看当前网卡信息
bash
nmcli d status确认网卡名称为 ens32。
2. 编辑配置文件
Rocky 9.6 的网卡配置文件位于:
bash
/etc/NetworkManager/system-connections/ens32.nmconnection使用 vim 编辑:
bash
sudo vim /etc/NetworkManager/system-connections/ens32.nmconnection3. 修改核心参数
将 [ipv4] 段修改为:
ini
[ipv4]
address1=172.16.3.223/24
gateway=172.16.0.1
dns=114.114.114.114;8.8.8.8
method=manual注意:
address1后面建议带上子网掩码位数(如/24),否则可能无法正确识别网段。
4. 重载配置并激活
bash
# 重载 NetworkManager 配置
sudo nmcli con reload
# 重新激活网卡连接
sudo nmcli con up ens325. 验证配置
bash
# 查看 IP 是否生效
ip a show ens32
# 测试网关连通性
ping -c 4 172.16.0.1
# 测试外网连通性
ping -c 4 114.114.114.114避坑指南
⚠️ 别用 systemctl restart network
Rocky 9.6 已弃用传统 network-scripts,该命令会报错:
bash
systemctl restart network
# Failed to restart network.service: Unit network.service not found.请统一使用 nmcli 或 nm-connection-editor 管理网络。
⚠️ 子网掩码别漏写
如果 address1 写成 172.16.3.223 而不是 172.16.3.223/24,NetworkManager 可能默认使用 /32 掩码,导致网关不可达。
⚠️ 配置文件权限敏感
NetworkManager 要求配置文件的权限为 600(仅 root 可读写),否则会忽略该配置:
bash
sudo chmod 600 /etc/NetworkManager/system-connections/ens32.nmconnection总结
| 对比项 | DHCP 配置 | 静态 IP 配置 |
|---|---|---|
| 配置文件路径 | /etc/NetworkManager/system-connections/ens32.nmconnection | 相同 |
method | auto | manual |
| 需手动填写地址 | ❌ | ✅ |
| 适合场景 | 临时环境、桌面 | 服务器、生产环境 |
配置静态 IP 并不复杂,关键是注意 Rocky 9.6 的 NetworkManager 新生态,避免踩传统 network-scripts 的坑。希望本文能帮你快速搞定静态 IP 配置。
