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.www58058.com/45146

(2)
paopao
上一篇 2016-09-10
下一篇 2016-09-10

相關推薦

  • LVM 邏輯卷管理器-增大LV容量

    LVM 邏輯卷管理器-增大LV的容量   步驟: (1)新增一個8e systemID的分區; (2)用pvcreate命令構建PV; (3)用vgextend命令將PV加入vg_1; (4)用lvresize命令將新加入的PV內的PE加入lv_1中; (5)通過resize2fs命令將LV的容量擴充到整個文件系統;   (1)新增一個8…

    Linux干貨 2016-09-19
  • N25期第三周作業

    1.列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可 who|awk ‘{print $1}’|sort -u 2.取出最后登錄到當前系統的用戶的相關信息 last -1 3.取出當前系統上被用戶當作其默認shell的最多的那個shell cat /etc/passwd|awk -F: ‘{print $NF}’|sort…

    Linux干貨 2016-12-12
  • rpm

    軟件運行環境 API:Application Programming Interface     POSIX:Portable OS 程序源代碼–> 預處理–> 編譯–> 匯編–> 鏈接   &nb…

    Linux干貨 2016-08-21
  • Linux程序包管理

      Linux的各個release版本開發商,在發布各種應用程序,以及一些團體發布應用程序時,通常會根據發布的程序所適應的開發語言,使用環境,預設參數等,事先編譯完成一個可以在相應平臺上安裝的程序包供使用者直接使用,該程序包含有安裝前(preinstall)操作系統環境檢測的腳本,程序包中所有文件的相關信息,程序預定義的配置參數文件,程序…

    Linux干貨 2016-11-30
  • N25_第四周作業(補)

    1、復制/etc/skel目錄到/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 [root@EASTED tmp]# cp -r /etc/skel/ /home/tuser1/ [root@EASTED tmp]# cp …

    Linux干貨 2017-01-02
  • 關于高級文件管理系統的幾個實驗

    實驗1:用軟件模擬生成RAID (1)選擇磁盤或者創建新的分區lsblk 選擇bcde四塊磁盤 創建新的分區 每個為5Gfdisk(gdisk) /dev/sdb 交互式方式創建 選擇分區ID為raid 6.7編號不一樣?。?! >可能需要同步內核中的磁盤分區表 centos6中用partx -a centos7中用 partprobe命令 (2)創建R…

    2017-08-12

評論列表(1條)

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

    文章思路清晰,結構命令,排版整潔。

欧美性久久久久