Handy Linux commands to administer IPs and routes

Handy Linux commands to administer IPs and routes

Using basic network commands comes very hand when one needs to debug a networking issue, to fix a problem or to simply try out a couple of things. This post is in no way an exhaustive list of Linux network-related commands, but tries to include a couple of the most basic ones and explain what they do.

Add an IP address

Manually set an IP address to interface eth0: # ip addr add 192.168.0.2 dev eth0

Check an IP address

Check that the address was set: # ip addr show or # ifconfig eth0

Remove an IP address

To remove (unset) an IP address: # ip addr del 192.168.0.2/24 dev eth0

Enable/disable a network interface

To enable the eth0 network interface: # ip link set eth0 up or its shortcut # ifup eth0

To disable the eth0 network interface: # ip link set eth0 down or the shortcut # ifdown eth0

View routes

Routes are necessary for the system to know which way (through which interface) it should send packets forward other devices.
To view all routes currently set: # ip route show or # route

Add a route

To add a route: # ip route add 192.168.0.0/24 via 192.168.0.254 dev eth0 or # route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.254 dev eth0

Remove a route

To remove a route: # ip route del 192.168.0.0/24 via 192.168.0.254 dev eth0 or # route del -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.254 dev eth0

Add default route

The default route is the one through which all packets not covered by all other routes are sent. This is normally your connection to the internet (or upstream provider) and it should include the IP address of your gateway router.

To add the default route: # ip route add 192.168.0.0/24 dev eth0 or # route add default gw 192.168.0.254 eth0

 

Remember to replace all identifiers (addresses, interfaces) with the correct ones for your own scenario. These commands are not permanent and all settings are lost on interface restart or system reboot. They are meant to be used for temporary / testing purposes.

PS: You may need to prepend sudo to commands if you’re logged in with a limited user.

Leave a Reply