Linux Networking Commands


Linux networking commands, part 1: IP




Linux networking commands are provided by two packages iproute2 and net-tools. You would have probably used commands like ifconfig, route, arp, hostname etc if you have done networking with linux devices. These commands are part of the package net-tools which has been part of all linux distributions for a long time. Though popular, most of the linux distributions are slowly trying replace these with the networking commands present in new package iproute2.


Iproute2 package contain commands for linux kernal’s advanced networking features like tunneling, traffic controlling, packet encryption along with commands for doing familiar tasks like configuration of interfaces (IP addresses, MAC addresses, MTU, Link status etc) , routing table, multicast addressing, arp table etc.


Networking commands which are part of of the iproute2 package are ip , tc , arpd , ss , bridge , devlink , rtmon, nstat, routef, routel, ctstat and lnstat.


This tutorial describes how to use “ip” command for configuring IP address, MAC address, routing table , ARP table etc.

IP – The all in one tool

“ip” command is the SwissKnife for networking included in the iproute2 package. “ip” can be used for configuring interfaces, multicast address, arp table, routes, NAT, packet encryption, tunneling, routing policies etc. This command can also be used for monitoring the status of interfaces, routes, addresses continuously.


Checking IP address and status of all interfaces

ip -brief address

(You can also use the command in abbreviated form as “ip -bri addr” or just “ip -bri a” )

checking ip address in linux

For those who like a fancy output, use “-c” option to get the output in color

highlighted view of ip address

If you need more detailed information about interfaces like MAC address, MTU size, broadcast address etc, use

ip address show

or in short

ip a

checking ip using ip address show

“ip -bri a” will show both IPv4 and IPv6 addresses, If you want to filter only IPv4 addresses use

ip -bri -4 a

displaying only IPv4 addresses

and to show only IPv6 addresses use

ip -bri -6 a

displaying only IPv6 addresses

Linux networking commands, part 1.1 : ip addr

“ip addr” can be used to add, delete, replace and show IP address of an interface.

To configure IP address in linux, use

sudo ip addr add [ip-address] dev [interface-name]

Example:

sudo ip addr add 192.168.1.1/24 dev enp2s0f0

configuring IP address in linux

To delete ip address replace the option “add” with “del” in the above command

Usage:

sudo ip addr del [ip-address] dev [interface-name]

Example:

sudo ip addr del 192.168.1.1/24 dev enp2s0f0


Note: “ip” command allows configuring multiple ip address in one interface.

configuring mulitiple IP address in the same interface

If multiple ip addresses are configured , you can delete all address using “flush”

sudo ip address flush dev enp2s0f0


Configuring IPv6 address in linux

You can use the option “-6” with the above command to configure or delete IPv6 address.

Usage:

sudo ip -6 addr add [ipv6-address] dev [interface-name]

Example:

sudo ip -6 addr add fc00::1/64 dev wlp3s0

configuring IPv6 address linux

To delete IPv6 address, use

sudo ip -6 addr add [ipv6-address] dev [interface-name]



You can configure interface parameters like MAC, MTU, status etc using “ip link”


To make an interface up (or down) in linux, use

sudo ip addr add [ip-address] dev [interface-name]

Example:

sudo ip link set dev enp2s0f0 down

sudo ip link set dev enp2s0f0 up


To change MAC address in linux

sudo ip link set [MAC-address] dev [interface-name]

Example:

sudo ip link set addr 00:11:22:33:44:55 dev enp2s0f0

command to change mac addresses in linux

To change MTU, use

sudo ip link set mtu [MTU-size] dev [interface-name]

Example:

sudo ip link set mtu 1000 dev enp2s0f0


To change interface name, use

sudo ip link set name [new-int-name] dev [current-int-name]

Example:

sudo ip link set name eth0 dev enp2s0f0

You might get the error “RTNETLINK answers: Device or resource busy” when trying to change the features of a interface, trying the command after making interface “down will work

command to change MTU size in linux

Linux networking commands, part 1.3: ip neighbor

ip neighbor command provides options to work with ARP table. You can add, delete, replace or flush entries in the ARP table using this command.


To check ARP table, use

ip neighbor show

or in short,

ip n

command to check ARP table in linux

To add or delete an entry in ARP table, use

sudo ip neighbor add [nbr-ip-address] lladdr [nbr-mac-address] dev [int-name]

Example:

sudo ip neighbor add 192.168.0.150 lladdr 00:11:22:33:44:55 dev wlp3s0


To delete an ARP entry, replace the option “add” with “del” in the above command.

sudo ip neighbor del 192.168.0.150 lladdr 00:11:22:33:44:55 dev wlp3s0


To clear ARP cache, use

sudo ip neighbor flush dev [int-name]



Linux networking commands, part 1.4: ip route

“ip route” can be used to check the routing table, manipulate routes and default gateway.


To check the routing table, use

ip route show

or in short,

ip r

linux command to check routing table

To check IPv6 routing table, use

ip -6 route show

or in short,

ip -6 r

command to check IPv6 routing table

To add a static route in linux, use

sudo ip route add [Network-address] via [NextHop-IP]

Example:

sudo ip route add 192.168.2.0/24 via 192.168.0.2

adding static routes linux

To delete a route, use

sudo ip route del [Network-address]

Example

sudo ip route del 192.168.2.0/24

To add a default route in linux, use

sudo ip route add default via [gateway-ip]

Example:

sudo ip route add default via 192.168.0.3

command to add default route in linux

If you want to check which path is selected to reach a destination, use

ip route get [destination-ip]

checking the path selected