ip command

最後更新: 2016-03-07

 

目錄

  • show something
  • neigh
  • route
  • link
  • rule
  • Simple source policy routing

 


介紹

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

 

ip neigh show

192.168.88.177 dev eth1 lladdr e0:cb:4e:ec:cb:4b ref 1 used 0/0/0 probes 0 REACHABLE
192.168.1.208 dev br-lan lladdr 00:7e:95:df:61:b9 ref 1 used 0/0/0 probes 1 REACHABLE

 


Set/Un-Set IP Address

 

# delete

ip addr delete IP/Mask dev ethN

# add

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

 


link

 

ip link set DEVICE < up | down >

 


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