Linux: ip command line examples

Hi,

the old style ifconfig, route and netstat commands does not support all network, especially IPv6, features shipped with modern linux distributions.

Replacement are ip (ifconfig, route) and ss (netstat). Here are some examples.

Show all interface addresses

root@devdeb ~ # ip address show

To show an compact view, less verbose output use the -brief or -br switch

root@devdeb ~ # ip -br address show

Or for a better overview use a colored output

root@devdeb ~ # ip -color -br address show

Te output can formatted as JSON for further processings

root@devdeb ~ # ip -j address show

Show IPv4 interface addresses. -4 is a shortcut for -family inet

root@devdeb ~ # ip -4 address show

Show IPv6 interface addresses. -6 is a shortcut for -family inet6

root@devdeb ~ # ip -6 address show

Show MAC Addresses. -0 is a shortcut for -family link

root@devdeb ~ # ip -0 address show

Show statistic (Send/received bytes, errors, etc) of the interfaces

root@devdeb ~ # ip -stats link show

Parameters can be shortened if the shortcut is unique. For example address and show

root@devdeb ~ # ip -0 a s

Show addresses of an specific interface

root@devdeb ~ # ip address show dev enp0s25

And without link local addresses

root@devdeb ~ # ip address show dev enp0s25 scope global

Set an IP (v4) Address

root@devdeb ~ # ip addr add 10.254.10.10/24 dev enp0s25

ifup

root@devdeb ~ # ip link set enp0s25 up

ifdown

root@devdeb ~ # ip link set enp0s25 down

Delete an IP (v4) Address

root@devdeb ~ # ip addr del 10.254.10.10/24 dev enp0s25

Show the IPv4 arp cache

root@devdeb ~ # ip neighbor show

Show the IPv6 neighbor cache

root@devdeb ~ # ip -6 neighbor show

Show IPv4 routing table

root@devdeb ~ # ip route show

Show IPv6 routing table

root@devdeb ~ # ip -6 route show

Add a IPv4 route

root@devdeb ~ # ip route add 10.253.1.0/24 via 10.254.10.1 dev enp0s25

Delete a IPv4 route

root@devdeb ~ # ip route del 10.253.1.0/24

Add a default route/default gateway

root@devdeb ~ # ip route add default via 10.254.10.1

Show default route

root@devdeb ~ # ip route show match default

Add a additional routing table for policy based routing

root@devdeb ~ # ip rule add fwmark 2 table 2
root@devdeb ~ # ip route add 10.11.1.0/0 table 2 via 10.254.10.1 dev enp0s25

This table is used when iptables MARKs a connection with number of the fwmark property of the table

root@devdeb ~ # iptables -A PREROUTING -t mangle -p tcp -s 10.254.1.25/32  --dport 443 -j MARK --set-mark 2

Show all routing tables

root@devdeb ~ # ip route show table all

And delete such a route

root@devdeb ~ # ip route del 10.11.1.0/0 table 2

To monitor change events on the network interfaces use

root@devdeb ~ # ip monitor

With switches -ts and label a time stamp and the family of the message is shown as prefix

root@devdeb ~ # ip -ts monitor label

The replacement for netstat is ss
The following command shows all listing tcp and udp sockets and the program that listens to

root@devdeb ~ # ss --tcp --udp --listening --processes --numeric

Display all TCP connections

root@devdeb ~ # ss -t -a

Display UNIX domain sockets

root@devdeb ~ # ss -x

Show multicast addresses/group memberships

root@devdeb ~ # ip maddress

Michael

One thought on “Linux: ip command line examples”

Leave a Reply