Linux ip Command: Show IP, Configure Network (ip addr, ip route)
The ip command in Linux is a powerful tool to show and manipulate network interfaces, configure IP addresses, modify routing tables, and create tunnels.
With this command it is possible to insert or modify entries in the routing table, add or modify the default route, and also configure network addresses.
This command replaces the Net-Tools commands ifconfig and route.
The objects that the ip command can work with are: link, addr, addrlabel, route, rule, neigh, ntable, tunnel, tuntap, maddr, mroute, mrule, monitor, xfrm, netns, l2tp, tcp_metrics, token and netconf.
In daily use, for a programmer, it is necessary to know how the objects addr (which configure the IP address), link (which enables an interface) and route (which modifies the routing table) work.
The first function of ip is to show the IP address of the host interfaces. Use the ip command followed by the addr object:
# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 2: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 link/ether 00:26:55:04:d3:95 brd ff:ff:ff:ff:ff:ff inet 172.19.1.34/20 brd 172.19.15.255 scope global enp0s25 inet6 fe80::226:55ff:fe04:d395/64 scope link
The ip command can be used to assign an IP address to the host, using the addr object, followed by the word add:
# ip addr add 192.168.50.5 dev eth1
In this case, the IP 192.168.50.5 will be assigned to interface eth1. You may or may not specify the network size in CIDR notation:
# ip addr add 192.168.50.5/24 dev eth1
The same idea can be used to remove an IP address from a given interface, with the addr object and the del command:
# ip addr del 192.168.50.5/24 dev eth1
To enable an interface, we use the link object, followed by the word set, the interface name and the up command:
# ip link set eth1 up
To disable an interface, the same, but with the down command:
# ip link set eth1 down
To check the host routing table, you can use the route object:
# ip routedefault via 172.19.1.1 dev enp0s25 172.19.0.0/20 dev enp0s25 proto kernel scope link src 172.19.1.34
To add a static route to the routing table, the route object is also used, followed by the word add, the network you want to add, and the interface and gateway it is connected to:
# ip route add 10.10.20.0/24 via 172.19.1.10 dev enp0s25
In this example, the network 10.10.20.0/24 will be added whose gateway will be address 172.19.1.10, which is connected on interface enp0s25.
When listing the routing table again:
# ip route showdefault via 172.19.1.1 dev enp0s25 10.10.20.0/24 via 172.19.1.10 dev enp0s25 172.19.0.0/20 dev enp0s25 proto kernel scope link src 172.19.1.34
The same logic applies to removing a route from the routing table:
# ip route del 10.10.20.0/24
It is also possible to add a default route to the routing table, using the route object, followed by add and the word default, followed by the default gateway IP address:
# ip route add default via 192.168.50.100
The ifconfig, route and netstat tools that are part of the legacy Net-Tools package have a parallel in the ip tool from iproute2, as follows:
| Net-Tools | Iproute2 | Description |
| ifconfig -a | ip link show | Display all network interfaces |
| ifconfig eth1 up | ip link set up eth1 | Enable network interface |
| ifconfig eth1 down | ip link set down eth1 | Disable network interface |
| ifconfig eth1 192.168.0.1/24 | ip addr add 192.168.0.1/24 dev eth1 | Set IP/Network mask |
| ifconfig eth1 0 | ip addr del 192.168.0.1/24 dev eth1 | Remove IP/Network mask definition |
| ifconfig eth1 | ip addr show dev eth1 | Show specific information for an interface |
| ifconfig eth1 hw ether 00:52:bc:33:25:a1 | ip link set dev eth1 address 00:52:bc:33:25:a1 | Change MAC-ADDRESS |
| route -n ou netstat -rn | ip route show | Display routing table |
| route add default gw 192.168.0.1 | ip route add default via 192.168.0.1 | Add default route |
| route add -net 192.168.0.0/24 eth1 | ip route add 192.168.0.0/24 dev eth1 | Add a static route |
| route del -net 192.168.0.0/24 | ip route del 192.168.0.0/24 | Delete a static route |
| route del default | ip route del default | Delete default route |
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.