next up previous contents
Next: 主機名稱設定 Up: IPv4 網路設定 Previous: *nmcli 修改網卡設定   Contents

nmcli 設定網卡

  1. 當使用 nmcli connection modify 無法修改 ipv4.gateway 時,可以刪除目前的 connection 再新增。查看網卡,除了 lo 外,有一張網卡 ens3。
    [root@kvm7 ~]# nmcli device show 
    GENERAL.DEVICE:                         ens3
    GENERAL.TYPE:                           ethernet
    GENERAL.HWADDR:                         52:54:00:B4:0A:A3
    GENERAL.MTU:                            1500
    GENERAL.STATE:                          30 (disconnected)
    GENERAL.CONNECTION:                     --
    GENERAL.CON-PATH:                       --
    WIRED-PROPERTIES.CARRIER:               on
    
    GENERAL.DEVICE:                         lo
    GENERAL.TYPE:                           loopback
    GENERAL.HWADDR:                         00:00:00:00:00:00
    GENERAL.MTU:                            65536
    GENERAL.STATE:                          10 (unmanaged)
    GENERAL.CONNECTION:                     --
    GENERAL.CON-PATH:                       --
    IP4.ADDRESS[1]:                         ip = 127.0.0.1/8, gw = 0.0.0.0
    IP6.ADDRESS[1]:                         ip = ::1/128, gw = ::
    
  2. 查詢 ens3 網卡設定為 dhcp。
    [root@kvm7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens3
    TYPE=Ethernet
    BOOTPROTO=dhcp
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_FAILURE_FATAL=no
    NAME=ens3
    UUID=e08e9bff-28d9-4ee3-8215-034a103a474e
    DEVICE=ens3
    ONBOOT=yes
    
  3. 刪除 ens3 網卡的連線,實際上是刪除網卡設定檔 /etc/sysconfig/network-script/ifcfg-ens3。
    [root@kvm7 ~]# nmcli connection delete ens3 
    [root@kvm7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens3
    cat: /etc/sysconfig/network-scripts/ifcfg-ens3: No such file or directory
    
  4. 增加 ens3 網卡的連線 ip 192.168.122.7,遮罩 255.255.255.0,閘道 192.168.122.1。
    [root@kvm7 ~]# nmcli connection add con-name ens3 type ethernet ifname ens3 ip4 192.168.122.7/24 gw4 192.168.122.1
    Connection 'ens3' (69bf76bb-71d3-4704-8b04-f09e21d9796d) successfully added.
    
  5. 實際上是編輯網卡設定檔 /etc/sysconfig/network-script/ifcfg-ens3。
    [root@kvm7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens3
    TYPE=Ethernet
    BOOTPROTO=none
    IPADDR0=192.168.122.7
    PREFIX0=24
    GATEWAY0=192.168.122.1
    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=ens3
    UUID=69bf76bb-71d3-4704-8b04-f09e21d9796d
    DEVICE=ens3
    ONBOOT=yes
    inactive
    
  6. 成功啟動 ens3 網卡的連線。
    [root@kvm7 ~]# nmcli connection up ens3
    Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
    
  7. 查看 ens3 ip 與設定相同。
    [root@kvm7 ~]# ip addr show ens3 
    2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 52:54:00:b4:0a:a3 brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.7/24 brd 192.168.122.255 scope global ens3
           valid_lft forever preferred_lft forever
        inet6 fe80::5054:ff:feb4:aa3/64 scope link 
           valid_lft forever preferred_lft forever
    
  8. 查看 ens3 連線詳細狀況,其中 DNS 沒設定。
    [root@kvm7 ~]# nmcli connection show ens3
    connection.id:                          ens3
    connection.uuid:                        69bf76bb-71d3-4704-8b04-f09e21d9796d
    connection.interface-name:              ens3
    connection.type:                        802-3-ethernet
    connection.autoconnect:                 yes
    connection.timestamp:                   1408114329
    connection.read-only:                   no
    connection.permissions:                 
    connection.zone:                        --
    connection.master:                      --
    connection.slave-type:                  --
    connection.secondaries:                 
    connection.gateway-ping-timeout:        0
    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:            
    ipv4.method:                            manual
    ipv4.dns:                               
    ipv4.dns-search:                        
    ipv4.addresses:                         { ip = 192.168.122.7/24, gw = 192.168.122.1 }
    ipv4.routes:                            
    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.routes:                            
    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-hostname:                     --
    GENERAL.NAME:                           ens3
    GENERAL.UUID:                           69bf76bb-71d3-4704-8b04-f09e21d9796d
    GENERAL.DEVICES:                        ens3
    GENERAL.STATE:                          activated
    GENERAL.DEFAULT:                        yes
    GENERAL.DEFAULT6:                       no
    GENERAL.VPN:                            no
    GENERAL.ZONE:                           --
    GENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/2
    GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/6
    GENERAL.SPEC-OBJECT:                    --
    GENERAL.MASTER-PATH:                    --
    IP4.ADDRESS[1]:                         ip = 192.168.122.7/24, gw = 192.168.122.1
    IP6.ADDRESS[1]:                         ip = fe80::5054:ff:feb4:aa3/64, gw = ::
    active
    
  9. ens3 連線設定 dns 192.168.122.1。
    [root@kvm7 ~]# nmcli connection modify ens3 ipv4.dns 192.168.122.1
    
  10. 設定 dns 後,可以再次啟動 ens3 網卡的連線,以確保 dns 生效。
    [root@kvm7 ~]# nmcli connection up ens3
    Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
    
  11. 再查看 ipv4 的 DNS 為 192.168.122.1。
    [root@kvm7 ~]# nmcli connection show ens3 | grep dns
    ipv4.dns:                               192.168.122.1
    ipv4.dns-search:                        
    ipv4.ignore-auto-dns:                   no
    ipv6.dns:                               
    ipv6.dns-search:                        
    ipv6.ignore-auto-dns:                   no
    
  12. 設定網路連線度操作示範



2017-07-10