ip Command

//TODO [ ] ip vs iptables 各自用在什么地方? my question: https://unix.stackexchange.com/questions/608560/what-is-the-difference-between-iptables-and-ip-route-table

ip command in Linux is present in the iproute package which is used for performing several network administration tasks. IP stands for Internet Protocol.

ifconfig is obsolete, ip is preferred.

It can perform several tasks like configuring and modifying the default and static routing, setting up tunnel over IP, listing IP addresses and property information, modifying the status of the interface, assigning, deleting and setting up IP addresses and routes.

Explanations: Linux ip Command with Examples Linux manual page: ip

Most frequently used subcommands:

  • link (l) - Display and modify network interfaces.
  • address (a) - Display and modify IP Addresses.
  • route ® - Display and alter the routing table.
  • neigh (n) - Display and manipulate neighbor objects (ARP table).
  • netns - deal with network namespace

The configurations set with the ip command are not persistent. After a system restart, all changes are lost. For permanent settings, you need to edit the distro-specific configuration files or add the commands to a startup script.

address

Show all IP address associated with all interfaces that are available

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
## ip address
ip addr
ip a
## only show ipv4
ip -4 a
## show specified device
ip addr show eth0
## abbr
ip a s eth0
## assign an IP address to an interface. (need append netmask)
## can assign multiple ip address to an interface
## not persistent
ip addr add 192.168.1.50/24 dev eth0
## delete an assigned IP address to an interface. (need append netmask)
ip addr del 192.168.1.50/24 dev eth0

It is used to display link layer information, like MAC address, it will fetch characteristics of the link layer devices currently available. Any networking device which has a driver loaded can be classified as an available device.

1
2
3
4
5
6
7
8
9
## show statistic of device with human readable format
## 显示有多少error, drop packets来看是不是网络由问题
## double -s will show error details
ip -s -s -h link
ip -s -h link show eth0
## bring up
ip link set eth0 up
## bring down
ip link set eth0 down

route

This command helps you to see the route packets your network will take as set in your kernel routing table. The first entry is the default route. Other commands perform the same: route, netstat -r

1
2
3
4
5
6
7
ip route
## route for a specific network
ip route list 9.30.204.0/22
## add a route to 192.168.121.0/24 via the gateway at 192.168.121.1
ip route add 192.168.121.0/24 via 192.168.121.1
## add a route to 192.168.121.0/24 that can be reached on device eth0.
ip route add 192.168.121.0/24 dev eth0

neigh

netns

0%