1、nmcli相关介绍
CentOS 5以及CentOS 6版本的Linux发行版,我们都习惯使用直接编辑网络配置文件或者使用类似于setup的命令对网络进行配置,但是在CentOS7版本上,出现了一个名为nmcli的工具,使得我们在配置和管理我们的网络时,变的非常方便。下面我们来了解下nmcli的一些常用功能:
CentOS 7 中默认的网络服务由 NetworkManager 提供,这是动态控制及配置网络的守护进程,它用于保持当前网络设备及连接处于工作状态,同时也支持传统的 ifcfg 类型的配置文件。
NetworkManager 可以用于以下类型的连接:Ethernet,VLANS,Bridges,Bonds,Teams,Wi-Fi,mobile boradband(如移动3G)以及 IP-over-InfiniBand。针对与这些网络类型,NetworkManager 可以配置他们的网络别名,IP 地址,静态路由,DNS,VPN连接以及很多其它的特殊参数。
2、nmcli的使用
2.1 查看nmcli的帮助命令:
[[email protected] ~]# nmcli help Usage: nmcli [OPTIONS] OBJECT { COMMAND | help } OPTIONS -t[erse] terse output -p[retty] pretty output -m[ode] tabular|multiline output mode -f[ields] <field1,field2,...>|all|common specify fields to output -e[scape] yes|no escape columns separators in values -n[ocheck] don‘t check nmcli and NetworkManager versions -a[sk] ask for missing parameters -w[ait] <seconds> set timeout waiting for finishing operations -v[ersion] show program version -h[elp] print this help OBJECT g[eneral] NetworkManager‘s general status and operations n[etworking] overall networking control r[adio] NetworkManager radio switches c[onnection] NetworkManager‘s connections d[evice] devices managed by NetworkManager a[gent] NetworkManager secret agent or polkit agent
说明:
a、device叫网络接口,是物理设备
b、connection是连接,偏重于逻辑设置
c、多个connection可以应用到同一个device,但同一时间只能启用其中一个connection。
d、这样的好处是针对一个网络接口,我们可以设置多个网络连接,比如静态IP和动态IP,再根据需要up相应的connection。
2.2 CentOS 7下网卡命名规则:
CentOS 7 开始对于网卡的编号则有另一套规则, 网卡的界面代号现在与网卡的来源有关,基本上的网卡名称会是这样分类的:
eno1 : 代表由主板 BIOS 内置的网卡
ens1 : 代表由主板 BIOS 内置的 PCI-E 界面的网卡
enp2s0 : 代表 PCI-E 界面的独立网卡, 可能有多个插孔, 因此会有 s0, s1... 的编号
eth0 : 如果上述的名称都不适用, 就回到原本的默认网卡编号
CentOS 7 也希望我们不要手动修改配置文件, 直接使用所谓的 nmcli 这个指令来设置网络参数即可
2.3 查看网络设备的相关状态:
[[[email protected] ~]# nmcli device status Warning: nmcli (1.0.6) and NetworkManager (1.0.0) versions don‘t match. Use --nocheck to suppress the warning. DEVICE TYPE STATE CONNECTION virbr0 bridge connected virbr0 eno16777736 ethernet connected eno16777736 lo loopback unmanaged -- virbr0-nic tap unmanaged -- [[email protected] ~]# nmcli device show eno16777736 Warning: nmcli (1.0.6) and NetworkManager (1.0.0) versions don‘t match. Use --nocheck to suppress the warning. GENERAL.DEVICE: eno16777736 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:C5:A0:8D GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/8 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 10.10.10.133/24 IP4.GATEWAY: 10.10.10.2 IP4.DNS[1]: 10.10.10.2 IP4.DNS[2]: 8.8.8.8 IP4.DOMAIN[1]: localdomain IP6.ADDRESS[1]: fe80::20c:29ff:fec5:a08d/64 IP6.GATEWAY: [[email protected] ~]# nmcli connection help Usage: nmcli connection { COMMAND | help } COMMAND := { show | up | down | add | modify | edit | delete | reload | load } show [--active] [[--show-secrets] [id | uuid | path | apath] <ID>] ... up [[id | uuid | path] <ID>] [ifname <ifname>] [ap <BSSID>] [passwd-file <file with passwords>] down [id | uuid | path | apath] <ID> ... add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+ edit [id | uuid | path] <ID> edit [type <new_con_type>] [con-name <new_con_name>] delete [id | uuid | path] <ID> reload load <filename> [ <filename>... ] [[email protected] ~]# nmcli connection show Warning: nmcli (1.0.6) and NetworkManager (1.0.0) versions don‘t match. Use --nocheck to suppress the warning. NAME UUID TYPE DEVICE virbr0 6e28dbac-ef8c-4065-b235-26da2a96e261 bridge virbr0 eno16777736 9abced2c-1ebf-4a70-9ecc-fc9c9d14d01c 802-3-ethernet eno16777736
2.4 配置接口的IP地址信息:
#----->在同一网卡上,配置多个IP地址 [[email protected] ~]# nmcli connection modify eno16777736 +ipv4.addresses 10.10.10.134/24 #----->如何让新添加的IP地址生效 nmcli con down eno16777736;nmcli con up eno16777736 #----->我们添加网卡新的ip地址:10.10.10.134/24 [[email protected] ~]# nmcli connection modify eno16777736 ipv4. ipv4.addresses ipv4.dhcp-send-hostname ipv4.gateway ipv4.may-fail ipv4.route-metric ipv4.dhcp-client-id ipv4.dns ipv4.ignore-auto-dns ipv4.method ipv4.routes ipv4.dhcp-hostname ipv4.dns-search ipv4.ignore-auto-routes ipv4.never-default [[email protected] ~]# nmcli connection modify eno16777736 +ipv4.addresses 10.10.10.134/24 [[email protected] ~]# ifconfig -a eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.133 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec5:a08d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c5:a0:8d txqueuelen 1000 (Ethernet) RX packets 26917 bytes 33375856 (31.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5265 bytes 334974 (327.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 0 (Local Loopback) RX packets 524 bytes 45724 (44.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 524 bytes 45724 (44.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 直接配置后,网卡的ip地址不能直接生效 #----->如何让添加的网卡生效 [[email protected] ~]# nmcli connection down eno16777736 ;nmcli connection up eno16777736 Connection ‘eno16777736‘ successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1) 成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2) #----->发现这里面仍旧看不到新添加的接口的IP信息 [[email protected] ~]# ifconfig -a eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.133 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec5:a08d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c5:a0:8d txqueuelen 1000 (Ethernet) RX packets 57185 bytes 71800786 (68.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10663 bytes 676788 (660.9 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 0 (Local Loopback) RX packets 548 bytes 48084 (46.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 548 bytes 48084 (46.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 #----->查看网卡的配置文件 [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736 TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=eno16777736 UUID=9abced2c-1ebf-4a70-9ecc-fc9c9d14d01c DEVICE=eno16777736 ONBOOT=yes IPADDR=10.10.10.134 PREFIX=24 PEERDNS=yes PEERROUTES=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_PRIVACY=no
#查看网卡的详细信息
[[email protected] ~]# nmcli device show eno16777736 GENERAL.设备: eno16777736 GENERAL.类型: ethernet GENERAL.硬盘: 00:0C:29:C5:A0:8D GENERAL.MTU: 1500 GENERAL.状态: 100 (连接的) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/5 WIRED-PROPERTIES.容器: 开 IP4.地址[1]: 10.10.10.133/24 IP4.地址[2]: 10.10.10.134/24 IP4.网关: 10.10.10.2 IP4.DNS[1]: 10.10.10.2 IP4.域[1]: localdomain IP6.地址[1]: fe80::20c:29ff:fec5:a08d/64 IP6.网关:
#这里如果发现是中文,可以使用以下命令进行修改
[[email protected] ~]# localectl list-locales [[email protected] ~]# localectl set-locale LANG=en_US.utf8 #断开连接,然后重新连接下 [[email protected] ~]# nmcli device show eno16777736 Warning: nmcli (1.0.6) and NetworkManager (1.0.0) versions don‘t match. Use --nocheck to suppress the warning. GENERAL.DEVICE: eno16777736 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:C5:A0:8D GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/8 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 10.10.10.133/24 IP4.GATEWAY: 10.10.10.2 IP4.DNS[1]: 10.10.10.2 IP4.DNS[2]: 8.8.8.8 IP4.DOMAIN[1]: localdomain IP6.ADDRESS[1]: fe80::20c:29ff:fec5:a08d/64 IP6.GATEWAY:
#消除我们添加的ip:10.10.10.134/24
[[email protected] ~]# nmcli connection modify eno16777736 -ipv4.addresses 10.10.10.134/24 [[email protected] ~]# echo $? 0 [[email protected] ~]# nmcli connection down eno16777736 ;nmcli connection up eno16777736 Connection ‘eno16777736‘ successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3) 成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/4) [[email protected] ~]# ifconfig -a eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.133 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec5:a08d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c5:a0:8d txqueuelen 1000 (Ethernet) RX packets 77939 bytes 96881263 (92.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 14880 bytes 957913 (935.4 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 0 (Local Loopback) RX packets 560 bytes 49264 (48.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 560 bytes 49264 (48.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736 TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=eno16777736 UUID=9abced2c-1ebf-4a70-9ecc-fc9c9d14d01c DEVICE=eno16777736 ONBOOT=yes PEERDNS=yes PEERROUTES=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_PRIVACY=no [[email protected] ~]# nmcli device show eno16777736 GENERAL.设备: eno16777736 GENERAL.类型: ethernet GENERAL.硬盘: 00:0C:29:C5:A0:8D GENERAL.MTU: 1500 GENERAL.状态: 100 (连接的) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/6 WIRED-PROPERTIES.容器: 开 IP4.地址[1]: 10.10.10.133/24 IP4.网关: 10.10.10.2 IP4.DNS[1]: 10.10.10.2 IP4.域[1]: localdomain IP6.地址[1]: fe80::20c:29ff:fec5:a08d/64 IP6.网关: [[email protected] ~]# ifconfig -a eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.133 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec5:a08d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c5:a0:8d txqueuelen 1000 (Ethernet) RX packets 537996 bytes 673538844 (642.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 97698 bytes 6097864 (5.8 MiB) 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 0 (Local Loopback) RX packets 616 bytes 54280 (53.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 616 bytes 54280 (53.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 #----->大家可以测试下,重启网络服务对通过nmcli命令添加和减少的网卡信息是否有效,我这边测试是没有任何效果。
# 手动设置网关(如果网关没有进行配置):
[[email protected] ~]# nmcli connection modify eno16777736 ipv4.gateway 10.10.10.2 # 手动设置DNS(如果DNS没有进行配置): [[email protected] ~]# nmcli connection modify eno16777736 +ipv4.dns 8.8.8.8 [[email protected] ~]# echo $? 0 [[email protected] ~]# nmcli device connect delete disconnect help show status wifi wimax [[email protected] ~]# nmcli connection down eno16777736 ;nmcli connection up eno16777736 Connection ‘eno16777736‘ successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6) 成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/7) [[email protected] ~]# nmcli device show eno16777736 GENERAL.设备: eno16777736 GENERAL.类型: ethernet GENERAL.硬盘: 00:0C:29:C5:A0:8D GENERAL.MTU: 1500 GENERAL.状态: 100 (连接的) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/7 WIRED-PROPERTIES.容器: 开 IP4.地址[1]: 10.10.10.133/24 IP4.网关: 10.10.10.2 IP4.DNS[1]: 10.10.10.2 IP4.DNS[2]: 8.8.8.8 IP4.域[1]: localdomain IP6.地址[1]: fe80::20c:29ff:fec5:a08d/64 IP6.网关:
CentOS7下面有类似于CentOS6下面的setup工具的命令,nmtui命令
功能非常强大:可以管理IP的相关信息,添加子接口,桥接等。