Linux command – ip

測試環境為 CentOS 7 x86_64 (虛擬機)

Oh , No 當初不知是怎麼安裝完成的 RedHat 7 ,沒有安裝到 #ifconfig 所以現在不知道要怎麼看 ip ,後來查了一下還有另外一個指令 # ip 可以來用.

而且我還發現該網路裝置開機後沒有被啟動,所以沒有 IP,所以第一步是需要先設定網路裝置.

現在的網卡命名採用了 一致性的網絡設備命名 Biosdevname & net.ifnames 詳細請參考 – http://benjr.tw/93340 .

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=899772a6-45c4-4559-b6d8-4970f9fcf66e
DEVICE=ens33
ONBOOT=yes

ONBOOT 改成 yes ,儲存用 #service network restart 即可 (新的方式採用 #systemctl restart network). 或是直接用 #NetworkManager 工具來設定.

ip

預設安裝有 ip 這一支工具來檢視網路狀態.

ip [ OPTIONS ] OBJECT { COMMAND | help }

OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | tcp_metrics | token | macsec }

OPTIONS := { -V[ersion] | -h[uman-readable] | -s[tatistics] | -d[etails] | -r[esolve] | -iec | -f[amily] { inet | inet6 | ipx | dnet | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maxi-mum-addr-flush-attempts } | -o[neline] | -rc[vbuf] [size] | -t[imestamp] | -ts[hort] | -n[etns] name | -a[ll] | -c[olor] | -br[ief] | -j[son] | -p[retty] }

常用語法如下:

  • 檢視目前 IP
    [root@localhost ~]# ip address show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:1b:63:14 brd ff:ff:ff:ff:ff:ff
        inet 192.168.111.167/24 brd 192.168.111.255 scope global noprefixroute dynamic ens33
           valid_lft 1738sec preferred_lft 1738sec
        inet6 fe80::cddd:a1cd:13e:ca24/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
        link/ether 52:54:00:d6:fd:8a brd ff:ff:ff:ff:ff:ff
        inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
           valid_lft forever preferred_lft forever
    4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
        link/ether 52:54:00:d6:fd:8a brd ff:ff:ff:ff:ff:ff
    

    如果要設定 static IP Address

    [root@localhost ~]# ip address add 192.168.42.172/24 dev enp0s3
    
  • 檢視網路連線狀況
    [root@localhost ~]# ip link
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
        link/ether 00:0c:29:1b:63:14 brd ff:ff:ff:ff:ff:ff
    3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
        link/ether 52:54:00:d6:fd:8a brd ff:ff:ff:ff:ff:ff
    4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT group default qlen 1000
        link/ether 52:54:00:d6:fd:8a brd ff:ff:ff:ff:ff:ff
    

    加入參數 -s (statistics) 還可以看到 RX (Receive) , TX (Transmit) packets , errors , dropped , overrun , mcast , arrier , collsns 等統計資料.

    [root@localhost ~]# ip -s link
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        RX: bytes  packets  errors  dropped overrun mcast   
        6472       76       0       0       0       0       
        TX: bytes  packets  errors  dropped carrier collsns 
        6472       76       0       0       0       0       
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
        link/ether 00:0c:29:1b:63:14 brd ff:ff:ff:ff:ff:ff
        RX: bytes  packets  errors  dropped overrun mcast   
        51232      403      0       0       0       0       
        TX: bytes  packets  errors  dropped carrier collsns 
        36313      248      0       0       0       0       
    3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
        link/ether 52:54:00:d6:fd:8a brd ff:ff:ff:ff:ff:ff
        RX: bytes  packets  errors  dropped overrun mcast   
        0          0        0       0       0       0       
        TX: bytes  packets  errors  dropped carrier collsns 
        0          0        0       0       0       0       
    4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT group default qlen 1000
        link/ether 52:54:00:d6:fd:8a brd ff:ff:ff:ff:ff:ff
        RX: bytes  packets  errors  dropped overrun mcast   
        0          0        0       0       0       0       
        TX: bytes  packets  errors  dropped carrier collsns 
        0          0        0       0       0       0    
    
  • 檢視 Routing Table
    如果要看 IPV4 Routing table 設定.

    [root@localhost ~]# ip route show
    default via 192.168.111.2 dev ens33 proto dhcp metric 100 
    192.168.111.0/24 dev ens33 proto kernel scope link src 192.168.111.167 metric 100 
    192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 

    如果要看 IPV6 Routing table 設定.

    [root@localhost ~]# ip -6 route show
    unreachable ::/96 dev lo metric 1024 error -113 pref medium
    unreachable ::ffff:0.0.0.0/96 dev lo metric 1024 error -113 pref medium
    unreachable 2002:a00::/24 dev lo metric 1024 error -113 pref medium
    unreachable 2002:7f00::/24 dev lo metric 1024 error -113 pref medium
    unreachable 2002:a9fe::/32 dev lo metric 1024 error -113 pref medium
    unreachable 2002:ac10::/28 dev lo metric 1024 error -113 pref medium
    unreachable 2002:c0a8::/32 dev lo metric 1024 error -113 pref medium
    unreachable 2002:e000::/19 dev lo metric 1024 error -113 pref medium
    unreachable 3ffe:ffff::/32 dev lo metric 1024 error -113 pref medium
    fe80::/64 dev ens33 proto kernel metric 100 pref medium
  • 新增 Routing Table
    [root@localhost ~]# ip route add 10.0.0.0/8 via 192.168.111.2 dev ens33
    [root@localhost ~]# ip route show
    default via 192.168.111.2 dev ens33 proto dhcp metric 100 
    10.0.0.0/8 via 192.168.111.2 dev ens33 
    192.168.111.0/24 dev ens33 proto kernel scope link src 192.168.111.167 metric 100 
    192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 

dhclient

如果環境有 DHCP 這時候就不需要辛苦設定 static IP Address ,直接用 #dhclient 指令即可.

[root@localhost ~]# dhclient enp0s3

ifconfig

如果一開始還是用不習慣 #ip ,還是可以安裝回來 #ifconfig ,安裝 net-tools 套件即可.

[root@localhost ~]# yum whatprovides "*ifconfig"
[root@localhost ~]# yum install net-tools
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料