In this blog post, we will explain how to master Linux Networking with ip command.
Linux, with its open-source nature, provides users with a vast array of tools to empower their networking endeavors. Among these tools, the IP command stands out as a versatile and powerful utility for managing various aspects of network configuration. IP command can be your key to mastering network configurations on your Linux system.
Understanding the ip command
The “ip” command is a part of the iproute2 package, which is pre-installed on most Linux distributions. It it supersedes the deprecated ifconfig command and serves as a one-stop solution for network configuration, addressing, routing, and more. Whether you’re setting up a new network interface, troubleshooting connectivity issues, or fine-tuning network parameters, the “ip” command is your go-to tool.
Without any further delay, let jump into the real time IP command examples which will help you to master the Linux networking.
1) Display Network Interfaces
The first step in Linux networking is to identify your network interfaces. Open the terminal and run following command:
$ ip link show
Output above shows that we have one network interface with name “enp0s3”.
2) Display Current Network Information
To display current IP address and Subnet etc for the interfaces, use ‘ip addr show‘ command
$ ip addr show or $ 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 a particular interface, command is
$ ip addr show enp0s3
where enp0s3 is the name of the interface.
3) Configure Network Interfaces
To configure an interface, use the ‘ip addr’ command. For instance, to set a static IP address:
$ sudo ip addr add 192.168.1.50/255.255.255.0 dev enp0s3
Additionally, 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
$ sudo ip addr add broadcast 192.168.1.255 dev enp0s3
We can also set standard broadcast address along with IP address by using the following ip command,
$ sudo ip addr add 192.168.1.50/24 brd + dev enp0s3
As shown in the above example, we can also use ‘brd’ in place on ‘broadcast’ to set broadcast ip address.
4) Adding Default Route
To add a default route, run following ip command with route option.
Syntax: $ sudo ip route add default via <Gateway_IP_Address>
$ sudo ip route add default via 192.168.1.1
5) Bring an Interface Up/Down
Use ‘ip link set‘ command to bring an interface up or down:
$ sudo ip link set enp0s3 up
Above command will enable the interface enp0s3.
$ sudo ip link set enp0s3 down
Command above will bring down or disable the interface enp0s3
6) Remove IP Address From an Interface
If we want to flush or remove the assigned ip address from the interface, then run beneath command
$ sudo ip addr del <IP-Address> dev <Interface-Name>
$ sudo ip addr del 192.168.1.50/24 dev enp0s3
7) Adding an Alias for an Interface
To add an alias i.e. assign more than one IP to an interface, execute below command
$ sudo ip addr add 192.168.1.120/24 dev enp0s3 label enp0s3:1
8) Display Routing Table
View your routing table with ‘ip route show’. Route table and default gateway information shows us the route a packet will take to reach the destination.
$ 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,
$ sudo ip route get 192.168.1.1 192.168.1.1 dev enp0s3 src 192.168.1.4 uid 1000 $
9) Adding a Static Route
To add a static route, specify the destination network and gateway, example is shown below:
$ sudo ip route add 172.16.32.32 via 192.168.1.150/24 dev enp0s3
10) Remove a Static Route
Remove a route using ‘ip route del’:
$ sudo ip route del 192.168.1.150/24
11) List 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.
Use ‘ip neigh‘ to list the entries in ARP cache i.e. MAC addresses of the devices connected in LAN.
$ ip neigh
12) Modifying ARP entries
To delete an ARP entry, the command used is
$ sudo ip neigh del 192.168.1.177 dev enp0s3
or if we want to add a new entry to ARP cache, the command is
$ sudo ip neigh add 192.168.1.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.
13) 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
$ ip -s link
14) Create a VLAN Interface
Create a VLAN interface using ‘ip link add’:
$ ip link add link enp0s3 name enp0s3.110 type vlan id 110
15) Set MTU Size of an Interface
To modify the MTU (Maximum Transmission Unit) size of an interface, run
$ ip link set enp0s3 mtu 1700
16) How to get help
If you want to find an 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
$ 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