Last updated: September 17, 2021.
Those using Linux for a long time remember that networking was configured using ifconfig
and route
. Those two commands are still available in some distributions, but they have disappeared in others. In addition, a number of other tools appeared. For example, NetworkManager became de facto standard of the network management service.
Let us cover a number of useful networking commands. You should replace the parts marked with <>
with your own configuration. For example, <ifce>
may be eth0 in your configuration. The $
sign marks your prompt.
Basic networking
Basic networking operations include showing available interfaces, their addresses and configuration options; it also includes routing.
Showing all interfaces with their addresses
You can show all interfaces with their status, and assigned addresses using one of the two commands (the second one is the abbreviated version):
$ ip addr show
$ ip a
It is an equivalent of (older) ifconfig
or ifconfig -a
.
Setting addresses
The commands for adding and deleting network addresses are quite similar. With ip they have the following form:
$ ip addr add <address> dev <ifce>
$ ip addr del <address>/<prefix> dev <ifce>
In the delete command, you can still (in 2021) specify the address without a prefix, but you will get a warning message.
They are equivalents of older:
$ ifconfig <ifce> add <address>
$ ifconfig <ifce> del <address>
Setting up MTU
In some cases we might want to change the MTU (Maximum Transmission Unit) value, giving the maximum size of a datagram packet in the network. With ip
the command is:
$ ip link set dev <ifce> mtu <mtu_value>
It is an equivalent of:
$ ifconfig <ifce> mtu <mtu_value>
Setting interfaces up and down
If we want temporarily disable an interface and stop receiving packets, we put the interface “down”. It will start receiving packets again when we move it back “up”. You can change the state of the interface using commands like:
$ ip link set <ifce> down
$ ip link set <ifce> up
They are equivalents of older:
$ ifconfig <ifce> down
$ ifconfig <ifce> up
Showing routing
If you want to show the routing information of the host, use the following command:
$ ip route show
It is an equivalent of the older route
command:
$ route -a
or
$ route
Adding a default gateway
The default gateway points to the gateway to be used if there are no other matches in the routing table. Usually it will be your external interface. To set up the default gateway use:
$ ip route add default via <gateway_ip>
It is an equivalent of:
$ route add default gw <gateway_ip>
Adding and removing routes
If you’d like to learn more about the routing concepts, you can read the introduction guide (using route-based commands).
The command to add and remove a route look like:
$ ip route add <network>/<mask> via <gateway_ip> dev <ifce>
$ ip route del <network>/<mask> via <gateway_ip> dev <ifce>
We can also start typing the beginning of the command, for example ip route del <network>/<mask>
The commands using route were:
$ route add -net <network>/<mask> gw <gateway_ip> <ifce>
$ route del -net <network>/<mask> gw <gateway_ip> <ifce>
Getting packet statistics
In Linux there are a number of ways to get statistics about received and sent packets. We can obtain them using the following commands:
$ ip -s link
$ cat /proc/net/dev
You can also look into individual files with the interface statistics, for example the number of received bytes is available in /sys/class/net/<ifce>/statistics/rx_bytes
. To show the value you can run:
$ cat /sys/class/net/<ifce>/statistics/rx_bytes
All of them can replace packet statistics of ifconfig
or netstat -i
NetworkManager
NetworkManager is a tool configuring automatically all types of network, included in most distributions. It has a number of graphical frontends, but also a handy command-line tool, nmcli
.
Making an interface unmanaged
In a typical configuration NetworkManager will be taking care of all network interfaces of your system. Sometimes we need more control, for example to set up an IP address manually for testing. To do this, we need to put the interface in the unmanaged mode for NetworkManager. This commands allows us to manually handle addresses and routing of an interface. If an interface stays in managed mode, NetworkManager will reapply its configuration and manual changes may disappear.
$ nmcli device set <ifce> managed no
This command will take effect only until a reboot or restarting NetworkManager. It is also possible to permanently move an interface into unmanaged mode. In this case, change /etc/NetworkManager/conf.d/99-unmanaged-devices.conf
or /etc/NetworkManager/NetworkManager.conf
and add the following:
[keyfile]
unmanaged-devices=interface-name:<ifce>
You can learn more about configuration of NetworkManager from the documentation.
Verifying status of interfaces in NetworkManager
If you need to check the high level status of devices (names, connected or not), the NetworkManager command is:
$ nmcli device status
Checking detailed status in NetworkManager
NetworkManager allows also to check all detailed information about system network interfaces, including the IP addresses, DNS, routing, MTU. The command is:
$ nmcli device show