深入解析Linux 常用命令--ip

深入解析Linux常用命令–ip

1.概述

ip命令用于显示/操作路由、设备、策略、隧道等信息。

2.用法

ip [ OPTIONS ] OBJECT { COMMAND | help }

OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] | -f[amily] { inet
       | inet6 | ipx | dnet | link } | -o[neline] }

OBJECT := { link | addr | addrlabel | route | rule | neigh | tunnel |
           maddr | mroute | monitor }
COMMAND :={ add | del |show | list}        

a.Options

-V 打印ip的版本号

-s 输出详细的统计信息

b.Object

link 网络设备

address/addr 设备ip地址

neighbor ARP条目

route 路由表

rule 路由策略规则

maddress 多播地址

mroute 多播路由

ip tunnel 隧道

利用help命令,由于命令太复杂,大家可以利用help命令进行查阅。

ip help

ip addr help

ip route help

[root@smart Desktop]# ip route add help
Usage: ip route { list | flush } SELECTOR
       ip route get ADDRESS [ from ADDRESS iif STRING ]
                            [ oif STRING ]  [ tos TOS ]
       ip route { add | del | change | append | replace | monitor } ROUTE
SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]
            [ table TABLE_ID ] [ proto RTPROTO ]
            [ type TYPE ] [ scope SCOPE ]
ROUTE := NODE_SPEC [ INFO_SPEC ]
NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]
             [ table TABLE_ID ] [ proto RTPROTO ]
             [ scope SCOPE ] [ metric METRIC ]
INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...
NH := [ via ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS
OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ]
           [ rtt TIME ] [ rttvar TIME ] [reordering NUMBER ]
           [ window NUMBER] [ cwnd NUMBER ] [ initcwnd NUMBER ]
           [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]
           [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]
TYPE := [ unicast | local | broadcast | multicast | throw |
          unreachable | prohibit | blackhole | nat ]
TABLE_ID := [ local | main | default | all | NUMBER ]
SCOPE := [ host | link | global | NUMBER ]
FLAGS := [ equalize ]
MP_ALGO := { rr | drr | random | wrandom }
NHFLAGS := [ onlink | pervasive ]
RTPROTO := [ kernel | boot | static | NUMBER ]
TIME := NUMBER[s|ms]
[root@smart Desktop]# ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
       ip [ -force ] -batch filename
where  OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |
                   tunnel | maddr | mroute | mrule | monitor | xfrm | token }
       OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -f[amily] { inet | inet6 | ipx | dnet | link } |
                    -o[neline] | -t[imestamp] | -b[atch] [filename] |
                    -rc[vbuf] [size]}
[root@smart Desktop]# ip addr help
Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]
                                                      [ CONFFLAG-LIST]
       ip addr del IFADDR dev STRING
       ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label STRING ] [ scope SCOPE-ID ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG  := [ permanent | dynamic | secondary | primary |
           tentative | deprecated | CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
CONFFLAG  := [ home | nodad ]
LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
LFT := forever | SECONDS
[root@smart Desktop]# ip addr add help
Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]
                                                      [ CONFFLAG-LIST]
       ip addr del IFADDR dev STRING
       ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label STRING ] [ scope SCOPE-ID ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG  := [ permanent | dynamic | secondary | primary |
           tentative | deprecated | CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
CONFFLAG  := [ home | nodad ]
LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
LFT := forever | SECONDS
[root@smart Desktop]# ip addr add help
Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]
                                                      [ CONFFLAG-LIST]
       ip addr del IFADDR dev STRING
       ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label STRING ] [ scope SCOPE-ID ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG  := [ permanent | dynamic | secondary | primary |
           tentative | deprecated | CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
CONFFLAG  := [ home | nodad ]
LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
LFT := forever | SECONDS
[root@smart Desktop]# ip link help
Usage: ip link add link DEV [ name ] NAME
                   [ txqueuelen PACKETS ]
                   [ address LLADDR ]
                   [ broadcast LLADDR ]
                   [ mtu MTU ]
                   type TYPE [ ARGS ]
       ip link delete DEV type TYPE [ ARGS ]

       ip link set DEVICE [ { up | down } ]
                      [ arp { on | off } ]
                      [ dynamic { on | off } ]
                      [ multicast { on | off } ]
                      [ allmulticast { on | off } ]
                      [ promisc { on | off } ]
                      [ trailers { on | off } ]
                      [ txqueuelen PACKETS ]
                      [ name NEWNAME ]
                      [ address LLADDR ]
                      [ broadcast LLADDR ]
                      [ mtu MTU ]
                      [ netns PID ]
              [ alias NAME ]
                      [ vf NUM [ mac LLADDR ]
                   [ vlan VLANID [ qos VLAN-QOS ] ]
                   [ rate TXRATE ] ] 
       ip link show [ DEVICE ]

TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | can }
[root@smart Desktop]# 

格式及用法:

[root@smart Desktop]# ip link help
Usage: ip link add link DEV [ name ] NAME
                   [ txqueuelen PACKETS ]
                   [ address LLADDR ]
                   [ broadcast LLADDR ]
                   [ mtu MTU ]
                   type TYPE [ ARGS ]
       ip link delete DEV type TYPE [ ARGS ]

       ip link set DEVICE [ { up | down } ]
                      [ arp { on | off } ]
                      [ dynamic { on | off } ]
                      [ multicast { on | off } ]
                      [ allmulticast { on | off } ]
                      [ promisc { on | off } ]
                      [ trailers { on | off } ]
                      [ txqueuelen PACKETS ]
                      [ name NEWNAME ]
                      [ address LLADDR ]
                      [ broadcast LLADDR ]
                      [ mtu MTU ]
                      [ netns PID ]
              [ alias NAME ]
                      [ vf NUM [ mac LLADDR ]
                   [ vlan VLANID [ qos VLAN-QOS ] ]
                   [ rate TXRATE ] ] 
       ip link show [ DEVICE ]

TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | can }
i.设备启动/关闭

ip link set dev eth0 up/down

类似于ifconfig eth0 up/down

[root@smart Desktop]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UP qlen 1000
    link/ether 00:0c:29:45:5e:f7 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
[root@smart Desktop]# ip link set eth1 up
[root@smart Desktop]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UP qlen 1000
    link/ether 00:0c:29:45:5e:f7 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
ii.改变设备传输队列长度

ip link set dev eth0 txqueuelen 100

[root@smart Desktop]# ip  link set  dev  eth0 txqueuelen 99
[root@smart Desktop]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UP qlen 99
    link/ether 00:01:4f:00:15:f1 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
iii改变网络设备MTU(最大传输单元的值)

ip link set dev eth0 mtu 1000

[root@smart Desktop]# ip  link  set  dev  eth0  mtu 999
[root@smart Desktop]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 999 qdisc pfifo_fast state UP qlen 99
    link/ether 00:01:4f:00:15:f1 brd ff:ff:ff:ff:ff:ff
iv.改变网络设备MAC地址

ip link set dev eth0 address 01:01:4f:00:15:f1

[root@smart Desktop]# ip  link  set dev eth0  address  01:01:4f:00:15:f1
[root@smart Desktop]# ip link 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1000 qdisc pfifo_fast state UP qlen 99
    link/ether 01:01:4f:00:15:f1 brd ff:ff:ff:ff:ff:ff

显示设备属性

ip -s link 显示所有网卡的设备状态

ip -s link ls eth0 显示网卡0的设备状态

ip -s -s link ls eth0 显示网卡0的设备状态,更细

[root@smart Desktop]# ip -s link 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX: bytes  packets  errors  dropped overrun mcast   
    17256      228      0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    17256      228      0       0       0       0      
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:f7 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    20389399   23841    0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    760904     11131    0       0       0       0      
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    55300      402      0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    19070      110      0       0       0       0      
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 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      
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 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      
[root@smart Desktop]# ip -s link eth0
Command "eth0" is unknown, try "ip link help".
[root@smart Desktop]# ip -s link eth0
Command "eth0" is unknown, try "ip link help".
[root@smart Desktop]# ip -s link ls eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:f7 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    20391499   23876    0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    761450     11144    0       0       0       0      
[root@smart Desktop]# ip -s link ls eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    55300      402      0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    19070      110      0       0       0       0      
[root@smart Desktop]# ip -s -s link ls eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    55300      402      0       0       0       0      
    RX errors: length  crc     frame   fifo    missed
               0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    19070      110      0       0       0       0      
    TX errors: aborted fifo    window  heartbeat
               0        0       0       0    

4.ip address/addr

格式及用法

[root@smart Desktop]# ip addr help
Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]
                                                      [ CONFFLAG-LIST]
       ip addr del IFADDR dev STRING
       ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label STRING ] [ scope SCOPE-ID ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG  := [ permanent | dynamic | secondary | primary |
           tentative | deprecated | CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
CONFFLAG  := [ home | nodad ]
LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]
LFT := forever | SECONDS
[root@smart Desktop]# 

a.显示所有设备的ip信息

ip address /ip addr

[root@smart Desktop]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1000 qdisc pfifo_fast state UP qlen 99
    link/ether 01:01:4f:00:15:f1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.103/24 brd 192.168.1.255 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.42.128/24 brd 192.168.42.255 scope global eth1
    inet6 fe80::20c:29ff:fe45:5e01/64 scope link 
       valid_lft forever preferred_lft forever
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
[root@smart Desktop]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1000 qdisc pfifo_fast state UP qlen 99
    link/ether 01:01:4f:00:15:f1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.103/24 brd 192.168.1.255 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.42.128/24 brd 192.168.42.255 scope global eth1
    inet6 fe80::20c:29ff:fe45:5e01/64 scope link 
       valid_lft forever preferred_lft forever
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
[root@smart Desktop]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1000 qdisc pfifo_fast state UP qlen 99
    link/ether 01:01:4f:00:15:f1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.103/24 brd 192.168.1.255 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.42.128/24 brd 192.168.42.255 scope global eth1
    inet6 fe80::20c:29ff:fe45:5e01/64 scope link 
       valid_lft forever preferred_lft forever
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff

b.添加地址/删除地址

ip addr add /del

格式: ip addr { add | del } IFADDR dev STRING

i.添加ip地址并设置标签

ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1

[root@smart Desktop]# ip addr add 192.168.4.2/24 brd + dev eth0 label eth0:1
[root@smart Desktop]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1000 qdisc pfifo_fast state UP qlen 99
    link/ether 00:0c:29:45:5e:f7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.106/24 brd 192.168.1.255 scope global eth0
    inet 192.168.4.2/24 brd 192.168.4.255 scope global eth0:1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:5e:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.42.128/24 brd 192.168.42.255 scope global eth1
    inet6 fe80::20c:29ff:fe45:5e01/64 scope link 
       valid_lft forever preferred_lft forever
5: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
    link/ether 52:54:00:3a:b7:a4 brd ff:ff:ff:ff:ff:ff
ii.给某个网卡添加ip地址

ip addr add 192.168.1.150/32 dev eth0

ip addr add 192.168.1.151/24 dev eth0

iii.删除IP地址

ip addr del 192.168.1.150/32 dev eth0

ip addr del 192.168.1.151/24 dev eth0

5.ip route

格式及用法

[root@smart Desktop]# ip route help
Usage: ip route { list | flush } SELECTOR
       ip route get ADDRESS [ from ADDRESS iif STRING ]
                            [ oif STRING ]  [ tos TOS ]
       ip route { add | del | change | append | replace | monitor } ROUTE
SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]
            [ table TABLE_ID ] [ proto RTPROTO ]
            [ type TYPE ] [ scope SCOPE ]
ROUTE := NODE_SPEC [ INFO_SPEC ]
NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]
             [ table TABLE_ID ] [ proto RTPROTO ]
             [ scope SCOPE ] [ metric METRIC ]
INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...
NH := [ via ADDRESS ] [ dev STRING ] [ weight NUMBER ] NHFLAGS
OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ]
           [ rtt TIME ] [ rttvar TIME ] [reordering NUMBER ]
           [ window NUMBER] [ cwnd NUMBER ] [ initcwnd NUMBER ]
           [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]
           [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]
TYPE := [ unicast | local | broadcast | multicast | throw |
          unreachable | prohibit | blackhole | nat ]
TABLE_ID := [ local | main | default | all | NUMBER ]
SCOPE := [ host | link | global | NUMBER ]
FLAGS := [ equalize ]
MP_ALGO := { rr | drr | random | wrandom }
NHFLAGS := [ onlink | pervasive ]
RTPROTO := [ kernel | boot | static | NUMBER ]
TIME := NUMBER[s|ms]

a.ip route list | flush

ip route { list | flush } SELECTOR

注意ip route 默认只显示系统默认路由(table 254),不显示其他table的路由

[root@smart Desktop]# ip route list
192.168.4.0/24 dev eth0  proto kernel  scope link  src 192.168.4.2 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.1.1 dev eth0  proto static 
[root@smart Desktop]# ip route
192.168.4.0/24 dev eth0  proto kernel  scope link  src 192.168.4.2 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.1.1 dev eth0  proto static 
[root@smart Desktop]# ip route flush
"ip route flush" requires arguments.
[root@smart Desktop]# ip route flush proto static
[root@smart Desktop]# ip route 
192.168.4.0/24 dev eth0  proto kernel  scope link  src 192.168.4.2 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 

[root@smart Desktop]# ip route l default
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.1.1 dev eth0 
[root@smart Desktop]# ip route 
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.1.1 dev eth0 
[root@smart Desktop]# ip route l t all
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.1.1 dev eth0 
192.168.4.0/24 via 192.168.1.1 dev eth0  table 5 
broadcast 192.168.1.0 dev eth0  table local  proto kernel  scope link  src 192.168.1.106 
broadcast 127.255.255.255 dev lo  table local  proto kernel  scope link  src 127.0.0.1 
broadcast 192.168.42.0 dev eth1  table local  proto kernel  scope link  src 192.168.42.128 
local 192.168.122.1 dev virbr0  table local  proto kernel  scope host  src 192.168.122.1 
local 192.168.42.128 dev eth1  table local  proto kernel  scope host  src 192.168.42.128 
broadcast 192.168.122.0 dev virbr0  table local  proto kernel  scope link  src 192.168.122.1 
local 192.168.1.106 dev eth0  table local  proto kernel  scope host  src 192.168.1.106 
broadcast 192.168.1.255 dev eth0  table local  proto kernel  scope link  src 192.168.1.106 
broadcast 192.168.42.255 dev eth1  table local  proto kernel  scope link  src 192.168.42.128 
broadcast 127.0.0.0 dev lo  table local  proto kernel  scope link  src 127.0.0.1 
broadcast 192.168.122.255 dev virbr0  table local  proto kernel  scope link  src 192.168.122.1 
local 127.0.0.1 dev lo  table local  proto kernel  scope host  src 127.0.0.1 
local 127.0.0.0/8 dev lo  table local  proto kernel  scope host  src 127.0.0.1 
unreachable ::/96 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable ::ffff:0.0.0.0/96 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 2002:a00::/24 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 2002:7f00::/24 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 2002:a9fe::/32 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 2002:ac10::/28 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 2002:c0a8::/32 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 2002:e000::/19 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
unreachable 3ffe:ffff::/32 dev lo  metric 1024  error -101 mtu 16436 advmss 16376 hoplimit 0
fe80::/64 dev virbr0  proto kernel  metric 256  mtu 1500 advmss 1440 hoplimit 0
fe80::/64 dev eth1  proto kernel  metric 256  mtu 1500 advmss 1440 hoplimit 0
unreachable default dev lo  table unspec  proto kernel  metric -1  error -101 hoplimit 255
local ::1 via :: dev lo  table local  proto none  metric 0  mtu 16436 advmss 16376 hoplimit 0
ff00::/8 dev virbr0  table local  metric 256  mtu 1500 advmss 1440 hoplimit 0
ff00::/8 dev eth1  table local  metric 256  mtu 1500 advmss 1440 hoplimit 0
unreachable default dev lo  table unspec  proto kernel  metric -1  error -101 hoplimit 255

b.ip route get

ip route get ADDRESS [ from ADDRESS iif STRING ][ oif STRING ] [ tos TOS ]

ip route get 192.168.1.9 from 192.168.1.7

ip route get to 192.168.1.7

[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@smart Desktop]# ip route get to 192.168.2.7
RTNETLINK answers: Network is unreachable
[root@smart Desktop]# ip route ^C
[root@smart Desktop]# route add default gw 192.168.1.1
[root@smart Desktop]# ip route get to 192.168.2.7
192.168.2.7 via 192.168.1.1 dev eth0  src 192.168.1.106 
    cache  mtu 1000 advmss 960 hoplimit 64
[root@smart Desktop]# ip route get 192.168.1.7 from 192.168.1.106
192.168.1.7 from 192.168.1.106 dev eth0 
    cache  mtu 1000 advmss 960 hoplimit 64
[root@smart Desktop]# ip route get 192.168.2.7 from 192.168.1.106
192.168.2.7 from 192.168.1.106 via 192.168.1.1 dev eth0 
    cache  mtu 1000 advmss 960 hoplimit 64
[root@smart Desktop]# ip route get 192.168.2.7 to 192.168.1.106
local 192.168.1.106 dev lo  src 192.168.1.106 
    cache <local>  mtu 16436 advmss 16396 hoplimit 64

c.ip route add

ip route add 192.168.2.0/24 dev eth0
ip route add 192.168.3.0/24 via 192.168.1.1

ip route add 192.168.4.0/24 via 192.168.1.1 table 5

[root@smart Desktop]# ip route add 192.168.2.0/24 dev eth0
[root@smart Desktop]# 
[root@smart Desktop]# ip route add 192.168.3.0/24 via 192.168.1.1 
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

添加默认路由

ip route add default via 192.168.1.1

[root@smart Desktop]# ip  route  add default  via 192.168.1.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

此处需要注意,采用ip route add default只能添加一条默认路由,而route add default则可以添加多条,不过也只有一条是有效的

[root@smart Desktop]# ip  route  add default  via 192.168.1.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route add default via 192.168.42.1
RTNETLINK answers: File exists
[root@smart Desktop]# ip route add default dev eth0
RTNETLINK answers: File exists
[root@smart Desktop]# ip route add default dev eth1
RTNETLINK answers: File exists
[root@smart Desktop]# route add default gw 192.168.42.1
[root@smart Desktop]# route add default dev eth1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 eth1
0.0.0.0         192.168.42.1    0.0.0.0         UG    0      0        0 eth1
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

d.ip route del

ip route del 192.168.1.0/24

ip route del 192.168.1.0/24 via 192.168.1.1

ip route del 192.168.1.0/24 dev eth0

ip route del 192.168.1.0/24 via 192.168.1.1 dev eth0

[root@smart Desktop]# ip route add 192.168.5.0/24 via 192.168.1.1
[root@smart Desktop]# ip route del 192.168.5.0/24 dev eth0
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 eth1
0.0.0.0         192.168.42.1    0.0.0.0         UG    0      0        0 eth1
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route add 192.168.5.0/24 via 192.168.1.1
[root@smart Desktop]# ip route
192.168.5.0/24 via 192.168.1.1 dev eth0 
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default dev eth1  scope link 
default via 192.168.42.1 dev eth1 
default via 192.168.1.1 dev eth0 
[root@smart Desktop]# ip route del 192.168.5.0/24 via 192.168.1.1 dev eth0
[root@smart Desktop]#

删除默认路由

ip route del default,每执行一次删除一条

[root@smart Desktop]# ip route
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default dev eth1  scope link 
default via 192.168.42.1 dev eth1 
default via 192.168.1.1 dev eth0 
[root@smart Desktop]# ip route del default
[root@smart Desktop]# ip route
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.42.1 dev eth1 
default via 192.168.1.1 dev eth0 
[root@smart Desktop]# ip route del default
[root@smart Desktop]# ip route
192.168.2.0/24 dev eth0  scope link 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.106  metric 1 
192.168.42.0/24 dev eth1  proto kernel  scope link  src 192.168.42.128  metric 1 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
default via 192.168.1.1 dev eth0 
[root@smart Desktop]# 

e.ip route change

[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.7.1     192.168.1.106   255.255.255.255 UGH   0      0        0 eth0
192.168.3.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route change 192.168.3.0/24 dev eth1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.7.1     192.168.1.106   255.255.255.255 UGH   0      0        0 eth0
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route change 192.168.3.0/24 via 192.168.1.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.7.1     192.168.1.106   255.255.255.255 UGH   0      0        0 eth0
192.168.3.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

f.ip route append

ip route add添加会在路由表前面

ip route append添加路由会在原有的类似路由后面

[root@smart Desktop]# ip route append 192.168.2.0/24 via 192.168.1.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route add 192.168.2.0/24 via 192.168.1.1
RTNETLINK answers: File exists
[root@smart Desktop]# ip route append 192.168.2.0/24 via 192.168.1.1
RTNETLINK answers: File exists
[root@smart Desktop]# ip route add 192.168.1.0/24 via 192.168.1.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# 

g.ip route replace

与ip route replace相似

[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route add 192.168.2.0/24 via 192.168.1.1
RTNETLINK answers: File exists
[root@smart Desktop]# ip route append 192.168.2.0/24 via 192.168.1.1
RTNETLINK answers: File exists
[root@smart Desktop]# ip route add 192.168.1.0/24 via 192.168.1.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route replace 192.168.2.0/24 via 192.168.2.1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     192.168.2.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route change 192.168.2.0/24 via 192.168.3.1
RTNETLINK answers: No such process
[root@smart Desktop]# ip route change 192.168.1.0/24 via 192.168.2.1
RTNETLINK answers: No such process
[root@smart Desktop]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     192.168.2.1     255.255.255.0   UG    0      0        0 eth0
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     1      0        0 eth0
192.168.42.0    *               255.255.255.0   U     1      0        0 eth1
192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# ip route change^C
[root@smart Desktop]# ip route replace 192.168.2.0/24 dev eth1
[root@smart Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.2.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
[root@smart Desktop]# 

6.ip rule

ip rule [ list | add | del | flush ] SELECTOR ACTION

格式及用法

[root@smart Desktop]# ip rule help
Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION
SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]
            [ dev STRING ] [ pref NUMBER ]
ACTION := [ table TABLE_ID ]
          [ prohibit | reject | unreachable ]
          [ realms [SRCREALM/]DSTREALM ]
          [ goto NUMBER ]
TABLE_ID := [ local | main | default | NUMBER ]

ip rule list

[root@smart Desktop]# ip rule
0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default 
[root@smart Desktop]# ip rule add from 192.168.7.0/24 table 5
[root@smart Desktop]# ip rule
0:  from all lookup local 
32765:  from 192.168.7.0/24 lookup 5 
32766:  from all lookup main 
32767:  from all lookup default 
[root@smart Desktop]# ip rule add to 192.168.7.0/24 talbe 5
Error: argument "talbe" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add to 192.168.7.0/24 table 5
[root@smart Desktop]# ip rule
0:  from all lookup local 
32764:  from all to 192.168.7.0/24 lookup 5 
32765:  from 192.168.7.0/24 lookup 5 
32766:  from all lookup main 
32767:  from all lookup default 
[root@smart Desktop]# ip rule add 192.168.1.0/24 reject
Error: argument "192.168.1.0/24" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add from 192.168.1.0/24 reject
Error: argument "reject" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add from 192.168.1.0/24 table 5 reject
Error: argument "reject" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add from 192.168.1.0/24 table 5 
[root@smart Desktop]# ip rule add from 192.168.5.0/24 table 5 
[root@smart Desktop]# ip rule add from 192.168.5.0/24 table 5 reject
Error: argument "reject" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add from 192.168.6.0/24 table 5 reject
Error: argument "reject" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add from 192.168.6.0/24 table 5 unreachabel
Error: argument "unreachabel" is wrong: Failed to parse rule type
[root@smart Desktop]# ip rule add from 192.168.6.0/24 table 5 unreachable
[root@smart Desktop]# 

7.ip neigh

邻接ip情况

[root@smart Desktop]# ip neigh help
Usage: ip neigh { add | del | change | replace } { ADDR [ lladdr LLADDR ]
          [ nud { permanent | noarp | stale | reachable } ]
          | proxy ADDR } [ dev DEV ]
       ip neigh {show|flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]

8.ip tunnel

ip隧道管理

[root@smart Desktop]# ip tunnel help
Usage: ip tunnel { add | change | del | show | prl } [ NAME ]
          [ mode { ipip | gre | sit | isatap } ] [ remote ADDR ] [ local ADDR ]
          [ [i|o]seq ] [ [i|o]key KEY ] [ [i|o]csum ]
          [ prl-default ADDR ] [ prl-nodefault ADDR ] [ prl-delete ADDR ]
          [ ttl TTL ] [ tos TOS ] [ [no]pmtudisc ] [ dev PHYS_DEV ]

Where: NAME := STRING
       ADDR := { IP_ADDRESS | any }
       TOS  := { NUMBER | inherit }
       TTL  := { 1..255 | inherit }
       KEY  := { DOTTED_QUAD | NUMBER }

9.ip maddr

多播管理

[root@smart Desktop]# ip maddr help
Usage: ip maddr [ add | del ] MULTIADDR dev STRING
       ip maddr show [ dev STRING ]
[root@smart Desktop]# ip maddr 
1:  lo
    inet  224.0.0.1
    inet6 ff02::1
2:  eth0
    link  01:00:5e:00:00:01
    inet  224.0.0.1
3:  eth1
    link  33:33:ff:45:5e:01
    link  33:33:00:00:00:01
    link  01:00:5e:00:00:01
    inet  224.0.0.1
    inet6 ff02::1:ff45:5e01
    inet6 ff02::1
5:  virbr0
    link  33:33:00:00:00:01
    link  01:00:5e:00:00:01
    inet  224.0.0.1
    inet6 ff02::1
6:  virbr0-nic
    link  33:33:00:00:00:01
    inet6 ff02::1       

10.ip monitor

ip monitor [ all | LISTofOBJECTS ]

用于监控ip情况

[root@smart Desktop]# ip monitor all
[NEIGH]192.168.42.254 dev eth1 lladdr 00:50:56:fa:5f:cf REACHABLE
[NEIGH]192.168.42.254 dev eth1 lladdr 00:50:56:fa:5f:cf STALE
[ROUTE]192.168.5.0/24 via 192.168.1.1 dev eth0 

Juyin@2017/12/21

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值