How To Use ip Command In Linux
In this post, I will go over the ip command and its usage through examples.
Ethernet ip address mapping file can found under following directory.
ls /etc/sysconfig/network-scripts/ifcfg-eth* /etc/sysconfig/network-scripts/ifcfg-eth0
Lets open the file and see the contents of this file.
cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static IPADDR=126.96.36.199 NETMASK=255.255.255.0 ONBOOT=yes GATEWAY=188.8.131.52 IPV6INIT=yes IPV6_AUTOCONF=no IPV6ADDR=2607:5300:0060:1908::1/128
As we see above device eth0 is assigned to ip address 184.108.40.206
However for this post we will focus on the command ip, which is used to assign static ip address to ethernet device.
ip command has different options. We will go through the following options.
- addr - Display and modify IP Addresses.
- link - Display and modify network interfaces.
- route - Display and alter the routing table.
How To Use ip addr Command In Linux
Lets go through one example. Lets say we want to assign ip address 220.127.116.11 to device eth0
ip addr add 18.104.22.168 dev eth0
Once it is done, we can check if our ip address successfully by using command ip addr show
ip addr show
You should see a long output and in that output you would see something like this...
inet 22.214.171.124/24 brd 126.96.36.199 scope global eth0
To delete an ip address use del instead of addr in the above command.
ip addr del 188.8.131.52 dev eth0
If it is successful; check the status again using ip addr show
How To Use ip link Command In Linux
Lets say now you added a new network interface eth1, we need to activate this network interface before we can assign static ip address to it. We can do using the ip command too.
ip link set eth1 up
Similarly we can also disable or remove a network interface using down option.
ip link set eth1 down
We can look at our routing table default via 184.108.40.206 dev eth0 220.127.116.11/24 dev eth0 proto kernel scope link src 18.104.22.168
I have only one ethernet device therefore it is showing only one entry; If you have more than one ; it will show other entries too.
How To Use ip route Command In Linux
We can also control the gateway and define how an ip address can be reached through which gateway.
ip route show default via 22.214.171.124 dev eth0 126.96.36.199/24 dev eth0 proto kernel scope link src 188.8.131.52
ip route add 184.108.40.206/24 via 220.127.116.11 dev eth0
In the above we are specifying the all the ip destinations 18.104.22.168/24 will go through the gateway 22.214.171.124
Similarly we can remove the above gateway for the ip address 126.96.36.199/24 using del option
ip route del 188.8.131.52/24
How To Use route -n Command In Linux
To check what our ethernet, ip to gateway routing table. Run the route -n command
route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 184.108.40.206 0.0.0.0 UG 0 0 0 eth0 220.127.116.11 0.0.0.0 255.255.255.0 U 0 0 0 eth0
In my case however all destination ip addresses from 18.104.22.168 to 22.214.171.124 are routing through 0.0.0.0 which means default gateway.
Above command will get you started on how to add, delete and show ip addresses.