Networking is one of the core elements of server management, and can be one of the more complex to manage. This recipe will show you how to use the IP tool to discover the details of, and make changes to, the networking setup of your server.
Although these are ephemeral changes, the ability to apply them at the command line is very powerful; for instance, it allows you to script the addition and removal of IP addresses. When looking at command-line tools, it's a good idea to think of not only how they can help you now, but also how they could be used for automation in future.
The IP tools come preinstalled on the major Linux distributions (RHEL and Debian based), so additional configuration is no longer required.
Let's configure the network interface as follows:
Sometimes you just want to know the IP address of the server you are working on; this can be found using the following command:
$ ip addr show
This should give you an output similar to to the following screenshot:
This output gives you the details of each interface, including the IP address, state, MAC address, and interface options.
To narrow this down to a single interface, you can use the following command:
$ ip addr show <interface>
Where
<interface>
is the name of the network interface you wish to see the details for. So, for example, to see the details ofeth0
you can use the following command:$ ip addr show eth0
To add a new IP4 address, you can use the
ip addr add
command. Note that you also need to supply a netmask in Classless Inter-Domain Routing (CIDR) format. The command looks like this:$ ip addr add <IP address>/<CIDR> dev <device>
For example, to add a new
RFC1918
-compliant address to the interface namedeth1
, you will use the following command:$ ip addr add 10.132.1.1/24 dev eth1
You can confirm that the new address is available on the interface using the
ip addr show
command:$ ip addr show eth1
Removing an IP address is a straightforward reversal of adding it. The command is the same as the command used to add addresses, but with the delete option:
$ ip addr del <IP address>/<CIDR> dev <device>
So, to remove the address that we just added, we can use the following command:
$ ip addr del 10.132.1.1/24 dev eth1
The IP command can be used to control the physical interfaces; thus, allowing you to bring them up and down from the command line.
To bring an interface
down
, you can use theip link
set command, followed by the interface name, and then the desired state. For example, you can use the following command to enable theeth2
interface:$ ip link set eth2 down
Conversely, to bring it back up, you can use the following command:
$ ip link set eth2 up
You can check the state of the interface using the
ip
command:$ ip addr eth2