ip command

最後更新: 2020-06-11

 

目錄

 


安裝

 

apt-get install iproute2

 


介紹

 

Help:

root@fw:~# ip help

Usage:

ip [ OPTIONS ] OBJECT { COMMAND | help }

OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |
                   tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |
                   netns | l2tp }

OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
                    -f[amily] { inet | inet6 | ipx | dnet | link } |
                    -l[oops] { maximum-addr-flush-attempts } |
                    -o[neline] | -t[imestamp] | -b[atch] [filename] |
                    -rc[vbuf] [size]}

 


Show something

 

ip addr

...

3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 26:da:6f:de:02:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.123.10/24 brd 192.168.123.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::be5f:f4ff:feaf:2149/64 scope link
       valid_lft forever preferred_lft forever
...

valid_lft 與 preferred_lft

# Section 5.5.4 of RFC 4862

valid_lft

When it expires, the address is removed by the kernel. Defaults to forever.

preferred_lft

When it expires, the address is no longer used for new outgoing connections. Defaults to forever.

qdisc

noqueue

fq_codel

詳見 tc

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:19:66:32:d6:c4 brd ff:ff:ff:ff:ff:ff

ip link show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:19:66:32:d6:c4 brd ff:ff:ff:ff:ff:ff

ip route [list]

default via 192.168.123.1 dev eth0  metric 100
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1
192.168.123.11 dev lxcbr0  scope link
192.168.123.12 dev lxcbr0  scope link
192.168.123.13 dev lxcbr0  scope link

ip rule [list]

0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default

 


neigh(arp)

 

ip neigh show

192.168.88.177 dev eth1 lladdr e0:cb:4e:xx:xx:xxref 1 used 0/0/0 probes 0 REACHABLE
192.168.1.208 dev br-lan lladdr 00:7e:95:xx:xx:xxref 1 used 0/0/0 probes 1 REACHABLE

 


Set/Un-Set IP Address

 

# delete

ip addr delete IP/Mask dev ethN

# add

# ip addr help

ip addr add IP/Mask dev ethN

 


route

 

某 table 內容:

ip route list

# 某 table 內的 route

ip route list table vpn

default via 10.8.0.1 dev tap0

刪除:

ip route default via 192.168.88.5

ip route del table <table_name>

加入:

ip route add default via 192.168.88.1 table vm

ip route add 192.168.88.0/24 via 192.168.88.1 table vm

替代:

route replace - Replace, or add if not defined, a route

# Replace the defined route for 192.168.1.0/24 to use device em1

ip route replace 192.168.1.0/24 dev em1

# Replace Default Gateway

# ip route add default via 192.0.2.1

# ip route del default via 10.0.0.1

ip route replace default via 1.2.3.4

查詢:

# route get Display the route an address will take

ip route get 192.168.1.5

192.168.1.5 via 192.168.88.1 dev vmbr0 src 192.168.88.150 uid 0
    cache

 


link

 

ip link set DEVICE < up | down >

ip link delete DEVICE

 


rule

 

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 ]

查看有什麼 rule:

ip rule list

0:      from all lookup local
32765:  from 192.168.1.101 lookup vpn
32766:  from all lookup main
32767:  from all lookup default

刪除 rule:

ip rule del prio 32767

 


Simple source policy routing

 

所 cmd:

ip rule add from 192.168.1.101 table vpn

ip route add default via 10.8.0.1 dev tap0 table vpn

ip route flush cache

如果單是行是會死人的 ....

ip route flush

原本 table 是 number 來, 現在為它加上名字

cat /etc/iproute2/rt_tables

#
# reserved values
#
255     local
254     main
253     default
10      vpn
0       unspec
#
# local
#
#1      inr.ruhep

 

Script

dualwanip.sh

#!/bin/bash
# eth0 is another network
# eth1 mount $_IP

_NIC=eth1
_IP=192.168.8.3
_GW=192.168.8.1
_NETWORK="192.168.8.0/24"
_TABLE="rt3"


########### Code ###########

echo "3 $_TABLE" >> /etc/iproute2/rt_tables

ip route add $_NETWORK dev $_NIC src $_IP table $_TABLE
ip route add default via $_GW dev $_NIC table $_TABLE

ip rule add from $_IP table $_TABLE
ip rule add to $_IP table $_TABLE

ip route flush cache

########### checking ###########

ip rule | grep $_TABLE

ip route list table $_TABLE

 


Statistics

 

-s, -stats, -statistics          # If the option appears twice, the amount of information increases.

ie.

ip -s link show vmbr0

4: vmbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue 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
    5132421755 27014470 0       0       0       0
    TX: bytes  packets  errors  dropped carrier collsns
    14002270010 4262135  0       0       0       0

ip -s -s link show vmbr0

...
    RX:  bytes packets errors dropped  missed   mcast
         10372     170      0       0       0       0
    RX errors:  length    crc   frame    fifo overrun
                     0      0       0       0       0
    TX:  bytes packets errors dropped carrier collsns
          3568      40      0       0       0       0
    TX errors: aborted   fifo  window heartbt transns
                     0      0       0       0       2

 


Details

 

-d, -details     # Output more detailed information.

 

 

 

 

 

 

 

 

Creative Commons license icon Creative Commons license icon