1. 程式人生 > >NetworkManager管理裝置與連線情況

NetworkManager管理裝置與連線情況

二、nmcli管理裝置與連線情況

  1. 檢視裝置狀態
[root@localhost ~]# nmcli device 
DEVICE       TYPE      STATE      CONNECTION  
virbr0       bridge    connected  virbr0      
eno16777736  ethernet  connected  eno16777736 
virbr0-nic   ethernet  connected  virbr0-nic  
lo           loopback  unmanaged  --          
[root@localhost
~]#
  1. 檢視指定裝置狀態
[[email protected] ~]# nmcli device show eno16777736 
GENERAL.DEVICE:                         eno16777736
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:69:1F:D8
GENERAL.MTU:                            1500
GENERAL.STATE:                          100
(connected) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 192.168.1.4/24 IP4.GATEWAY: 192.168.1.1 IP4.DNS
[1]: 202.101.172.35 IP4.DNS[2]: 8.8.8.8 IP6.ADDRESS[1]: fe80::20c:29ff:fe69:1fd8/64 IP6.GATEWAY: fe80::1 [[email protected] ~]#
  1. 檢視連結狀態
[root@localhost ~]# nmcli connection 
add     delete  down    edit    help    load    modify  reload  show    up      
[root@localhost ~]# nmcli connection 
NAME         UUID                                  TYPE            DEVICE      
virbr0-nic   7fb8f43e-926a-42a9-a205-cd6ef7033f3c  802-3-ethernet  virbr0-nic  
virbr0       72125c4c-417e-47ba-bd21-45d7162fb40e  bridge          virbr0      
eno16777736  be3b17c4-48df-4513-aa1d-a653b7a57cc2  802-3-ethernet  eno16777736 
[root@localhost ~]#
  1. 查詢所有活動的連線
[[email protected] ~]# nmcli connection show --active 
NAME         UUID                                  TYPE            DEVICE      
virbr0-nic   7fb8f43e-926a-42a9-a205-cd6ef7033f3c  802-3-ethernet  virbr0-nic  
virbr0       72125c4c-417e-47ba-bd21-45d7162fb40e  bridge          virbr0      
eno16777736  be3b17c4-48df-4513-aa1d-a653b7a57cc2  802-3-ethernet  eno16777736 
[[email protected] ~]#  
  1. 檢視指定ID連線的詳細情況
 [root@localhost ~]# nmcli connection show eno16777736
  1. 使用nmcli建立新的連線(不是物理裝置)

    • 定義一個名為ens33-dhcp的連線,配置動態IP地址,對應的裝置是eno16777736,設定開機自啟動,連線型別是乙太網,iPv4地址自動獲取
// 其實就是在/etc/sysconfig/network-scripts/下新增了一個ifcfg-ens33-dhcp的配置檔案
[root@localhost ~]# nmcli connection add con-name ens33-dhcp ifname eno16777736 autoconnect yes type ethernet
Connection 'ens33-dhcp' (e5edbcf2-b4b4-4922-9390-b8d67409e52c) successfully added.
[root@localhost ~]# 

// 當想要將連線切換到ens33-dhcp時:
[root@localhost ~]# nmcli connection up ens33-dhcp 

// 刪除連線eno16777736
[root@localhost ~]# nmcli connection delete ens33-dhcp 
  • 定義一個名為static的連線,配置靜態IP,掩碼,閘道器等
// 我們先新增一個網絡卡裝置:ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:69:1f:e2  txqueuelen 1000  (Ethernet)

// 設定名為ens34-static的靜態連線(BOOTPROTO=sataic需要在配置檔案中改),
[[email protected] ~]# nmcli connection add con-name ens34-static ifname ens38 autoconnect yes type ethernet gw4 192.168.1.1 ip4 192.168.1.7
Connection 'ens34-static' (02b15e6c-33d6-475f-b854-2a4fe2f39a60) successfully added.
[[email protected] ~]# 
  • 啟用static連線(預設連線不自動啟用,處於斷開狀態)
    一般配置完成後就是自動啟用的,如果沒有啟用可以手動啟用
[root@localhost ~]# nmcli connection up ens34-static
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)
[root@localhost ~]# nmcli connection show 
NAME          UUID                                  TYPE            DEVICE      
virbr0-nic    7fb8f43e-926a-42a9-a205-cd6ef7033f3c  802-3-ethernet  virbr0-nic  
virbr0        72125c4c-417e-47ba-bd21-45d7162fb40e  bridge          virbr0      
eno16777736   be3b17c4-48df-4513-aa1d-a653b7a57cc2  802-3-ethernet  eno16777736 
ens34-static  02b15e6c-33d6-475f-b854-2a4fe2f39a60  802-3-ethernet  ens38       
ens33-dhcp    e5edbcf2-b4b4-4922-9390-b8d67409e52c  802-3-ethernet  --          
[root@localhost ~]# 
  1. 使用++nmcli++修改已有的網路連線屬性(這種方式修改後不用重啟網絡卡)
// 可以修改很多引數,下面只是顯示了,並沒有改。
[root@localhost ~]# nmcli connection modify ens34-static ipv
ipv4.addresses           ipv4.ignore-auto-dns     ipv6.addresses           ipv6.ignore-auto-routes
ipv4.dhcp-client-id      ipv4.ignore-auto-routes  ipv6.dhcp-hostname       ipv6.ip6-privacy
ipv4.dhcp-hostname       ipv4.may-fail            ipv6.dhcp-send-hostname  ipv6.may-fail
ipv4.dhcp-send-hostname  ipv4.method              ipv6.dns                 ipv6.method
ipv4.dns                 ipv4.never-default       ipv6.dns-search          ipv6.never-default
ipv4.dns-search          ipv4.route-metric        ipv6.gateway             ipv6.route-metric
ipv4.gateway             ipv4.routes              ipv6.ignore-auto-dns     ipv6.routes
[root@localhost ~]#

//修改連線後重新載入下連線
[root@localhost ~]# nmcli connection down ens34-static && nmcli connection up ens34-static 
Connection 'ens34-static' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14)
[root@localhost ~]# 
  1. 新增連線方法總結
    • 新增物理網絡卡
    • 拷貝原有的配置檔名稱就是新的連線名
    • 刪除配置檔案裡的UUID,NAME名稱,DEVCE裝置名稱,修改ip等等
    • 在使用nmcli connection reload 重新載入配置檔案
    • 啟用連線nmcli connection up [連線名]
    • 當然我們可以不用拷貝原有連線的配置檔案這麼low的方式,你可以直接用上面教的nmcli connetion add 來實現新增新的連線
    • 最後我們需要檢視修改的ip是否生效:ifconfig