Networking

Basic Linux Network Commands

The ip command
The ifconfig command
The iwconfig command
The dhclient command
The route command
The host command
The netstat command

This tutorial briefly describes some basic commands any Linux user should know to check the network configuration, state and configure it. This tutorial will show basic applications of commands, a new tutorial is being written to explain more advanced scenarios. For advanced tips on the following and additional tutorials check Advanced Linux Network Commands. Here you’ll learn how to change IP addresses on Linux, how to disable and enable network cards under Linux using different commands, how to set your wireless card in monitor and managed modes with iwconfig, how to get an IP address from your DHCP server using the dhclient command, how to set a gateway under Linux, get the IP address of a domain name and monitor the traffic between your device and the network with netstat, in some cases the same functions with different commands.

The ip command

The ip command allows to print information on the network interfaces and configuration, as well to apply changes, it replaces the old ifconfig command which will be explained later in this tutorial.

To get a general view on your network devices configuration you can run:

ip address

An equivalent to ip address is ip addr show, it will throw the same result:

ip addr show

If you want to limit the information to IPv4 information run:

ip -4 a

Note: if you work with IPv6 run ip -6 a

To show the information on a single network device apply the option dev as in the next example:

ip addr show dev <device>

In my case I executed:

ip addr show dev wlp3s0

If you want to show the IPv4 information of a single device you can run:

ip -4 addr show dev wlp3s0

To remove an existing IP address for a device you can run:

Ip addr del <IP-Address> dev <Device-Name>

In my case I ran:

ip addr del 192.168.0.14 dev wlp3s0

Then, to assign a new IP address run:

Ip addr add <IP-Address> dev <Device-Name>

In my case I executed:

ip addr add 192.168.0.16 dev wlp3s0

To disable a network card the command is:

ip link set down <Device-Name>

In my case I run:

ip link set down enp2s0

To enable the network card called enp2s0 back I run:

The ifconfig command

The ifconfig command is predecessor to the command ip. Not all modern Linux distributions include it, to get it working some distributions need the user to install the package net-tools.

To get a general view on your devices configuration run as root:

ifconfig

You can also print information on a specific device by adding it after invoking ifconfig, in the following example I print information for the wlp3s0 device:

ifconfig wlp3s0

With ifconfig you can change the address by specifying it after the network device

ifconfig <Device-Name>  <IP-Address> up

In my case it would be like:

ifconfig wlp3s0 192.168.0.17 up

Note: the examples above show how to enable the network card using the command ifconfig, but you can also use the command ip link set down <device> as explained in the ip command section.

It is recommended to include the proper netmask as in the next example by adding the netmask option.

ifconfig wlp3s0 192.168.0.17 netmask 255.255.255.0 up

For more information on the ifconfig command, check the tutorial How to use ifconfig.

The iwconfig command

Very similar to ifconfig, Linux includes the iwconfig command too which is useful to set configurations exclusive for wireless network cards. In the following examples the iwconfig command will be used to change the wireless network card modes between managed mode and monitor mode. The monitor mode is useful to sniff packets without being associated with other stations or routers, it is mandatory when cracking wireless routers with programs like reaver or aircrack-ng.

As you can see by default our network cards are set in Managed mode, which allows the wifi card to act as a network station. Run:

iwconfig wlp3s0

Note: where “wlp3s0” replace it with your network device name.

To change the Managed mode shown in the image above, first we need to disable the wifi card using the command ifconfig.

ifconfig wlp3s0 down

Then set the monitor mode by running:

iwconfig wlp3s0 mode monitor

And now enable the wireless network card back

ifconfig wlp3s0 up

Check the new mode by running iwconfig wlp3s0:

iwconfig wlp3s0

To restore the managed mode repeat the process replacing the step iwconfig wlp3s0 mode monitor for iwconfig wlp3s0 mode managed

The dhclient command

When joining a network you obviously need a free IP address to attach your device to it. While you can ping or nmap the network to set up a new device, many routers assign it through DHCP. if you want  your new device to automatically get an IP address and it didn’t by default you can use the dhclient command. In many cases when trying to join a wireless network using WICD, I found the “Obtaining IP” process to get stuck or taking a long time and managed to help it by running this command.

The syntax is:

dhclient <Device-Name>

In my case

dhclient enp2s0

The route command

When joining a network which wasn’t properly configured to allow new devices to connect you will need to set a proper IP address, usually it will be manually through the command ip or ifconfig, to set the proper gateway using the command route and enable proper DNS, like 8.8.8.8 to access internet. The following example shows how to add and remove gateways.
To remove a defined gateway run:

route del default gw <IP-Address> <Device-Name>

In my case I ran:

route del default gw 192.168.0.1 wlp3s0

To add a new gateway run:

route add default gw 192.168.0.1 wlp3s0

To check your defined gateways run:

route

The host command

The command host is useful for domain name lookup, to learn the IP address of a specific domain name. For example, to learn linuxhint.com IP address just run

host linuxhint.com

The netstat command

To end this tutorial lets see how to monitor our network traffic with the command netstat.
Run the command netstat without options:

netstat

By default the netstat command will show ESTABLISHED connections, If you want to list the listening ports only run the command with the -l option:

netstat -l

By default, netstat will also list all TCP and UDP connections, if you want to show TCP connections, or listening ports only run:

netstat -at

Note: the option -a prevents lookup to speed up results.

If you want to show UDP connections only then run:

netstat -au

I hope you found this article useful to learn basic network commands, learning them is a must for every Linux user to afford any network inconvenient. Keep following LinuxHint for more tips and updates on Linux and networking.

About the author

Ivan Vanney

Ivan Vanney

Ivan Vanney has over 2 years as writer for LinuxHint, he is co-founder of the freelance services marketplace GIGopen.com where he works as a sysadmin.