- 谈到nmclin那就要说到NetworkManager
- NetworkManager最初由 Redhat 公司开发,现在由 GNOME 管理
- nmcli是命令行的NetworkManager工具,会自动把配置写到/etc/sysconfig/network-scripts/目录下面
- nmcli说简单就是个配置文件,一个硬件可以有多个配置文件,用NetworkManager来管理而已
- 简单理解就是,一个连接就是(connection)就是/etc/sysconfig/network-scripts/目录下的一个配置文件,接口(device)是物理设备,一个物理设置可以拥有多个配置文件,但只能有一个配置文件属于使用(active)状态;配置文件的生成与使用状态均由NetworkManager控制
nmcli命令表
命令 |
使用 |
nmcli dev status |
List all devices |
nmcli con show |
List all connections |
nmcli con up "ID" |
Activate a connection |
nmcli con down "ID" |
Deactivate a connection . The connection will restart if autoconnect is yes. |
nmcli dev dis "DEV" |
Disble all managed interfaces |
nmcli con add... |
Add anew connection |
nmcli con mod "ID"... |
Modify a copnnection |
nmcli con del "ID" |
Delite a connection... |
配置文件和文件的对应关系
nmcli con mod |
ifcfg-* |
ipv4.method manual |
BOOTPROTO=none |
ipv4.method auto |
BOOTPROTO=dhcp |
ipv4.addresses “192.0.2.1/24 |
192.0.2.254 |
IPADDR0=192.0.2.1 |
PREFIX0=24 |
GATEWAY0=192.0.2.254 |
ipv4.dns 8.8.8.8 |
DNS0=8.8.8.8 |
ipv4.dns-search example.com |
DOMAIN=example.com |
ipv4.ignore-auto-dns true |
PEERDNS=no |
connection.autoconnect yes |
ONBOOT=yes |
connection.id eth0 |
NAME=eth0 |
connection.interface-name eth0 |
DEVICE=eth0 |
connection.interface-name eth0 |
DEVICE=eth0 |
802-3-ethernet.mac-address . . . |
HWADDR= . . . |
动态和静态配置文件
动态配置 |
静态配置 |
DEVICE=ethX |
DEVICE=ethX |
HWADDR=0:02:8A:A6:30:45 |
HWADDR=0:02:8A:A6:30:45 |
BOOTPROTO=dhcp |
IPADDR=192.168.0.123 |
ONBOOT=yes |
NETMASK=255.255.255.0 |
type=Ethernet |
GATEWAy=192.168.0.254 |
|
ONBOOT=yes |
|
type=Ethernet |
nmcli的使用
- 首先我们先用 nmcli connection show 命令来查看当前计算机有几套配置,换句话说就是几个链接
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
Wired connection 1 7f8ee0f7-41b8-41c3-a922-49266a2b9303 802-3-ethernet eth1 <<<这是套配置
eno33554960 eb15cda5-85ec-44d9-8ea4-fc5dd7ea76c3 802-3-ethernet --
eno16777736 da0d66c1-91b9-456e-8a51-f0e0772212bc 802-3-ethernet --
virbr0-nic 45f38f6d-fb8b-49f4-a261-daa9ff952746 generic virbr0-nic
virbr0 3ce60a22-a7d8-4e7d-9997-6813be97eead bridge virbr0 <<<这是虚拟网卡
Wired connection 2 46f4952d-ecd7-4a58-afd2-feea00d087fa 802-3-ethernet eth0
- 我们来修改以上的
Wired connection 1
的名字,用nmcli connection modify Wired\ connection\ 1 connection.id eth1-office
命令
[root@localhost ~]# nmcli connection modify Wired\ connection\ 1 connection.id eth1-office
- 在用
nmcli connection show
来查看下
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
eth1-office 7f8ee0f7-41b8-41c3-a922-49266a2b9303 802-3-ethernet eth1 <<<看已经改了
eno33554960 eb15cda5-85ec-44d9-8ea4-fc5dd7ea76c3 802-3-ethernet --
eno16777736 da0d66c1-91b9-456e-8a51-f0e0772212bc 802-3-ethernet --
virbr0-nic 45f38f6d-fb8b-49f4-a261-daa9ff952746 generic virbr0-nic
virbr0 3ce60a22-a7d8-4e7d-9997-6813be97eead bridge virbr0
Wired connection 2 46f4952d-ecd7-4a58-afd2-feea00d087fa 802-3-ethernet eth0
- 最后一行
Wired connection 2
我们也可以把他改了或删掉,那我们来删掉吧
- 运行命令
nmcli con delete Wired\ connection\ 2
[root@localhost ~]# nmcli con delete Wired\ connection\ 2
Connection 'Wired connection 2' (46f4952d-ecd7-4a58-afd2-feea00d087fa) successfully deleted.
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
eth1-office 7f8ee0f7-41b8-41c3-a922-49266a2b9303 802-3-ethernet eth1
eno33554960 eb15cda5-85ec-44d9-8ea4-fc5dd7ea76c3 802-3-ethernet --
eno16777736 da0d66c1-91b9-456e-8a51-f0e0772212bc 802-3-ethernet --
virbr0-nic 45f38f6d-fb8b-49f4-a261-daa9ff952746 generic virbr0-nic
virbr0 3ce60a22-a7d8-4e7d-9997-6813be97eead bridge virbr0
- 我们在不同的环境,创建不同的链接,下图已经创建成功
[root@localhost ~]# nmcli connection add con-name eyh1-lan type ethernet ifname eth1
Connection 'eyh1-lan' (077284ec-39f6-47f8-becd-168b8bd3b64c) successfully added.
[root@localhost network-scripts]# ls
ifcfg-eno16777736 ifdown ifdown-ipv6 ifdown-sit ifup-aliases ifup-ipv6 ifup-ppp ifup-tunnel
ifcfg-eno33554960 ifdown-bnep ifdown-isdn ifdown-Team ifup-bnep ifup-isdn ifup-routes ifup-wireless
ifcfg-eth1-office ifdown-eth ifdown-post ifdown-TeamPort ifup-eth ifup-plip ifup-sit init.ipv6-global
ifcfg-eyh1-lan ifdown-ib ifdown-ppp ifdown-tunnel ifup-ib ifup-plusb ifup-Team network-functions
ifcfg-lo ifdown-ippp ifdown-routes ifup ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6
- 我们只是创建一套配置而已,里面还没有IP地址,我们来cat来查看下我们创建的配置文件
[root@localhost network-scripts]# cat ifcfg-eyh1-lan
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eyh1-lan
UUID=077284ec-39f6-47f8-becd-168b8bd3b64c
DEVICE=eth1
ONBOOT=yes
- 我们在用
nmcli connection show
来查看下
[root@localhost network-scripts]# nmcli connection show
NAME UUID TYPE DEVICE
eth1-office 7f8ee0f7-41b8-41c3-a922-49266a2b9303 802-3-ethernet eth1
eno33554960 eb15cda5-85ec-44d9-8ea4-fc5dd7ea76c3 802-3-ethernet --
eno16777736 da0d66c1-91b9-456e-8a51-f0e0772212bc 802-3-ethernet --
eyh1-lan 077284ec-39f6-47f8-becd-168b8bd3b64c 802-3-ethernet --
virbr0-nic 45f38f6d-fb8b-49f4-a261-daa9ff952746 generic virbr0-nic
virbr0 3ce60a22-a7d8-4e7d-9997-6813be97eead bridge virbr0
[root@localhost network-scripts]# nmcli connection modify eyh1-lan connection.autoconnect yes ipv4.addresses 172.17.100.11/16 ipv4.method manual <<<使用了这条命令 ,这项很重要记住了,不然配置文件不会生效
[root@localhost network-scripts]# cat ifcfg-eyh1-lan
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eyh1-lan
UUID=077284ec-39f6-47f8-becd-168b8bd3b64c
DEVICE=eth1
ONBOOT=yes
IPADDR=172.17.100.11
PREFIX=16
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[root@localhost network-scripts]# nmcli connection modify eyh1-lan connection.autoconnect yes ipv4.addresses 172.17.100.11/16 ipv4.method manual ipv4.gateway 172.17.0.1 ipv4.dns 172.17.0.1
[root@localhost network-scripts]# cat ifcfg-eyh1-lan
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eyh1-lan
UUID=077284ec-39f6-47f8-becd-168b8bd3b64c
DEVICE=eth1
ONBOOT=yes
IPADDR=172.17.100.11
PREFIX=16
GATEWAY=172.17.0.1
DNS1=172.17.0.1
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[root@localhost Desktop]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:4f:db:89 txqueuelen 1000 (Ethernet)
RX packets 3 bytes 276 (276.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
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.100.11 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::20c:29ff:fe4f:db93 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4f:db:93 txqueuelen 1000 (Ethernet)
RX packets 2078 bytes 207195 (202.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 133 bytes 20881 (20.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost network-scripts]# cp ifcfg-eyh1-lan ifcfg-eyh1-home
[root@localhost network-scripts]# vi ifcfg-eyh1-home
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eyh1-lan <<<<名字
UUID=077284ec-39f6-47f8-becd-168b8bd3b64c UID:必须改
DEVICE=eth1
ONBOOT=yes
IPADDR=172.17.100.11 <<<IP:自己看情况改
PREFIX=16
GATEWAY=172.17.0.1
DNS1=172.17.0.1
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[root@localhost network-scripts]# nmcli connection show
NAME UUID TYPE DEVICE
eyh1-lan 077284ec-39f6-47f8-becd-168b8bd3b64c 802-3-ethernet --
eno16777736 da0d66c1-91b9-456e-8a51-f0e0772212bc 802-3-ethernet --
eth1-office 7f8ee0f7-41b8-41c3-a922-49266a2b9303 802-3-ethernet eth1
virbr0-nic 46e06189-1049-446e-bd47-255f006d02e3 generic virbr0-nic
virbr0 69e7fc2c-61e8-4a6d-9a11-ffa5905d1302 bridge virbr0
eno33554960 eb15cda5-85ec-44d9-8ea4-fc5dd7ea76c3 802-3-ethernet --
- 上图没有显示我刚复制的配置文件,需要执行一条命令来读一下,
nmcli connection reload
[root@localhost network-scripts]# nmcli connection reload
[root@localhost network-scripts]# nmcli connection show
NAME UUID TYPE DEVICE
eyh1-lan 077284ec-39f6-47f8-becd-168b8bd3b64c 802-3-ethernet --
eno16777736 da0d66c1-91b9-456e-8a51-f0e0772212bc 802-3-ethernet --
eth1-office 7f8ee0f7-41b8-41c3-a922-49266a2b9303 802-3-ethernet eth1
eyh1-home c9b75b5d-6975-b4e8-0e17-d32f44f56ef2 802-3-ethernet --
virbr0-nic 46e06189-1049-446e-bd47-255f006d02e3 generic virbr0-nic
virbr0 69e7fc2c-61e8-4a6d-9a11-ffa5905d1302 bridge virbr0
eno33554960 eb15cda5-85ec-44d9-8ea4-fc5dd7ea76c3 802-3-ethernet --
- 我们还可以用
nmcli connection delete eth1-office
把他删除了,他会自动找个配置文件来顶替
[root@localhost network-scripts]# nmcli connection delete eth1-office
Connection 'eth1-office' (7f8ee0f7-41b8-41c3-a922-49266a2b9303) successfully deleted.
- 这就是多套配置,我们可以准备多套配置,只需要使用一个就可以了,可以根据环境来使用