# ip command usage
###### tags: `linux` `network` `ip`
### add alias in ~/.bashrc or ~/.bash_aliases for serveral ip commands
```
alias ip4-a="ip -4 a"
alias ip4-ba="ip -4 -br a"
alias ip4-bl="ip -br l"
alias ip4-sa="ip -4 -s a"
alias ip4-sl="ip -s l"
alias ip4-as="ip -4 a show"
alias ip4-ls="ip l show"
```
### list interfaces and IP
```
ip addr
ip -4 -brief addr
ip -4 -color addr
ip -4 -json addr
ip -4 -details addr
ip -4 -stats addr
ip -f link -br addr
ip -0 -br a
ip -br link
ip -o link
ip -4 -o addr
```
### add / delete IP, set link
```
ip addr add 192.168.0.1/24 dev eth0
ip addr del 192.168.0.1/24 dev eth0
ip link set eth0 [ up | down ]
ip link set eth0 mtu 9000
ip link set eth0 promisc [ on | off ]
# rename interface from eth0 to eth1
ip link set eth0 down
ip link set eth0 name eth1
ip link set eth1 up
```
### route
```
ip route
ip -d route
ip -4 route
ip route show default
ip route show 192.168.0.0/24
ip route add default via 192.168.0.254
ip route del default via 192.168.0.254
ip route add 172.16.0.0/24 via 192.168.0.100 dev eth0
ip route del 172.16.0.0/24
ip route replace 172.16.0.0/24 dev eth1
# check which route is used for a destination IP
ip route get 172.16.0.1
# for persistence static routes in Ubuntu, add line in /etc/network/interfaces
up ip route add 172.16.00.0/24 via 192.168.0.100 dev eth0
```
### neighbor cache
```
ip neigh add 192.168.0.100 lladdr aa:bb:cc:11:22:33 dev eth0
ip neigh del 192.168.0.100 dev eth0
ip neigh replace 192.168.0.100 lladdr aa:bb:cc:44:55:66 dev eth0
```
### network namespace
```
ip netns
ip netns [ add | del ] ns1
ip netns exec ns1 ip addr
```
### virtual etherenet device (veth)
```
ip link add veth1 type veth peer name veth2
ip link set veth1 netns ns1
# create veth in specific network namesapces
ip add veth_name1 netns ns1 type veth peer veth2 netns ns2
# check veth peer interface ID
ethtool -S veth1
# delete veth pair
ip link del veth1
```
### monitor
```
ip monitor
ip -t monitor
ip -ts monitor
```
### gre tunnel
```
ip tunnel show
# Create gre tunnel interface
ip tunnel add tunnel0 mode gre remote {Remote Peer IP} local {Local Peer IP} ttl 255
ip link set tunnel0 up
# assign IP on tunnel port (optional)
ip addr add {IP Address/Mask} dev tunnel0
# add route
ip route add {Destination Network Address/Mask} dev tunne0
ip tunnel delete
```
### virtual routing and forwarding (VRF)
```
ip link show type vrf
ip -br link show type vrf
ip neigh show vrf {VRF Name}
ip addr show vrf {VRF Name}
ip route show vrf {VRF Name}
ip route show {Table ID}
ip route get vrf {VRF Name} {Network Address}
ip link add {VRF Name} type vrf table {Table ID}
# ex. ip link add vrf1 type vrf table 1
ip link set dev {DEV Name} nomaster
```
### create vlan interface with specific mac
```
# create vlan interface from ens3 with specifi mac
ip link add link ens3 name vlan20 address <Mac address> type vlan id 20
```
### reference:
http://www.routereflector.com/2016/11/working-with-vrf-on-linux/
https://www.kernel.org/doc/Documentation/networking/vrf.txt