For years & years we have been using ‘ifconfig‘ command to perform network related tasks like checking network interfaces or configuring them. But ‘ifconfig’ is no longer being maintained & has been deprecated on the recent versions of Linux. ‘ifconfig’ command has been replaced with ‘ip‘ command.
Linux ip command somewhat similar to ‘ifconfig’ command but it’s much more powerful with much more functionalities attached to it. ‘ip’ command is able to perform several tasks which were not possible to perform with ‘ifconfig’ command.
In this tutorial, we are going to discuss 12 ‘ip’ command examples in linux, so let’s get going,
Example 1) View Current network information for interfaces ( LAN Cards )
To display IP address and Subnet etc for the interfaces, use ‘ip addr show‘ command
[linux[email protected]]$ ip addr show or [[email protected]]$ ip a s
This will show network information related to all interfaces available on our system, but if we want to view same information for single interface, command is
[[email protected]]$ ip addr show enp0s3
where enp0s3 is the name of the interface.
Example 2) Enabling & disabling network interface (or LAN card)
We can enable or disable network interface (enp0s3) with ip command, example is shown below:
[[email protected]]$ sudo ip link set enp0s3 up
Above command enable the interface enp0s3 and to disable the network interface we will use ‘down’ trigger,
[[email protected]]$ sudo ip link set enp0s3 down
Example 3) Assign IP address & broadcast address to an interface
To assign IP address to interface with ip command , we will use below:
[[email protected]]$ sudo ip addr add 192.168.0.50/255.255.255.0 dev enp0s3
We can also set broadcast address to interface with ‘ip’ command. By default no broadcast address is set, so to set a broadcast address command is
[linuxtechi@localhost]$ sudo ip addr add broadcast 192.168.0.255 dev enp0s3
We can also set standard broadcast address along with IP address by using the following command,
[linuxtec[email protected]]$ sudo ip addr add 192.168.0.10/24 brd + dev enp0s3
As shown in the above example, we can also use ‘brd’ in place on ‘broadcast’ to set broadcast ip address.
Example 4) Remove IP address from the interface
If we want to flush or remove the assigned IP from the interface, then run beneath ip command
[li[email protected]]$ sudo ip addr del 192.168.0.10/24 dev enp0s3
Example 5) Adding an Alias for an interface (enp0s3)
To add an alias i.e. assign more than one IP to an interface, execute below command
[[email protected]]$ sudo ip addr add 192.168.0.20/24 dev enp0s3 label enp0s3:1
Example 6) View route table and default gateway information
Checking route table and default gateway information shows us the route a packet will take to reach the destination. To check the network routing information, execute the following ip command,
[[email protected]]$ ip route show
In the above output we will see the routing information for packets for all the network interfaces. We can also get the routing information to a particular ip using,
[[email protected]]$ sudo ip route get 192.168.0.1
Example 7) Adding a static route with ip command
If we want to change the default route taken by packets, we can do so with IP command by adding a static route.
To assign a new default gateway, use following ‘ip route’ command
[linux[email protected]]$ sudo ip route add default via 192.168.0.150/24
So now all network packets will travel via 192.168.0.150 as opposed to old default route. For changing the default route for a single interface & to make change route further, execute
[[email protected]]$ sudo ip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3
Example 8) Delete static route
To delete or remove the previously added default route, run ‘ip route del’ command
[[email protected]]$ sudo ip route del 192.168.0.150/24
Note:- Changes made to default route using the above mentioned commands are only temporary & all changes will be lost after a system has been restarted. To make a persistence route change, we need to modify / create route-enp0s3 file . Add the following line to it, demonstration is shown below
[[email protected]]$ sudo vi /etc/sysconfig/network-scripts/route-enp0s3 172.16.32.32 via 192.168.0.150/24 dev enp0s3
Save and Exit the file.
If you are using Ubuntu or Debian based OS, then the location of the file is ‘/etc/network/interfaces‘ and add the line “ip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3” to the bottom of the file.
Example 9) View all ARP entries (ip neigh)
ARP, short for ‘Address Resolution Protocol‘ , is used to convert an IP address to physical address (also known as MAC address) & all the IP and their corresponding MAC details are stored in a table known as ARP cache.
To view entries in ARP cache i.e. MAC addresses of the devices connected in LAN, the IP command used is
[[email protected]]$ ip neigh
To display entries of arp table run ,
$ arp -a
Example 10) Modifying ARP entries
To delete an ARP entry, the command used is
[l[email protected]]$ sudo ip neigh del 192.168.0.106 dev enp0s3
or if we want to add a new entry to ARP cache, the command is
[[email protected]]$ sudo ip neigh add 192.168.0.150 lladdr 33:1g:75:37:r3:84 dev enp0s3 nud perm
where nud means neighbour state, it can be
- perm – permanent & can only be removed by administrator,
- noarp – entry is valid but can be removed after lifetime expires,
- stale – entry is valid but suspicious,
- reachable – entry is valid until timeout expires.
Example 11) View network statistics
With ‘ip’ command we can also view the network statistics like bytes and packets transferred, errors or dropped packets etc for all the network interfaces. To view network statistics, use ‘ip -s link‘ command
[[email protected]]$ ip -s link
Example 12) How to get help
If you want to find a option which is not listed in above examples, then you can look for help. In Fact you can use help for all the commands. To list all available options that can be used with ‘ip’ command, use
[[email protected]]$ ip --help
Remember that ‘ip’ command is very important command for Linux admins and it should be learned and mastered to configure network with ease. That’s it for now, please do provide your suggestions & leave your queries in the comment box below.
Also Read : How to Automate tasks in Linux using Crontab
4 thoughts on “12 IP Command Examples for Linux Users”
The commentary for Example 7 gives the gateway as 192.168.1.150
Shouldn’t that be 192.168.0.150?
It was a typo, i have corrected it now., thank you very much for pointing this error.
Thank you. Changing over from ifconfig. ip much more powerful. i like it. ip neigh is good too. there much for to learn on ip. i think it even loops and was made for scripting.
Your example of adding alias IP addresses is only a temporary fix. It does not write anything to the /etc/sysconfig/network-scripts directory. But your method DOES work better than anything I have tried to add to a network-script, the only problem is your method won’t survive a reboot. My method of adding it to the network-scripts creates duplicate routes.. Would love to see how you can get your method to save and survive a reboot with the same results.