重启 CentOS 7 系统后的 IP 地址问题

时间:2022-07-26
本文章向大家介绍重启 CentOS 7 系统后的 IP 地址问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在配置 Zookeeper 的时候,我重启了 CentOS 7,发现使用 XShell 远程连接系统(主机:192.168.186.128)超时。

对于操作系统和写代码时候出现的各种 BUG 和意外,我早已经习惯;因为我知道,在有限的时间内,都可以被解决。

然后,我直接进入系统页面,打开命令行输入 ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.186.130  netmask 255.255.255.0  broadcast 192.168.186.255
        inet6 fe80::20c:29ff:fe1a:6c13  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1a:6c:13  txqueuelen 1000  (Ethernet)
        RX packets 636  bytes 48167 (47.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 237  bytes 26851 (26.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

根据之前的系统快照备份,可以得到之前的信息为:

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.186.128  netmask 255.255.255.0  broadcast 192.168.186.255
        inet6 fe80::20c:29ff:fed8:e9b9  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:e9:b9  txqueuelen 1000  (Ethernet)
        RX packets 1370575  bytes 1933632481 (1.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 360561  bytes 85054851 (81.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 19044  bytes 38146469 (36.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19044  bytes 38146469 (36.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

由此可见,重启之后主机地址由 192.168.186.128 变为了 192.168.186.130,原来的 eno16777736 也变成了 ens33

然后,我尝试执行重启网络的命令 service network restart 或者 systemctl restart network,发现报错:

Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.

为了查看详细错误信息,执行命令 cat /var/log/messages | grep network,发现了错误所在:

...
Sep 25 10:55:30 localhost network: [FAILED]
Sep 25 10:55:30 localhost NetworkManager[16202]: <info>  renaming /etc/sysconfig/network-scripts/ifcfg-eno16777736 -> /etc/sysconfig/network-scripts/ifcfg-ens33
Sep 25 10:55:30 localhost network: Bringing up interface ens33:  Error: no device found for connection 'eno16777736'.
...

错误提示是加载网卡 eno16777736 失败。

于是执行命令 cd /etc/sysconfig/network-scripts/,进入存放网络配置的文件夹。

查看目录下的文件发现:只存在原来的网卡 eno16777736 对应的配置文件 ifcfg-eno16777736,但是没有网卡 ens33 对应的配置文件。所以推断出,重启之后,系统把原来的网卡删除了,然后启用了新的网卡有了新的 IP 地址。为此,解决方案是,更换网卡配置文件并重新配置 IP 地址为旧的 IP 地址,并重启网络。

首先执行命令 cp ifcfg-eno16777736 ifcfg-ens33,复制文件。

然后,执行命令编辑文件 vim ifcfg-ens33;把其中的网卡名字由原来的 eno16777736 更换为 ens33,同时确保 IP 地址是原来的地址:

IPADDR=192.168.186.128
NAME=ens33
DEVICE=ens33

同时执行命令删除无效网卡的配置文件 rm ifcfg-eno16777736

然后执行重启网络的命令 systemctl restart network,接着执行 ifconfig 可以发现 IP 地址成功更换为原来的 IP 地址了:

[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.186.128  netmask 255.255.255.0  broadcast 192.168.186.255
        inet6 fe80::20c:29ff:fe1a:6c13  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1a:6c:13  txqueuelen 1000  (Ethernet)
        RX packets 777  bytes 57063 (55.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 280  bytes 31690 (30.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 4  bytes 352 (352.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 352 (352.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

如果重启一次没有报错但是 IP 地址没有改变,那就再执行一次重启,以防止缓存问题。

重新使用 XShell 连接系统,第一次重新尝试连接超时的话,就关掉再来一次,同样防止缓存问题。

最后,重启系统,发现 IP 地址和网卡都正常,没有变更,可以正常地使用 XShell 远程访问 CentOS 7 系统。