Table of Contents

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*

Lets open the file and see the contents of this file.

cat /etc/sysconfig/network-scripts/ifcfg-eth0

As we see above device eth0 is assigned to ip address

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.

  1. addr - Display and modify IP Addresses.
  2. link - Display and modify network interfaces.
  3. 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 to device eth0

ip addr add 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 brd scope global eth0

To delete an ip address use del instead of addr in the above command.

ip addr del 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 dev eth0 dev eth0 proto kernel scope link src 

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 dev eth0 dev eth0 proto kernel scope link src 

ip route add via dev eth0

In the above we are specifying the all the ip destinations will go through the gateway

Similarly we can remove the above gateway for the ip address using del option

ip route del

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         UG    0      0        0 eth0   U     0      0        0 eth0

In my case however all destination ip addresses from to are routing through 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

Check Listening Ports

Check Linux lsof

Related Posts