rocky Linux 中配置 eth0网络接口

在 Rocky Linux 中配置 eth0 网络接口并设置开机自动启动,可以通过 NetworkManager传统 ifcfg 文件 进行配置。


1. 确认网卡名称

首先检查网卡是否真的叫 eth0(现代 Linux 可能使用 ens33enp0s3 等):

1
ip a

1
nmcli device status

如果显示的是 ensXX 而不是 eth0,可以:

  • 使用实际网卡名(如 ens33),或
  • 修改为传统命名方式 eth0

2. 强制使用 eth0命名

如果系统默认使用 ens33 等新命名方式,可以改回 eth0

1.修改 GRUB 配置**

1
vi /etc/default/grub

GRUB_CMDLINE_LINUX 行添加:

1
GRUB_CMDLINE_LINUX="... net.ifnames=0 biosdevname=0"

2.重新生成 GRUB 配置

1
grub2-mkconfig -o /boot/grub2/grub.cfg

3.重启生效

1
reboot

重启后,网卡名会变成 eth0

3. 确保 NetworkManager 启用

Rocky Linux 默认使用 NetworkManager,确保它正在运行:

1
systemctl enable --now NetworkManager

4. 配置 eth0 开机自动连接

方法 1:使用 nmcli

  1. 查看当前连接
1
nmcli connection show
  1. 修改 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. 启用连接
1
nmcli connection up eth0

方法 2:使用 nmtui(交互式界面)

1
nmtui

选择:

  1. Edit a connection → 选择 eth0(或你的网卡名)
  2. 勾选 Automatically connect
  3. 保存(OK)并退出。

5. 手动配置 /etc/sysconfig/network-scripts/ifcfg-eth0(传统方式)**

如果仍然使用 ifcfg 文件(Rocky Linux 8/9 仍兼容):

  1. 编辑配置文件**
1
vi /etc/sysconfig/network-scripts/ifcfg-eth0
  1. 确保 ONBOOT=yes**
1
2
3
4
5
6
7
DEVICE=eth0
BOOTPROTO=dhcp # 或 static(静态 IP)
ONBOOT=yes # 关键!确保开机启动
IPADDR=192.168.1.100 # 如果是静态 IP
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
  1. 重启网络**
1
2
nmcli connection reload  # 重新加载配置
systemctl restart NetworkManager

或(如果使用旧版 network 服务):

1
systemctl restart network

6. 验证网络和开机启动

1
2
3
ip a                   # 检查 IP 是否正常
ping google.com # 测试网络连通性
systemctl is-enabled NetworkManager # 确保 NetworkManager 开机启动