ipコマンド

参考

* [Network Configuration (日本語) - ArchWiki](https://wiki.archlinux.org/index.php/Network_Configuration_(%E6%97%A5%E6%9C%AC%E8%AA%9E)#.E6.89.8B.E5.8B.95.E3.81.A7.E5.89.B2.E3.82.8A.E5.BD.93.E3.81.A6.E3.82.8B)
* [Linux Set Up Routing with ip Command](http://www.cyberciti.biz/faq/howto-linux-configuring-default-route-with-ipcommand/)
* iprouteパッケージに入っています。

# よく使う

```bash:固定IP
ip addr add 10.0.2.15/24 dev eth0
```

```bash:デフォルトゲートウェイ
ip route add default via 10.0.2.2
```

```bash:ルーティング確認
route -n
```

> `RTNETLINK answers: File exists`が出る場合

* 一度ネットワーク設定を飛ばせば解決した。["RTNETLINK answers: File exists"を解消する - R42日記](http://takahashikzn.root42.jp/entry/2014/04/29/181515)

```bash:
ip addr flush dev eth0
```

## ルーティング追加

```bash:ルーティング追加
ip route add 210.210.210.222 dev eth0
```

## IP表示

```bash:IP表示
ip -f inet addr
```

# 出力例

## 1.ipアドレス表示

```bash:
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:1c:cf:2b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet6 fe80::a00:27ff:fe1c:cf2b/64 scope link 
       valid_lft forever preferred_lft forever
```

```bash:
# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 08:00:27:1c:cf:2b brd ff:ff:ff:ff:ff:ff
```

## 2. 1の情報少ない版

```bash:
# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    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 mode DEFAULT qlen 1000
    link/ether 08:00:27:1c:cf:2b brd ff:ff:ff:ff:ff:ff
```

## 3.ARPテーブル表示

```bash:
# ip neigh
10.0.2.2 dev eth0 lladdr 52:54:10:12:45:02 REACHABLE
```

## 4.ルーティング表示

```bash:
# ip route
default via 10.0.2.2 dev eth0 
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15
```

## 5.コネクション先の履歴？

```bash:
# ip tcp_metrics | sort | more
207.200.74.27 age 2630.104sec rtt 99500us rttvar 93000us cwnd 10
...
```

## 6.ARPテーブルのモニタリング？

```bash:
# ip monitor
```

## 7.help

helpをつけるとコマンドのオプションが表示されます。

```bash:
$ ip link help
Usage: ip link add [link DEV] [ name ] NAME
                   [ txqueuelen PACKETS ]
                   [ address LLADDR ]
                   [ broadcast LLADDR ]
                   [ mtu MTU ]
                   [ numtxqueues QUEUE_COUNT ]
                   [ numrxqueues QUEUE_COUNT ]
                   type TYPE [ ARGS ]
       ip link delete DEV type TYPE [ ARGS ]

       ip link set { dev DEVICE | group DEVGROUP } [ { 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 ]
	                  [ netns NAME ]
			  [ alias NAME ]
	                  [ vf NUM [ mac LLADDR ]
				   [ vlan VLANID [ qos VLAN-QOS ] ]
				   [ rate TXRATE ] ] 
				   [ spoofchk { on | off} ] ] 
			  [ master DEVICE ]
			  [ nomaster ]
       ip link show [ DEVICE | group GROUP ]

TYPE := { vlan | veth | vcan | dummy | ifb | macvlan | can | bridge | ipoib }
```