最近安装了 CentOS Stream9 系统,发现修改 IP 地址与以前的 CentOS 有所区别。于是利用该文件进行记录,方便以后查阅。
需要将 CentOS Stream9 的 IP 地址从自动设置为手动方式。IP信息如下:
IP 地址:192.168.116.51
子网掩码:255.255.255.0
网关:192.168.51.2
DNS:192.168.51.2
(1)进入到 /etc/NetworkManager/system-connections/ 目录,命令如下:
[root@localhost ~]# cd /etc/NetworkManager/system-connections/
[root@localhost system-connections]# ll
total 4
-rw-------. 1 root root 288 Apr 2 22:13 ens33.nmconnection(2)使用 vim 编辑 ens33.nmconnection 文件,命令如下:
[connection]
id=ens33
uuid=54fa998a-7f1b-3b92-94fa-7309f3b4adff
type=ethernet
autoconnect-priority=-999
interface-name=ens33
timestamp=1648933602
[ethernet]
[ipv4]
method=manual
address1=192.168.116.51/24,192.168.116.2
dns=192.168.116.2
[ipv6]
addr-gen-mode=eui64
method=auto
[proxy]上面配置中,method 设置为 auto 表示自动IP,manual 表示手动IP;address1 用来配置 IP 地址;dns 用来配置 DNS;
注意:
上面配置的 192.168.116.51/24 IP地址后面的 24 是什么意思?代表当前的 IP 为192.168.116.51,后面的24代表子网掩码掩前24位,二进制为:
11111111 11111111 11111111 00000000
将上面二进制换算成十进制就是 255.255.255.0
(3)重新加载配置文件,命令如下:
[root@localhost ~]# nmcli c reload(4)重启ens33网卡,命令如下:
[root@localhost ~]# nmcli c up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)(5)使用 ifconfig 检查配置的 IP 地址是否有效,命令如下:
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.116.51 netmask 255.255.255.0 broadcast 192.168.116.255
inet6 fe80::20c:29ff:fed2:8667 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d2:86:67 txqueuelen 1000 (Ethernet)
RX packets 874 bytes 68155 (66.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 590 bytes 62866 (61.3 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 1000 (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(6)使用 ping 验证网络是否可用,命令如下:
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=128 time=39.7 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=128 time=39.8 ms
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=128 time=39.7 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2006ms
rtt min/avg/max/mdev = 39.663/39.707/39.773/0.047 ms到这里,IP 修改成功了……