ip命令

2021-02-15, updated 2021-09-12

Linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者。使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务。iproute2套件里提供了许多增强功能的命令,ip命令即是其中之一。

参数

route

查看默认路由表

$ ip route
default via 192.168.30.2 dev ens33 
192.168.30.0/24 dev ens33 proto kernel scope link src 192.168.30.128 

查看指定路由表

$ ip route show table main
default via 192.168.30.2 dev ens33 
192.168.30.0/24 dev ens33 proto kernel scope link src 192.168.30.128 

删除指定路由

查看路由表

# ip route
default via 192.168.202.2 dev ens33 
192.168.0.0/24 dev ens38  proto kernel  scope link  src 192.168.0.59 
192.168.202.0/24 dev ens33  proto kernel  scope link  src 192.168.202.129 

删除指定路由

# ip route del 192.168.0.0/24 dev ens38  proto kernel  scope link  src 192.168.0.59

查看删除后路由表

# ip route
default via 192.168.202.2 dev ens33 
192.168.202.0/24 dev ens33  proto kernel  scope link  src 192.168.202.129 

addr

查看ip地址

$ ip addr
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:5a:08:1e brd ff:ff:ff:ff:ff:ff
    inet 192.168.30.128/24 brd 192.168.30.255 scope global dynamic ens33
       valid_lft 75641sec preferred_lft 75641sec
    inet6 fe80::20c:29ff:fe5a:81e/64 scope link 
       valid_lft forever preferred_lft forever

$ ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:5a:08:1e brd ff:ff:ff:ff:ff:ff
    inet 192.168.30.128/24 brd 192.168.30.255 scope global dynamic ens33
       valid_lft 75395sec preferred_lft 75395sec
    inet6 fe80::20c:29ff:fe5a:81e/64 scope link 
       valid_lft forever preferred_lft forever

添加ip地址

ip addr add 1.1.1.1/24 dev ens33

删除ip地址

ip addr del 1.1.1.1/24 dev ens33
$ 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   
    0          0        0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    0          0        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:5a:08:1e brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    2263451    15477    0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    12508398   15167    0       0       0       0       

$ ip -s link show ens33
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:5a:08:1e brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    2337357    16242    0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    12594556   15705    0       0       0       0 

neighbour(arp相关)

查看arp

# ip neigh show
192.168.0.20 dev bridge0 lladdr 92:8d:73:d8:47:cf REACHABLE

清除arp

# ip neigh flush dev eth0
words: 609 tags: command