centos 7 之nmcli命令

1、nmcli命令的作用

nmcli : command‐line tool for controlling NetworkManager
    NetworkManager:动态管理和监控网络设置的守护进程。用户和脚本都可使用命令行工具 nmcli 控制 NetworkManager

2、语法

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 (只要不造成歧义,可简写,OBJIECT后按TAB键可以看到更多参数)
  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

3、具体用法

在 nmcli 中采用命令完成功能,无论何时您不确定可用的命令选项时,都可以按 Tab 查看。

(1)显示所有网络连接

[root@linuxpao network-scripts]# nmcli -p connection show (显示结果因人而异)
==============================================================================
                      NetworkManager connection profiles
==============================================================================
NAME        UUID                                  TYPE            DEVICE     
------------------------------------------------------------------------------
team0       74a046f6-5475-486e-a33d-56cee384eca4  team            team0      
team0-eth0  9cf45811-0b16-477b-a525-14c4b2ef923e  802-3-ethernet  eth0       
team0-eth1  2d6f061a-51d7-47dc-9475-f33328d9a265  802-3-ethernet  eth1       
virbr0-nic  8b14977b-69e7-4442-8ef2-71711842618b  generic         virbr0-nic 
virbr0      5274f1f7-629b-41fa-9208-2f517b07c03f  bridge          virbr0     
eth0        e5771ae1-0195-44d4-ab6a-bdd2dfb1c8e5  802-3-ethernet  --

注意:输出结果中的 NAME 字段永远代表连接 ID(名称)。它不是接口名称(尽管看起来很像)。只要不产生歧义,nmcli命令后接的参数都可以简写的如 connection 简写成 c ,show 简写成 s 。

[root@linuxpao network-scripts]# nmcli c s (用TAB键可以看到show后接选项)
--active        eth0            id              --show-secrets  team0-eth0      uuid            virbr0-nic
apath           help            path            team0           team0-eth1 
 virbr0

(2)显示具体网络连接的状态

[root@linuxpao network-scripts]# nmcli -p connection show eth0
===============================================================================
                       Connection profile details (eth0)
===============================================================================
connection.id:                          eth0
connection.uuid:                        e5771ae1-0195-44d4-ab6a-bdd2dfb1c8e5
connection.interface-name:              eth0
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.timestamp:                   1473145305
connection.read-only:                   no
connection.permissions:                 
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:                 
connection.gateway-ping-timeout:        0
connection.metered:                     unknown
-------------------------------------------------------------------------------
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-3-ethernet.mac-address:             --
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.mac-address-blacklist:   
802-3-ethernet.mtu:                     auto
802-3-ethernet.s390-subchannels:        
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:            
802-3-ethernet.wake-on-lan:             1 (default)
802-3-ethernet.wake-on-lan-password:    --
-------------------------------------------------------------------------------
ipv4.method:                            auto
ipv4.dns:                               
ipv4.dns-search:                        
ipv4.addresses:                         
ipv4.gateway:                           --
ipv4.routes:                            
ipv4.route-metric:                      -1
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
-------------------------------------------------------------------------------
ipv6.method:                            auto
ipv6.dns:                               
ipv6.dns-search:                        
ipv6.addresses:                         
ipv6.gateway:                           --
ipv6.routes:                            
ipv6.route-metric:                      -1
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (unknown)
ipv6.dhcp-send-hostname:                yes
ipv6.dhcp-hostname:                     --
-------------------------------------------------------------------------------

(3)显示所有设备状态

[root@linuxpao network-scripts]# nmcli device status 
DEVICE      TYPE      STATE      CONNECTION 
virbr0      bridge    connected  virbr0     
eth0        ethernet  connected  team0-eth0 
eth1        ethernet  connected  team0-eth1 
virbr0-nic  tap       connected  virbr0-nic 
team0       team      connected  team0      
lo          loopback  unmanaged  --

(4)创建新连接default ,IP 自动通过dhcp

[root@linuxpao network-scripts]# nmcli connection add con-name defalut type ethernet ifname eth0
Connection 'defalut' (4cff2e32-14ce-4ece-923e-515727e29c5f) successfully added.

(5)删除网络连接

[root@linuxpao network-scripts]# nmcli con del defalut 
Connection 'defalut' (4cff2e32-14ce-4ece-923e-515727e29c5f) successfully deleted.

(6)创建新连接 static ,指定静态 IP ,不自动连接

[root@linuxpao ~]# nmcli connection add  con-name static ifname eth1 type ethernet autoconnect no ip4 10.1.166.188/16 gw4 10.1.0.1
Connection 'static' (d2f2ec2f-af80-4d0e-b14a-ab0be385bfdb) successfully added.

(7)启用网络连接

[root@linuxpao ~]# nmcli connection up static (禁用即将up改为down)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/15)

[root@linuxpao ~]# ifconfig eth1
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.166.188  netmask 255.255.0.0  broadcast 10.1.255.255
        inet6 fe80::20c:29ff:fe0e:27d7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:0e:27:d7  txqueuelen 1000  (Ethernet)
        RX packets 54579  bytes 5195339 (4.9 MiB)
        RX errors 0  dropped 43368  overruns 0  frame 0
        TX packets 131  bytes 17279 (16.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(8)添加DNS

[root@linuxpao ~]# nmcli connection modify static ipv4.dns 8.8.8.8 
[root@linuxpao ~]# nmcli connection modify static +ipv4.dns 114.114.114.114(+号表示增加,-号表示减少)

(9)添加IP

[root@linuxpao network-scripts]# nmcli connection modify static +ipv4.addresses 172.25.28.10/24
[root@linuxpao network-scripts]# nmcli connection show static
......
ipv4.method:                            manual
ipv4.dns:                               8.8.8.8,114.114.114.114
ipv4.dns-search:                        
ipv4.addresses:                         10.1.166.188/16, 172.25.28.10/24
ipv4.gateway:                           10.1.0.1
ipv4.routes:                            
ipv4.route-metric:                      -1
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
......

(10)使修改生效

systemctl restart network 或 nmcli con reload

(11)禁用网卡,防止被自动激活

[root@linuxpao network-scripts]# nmcli device disconnect eth1 
Device 'eth1' successfully disconnected.

原创文章,作者:pao,如若转载,请注明出处:http://www.178linux.com/45146

(2)
paopao
上一篇 2016-09-10 13:21
下一篇 2016-09-10 13:47

相关推荐

  • sed工具的基本用法

    sed -n(不输出模式,不自动打印) sed -d(删除匹配到的内容) sed-e(多点编辑) sed-r(支持扩展正则表达式) sed#,#(地址定界,在此范围中匹配) sed 1~2   sed 2~2 (找奇数行和偶数行) sed =(为打印到的内容编辑行号)  sed!(取反) sed s///(查找替换,支持使用其他…

    Linux干货 2017-05-02
  • 计算机浅谈及Linux简谈

    一、计算机浅谈: 电子计算机(英语:computer),亦称电脑,是一种利用电子学原理,根据一系列指令对数据进行处理的工具。 在现代,机械计算机的应用已经完全被电子计算机所替换,其所相关的技术研究叫计算机科学,而“计算机技术”指的是将计算机科学的成果应用于工程实践所派生的诸多技术性和经验性成果的总合。“计算机技术”与“计算机科学”是两个相关而又不同的概念,它…

    Linux干货 2016-11-29
  • shell脚本编程之函数、case语句

    一、函数 调用函数前必须先定义,调用时仅使用函数名即可。 语法: function  函数名 {      函数体 } 也可表示为: 函数名 () { 函数体 } 示例:写一个脚本,判定网络内有哪些主机在线,在线的用绿色显示,不在线的用红示表示: c_net&…

    Linux干货 2015-08-24
  • linux —————-目录配配置【FHS】

    linux 目录配配置【FHS】 /根目录的意义与内容 根目录是整个系统最重要的一个目录;根目录与开机、还原、系统修复等操作有关 FHS建议标准: 根目录所在的分区应该越小越好,且应用程序所安装的软件最好不要与根目录放在同一个分区内,保持根目录越小越好 FHS定义的目录含义 /bin :系统存放执行文件的目录,但是bin比较特殊【bin放置的是在单用户维护模…

    Linux干货 2017-04-17
  • 马哥教育网络班21期+第12周课程练习

    httpd学习前知识必备: I/O类型 同步和异步 synchronous asynchronous  关注的是消息通知机制,如何通知调用者,站在被调用者的角度 同步:调用发出后不会立即返回,一旦返回即是最终结果 异步:调用发出后会立即返回消息,但不是最终结果,被调用者通过状态,通知机制,或回调函数处理结果 阻塞和非阻塞 block nonbloc…

    Linux干货 2016-08-15
  • Linux进程和计划任务

                                                      &nbsp…

    系统运维 2016-09-21

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-10 14:07

    文章思路清晰,结构命令,排版整洁。