系统版本为:almalinux9.2
通过nmcli命令配置网络信息,此命令由 NetworkManager 包提供。
# 查看当前配置文件,从RHEL9.0开始,以key-file格式保存在下面路径中 [root@localhost ~]# cat /etc/NetworkManager/system-connections/ens192.nmconnection [connection] id=ens192 uuid=c887eead-1da4-31f7-9f28-3277c4a72774 type=ethernet autoconnect-priority=-999 interface-name=ens192 timestamp=1694517980 [ethernet] [ipv4] method=auto [ipv6] addr-gen-mode=eui64 method=auto [proxy]
通过method=auto判断现在是自动获取的状态。改为固定ip。
# 配置网卡ip,ens192为网卡名 [root@localhost ~]# nmcli connection modify ens192 ipv4.method manual ipv4.addresses 192.168.12.240/24 ipv4.gateway 192.168.12.1 ipv4.dns 114.114.114.114,8.8.8.8 connection.autoconnect yes [root@localhost ~]# cat /etc/NetworkManager/system-connections/ens192.nmconnection [connection] id=ens192 uuid=c887eead-1da4-31f7-9f28-3277c4a72774 type=ethernet autoconnect-priority=-999 interface-name=ens192 timestamp=1694662356 [ethernet] [ipv4] address1=192.168.12.240/24,192.168.12.1 dns=114.114.114.114;8.8.8.8; method=manual [ipv6] addr-gen-mode=eui64 method=auto [proxy]
# 添加额外的ip,单网卡多ip。 [root@localhost ~]# nmcli connection modify ens192 +ipv4.addresses 192.168.89.11/24 [root@localhost ~]# nmcli connection modify ens192 +ipv4.addresses 192.168.89.12/24 [root@localhost ~]# nmcli connection modify ens192 +ipv4.addresses 192.168.89.13/24 [root@localhost ~]# cat /etc/NetworkManager/system-connections/ens192.nmconnection [connection] id=ens192 uuid=c887eead-1da4-31f7-9f28-3277c4a72774 type=ethernet autoconnect-priority=-999 interface-name=ens192 timestamp=1694662356 [ethernet] [ipv4] address1=192.168.12.240/24,192.168.12.1 address2=192.168.89.11/24 address3=192.168.89.12/24 address4=192.168.89.13/24 dns=114.114.114.114;8.8.8.8; method=manual [ipv6] addr-gen-mode=eui64 method=auto [proxy]
# 激活配置 [root@localhost ~]# nmcli connection up ens192
# 删除一个ip [root@localhost ~]# nmcli connection modify ens192 -ipv4.addresses 192.168.89.13/24 [root@localhost ~]# cat /etc/NetworkManager/system-connections/ens192.nmconnection [connection] id=ens192 uuid=c887eead-1da4-31f7-9f28-3277c4a72774 type=ethernet autoconnect-priority=-999 interface-name=ens192 timestamp=1694669933 [ethernet] [ipv4] address1=192.168.12.240/24,192.168.12.1 address2=192.168.89.11/24 address3=192.168.89.12/24 dns=114.114.114.114;8.8.8.8; method=manual [ipv6] addr-gen-mode=eui64 method=auto [proxy]
# 另外仍然支持ifcfg-配置文件形式(但不建议) [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens192 TYPE=Ethernet Device=ens192 IPADDR=192.168.12.240 NETMASK=255.255.255.0 GATEWAY=192.168.12.1 DNS1=114.114.114.114 ONBOOT=yes BOOTPROTO=static # 通过systemctl重启无效 [root@localhost ~]# systemctl restart NetworkManager # 需要下面方式重启 [root@localhost ~]# nmcli connection reload [root@localhost ~]# nmcli networking off [root@localhost ~]# nmcli networking on
原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/5320.html