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=198.27.64.8 NETMASK=255.255.255.0 ONBOOT=yes GATEWAY=198.27.64.254 IPV6INIT=yes IPV6_AUTOCONF=no IPV6ADDR=2607:5300:0060:1908::1/128
As we see above device eth0 is assigned to ip address 198.27.64.8
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 198.27.64.8 to device eth0
ip addr add 198.27.64.8 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 198.27.64.8/24 brd 198.27.64.255 scope global eth0
To delete an ip address use del instead of addr in the above command.
ip addr del 198.27.64.8 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 198.27.64.254 dev eth0
198.27.64.0/24 dev eth0 proto kernel scope link src 198.27.64.8
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 198.27.64.254 dev eth0
198.27.64.0/24 dev eth0 proto kernel scope link src 198.27.64.8
ip route add 198.27.64.0/24 via 198.27.64.254 dev eth0
In the above we are specifying the all the ip destinations 198.27.64.0/24 will go through the gateway 198.27.64.254
Similarly we can remove the above gateway for the ip address 198.27.64.0/24 using del option
ip route del 198.27.64.0/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 198.27.64.254 0.0.0.0 UG 0 0 0 eth0 198.27.64.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
In my case however all destination ip addresses from 198.27.64.0 to 198.27.64.255 are routing through 0.0.0.0 which means default gateway.
Wrap Up!
Above command will get you started on how to add, delete and show ip addresses.
Related Topics:
What Is Https and How To Enable Https