一、前言:
centos已经死了,作为用户量最大的RHEL衍生发行版,在2022年1月1日结束了centos8生命周期;而到2024年6月centos7支持结束之前,时间会给我们一个最合格的centos继任者,在此,我的选择是Alma linux。
二、centos8/alma8网络管理命令变化:
centos8/alma8系统,弃用.NETwork.service,采用NetworkManager(NM)网络管理器,因此命令为nmcli。
三、操作命令:
查看当前网络信息
[root@alma8 ~]# nmcli
ens224: connected to ens224
"VMware VMXNET3"
ethernet (vmxnet3), 00:50:56:8E:02:B1, hw, mtu 1500
inet4 192.168.200.10/24
route4 192.168.200.0/24
inet6 fe80::9c44:37c2:db12:5fcd/64
route6 fe80::/64
lo: unmanaged
"lo"
loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536
DNS configuration:
servers: 192.168.200.1 192.168.200.2
domains: 9sb.org
interface: ens224
重新加载网卡配置文件
[root@alma8 ~]# nmcli connection reload
停止网卡、激活网卡
激活不仅是启动网卡,还会重新加载网卡配置文件
[root@alma8 ~]# nmcli connection down ens224
Connection 'ens224' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@alma8 ~]# nmcli connection up ens224
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
查看全部网卡信息
[root@alma8 ~]# nmcli device show
GENERAL.DEVICE: ens224
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:50:56:8E:02:B1
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: ens224
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/5
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 192.168.200.10/24
IP4.GATEWAY: --
IP4.ROUTE[1]: dst = 192.168.200.0/24, nh = 0.0.0.0, mt = 101
IP4.DNS[1]: 192.168.200.1
IP4.DNS[2]: 192.168.200.2
IP4.DOMAIN[1]: 9sb.org
IP6.ADDRESS[1]: fe80::9c44:37c2:db12:5fcd/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 101
GENERAL.DEVICE: lo
GENERAL.TYPE: loopback
GENERAL.HWADDR: 00:00:00:00:00:00
GENERAL.MTU: 65536
GENERAL.STATE: 10 (unmanaged)
GENERAL.CONNECTION: --
GENERAL.CON-PATH: --
IP4.ADDRESS[1]: 127.0.0.1/8
IP4.GATEWAY: --
IP6.ADDRESS[1]: ::1/128
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = ::1/128, nh = ::, mt = 256
查看单个网卡信息
[root@alma8 ~]# nmcli device show ens224
GENERAL.DEVICE: ens224
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:50:56:8E:02:B1
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: ens224
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/5
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 192.168.200.10/24
IP4.GATEWAY: --
IP4.ROUTE[1]: dst = 192.168.200.0/24, nh = 0.0.0.0, mt = 101
IP4.DNS[1]: 192.168.200.1
IP4.DNS[2]: 192.168.200.2
IP4.DOMAIN[1]: 9sb.org
IP6.ADDRESS[1]: fe80::9c44:37c2:db12:5fcd/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 101
查看网卡设备状态
[root@alma8 ~]# nmcli device status
DEVICE TYPE STATE CONNECTION
ens224 ethernet connected ens224
lo loopback unmanaged --
查看网卡信息
[root@alma8 ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens224 bcb687ae-9883-4453-ad03-74087a9f4477 ethernet ens224
创建网卡配置文件
[root@alma8 ~]# nmcli con add type ethernet con-name ens224.1 ifname ens224
Connection 'ens224.1' (4187eb22-d317-4dc1-82f5-c81a97d8bbd4) successfully added.
[root@alma8 ~]# nmcli connection show
NAME UUID TYPE DEVICE
ens224 bcb687ae-9883-4453-ad03-74087a9f4477 ethernet ens224
ens224.1 4187eb22-d317-4dc1-82f5-c81a97d8bbd4 ethernet --
删除网卡配置文件
[root@alma8 ~]# nmcli con delete ens224.1
Connection 'ens224.1' (4187eb22-d317-4dc1-82f5-c81a97d8bbd4) successfully deleted.
修改网卡是否为自启,默认自启
[root@alma8 ~]# nmcli con mod ens224 connection.autoconnect no
[root@alma8 ~]# grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-ens224
ONBOOT=no
[root@alma8 ~]# nmcli con mod ens224 connection.autoconnect yes
[root@alma8 ~]# grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-ens224
ONBOOT=yes
修改网卡是否自动获取IP,并配置静态IP地址
[root@alma8 ~]# nmcli con modif ens224 ipv4.method auto
[root@alma8 ~]# grep BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-ens224
BOOTPROTO=dhcp
[root@alma8 ~]# nmcli con modif ens224 ipv4.method manual ipv4.addresses 192.168.200.200/24 ipv4.gateway 192.168.200.1 connection.autoconnect yes ipv4.dns 114.114.114.114,114.114.115.15
[root@alma8 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens224
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens224
UUID=bcb687ae-9883-4453-ad03-74087a9f4477
DEVICE=ens224
ONBOOT=yes
IPADDR=192.168.200.200
PREFIX=24
GATEWAY=192.168.200.1
DNS1=114.114.114.114
DNS2=114.114.115.15
[root@alma8 ~]# nmcli con up ens224
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/10)
[root@alma8 ~]# ip add
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:8e:02:b1 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.200/24 scope global noprefixroute ens224
valid_lft forever preferred_lft forever
inet6 fe80::9c44:37c2:db12:5fcd/64 scope link noprefixroute
valid_lft forever preferred_lft forever
四、完成一次完整操作,服务器添加一块网卡,配置IP、DNS、网关。
添加网卡后,出现了Wired connection 1,但是没有对应的配置文件
[root@alma8 ~]# nmcli con show
NAME UUID TYPE DEVICE
Wired connection 1 ee5fc609-9606-3e44-841d-601fc7d562ab ethernet ens256
[root@alma8 ~]# ll /etc/sysconfig/network-scripts/
total 8
删除Wired connection 1
[root@alma8 ~]# nmcli con delete "Wired connection 1"
Connection 'Wired connection 1' (ee5fc609-9606-3e44-841d-601fc7d562ab) successfully deleted.
新建ens256
[root@alma8 ~]# nmcli con add type ethernet con-name ens256 ifname ens256
Connection 'ens256' (d90d5e30-7c76-4a62-92de-0fb4d4a0a41f) successfully added.
已生成ens256配置文件
[root@alma8 ~]# ll /etc/sysconfig/network-scripts/
total 12
-rw-r--r--. 1 root root 282 May 4 13:54 ifcfg-ens256
为ens256配置网络
[root@alma8 ~]# nmcli con modif ens256 ipv4.method manual ipv4.addresses 192.168.200.201/24 ipv4.gateway 192.168.200.1 connection.autoconnect yes ipv4.dns 114.114.114.114,114.114.115.15
重新加载ens256配置文件
[root@alma8 ~]# nmcli con up ens256
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
查看网卡信息
[root@alma8 ~]# ip addr
4: ens256: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:8e:4d:94 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.201/24 brd 192.168.200.255 scope global noprefixroute ens256
valid_lft forever preferred_lft forever
inet6 fe80::374f:9c09:1e47:e0fa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
五、总结
不爽,太不爽了!