签到成功

知道了

CNDBA社区CNDBA社区

Linux 7 中网络配置管理(nmcli工具) 说明

2016-09-02 13:58 4945 0 原创 Linux
作者: dave

 

 

1         Nmcli命令管理网络

Linux 7中默认的网络服务由NetworkManager提供,这是一个动态的网络控制和配置守护进程,它在网络设备和连接可用时保持链接正常,同时也提供了典型的ifcfg类型的配置文件。

 

1.1        Nmcli 使用帮助

NetworkManager对应的命令行工具就是nmcli,使用nmcli可以方便的管理网络。http://www.cndba.cn/dave/article/151

 

 

Nmcli 使用帮助:

[root@dave network-scripts]# nmcli
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
 
OPTIONS
  -t[erse]                                   terse output
  -p[retty]                                  pretty output
  -m[ode] tabular|multiline                  output mode
  -f[ields] 
 
 
  
  |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] 
  
  
                              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   [root@dave network-scripts]# 
  
 
 

 http://www.cndba.cn/dave/article/151

--显示NetworkManager的整体状态

[root@dave network-scripts]# nmcli general status
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN   
connected  full          enabled  enabled  enabled  enabled

 

--显示所有的连接

[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE            DEVICE    
virbr0-nic  855288c2-8ff7-4b42-a4cb-0f4bddb18c50  generic         virbr0-nic
virbr0      7823caa6-475b-4bab-bdda-d8b0106b85bd  bridge          virbr0    
enp0s25     21f89071-c799-41d0-b024-0a5556302c66  802-3-ethernet  enp0s25

 

--显示活跃的连接:

[root@dave network-scripts]# nmcli device status
DEVICE      TYPE      STATE      CONNECTION
virbr0      bridge    connected  virbr0    
enp0s25     ethernet  connected  enp0s25   
virbr0-nic  tap       connected  virbr0-nic
lo          loopback  unmanaged  --        
[root@dave network-scripts]#

 

 

1.2        启动/停止网络设备

可以使用nmcli从命令行启动或者停止网络设备,等同于ifconfig中的updown

 

停止网络设备:

[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE            DEVICE    
virbr0-nic  855288c2-8ff7-4b42-a4cb-0f4bddb18c50  generic         virbr0-nic
virbr0      7823caa6-475b-4bab-bdda-d8b0106b85bd  bridge          virbr0    
enp0s25     21f89071-c799-41d0-b024-0a5556302c66  802-3-ethernet  enp0s25   
 
[root@dave network-scripts]# nmcli device disconnect enp0s25
 
[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE            DEVICE    
virbr0-nic  855288c2-8ff7-4b42-a4cb-0f4bddb18c50  generic         virbr0-nic
virbr0      7823caa6-475b-4bab-bdda-d8b0106b85bd  bridge          virbr0    
enp0s25     21f89071-c799-41d0-b024-0a5556302c66  802-3-ethernet  --

 

启动网络设备:

[root@dave network-scripts]# nmcli device connect enp0s25
Device 'enp0s25' successfully activated with '21f89071-c799-41d0-b024-0a5556302c66'.
 
[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE            DEVICE    
virbr0-nic  855288c2-8ff7-4b42-a4cb-0f4bddb18c50  generic         virbr0-nic
virbr0      7823caa6-475b-4bab-bdda-d8b0106b85bd  bridge          virbr0    
enp0s25     21f89071-c799-41d0-b024-0a5556302c66  802-3-ethernet  enp0s25

  

 

1.3        配置静态IP

 

使用nmcli命令配置IP的语法如下:

# nmcli connection add type ethernet con-name NAME_OF_CONNECTION ifname INTERFACE-NAME ip4 IP_ADDRESS gw4 GW_ADDRESS

 

示例:

[root@dave network-scripts]# nmcli connection add type ethernet con-name NEW_STATIC ifname enp0s25 ip4 192.168.0.179 gw4 192.168.0.2
Connection 'NEW_STATIC' (63b6aecf-71c3-49c3-a0dd-3d673ac80548) successfully added.
[root@dave network-scripts]#

 

注:这里的con-name可以直接写网卡名,因为最终生成的配置文件ifcfg-xxx 是根据这里来的。

 

也可以直接修改当前连接的IP地址:

[root@dave ~]# nmcli device status
DEVICE      TYPE      STATE      CONNECTION
virbr0      bridge    connected  virbr0    
enp0s25     ethernet  connected  NEW_DHCP  
virbr0-nic  tap       connected  virbr0-nic
lo          loopback  unmanaged  --    
 
[root@dave ~]# nmcli connection modify eth0 ipv4.addresses "192.168.0.179/24" ipv4.gateway 192.168.0.2

 

这里修改的属性可以使用如下命令查看:

[root@dave ~]# nmcli -p connection show eth0

 

 

设置DNS服务器:

[root@dave network-scripts]# nmcli connection modify NEW_STATIC ipv4.dns "192.168.0.4"

 

启用新的以太网连接:

[root@dave network-scripts]# nmcli connection up NEW_STATIC ifname enp0s25
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

 

查看新配置连接的详细信息:

[root@dave network-scripts]# nmcli -p connection show NEW_STATIC
===============================================================================
                    Connection profile details (NEW_STATIC)
===============================================================================
connection.id:                          NEW_STATIC
connection.uuid:                        63b6aecf-71c3-49c3-a0dd-3d673ac80548
connection.interface-name:              enp0s25
connection.type:                        802-3-ethernet
……
-------------------------------------------------------------------------------
IP4.ADDRESS[1]:                         192.168.0.179/32
IP4.GATEWAY:                            192.168.0.2
IP4.DNS[1]:                             192.168.0.4
-------------------------------------------------------------------------------
IP6.ADDRESS[1]:                         fe80::527b:9dff:fe53:676c/64
IP6.GATEWAY:                           
-------------------------------------------------------------------------------
[root@dave network-scripts]#

 

注意上面的是修改connection的信息,可以使用如下命令查看:

[root@dave ~]# nmcli device status

 

添加的连接都会自动创建配置文件,如:

[root@dave network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@dave network-scripts]# ls ifcfg-*
ifcfg-enp0s25  ifcfg-lo  ifcfg-NEW_STATIC
[root@dave network-scripts]#

 

配置文件里面会记录配置信息:

[root@dave network-scripts]# cat ifcfg-NEW_STATIC
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=NEW_STATIC
UUID=63b6aecf-71c3-49c3-a0dd-3d673ac80548
DEVICE=enp0s25
ONBOOT=yes
IPADDR=192.168.0.179
PREFIX=32
GATEWAY=192.168.0.2
DNS1=192.168.0.4
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

 

另外在默认的网卡配置上也会多出新的IP地址的信息,如:

[root@dave network-scripts]# vim ifcfg-enp0s25
 
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s25
UUID=21f89071-c799-41d0-b024-0a5556302c66
DEVICE=enp0s25
ONBOOT=yes
IPADDR1=192.168.0.180
PREFIX1=24
IPADDR=192.168.0.179
PREFIX=24
IPADDR2=192.168.0.181
PREFIX2=24
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no

 

1.4        配置DHCP

 

如果想添加一个使用DHCP来配置接口IP地址、网关地址和dns服务器地址的新的连接,那么只需要忽略上述命令中的ip/gw部分,NetworkManager会自动使用DHCP来获取配置细节。

 

如创建一个新的叫NEW_DHCPDHCP连接,在设备enp0s25上你可以使用下面的命令:

[root@dave ~]#  nmcli connection add type ethernet con-name NEW_DHCP ifname enp0s25
Connection 'NEW_DHCP' (7ef57ec8-6254-4904-a00a-2cfc424e83a8) successfully added.
 
[root@dave ~]# nmcli connection up NEW_DHCP ifname enp0s25
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
[root@dave ~]#

 

2         使用图形化界面管理系统网络

   RHEL7之前的系统中可以使用setup命令图形化配置网络,在RHEL7中使用nm-connection-editor或者nmtui来编辑网络。

 

运行nm-connection-editor命令的图片:http://www.cndba.cn/dave/article/151http://www.cndba.cn/dave/article/151


 

从这可以看出来,一个网卡可以对应多个连接。

 1.png

 

 

运行nmtui图形化配置界面:

 2.pnghttp://www.cndba.cn/dave/article/151


 

3         使用传统方式管理

 

可以直接修改对应的配置文件。

 

这里把IP地址从179改成180http://www.cndba.cn/dave/article/151

[root@dave network-scripts]# cat ifcfg-enp0s25
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s25
UUID=21f89071-c799-41d0-b024-0a5556302c66
DEVICE=enp0s25
ONBOOT=yes
IPADDR=192.168.0.180
PREFIX=24
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no

 http://www.cndba.cn/dave/article/151

重启网络:

 

[root@dave ~]# systemctl status network.service
network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network)
   Active: active (exited) since Thu 2016-03-03 19:41:31 CST; 2min 8s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 14739 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS)
  Process: 14959 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
 
[root@dave ~]# systemctl restart network

 

关于Linux 7中的服务管理参考:http://www.cndba.cn/dave/article/151

Linux 7 Service 服务管理 说明

http://www.cndba.cn/account/article/details/152

 

[root@dave ~]# ifconfig
enp0s25: flags=4163
 
 
  
    mtu 1500
        inet 192.168.0.180  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::527b:9dff:fe53:676c  prefixlen 64  scopeid 0x20
  
  
        ether 50:7b:9d:53:67:6c  txqueuelen 1000  (Ethernet)
        RX packets 586057  bytes 69175196 (65.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 147166  bytes 34397582 (32.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xd0700000-d0720000
 
 

 

 

4         修改RHEL7 网卡名到旧的风格

在之前的操作也已经看到,在Linux 7中网卡的名称发生了改变,不是之前默认的eth0eth1 而是enp0s+数字,可以通过如下方式修改成默认的的模式。

 

4.1        修改grub

修改/etc/default/grub文件中GRUB_CMDLINE_LINUX的值,增加net.ifnames=0 biosdevname=0http://www.cndba.cn/dave/article/151


[root@dave ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet net.ifnames=0 biosdevname=0"
GRUB_DISABLE_RECOVERY="true"
[root@dave ~]#


 

 

执行以下命令,使配置生效:

[root@dave network-scripts]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-6225a51e7af64a0aa214d117d41dba46
Found initrd image: /boot/initramfs-0-rescue-6225a51e7af64a0aa214d117d41dba46.img
Done
 
[root@dave network-scripts]# reboot

 

重启系统可以看到网卡的名称已变回跟RHEL6一样,但此时网络还不能工作。

 http://www.cndba.cn/dave/article/151

4.2        删除旧的connection,在删除时会连配置文件一起删除

[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE            DEVICE    
virbr0-nic  8349708b-cdf5-4ea2-962a-c7b530baccc3  generic         virbr0-nic
virbr0      66a55655-5dbe-44f2-a26e-9ef794f7e227  bridge          virbr0    
enp0s25     21f89071-c799-41d0-b024-0a5556302c66  802-3-ethernet  --     
 
[root@dave network-scripts]# nmcli connection delete enp0s25
Connection 'enp0s25' (21f89071-c799-41d0-b024-0a5556302c66) successfully deleted.
 
[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE     DEVICE    
virbr0-nic  8349708b-cdf5-4ea2-962a-c7b530baccc3  generic  virbr0-nic
virbr0      66a55655-5dbe-44f2-a26e-9ef794f7e227  bridge   virbr0    
 
[root@dave network-scripts]# ls
ifcfg-lo     ifdown-ppp       ifup-eth    ifup-routes
ifdown       ifdown-routes    ifup-ib     ifup-sit
ifdown-bnep  ifdown-sit       ifup-ippp   ifup-Team
ifdown-eth   ifdown-Team      ifup-ipv6   ifup-TeamPort
ifdown-ib    ifdown-TeamPort  ifup-isdn   ifup-tunnel
ifdown-ippp  ifdown-tunnel    ifup-plip   ifup-wireless
ifdown-ipv6  ifup             ifup-plusb  init.ipv6-global
ifdown-isdn  ifup-aliases     ifup-post   network-functions
ifdown-post  ifup-bnep        ifup-ppp    network-functions-ipv6
[root@dave network-scripts]#

 

 

4.3        添加新的Connection,会自动生成配置文件并激活网卡

[root@dave network-scripts]# ifup eth0
/usr/sbin/ifup: configuration for eth0 not found.
Usage: ifup 
 
 
  
  
 
[root@dave network-scripts]# nmcli connection add type ethernet con-name eth0 ifname eth0 ip4 192.168.0.179/24
Connection 'eth0' (3d389940-0ca5-45b3-a83c-429e915592b8) successfully added.
 
[root@dave network-scripts]# nmcli connection show
NAME        UUID                                  TYPE            DEVICE    
virbr0-nic  8349708b-cdf5-4ea2-962a-c7b530baccc3  generic         virbr0-nic
virbr0      66a55655-5dbe-44f2-a26e-9ef794f7e227  bridge          virbr0    
eth0        3d389940-0ca5-45b3-a83c-429e915592b8  802-3-ethernet  eth0      
 
[root@dave network-scripts]# ifconfig
eth0: flags=4163
  
  
      mtu 1500         inet 192.168.0.179  netmask 255.255.255.0  broadcast 192.168.0.255         inet6 fe80::527b:9dff:fe53:676c  prefixlen 64  scopeid 0x20 
            ether 50:7b:9d:53:67:6c  txqueuelen 1000  (Ethernet)         RX packets 876  bytes 102367 (99.9 KiB)         RX errors 0  dropped 0  overruns 0  frame 0         TX packets 30  bytes 4219 (4.1 KiB)         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0         device interrupt 20  memory 0xd0700000-d0720000    [root@dave network-scripts]# ls ifcfg-eth0     [root@dave network-scripts]# cat ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none IPADDR=192.168.0.179 PREFIX=24 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=eth0 UUID=3d389940-0ca5-45b3-a83c-429e915592b8 DEVICE=eth0 ONBOOT=yes [root@dave network-scripts]# 
  
 
 

 


版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2261
    原创
  • 3
    翻译
  • 578
    转载
  • 191
    评论
  • 访问:7969363次
  • 积分:4346
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ