目录

ip命令

概述

ip 命令对于程序员来说,同样很重要,很多时候一些奇怪的网络问题,就是通过这些简单的工具排查出来的。

ip命令

下面这些命令一般都得再带点选项或者子命令才能执行。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
address - protocol (IP or IPv6) address on a device.
addrlabel - label configuration for protocol address selection.
l2tp - tunnel ethernet over IP (L2TPv3).
link - network device.
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.

ip address

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
EXAMPLES
       ip address show
           Shows IPv4 and IPv6 addresses assigned to all network interfaces. The 'show' subcommand can be omitted.

       ip address show up
           Same as above except that only addresses assigned to active network interfaces are shown.

       ip address show dev eth0
           Shows IPv4 and IPv6 addresses assigned to network interface eth0.

       ip address add 2001:0db8:85a3::0370:7334/64 dev eth1
           Adds an IPv6 address to network interface eth1.

       ip address delete 2001:0db8:85a3::0370:7334/64 dev eth1
           Delete the IPv6 address added above.

       ip address flush dev eth4 scope global
           Removes all global IPv4 and IPv6 addresses from device eth4. Without 'scope global' it would remove all addresses including IPv6 link local ones.

ip addr sh label eth*

1
2
3
4
5
6
7
8
9
# ip addr sh label eth*
2: eth0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 52:54:0a:09:cc:4d brd ff:ff:ff:ff:ff:ff
    inet 10.9.204.77/24 brd 10.9.204.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.9.204.5/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:44:a0:57 brd ff:ff:ff:ff:ff:ff

虚拟网卡

下面是创建虚拟网卡来做测试的例子。

1
2
3
4
# 添加虚拟网卡,并指定网卡别名
ip addr add 192.168.2.11/24 dev eth0 label eth0:0
# 删除虚拟网卡
ip addr del 192.168.2.11/24 dev eth0:0
1
ip addr add 10.10.10.20/24 dev eth1 scope link

上面的[scope {global|link|host}]: 指明作用域【global: 全局可用;link: 仅链接可用;host: 本机可用;】

此外,还可以设置 broadcast 广播地址,如: ip addr add 10.10.20.20 broadcast 10.255.255.255 dev eth1

1
2
3
4
5
6
7
8
9
# 清空eth0网卡的配置
ip addr flush eth0
# 清空eth1上的192.168.2.10/24
ip addr flush 
eth1 to 192.168.2.10/24
# 添加主机路由【不加掩码】
ip route add 192.168.20.2 via 192.168.2.2 dev eth0 src 192.168.2.13
# 添加网络路由【需要加掩码】
ip route add 192.168.20.0/24 via 192.168.2.2 dev eth0 src 192.168.2.13

参考资料

警告
本文最后更新于 2017年2月1日,文中内容可能已过时,请谨慎参考。