ip

網絡配置工具

補充說明

ip命令 用來顯示或操縱Linux主機的路由、網絡設備、策略路由和隧道,是Linux下較新的功能強大的網絡配置工具。

語法

1ip(選項)(對象)
2Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
3       ip [ -force ] -batch filename

對象

 1OBJECT := { link | address | addrlabel | route | rule | neigh | ntable |
 2       tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm |
 3       netns | l2tp | macsec | tcp_metrics | token }
 4       
 5-V:顯示指令版本信息;
 6-s:輸出更詳細的信息;
 7-f:強制使用指定的協議族;
 8-4:指定使用的網絡層協議是IPv4協議;
 9-6:指定使用的網絡層協議是IPv6協議;
10-0:輸出信息每條記錄輸出一行,即使內容較多也不換行顯示;
11-r:顯示主機時,不使用IP地址,而使用主機的域名。

選項

 1OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
 2        -h[uman-readable] | -iec |
 3        -f[amily] { inet | inet6 | ipx | dnet | bridge | link } |
 4        -4 | -6 | -I | -D | -B | -0 |
 5        -l[oops] { maximum-addr-flush-attempts } |
 6        -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |
 7        -rc[vbuf] [size] | -n[etns] name | -a[ll] }
 8        
 9網絡對象:指定要管理的網絡對象;
10具體操作:對指定的網絡對象完成具體操作;
11help:顯示網絡對象支持的操作命令的幫助信息。

實例

 1ip link show                    # 顯示網絡接口信息
 2ip link set eth0 up             # 開啓網卡
 3ip link set eth0 down            # 關閉網卡
 4ip link set eth0 promisc on      # 開啓網卡的混合模式
 5ip link set eth0 promisc offi    # 關閉網卡的混合模式
 6ip link set eth0 txqueuelen 1200 # 設置網卡隊列長度
 7ip link set eth0 mtu 1400        # 設置網卡最大傳輸單元
 8ip addr show     # 顯示網卡IP信息
 9ip addr add 192.168.0.1/24 dev eth0 # 爲eth0網卡添加一個新的IP地址192.168.0.1
10ip addr del 192.168.0.1/24 dev eth0 # 爲eth0網卡刪除一個IP地址192.168.0.1
11
12ip route show # 顯示系統路由
13ip route add default via 192.168.1.254   # 設置系統默認路由
14ip route list                 # 查看路由信息
15ip route add 192.168.4.0/24  via  192.168.0.254 dev eth0 # 設置192.168.4.0網段的網關爲192.168.0.254,數據走eth0接口
16ip route add default via  192.168.0.254  dev eth0        # 設置默認網關爲192.168.0.254
17ip route del 192.168.4.0/24   # 刪除192.168.4.0網段的網關
18ip route del default          # 刪除默認路由
19ip route delete 192.168.1.0/24 dev eth0 # 刪除路由

用ip命令顯示網絡設備的運行狀態

1[root@localhost ~]# ip link list
21: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
3    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
42: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
5    link/ether 00:16:3e:00:1e:51 brd ff:ff:ff:ff:ff:ff
63: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
7    link/ether 00:16:3e:00:1e:52 brd ff:ff:ff:ff:ff:ff

顯示更加詳細的設備信息

 1[root@localhost ~]# ip -s link list
 21: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
 3    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 4    RX: bytes  packets  errors  dropped overrun mcast   
 5    5082831    56145    0       0       0       0      
 6    TX: bytes  packets  errors  dropped carrier collsns
 7    5082831    56145    0       0       0       0      
 82: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
 9    link/ether 00:16:3e:00:1e:51 brd ff:ff:ff:ff:ff:ff
10    RX: bytes  packets  errors  dropped overrun mcast   
11    3641655380 62027099 0       0       0       0      
12    TX: bytes  packets  errors  dropped carrier collsns
13    6155236    89160    0       0       0       0      
143: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
15    link/ether 00:16:3e:00:1e:52 brd ff:ff:ff:ff:ff:ff
16    RX: bytes  packets  errors  dropped overrun mcast   
17    2562136822 488237847 0       0       0       0      
18    TX: bytes  packets  errors  dropped carrier collsns
19    3486617396 9691081  0       0       0       0     

顯示核心路由表

1[root@localhost ~]# ip route list 
2112.124.12.0/22 dev eth1  proto kernel  scope link  src 112.124.15.130
310.160.0.0/20 dev eth0  proto kernel  scope link  src 10.160.7.81
4192.168.0.0/16 via 10.160.15.247 dev eth0
5172.16.0.0/12 via 10.160.15.247 dev eth0
610.0.0.0/8 via 10.160.15.247 dev eth0
7default via 112.124.15.247 dev eth1

顯示鄰居表

1[root@localhost ~]# ip neigh list
2112.124.15.247 dev eth1 lladdr 00:00:0c:9f:f3:88 REACHABLE
310.160.15.247 dev eth0 lladdr 00:00:0c:9f:f2:c0 STALE

獲取主機所有網絡接口

1ip link | grep -E '^[0-9]' | awk -F: '{print $2}'

來源:https://github.com/jaywcjlove/linux-command

最後修改於: Wednesday, January 31, 2024

相關文章:

翻譯: