Linux 中如何使用 IP 命令
老版本的 Linux 中都是使用
ifconfig
命令檢查和配置網路介面,但是該命令目前已經沒有維護了,取而代之的是
ip
命令
ip
命令和
ifconfig
命令很相似,但是 相比起來,
ip
命令的功能更強大,它能執行多種
ifconfig
命令無法執行的任務
本文將介紹
ip
命令常用的一些操作
顯示所有網路介面資訊
輸入
ip addr
或者
ip a
命令可以顯示所有的網路介面資訊
[root@ecs-centos-7 ~]# ip addr
1: lo:
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: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
inet 192。168。0。9/24 brd 192。168。0。255 scope global noprefixroute dynamic eth0
valid_lft 74409sec preferred_lft 74409sec
inet6 fe80::f816:3eff:fee0:4fb8/64 scope link
valid_lft forever preferred_lft forever
[root@ecs-centos-7 ~]#
上面的輸出中,可以看出系統目前的網路介面分別是
lo
和
eth0
, 前者是網路環回介面,後者是常規的網路介面
輸出介面資訊的 欄位詳解
UP 網路介面處於開啟狀態
LOWER_UP 表示網路電纜已插入並且已連上網路
mtu 1500: 最大傳輸的單位1500位元組
qdisc pfifo_fast: 資料包排隊
state UP: 介面狀態已開啟
qlen 1000: 傳輸佇列長度
link/ether fa:16:3e:e0:4f:b8 Mac地址
brd ff:ff:ff:ff:ff:ff 廣播地址
inet 192。168。0。9/24 : IPV4
scope global: 全域性有效
dynamic eth0: 動態分配地址
valid_lft 74409sec: IPV4有效期
preferred_lft 74409sec: IPV4首選生存期
inet6 fe80::f816:3eff:fee0:4fb8/64 IPV6
scope link: 僅在此介面上生效
除了輸出所有的網路介面資訊外,還可以按照以下方式個性化輸出網路介面資訊
按照是否支援 IPV4 或 IPV6輸出
命令
ip -4 a
會輸出只支援IPV4的介面資訊
同樣,命令
ip -6 a
會輸出只支援IPV6的介面資訊
按照介面名字輸出
命令
ip a show eth0
會輸出
eth0
介面的資訊
[root@ecs-centos-7 ~]# ip a show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
inet 192。168。0。9/24 brd 192。168。0。255 scope global noprefixroute dynamic eth0
valid_lft 73494sec preferred_lft 73494sec
inet6 fe80::f816:3eff:fee0:4fb8/64 scope link
valid_lft forever preferred_lft forever
按照網路介面開啟和關閉狀態輸出
命令
ip link ls up
會輸出所有處於開啟狀態的網路介面資訊
那麼,
ip link ls down
就會輸出所有處於關閉狀態的網路介面資訊
為網路介面 新增、移除 IP地址
新增IP
為指定網路介面分配IP地址的命令格式:
ip a add ip地址 dev 介面名字
比如:現在要為
eth0
網路介面分配一個網路掩碼為 20 的IP
192。168。1。10
, 具體的輸入請看下面的例項
[root@ecs-centos-7 ~]# ip a add 192。168。1。10/20 dev eth0
[root@ecs-centos-7 ~]# ip a show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
inet 192。168。0。9/24 brd 192。168。0。255 scope global noprefixroute dynamic eth0
valid_lft 71787sec preferred_lft 71787sec
inet 192。168。1。10/20 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::f816:3eff:fee0:4fb8/64 scope link
valid_lft forever preferred_lft forever
從上面例子的輸出結果可以看出,網路介面
eth0
上多了一個
192。168。1。10
的 IP
命令
ip a add 192。168。1。10/20 dev eth0
執行成功的話,是沒有任何輸出,如果網路介面
eth0
不存在的話,會輸出
Cannot find device “eth0”
的錯誤資訊
其實,還可以透過執行
ping 192。168。1。10
命令來測試網路介面上IP是否分配成功,如果能 ping 通IP的話,表示分配成功,否則表示失敗
說明:192。168。1。10/20 中 20 是 CIDR 表示法的網路掩碼,想詳細瞭解的可以自行查閱網路掩碼的資料
移除IP
網路介面上添加了IP之後,可使用
ip a del ip地址 dev 介面名字
命令從指定的介面移除指定的IP,移除的時候IP後面需要攜帶網路掩碼
[root@ecs-centos-7 ~]# ip a del 192。168。1。10/20 dev eth0
[root@ecs-centos-7 ~]# ip a show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
inet 192。168。0。9/24 brd 192。168。0。255 scope global noprefixroute dynamic eth0
valid_lft 71414sec preferred_lft 71414sec
inet6 fe80::f816:3eff:fee0:4fb8/64 scope link
valid_lft forever preferred_lft forever
修改網路介面狀態
要關閉或者開啟網路介面,可以使用
ip link set dev 介面名字 up/down
命令
例如:有
lo
和
eth0
兩個網路介面, 具體介面資訊如下
[root@ecs-centos-7 ~]# ip a
1: lo:
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: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
inet 192。168。0。9/24 brd 192。168。0。255 scope global noprefixroute dynamic eth0
valid_lft 59216sec preferred_lft 59216sec
inet6 fe80::f816:3eff:fee0:4fb8/64 scope link
valid_lft forever preferred_lft forever
透過
ping -w 3 127。0。0。1
命令測試,結果表明
lo
介面目前是開啟狀態,具體的測試輸出如下所示
[root@ecs-centos-7 ~]# ping -w 3 127。0。0。1
PING 127。0。0。1 (127。0。0。1) 56(84) bytes of data。
64 bytes from 127。0。0。1: icmp_seq=1 ttl=64 time=0。014 ms
64 bytes from 127。0。0。1: icmp_seq=2 ttl=64 time=0。026 ms
64 bytes from 127。0。0。1: icmp_seq=3 ttl=64 time=0。022 ms
64 bytes from 127。0。0。1: icmp_seq=4 ttl=64 time=0。023 ms
——- 127。0。0。1 ping statistics ——-
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0。014/0。021/0。026/0。005 ms
現在關閉
lo
網路介面,並再次測試該介面的狀態,具體命令如下:
[root@ecs-centos-7 ~]# ip link set dev lo down
[root@ecs-centos-7 ~]# ip link show lo
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@ecs-centos-7 ~]# ping -w 3 127。0。0。1
PING 127。0。0。1 (127。0。0。1) 56(84) bytes of data。
——- 127。0。0。1 ping statistics ——-
4 packets transmitted, 0 received, 100% packet loss, time 2999ms
執行
ip link set dev lo down
命令之後,關閉了網路介面
lo
,透過
ip link show lo
命令檢視該介面的資訊,結果表明介面是關閉狀態,再次透過
ping -w 3 127。0。0。1
測試介面的狀態,再一次確認了介面是關閉狀態
修改網路介面傳輸佇列長度
透過
p l set txqueuelen 長度 dev 介面名字
命令可以設定網路介面的傳輸佇列長度
[root@ecs-centos-7 ~]# ip l show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
[root@ecs-centos-7 ~]# ip l set txqueuelen 1000 dev eth0
[root@ecs-centos-7 ~]# ip l show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
上面的例子中,字串
qlen 1200
表示傳輸佇列長度是
1200
從例子中可以看出,修改傳輸佇列長度之前,傳輸佇列長度是
1200
執行
ip l set txqueuelen 1000 dev eth0
命令把傳輸佇列長隊修改為
1000
, 然後再執行
ip l show eth0
命令查詢
eth0
網路介面的資訊,查詢結果中
default qlen 1000
說明修改成功
修改網路介面的MTU(最大傳輸單元)
透過
p l set mtu 長度 dev 介面名字
命令可以設定網路介面的最大傳輸單元
[root@ecs-centos-7 ~]# ip l show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
[root@ecs-centos-7 ~]# ip l set set mtu 1500 dev eth0
[root@ecs-centos-7 ~]# ip l show eth0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
上面的例子中,字串
mtu 1600
表示最大傳輸單元是
1600
從例子中可以看出,修改傳輸佇列長度之前,最大傳輸單元是
1600
執行
ip l set set mtu 1500 dev eth0
命令把最大傳輸單元修改為
1500
, 然後再執行
ip l show eth0
命令查詢
eth0
網路介面的資訊,查詢結果中
mtu 1500
說明修改成功
檢視IP路由表
下面幾條命令都可以檢視 IP 路由表
ip r
ip route
ip r list
ip route list
檢視路由表
[root@ecs-centos-7 ~]# ip r
default via 192。168。0。1 dev eth0 proto dhcp metric 100
169。254。169。254 via 192。168。0。254 dev eth0 proto dhcp metric 100
192。168。0。0/24 dev eth0 proto kernel scope link src 192。168。0。9 metric 100
檢視指定IP以及網路掩碼的路由表
[root@ecs-centos-7 ~]# ip r list 192。168。0。0/24
192。168。0。0/24 dev eth0 proto kernel scope link src 192。168。0。9 metric 100
檢視ARP快取
ARP是 Address Resolution Protocol 的縮寫, 全稱是地址解析協議,它是用於實現從IP地址到Mac地址的對映,也就是詢問目標IP的Mac地址
在本地儲存了一份IP對應Mac地址的對映表,這張表叫做 ARP快取表,可以使用
ip n
命令檢視快取表
[root@ecs-centos-7 ~]# ip n
192。168。0。1 dev eth0 lladdr fa:16:3e:64:38:a8 REACHABLE
192。168。0。254 dev eth0 lladdr fa:fa:fa:fa:fa:01 STALE
更改網絡卡Mac地址
大部分場景中是不需要修改Mac地址的,但有時你可能想隱藏真實的Mac地址(物理地址),這時可以修改Mac地址,修改Mac地址的命令如下
ip link set dev 介面名 address 新的Mac地址
下面以修改
lo
網路介面的Mac地址為例來說明
[root@ecs-centos-7 ~]# ip l list lo
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@ecs-centos-7 ~]# ip l set lo address fa:16:3e:e0:4f:b4
[root@ecs-centos-7 ~]# ip l list lo
1: lo:
link/loopback fa:16:3e:e0:4f:b4 brd 00:00:00:00:00:00
從上面的例子中可以看出,
lo
介面初始的Mac地址是
00:00:00:00:00:00
執行
ip l set lo address fa:16:3e:e0:4f:b4
命令修改Mac地址,然後再執行
ip l list lo
檢視
lo
介面的資訊,輸出的結果資訊表明Mac地址修改成功
注意:如果當前網路介面上執行上面的命令,當前會話會中斷,需要重啟機器才能恢復,所以最好是在其他的網路介面上測試
檢視網路統計資訊
透過
ip -s link
命令可以檢視網路介面上的一些統計資料,比如介面上傳輸的位元組數、報文數,錯誤或者丟棄的報文數等等
[root@ecs-centos-7 ~]# ip -s link
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
272 2 0 0 0 0
TX: bytes packets errors dropped carrier collsns
272 2 0 0 0 0
2: eth0:
link/ether fa:16:3e:e0:4f:b8 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
47949 531 0 0 0 0
TX: bytes packets errors dropped carrier collsns
50816 492 0 0 0 0
[root@ecs-centos-7 ~]#
想檢視更詳細的資訊可以使用
ip -s -s link
命令, 這個在排除網路故障時經常用到
小結
本文介紹了
ip
命令的常見用法,更多的用法請使用
man ip
命令檢視
碼字不易,走過路過麻煩點個贊,謝謝!