0%

Linux 之 ip 命令

Linux ip命令

Linux ip 命令与 ifconfig 命令类似,但比 ifconfig 命令更加强大,主要用于显示或设置网络设备。

已经在Linux 2.2 加入到了内核。所以ip是加强版的网络配置工具,用来替代ifconfig并强化其他功能。

官方定义为:

ip - show / manipulate routing, devices, policy routing and tunnels

对于这个命令,命令集是相当的多。先说一些基础的,其他就要自己摸索了。

使用方法为:

1
2
3
4
5
6
7
8
9
$ ip [ OPTIONS ] OBJECT { COMMAND | help }

$ ip [ -force ] -batch filename

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

# OPTIONS的取值
# 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] { maximum-addr-flush-attempts } | -o[neline] | -rc[vbuf] [size] | -t[imestamp] | -ts[hort] | -n[etns] name | -a[ll] | -c[olor] }

COMMAND的值主要取决于OBJECT,可能有所不同,一般可以使用adddeleteshow(或者list),均可以输入help来进行查询。

OBJECT中常用的为:

  • link 网络设备
  • address 设备上的协议地址
  • -s, -stats, -statistics 统计化输出

显示网络设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 显示网络设备
$ ip link show
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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

# 显示IP等更多信息
$ 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: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.1.123/24 brd 192.168.254.255 scope global noprefixroute eno1
valid_lft forever preferred_lft forever
inet6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/64 scope global noprefixroute
valid_lft forever preferred_lft forever
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

命令中的show为默认,也可以直接使用ip link或者ip address,结果一致。

设置IP地址

可以通过ip addr add/del xxx.xxx.xxx.xxx dev interface 来设置或者删除IP地址。

如下设置or删除eth0的IP地址。

1
2
3
4
5
# 设置IP地址
$ ip addr add 192.168.0.1/24 dev eth0

# 删除IP地址
$ ip addr del 192.168.0.1/24 dev eth0

启动关闭网卡

与ifconfig类似,也使用up与down来进行启动和关闭,具体如下:

1
2
3
4
5
# 开启网卡
$ ip link set eth0 up

# 关闭网卡
$ ip link set eth0 down

统计方便阅读

选项-s可以统计一些信息方便我们阅读,如下看看网络的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ 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
871883256468 251700492 0 0 0 0
TX: bytes packets errors dropped carrier collsns
871883256468 251700492 0 0 0 0
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
64930085920632 50955323447 0 613156 0 472190933
TX: bytes packets errors dropped carrier collsns
17534345850354 17448077191 0 0 0 0
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx 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

可以看到对输出进行了一些格式化,看起来更直观。

#TODO

OBJECT 取值含义如下:

  • addrlabel:协议地址选择的标签配置
  • route:路由表条目
  • rule:路由策略数据库中的规则

OPTIONS 为常用选项,值可以是以下几种:

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
ip link set eth0 promisc on      # 开启网卡的混合模式
ip link set eth0 promisc offi # 关闭网卡的混个模式
ip link set eth0 txqueuelen 1200 # 设置网卡队列长度
ip link set eth0 mtu 1400 # 设置网卡最大传输单元

ip route show # 显示系统路由
ip route add default via 192.168.1.254 # 设置系统默认路由
ip route list # 查看路由信息
ip route add 192.168.4.0/24 via 192.168.0.254 dev eth0 # 设置192.168.4.0网段的网关为192.168.0.254,数据走eth0接口
ip route add default via 192.168.0.254 dev eth0 # 设置默认网关为192.168.0.254
ip route del 192.168.4.0/24 # 删除192.168.4.0网段的网关
ip route del default # 删除默认路由
ip route delete 192.168.1.0/24 dev eth0 # 删除路由

用 ip 命令显示网络设备的运行状态:

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

显示核心路由表:

1
2
3
4
5
6
7
[root@localhost ~]# ip route list 
112.124.12.0/22 dev eth1 proto kernel scope link src 112.124.15.130
10.160.0.0/20 dev eth0 proto kernel scope link src 10.160.7.81
192.168.0.0/16 via 10.160.15.247 dev eth0
172.16.0.0/12 via 10.160.15.247 dev eth0
10.0.0.0/8 via 10.160.15.247 dev eth0
default via 112.124.15.247 dev eth1

显示邻居表:

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

获取主机所有网络接口炫技

1
ip link | grep -E '^[0-9]' | awk -F: '{print $2}'
   -h, -human, -human-readable
          output statistics with human readable values followed by suffix.

   -b, -batch <FILENAME>
          Read commands from provided file or standard input and invoke them.  First failure will cause termination of ip.

   -force Don't terminate ip on errors in batch mode.  If there were any errors during execution of the commands, the application return code will be non zero.




-d, -details
Output more detailed information.

   -l, -loops <COUNT>
          Specify maximum number of loops the 'ip address flush' logic will attempt before giving up. The default is 10.  Zero (0) means loop until all addresses are
          removed.

   -f, -family <FAMILY>
          Specifies the protocol family to use. The protocol family identifier can be one of inet, inet6, bridge, ipx, dnet, mpls or link.  If this option is not present,
          the protocol family is guessed from other arguments. If the rest of the command line does not give enough information to guess the family, ip falls back to the
          default one, usually inet or any.  link is a special family identifier meaning that no networking protocol is involved.


​ -o, -oneline
​ output each record on a single line, replacing line feeds with the ‘' character. This is convenient when you want to count records with wc(1) or to grep(1) the
​ output.

-r, -resolve use the system’s name resolver to print DNS names instead of host addresses.


​ -n, -netns
​ switches ip to the specified network namespace NETNS. Actually it just simplifies executing of:

ip netns exec NETNS ip [ OPTIONS ] OBJECT { COMMAND | help }

              to

              ip -n[etns] NETNS [ OPTIONS ] OBJECT { COMMAND | help }

       -a, -all
              executes specified command over all objects, it depends if command supports this option.

       -c, -color
              Use color output.

       -t, -timestamp
              display current time when using monitor option.

       -ts, -tshort
              Like -timestamp, but use shorter format.

       -rc, -rcvbuf<SIZE>
              Set the netlink socket receive buffer size, defaults to 1MB.

       -iec   print human readable rates in IEC units (e.g. 1Ki = 1024).

IP - COMMAND SYNTAX
   OBJECT

       addrlabel
              - label configuration for protocol address selection.

       l2tp   - tunnel ethernet over IP (L2TPv3).

       maddress
              - multicast address.

       monitor
              - watch for netlink messages.

       mroute - multicast routing cache entry.

       mrule  - rule in multicast routing policy database.

       neighbour
              - manage ARP or NDISC cache entries.

       netns  - manage network namespaces.

       ntable - manage the neighbor cache's operation.


​ route - routing table entry.

rule - rule in routing policy database.

   tcp_metrics/tcpmetrics
          - manage TCP Metrics

   token  - manage tokenized interface identifiers.

   tunnel - tunnel over IP.

   tuntap - manage TUN/TAP devices.

   xfrm   - manage IPSec policies.

   The names of all objects may be written in full or abbreviated form, for example address can be abbreviated as addr or just a.

处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道