使用nmcli命令配置网络
!!!前言
nmcli是redhat7或者centos7之后的命令该命令可以完成网卡上所有的配置工作,并且可以写入配置文件,永久生效
1、NetworkManager
NetworkManager是管理和监控网络设置的守护进程,设备既就是网络接口,连接是对网络接口的配置,一个网络接口可以有多个连接配置,但同时只有一个连接配置生效。
2、常用命令列表
nmcli connection show 查看所有网络连接 nmcli connection show -active 查看活动的网络连接 nmcli connection show 'DEVICE' 查看指定网卡的详细信息 nmcli device status 显示设备的连接状态 nmcli device show 显示所有设备网络设备详情信息 nmcli device show 'DEVICE' 显示指定网络设备的详细信息 nmcli connection reload 重新加载配置 nmcli connection add help 查看帮助
一、查看网卡信息命令
1、查看所有网络连接
[root@localhost ~]# nmcli connection show NAME UUID TYPE DEVICE ens160 cf57c3f1-7eaf-489c-86d4-2ebbc39f6105 ethernet ens160 //ens160就是我的网卡
2、查看活动的网络连接
[root@localhost ~]# nmcli connection show -active NAME UUID TYPE DEVICE ens160 cf57c3f1-7eaf-489c-86d4-2ebbc39f6105 ethernet ens160 //我这只有一张网卡,所以只有一个网卡信息
3、查看指定网卡的详细信息
[root@localhost ~]# nmcli connection show ens160 connection.id: ens160 connection.uuid: cf57c3f1-7eaf-489c-86d4-2ebbc39f6105 connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: ens160 connection.autoconnect: 是 connection.autoconnect-priority: 0 connection.autoconnect-retries: -1 (default) connection.multi-connect: 0(default) connection.auth-retries: -1 connection.timestamp: 1595930397 connection.read-only: 否 connection.permissions: -- connection.zone: -- connection.master: -- connection.slave-type: -- connection.autoconnect-slaves: -1(default) connection.secondaries: -- connection.gateway-ping-timeout: 0 connection.metered: 未知 connection.lldp: default connection.mdns: -1(default) connection.llmnr: -1(default) connection.wait-device-timeout: -1 //这里显示的都是网卡详细信息
4、显示设备的连接状态
[root@localhost ~]# nmcli device status DEVICE TYPE STATE CONNECTION ens160 ethernet 已连接 ens160 lo loopback 未托管 -- //我这已经连接上了
5、显示所有设备网络设备详情信息
[root@localhost ~]# nmcli device show GENERAL.DEVICE: ens160 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:CD:6A:1B GENERAL.MTU: 1500 GENERAL.STATE: 100(已连接) GENERAL.CONNECTION: ens160 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.CARRIER: 开 IP4.ADDRESS[1]: 10.0.0.140/24 IP4.GATEWAY: 10.0.0.2 IP4.ROUTE[1]: dst = 10.0.0.0/24, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 10.0.0.2, mt = 100 IP4.DNS[1]: 8.8.8.8 IP6.ADDRESS[1]: fe80::ca08:fe32:678c:b392/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100 IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255 GENERAL.DEVICE: lo GENERAL.TYPE: loopback GENERAL.HWADDR: 00:00:00:00:00:00 GENERAL.MTU: 65536 GENERAL.STATE: 10(未托管) 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
6、显示指定网络设备的详细信息
[root@localhost ~]# nmcli device show ens160 GENERAL.DEVICE: ens160 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:CD:6A:1B GENERAL.MTU: 1500 GENERAL.STATE: 100(已连接) GENERAL.CONNECTION: ens160 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.CARRIER: 开 IP4.ADDRESS[1]: 10.0.0.140/24 IP4.GATEWAY: 10.0.0.2 IP4.ROUTE[1]: dst = 10.0.0.0/24, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 10.0.0.2, mt = 100 IP4.DNS[1]: 8.8.8.8 IP6.ADDRESS[1]: fe80::ca08:fe32:678c:b392/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100 IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
二、网卡状态修改命令
1、启用网络连接
[root@localhost ~]# nmcli connection up ens160 连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
2、停用网络连接(可以被自动激活)
[root@localhost ~]# nmcli connection down ens160 成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
3、删除网络连接的配置文件
[root@localhost ~]# nmcli connection delete ens160
4、重新加载网络配置文件
[root@localhost ~]# nmcli connection reload
三、修改网卡配置
1、设置IP地址获取方式是手动(none)或者 DHCP
[root@localhost ~]# nmcli connection modify ens160 ipv4.method manual ipv4.addresses 10.0.0.140/24 //这是修改为BOOTPROTO=none //IPADDR=10.0.0.140 //PREFIX=24 这个代表24位掩码 [root@localhost ~]# nmcli connection modify ens160 ipv4.method auto //周是修改BOOTPROTO=dhcp
2、设置自动启动网卡
[root@localhost ~]# nmcli connection modify ens160 connection.autoconnect yes //将ONBOOT=on 改成 ONBOOT=yes
3、修改IP地址
[root@localhost ~]# nmcli connection modify ens160 ipv4.addresses 10.0.0.140/24 //这样修改后IPADDR=IP ; NETMASK=掩码 ;然后会多出一个 ipv4的前缀 :PREFIX=掩码位 例如 PREFIX=24 表示24的掩码前缀
4、添加第二个IP地址
[root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 10.0.0.141/24 //IPADDR1=10.0.0.141 //PREFIX1=24
5、删除第二个IP地址
[root@localhost ~]# nmcli connection modify ens160 -ipv4.addresses 10.0.0.141/24
6、修改网关(Gateway)
[root@localhost ~]# nmcli connection modify ens160 ipv4.gateway 10.0.0.2 //GATEWAY=10.0.0.2
7、添加DNS
[root@localhost ~]# nmcli connection modify ens160 ipv4.dns 8.8.8.8 //DNS1=8.8.8.8
8、添加第二个DNS
[root@localhost ~]# nmcli connection modify ens160 +ipv4.dns 114.114.114.114 //DNS2=114.114.114.114
9、删除第二个DNS
[root@localhost ~]# nmcli connection modify ens160 -ipv4.dns 114.114.114.114
10.查看刚刚修改的IP信息
[root@localhost ~]# ifconfig ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.140 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::ca08:fe32:678c:b392 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:cd:6a:1b txqueuelen 1000 (Ethernet) RX packets 2859 bytes 234214 (228.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1987 bytes 197799 (193.1 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 124 bytes 10508 (10.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 124 bytes 10508 (10.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
11、查看网关(Gateway)
[root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 10.0.0.2 0.0.0.0 UG 0 0 0 ens160 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160 或者 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 ens160 10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
12、查看DNS
[root@localhost ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 8.8.8.8
13、IP是否能ping通外网
[root@localhost ~]# ping -c 3 www.baidu.com PING www.a.shifen.com (112.80.248.76) 56(84) bytes of data. 64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=1 ttl=128 time=20.3 ms 64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=2 ttl=128 time=25.8 ms 64 bytes from 112.80.248.76 (112.80.248.76): icmp_seq=3 ttl=128 time=14.9 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 14ms rtt min/avg/max/mdev = 14.946/20.353/25.766/4.417 ms