在 Rocky Linux 中配置 eth0 网络接口并设置开机自动启动,可以通过 NetworkManager 或 传统 ifcfg 文件 进行配置。
1. 确认网卡名称
首先检查网卡是否真的叫 eth0(现代 Linux 可能使用 ens33、enp0s3 等):
或
如果显示的是 ensXX 而不是 eth0,可以:
- 使用实际网卡名(如
ens33),或
- 修改为传统命名方式
eth0
2. 强制使用 eth0命名
如果系统默认使用 ens33 等新命名方式,可以改回 eth0:
1.修改 GRUB 配置**
在 GRUB_CMDLINE_LINUX 行添加:
1
| GRUB_CMDLINE_LINUX="... net.ifnames=0 biosdevname=0"
|
2.重新生成 GRUB 配置
1
| grub2-mkconfig -o /boot/grub2/grub.cfg
|
3.重启生效
重启后,网卡名会变成 eth0。
3. 确保 NetworkManager 启用
Rocky Linux 默认使用 NetworkManager,确保它正在运行:
1
| systemctl enable --now NetworkManager
|
4. 配置 eth0 开机自动连接
方法 1:使用 nmcli
- 查看当前连接
- 修改
eth0 确保开机启动
1 2 3 4 5 6 7 8
| nmcli connection modify eth0 connection.autoconnect yes
nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24 nmcli connection modify eth0 ipv4.gateway 192.168.1.1 nmcli connection modify eth0 ipv4.dns "8.8.8.8,8.8.4.4" nmcli connection modify eth0 ipv4.method manual nmcli connection up eth0
|
- 启用连接
1
| nmcli connection up eth0
|
方法 2:使用 nmtui(交互式界面)
选择:
- Edit a connection → 选择
eth0(或你的网卡名)
- 勾选 Automatically connect
- 保存(
OK)并退出。
5. 手动配置 /etc/sysconfig/network-scripts/ifcfg-eth0(传统方式)**
如果仍然使用 ifcfg 文件(Rocky Linux 8/9 仍兼容):
- 编辑配置文件**
1
| vi /etc/sysconfig/network-scripts/ifcfg-eth0
|
- 确保
ONBOOT=yes**
1 2 3 4 5 6 7
| DEVICE=eth0 BOOTPROTO=dhcp ONBOOT=yes IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=8.8.8.8
|
- 重启网络**
1 2
| nmcli connection reload systemctl restart NetworkManager
|
或(如果使用旧版 network 服务):
1
| systemctl restart network
|
6. 验证网络和开机启动
1 2 3
| ip a ping google.com systemctl is-enabled NetworkManager
|