Debian: Network Configuration

Practice



Network configuration in Linux Debian. This topic is intended to become a cheat sheet for a system administrator on "how to manage the network in Debian". It describes the basic principles and actions for managing network settings on this family of operating systems.


Network interfaces

Debian names network cards by the name eth and a number. Virtual interfaces (loopback, vpn, etc.) Debian names separately, likewise by a name (for example, tun, tap) followed by a number.

For example, if you have 2 network cards in your computer, then by default they will be named eth0 and eth1.

Note that this operating system permanently binds the MAC address of the physical network interface (network card) to the ethX number. So, if you had a network card in the computer named eth0 and you replaced it, then after booting following the replacement you will not see the expected eth0, but will instead see eth1.

As a particular but illustrative case: a server with two network interfaces eth0 and eth1. One of the network cards burns out and the administrator has to replace it. After starting the server, he never gets a response to a ping on that network interface. Logging into the console - what does he see? He sees the old second network card named eth1, and the new one, but not named eth0 as expected, instead named eth2! This is because Debian has bound a specific interface with a specific MAC address to eth0.

You can view the bound cards and their MAC addresses at the path:

/etc/udev/rules.d/70-persistent-net.rules

By editing this file you can remove old network cards from Debian's list.


IP addresses

Persistent settings are stored in the file /etc/network/interfaces

Here is an example file:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240

In this file the IPv4 address 192.168.0.1 is defined on the network card eth0. The default gateway 192.168.0.250 is also defined. The dns-nameservers option is left in case the file /etc/resolv.conf lacks DNS server information.

Note that modern Debian does not need the network and broadcast lines, computing them perfectly well on its own from the IP address and mask. However, if you want to override these values, you will need to specify such options.

And here is an example of this file for 2 network cards:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240

auto eth1
iface eth1 inet static
address 192.168.100.1
netmask 255.255.255.0
network 192.168.100.0
broadcast 192.168.100.255


You can change IP address values on the fly using the ifconfig utility:

# ifconfig eth0 inet 192.168.0.33 netmask 255.255.255.0

This command will change the IPv4 address of the network card eth0 to 192.168.0.33 with a /24 mask.


If network settings need to be obtained via DHCP - specify this in the interfaces file, for example:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp


Specifying the DNS server

DNS servers are specified (no more than three) in the file:

/etc/resolv.conf

For example, here is an example of such a file:

nameserver 192.168.0.240
nameserver 192.168.0.242

After changing the values in the file, they take effect immediately.


Machine name and domain

You can view or change the machine name on the fly with the hostname command:

# hostname
myserver
# hostname boo
# hostname
boo

The domain is specified in the file:

/etc/resolv.conf

for example, here is an example of a resolv.conf file:

domain mydomain.ru
nameserver 192.168.0.240
nameserver 192.168.0.242
nameserver 192.168.0.244

The machine name can be changed permanently in the file:

/etc/hostname

But remember that after this you also need to change the hostname in the file

/etc/hosts

So that the values in it correspond to the new hostname (things like 127.0.0.1 are specified there, for example).


Routing

The static routing scheme in Debian is traditional: static routes and a default gateway.

Routing can be managed on the fly through the route utility:

# route add -net 10.0.3.0/24 gw 192.168.0.251

This command will add a route to the network 10.0.3.0:255.255.255.0 via the router with address 192.168.0.251.

# route del -net 10.0.3.0/24

And this command, conversely, will remove the route 10.0.3.0/24.

The default router is specified as 0.0.0.0/32 or default, for example:

# route add default gw 192.168.0.250

This command will add the default gateway 192.168.0.250.

You can view the current routes with the command:

# netstat -r


Static routes are specified permanently in the file

/etc/network/interfaces

For example, let's take the following file:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240
up route add -net 10.0.0.0/24 gw 192.168.0.251
up route add -net 10.0.1.0/24 gw 192.168.0.252
up route add -net 10.0.2.0/24 gw 192.168.0.253

From this file we can see that:

a) The default gateway is 192.168.0.250

b) When the interface comes up, routes to networks 10.0.0.0/24, 10.0.1.0/24 and 10.0.2.0/24 will be added via, respectively, 192.168.0.251, 192.168.0.252 and 192.168.0.253.


Router

By default the system does not route passing packets. To enable this (turn our server into a router) you need to set the kernel variable

net.ipv4.ip_forward

to the value "1".

For example:

# sysctl net.ipv4.ip_forward=1

To make this value persist after a reboot - add it to the file

/etc/sysctl.conf

with the following line:

sysctl net.ipv4.ip_forward=1


Network aliases (multiple IP addresses on one interface)

It is not uncommon to encounter a situation where one network card (or simply one network interface) needs to be assigned not one, but several IP addresses. In that case:

a) A primary address must be assigned to the ethX interface
b) Aliases (additional IP addresses) are assigned as ethX:Y, where Y is the alias number

For example, this is how we would assign 2 more IP addresses to the eth0 interface:

# ifconfig eth0:1 inet 192.168.1.1 netmask 255.255.255.0
# ifconfig eth0:2 inet 192.168.2.1 netmask 255.255.255.0

These interfaces are specified permanently in the same file as all the others:

/etc/network/interfaces

Let's give an example of such a file:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 127.0.0.1

auto eth0:1
iface eth0:1 inet static
address 10.0.1.2
netmask 255.255.255.0
network 10.0.1.0
broadcast 10.0.1.255

auto eth0:2
iface eth0:2 inet static
address 10.100.10.3
netmask 255.255.255.0
network 10.100.10.0
broadcast 10.100.10.255

auto eth0:3
iface eth0:3 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

Here we can see that we specified one primary address and 3 aliases. Note that the gateway or dns-nameservers options cannot be applied to aliases.


VLANs

First, make sure the vlan package is installed:

# dpkg --list | grep vlan
ii vlan 1.9-3 user mode programs to enable VLANs on your ethernet devices

If not, install it:

# aptitude install vlan

So that VLANs are set up on reboot, they need to be listed in the file

/etc/network/interfaces

For example, here is an example of such a file:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 127.0.0.1

auto vlan333
iface vlan333 inet static
address 10.0.1.2
netmask 255.255.255.0
vlan_raw_device eth0

auto eth0.999
iface eth0.999 inet static
address 10.100.10.3
netmask 255.255.255.0
vlan_raw_device eth0

auto eth0.100
iface eth0.100 inet static
address 192.168.1.1
netmask 255.255.255.0
vlan_raw_device eth0

Here we see the declaration of 3 VLANs. Both the vlan333 notation and the eth0.999-style notation are correct. The difference will only be in their naming as interfaces. I prefer the eth.XXX form, which immediately tells me which interface the VLAN sits on.

The vlan_raw_device option was also added, indicating to the system which network card this VLAN will be tagged on.

VLANs can be managed at runtime through the vconfig utility. For example:

# vconfig add eth0 777

This command will add VLAN=777 to the eth0 interface.

# vconfig rem vlan777

And this command will remove the VLAN vlan777.


Network bridges

You can read about what this is on the internet. To set one up we need bridge-utils installed. Let's check for it:

# dpkg --list | grep bridge-utils
ii bridge-utils 1.4-5 Utilities for configuring the Linux Ethernet bridge

If not present, install it:

# aptitude install bridge-utils

Bridges are specified in the file

/etc/network/interfaces

For example, like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# Physical interface
#auto eth0
#iface eth0 inet manual

#auto eth1
#iface eth1 inet manual

# The primary network interface
auto br0
iface br0 inet static
bridge_ports eth0 eth1
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240

Here you can see that we do not configure the network cards themselves (we do not assign them an IP address, we do not let them get settings via DHCP). This must be done this way!

We name the network bridge br0. It is on this bridge that we specify all the necessary settings and the bridge_ports option, which shows which network interfaces to include in this bridge.

On the fly, bridges are managed with the brctl utility. For example, this is how you can create a network bridge from 2 network cards (the cards must not be configured and must be in the down state):

# brctl addbr "br0"
# brctl addif br0 eth0
# brctl addif br0 eth1
# ifconfig br0 up

This sequence of commands:
a) Creates the virtual switch br0
b) Adds the network interfaces eth0 and eth1 to it
c) "Brings up" the bridge (up), i.e. enables it

After this, the bridge can be given network settings (or not - in that case these network cards will work as an unmanaged switch, and it will not be possible to connect to them from outside via these connections).

# ifconfig br0 inet 192.168.0.254 netmask 255.255.255.0

Other examples:

# brctl delif br0 eth1

This command will remove the eth1 interface from the virtual switch (bridge).

# brctl delbr br0

And this command will remove the br0 bridge entirely.


Link aggregation (Bonding)

Interface aggregation is a way to increase data transfer speed and/or provide fault tolerance by using two or more physical network cards. It works so that several network cards act as one virtual one (not to be confused with a bridge!), and depending on the operating mode, these network cards either increase throughput (by a factor equal to the number of network cards combined), or provide fault tolerance (i.e. one card works, and if it dies, the second one starts working), or both.

This requires the ifenslave-2.6 package to be installed. If it isn't installed, install it:

# aptitude install ifenslave-2.6

The configuration is specified in the file

/etc/network/interfaces

For example, like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# Physical interface
#auto eth0
#iface eth0 inet manual

#auto eth1
#iface eth1 inet manual

# The primary network interface
auto bond0
iface bond0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.250
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.0.240
slaves eth0 eth1
bond_mode round-robin
bond_miimon 100
bond_downdelay 200
bond_updelay 200

Here you can see that the physical cards are not configured - they are not assigned static IP addresses and are not allowed to use DHCP to get settings. Instead, they participate in creating the bond0 interface, which itself receives the network settings.

ATTENTION! On the other end of the network cables, the network cards must be configured identically (i.e. operate in the same mode). If these are hardware switches or routers, you will have to pick the operating mode - whichever is supported by the device on the other end. If it is a server, it is best to use:

a) the same network cards (if possible)
b) the same OS (this will reduce the likelihood of aggregation failures)


Automatically running scripts when an interface comes up/down

4 directories serve this purpose:

/etc/network/if-pre-up.d
/etc/network/if-up.d
/etc/network/if-down.d
/etc/network/if-post-down.d

Accordingly, the if-pre-up.d directory holds scripts that run before the interface comes up; if-up.d holds scripts that run after the interface comes up; if-down.d holds scripts that run before the interface goes down, and if-post-down.d holds scripts that run after it goes down.

Among other things, the scripts being called can use special variables from the calling process's environment, for example $IFACE (the interface name) or $MODE (the operation). For more details - see the man pages.

See also

  • [[b6537]]

See also

    Comments

    To leave a comment

    If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
    To reply

    Lectures and tutorial on "Computer networks"

    Terms: Computer networks