Skip to content

Debian Network Configuration

Basic network configuration for Debian systems
Section titled “Basic network configuration for Debian systems”

Network configuration examples for Debian 12 and 13.

Tried to be as thorough as possible: single NIC, multiple NICs, multiple networks, NIC bonding.

Debian and Ubuntu network configurations are very different, so there are separate tutorials for Debian and Ubuntu.

Debian 12 & 13 use ifupdown for network configuration.

The main configuration file is /etc/network/interfaces. This file includes all files in the /etc/network/interfaces.d/ directory.

It is good practice to keep /etc/network/interfaces as shown below and create a separate configuration file for each NIC in the /etc/network/interfaces.d/ directory.

Terminal window
sudo nano /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
#
# The loopback network interface
auto lo
iface lo inet loopback

The name server configuration file is /etc/resolv.conf. It can be as simple as:

nameserver 192.168.1.1

Or it may have more detailed configuration:

/etc/resolv.conf
# Primary DNS server
nameserver 8.8.8.8
# Secondary DNS server
nameserver 8.8.4.4
# Tertiary DNS server (optional)
#nameserver 1.1.1.1
# Search domains (optional)
# If a domain name is provided without a fully qualified domain name (FQDN),
# the system appends these search domains to the provided domain to perform
# DNS lookups.
# For example, if "example.com" is provided in a lookup, and "search
# localdomain" is configured, the system will also try to resolve
# example.com.localdomain".
search localdomain

Stop a NIC:

Terminal window
sudo ifdown enp0s3

Start a NIC:

Terminal window
sudo ifup enp0s3

To restart a NIC, stop it and start it again:

Terminal window
sudo ifdown enp0s3 && sudo ifup enp0s3

Restart networking (restarts all NICs and other networking services):

Terminal window
sudo systemctl restart networking


Our NIC is enp0s3.

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3

Fill as below:

auto enp0s3
iface enp0s3 inet dhcp

Restart networking:

Terminal window
sudo systemctl restart networking

Our NIC is enp0s3.

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3

Fill as below:

auto enp0s3
iface enp0s3 inet static
address 192.168.1.135/24
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
Terminal window
sudo systemctl restart networking

Our NIC is enp0s3.

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3

Fill as below:

auto enp0s3
iface enp0s3 inet static
address 192.168.1.196/24
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
iface enp0s3 inet static
address 10.1.1.1/8
Terminal window
sudo systemctl restart networking

Our NICs are enp0s3 and enp0s8.

First NIC:

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3

Fill as below:

auto enp0s3
iface enp0s3 inet static
address 192.168.1.196/24
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1

Second NIC:

Terminal window
sudo nano /etc/network/interfaces.d/enp0s8

Fill as below:

auto enp0s8
iface enp0s8 inet static
address 10.1.1.1/8
broadcast 10.255.255.255
network 10.0.0.0

Restart networking:

Terminal window
sudo systemctl restart networking


We have 2 separate networks (192.168.1.X and 10.X.X.X). Some hosts from one network need to reach hosts in the other network.

We will install a new host to act as a router between the networks.

The host will have 2 NICs (one in each network), and we’ll enable IP routing on it.

This way, hosts in one network will be able to reach hosts in the other network. This will be possible by defining IP routes on the hosts to use the server with 2 NICs as a router to the other network.

Hosts in the 192.168.1.X network use 192.168.1.1 as the default gateway; hosts in the 10.X.X.X network use 10.1.1.1 as the default gateway.

Our router will have 2 NICs: one with IP 192.168.1.196 and the other with IP 10.1.1.196.

Hosts in the 192.168.1.X network will use 192.168.1.196 to reach the 10.X.X.X network. Hosts in the 10.X.X.X network will use 10.1.1.196 to reach the 192.168.1.X network.

We will configure:

  • The router (192.168.1.196 & 10.1.1.196)
  • The host in the first network (192.168.1.197)
  • The host in the second network (10.1.1.198)

Then we’ll check connectivity between them.

We have 2 NICs (enp0s3 - 192.168.1.X network, and enp0s8 -10.X.X.X network).

Clean the /etc/network/interfaces file:

Terminal window
sudo nano /etc/network/interfaces

Set as below:

source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

Configure NICs:

enp0s3 (192.168.1.196/24):

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3

Fill as below:

auto enp0s3
iface enp0s3 inet static
address 192.168.1.196/24
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1

enp0s8 (10.1.1.196/8):

Terminal window
sudo nano /etc/network/interfaces.d/enp0s8

Fill as below:

auto enp0s8
iface enp0s8 inet static
address 10.1.1.196/8
broadcast 10.255.255.255
network 10.0.0.0

Restart networking (your SSH connection may break; reconnect if needed):

Terminal window
sudo systemctl restart networking

Enable IP Forwarding:

Terminal window
sudo nano /etc/sysctl.conf

Add the following line to the end:

net.ipv4.ip_forward = 1

Activate:

Terminal window
sudo sysctl -p

Set name server (if not already configured):

Terminal window
sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

We have 1 NIC (enp0s3 - 192.168.1.X network).

Clear the /etc/network/interfaces file:

Terminal window
sudo nano /etc/network/interfaces

Set as below:

source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

Configure NIC:

enp0s3 (192.168.1.197/24):

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3
auto enp0s3
iface enp0s3 inet static
address 192.168.1.197/24
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1

Define route:

Create a script file to add the route when the interface comes up:

Terminal window
sudo nano /etc/network/if-up.d/routes

Fill as below:

#!/bin/sh
if [ "$IFACE" = "enp0s3" ]; then
ip route del 10.0.0.0/8 via 192.168.1.196 dev enp0s3
ip route add 10.0.0.0/8 via 192.168.1.196 dev enp0s3
fi

Make the script executable:

Terminal window
sudo chmod 750 /etc/network/if-up.d/routes

Restart networking (your SSH connection may break; reconnect if needed):

Terminal window
sudo systemctl restart networking

Set name server (if not already configured):

Terminal window
sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

We have 1 NIC (enp0s3 - 10.X.X.X network).

Clear the /etc/network/interfaces file:

Terminal window
sudo nano /etc/network/interfaces

Set as below:

source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

Configure NIC:

enp0s3 (10.1.1.198/8):

Terminal window
sudo nano /etc/network/interfaces.d/enp0s3
auto enp0s3
iface enp0s3 inet static
address 10.1.1.198/8
broadcast 10.255.255.255
network 10.0.0.0
gateway 10.1.1.1

Define route:

Create a script file to add the route when the interface comes up:

Terminal window
sudo nano /etc/network/if-up.d/routes

Fill as below:

#!/bin/sh
if [ "$IFACE" = "enp0s3" ]; then
ip route del 192.168.1.0/24 via 10.1.1.196 dev enp0s3
ip route add 192.168.1.0/24 via 10.1.1.196 dev enp0s3
fi

Make the script executable:

Terminal window
sudo chmod 750 /etc/network/if-up.d/routes

Restart networking (your SSH connection may break; reconnect if needed):

Terminal window
sudo systemctl restart networking

Set name servers (if not already configured):

Terminal window
sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

The host in the first network can ping the host in the other network now, and vice versa.

Try on the first host (192.168.1.197)

Terminal window
ping 10.1.1.198

Try on the second host (10.1.1.198)

Terminal window
ping 192.168.1.197

For a host to connect to another host on the other network, routing must be defined on both hosts.



Network Interface Card (NIC) bonding involves using two or more NICs together to achieve redundancy and/or increased throughput.

The most commonly used bonding modes are:

  • Active Backup: Also known as failover mode. In this mode, one interface is active while the other interfaces remain in standby mode. If the active interface fails, one of the standby interfaces takes over. It provides redundancy but does not offer load balancing.

  • Balance-rr (Round-Robin): Packets are transmitted sequentially across the bonded interfaces in a round-robin fashion. This provides load balancing and increased throughput, but does not provide fault tolerance.

  • Balance-xor: Combines the MAC addresses of the interfaces and applies an XOR operation to determine the outgoing interface. It provides load balancing and fault tolerance, but requires switch support for optimal performance.

  • Broadcast: All packets are sent on all interfaces. This mode is typically used when the switch does not support other bonding modes. It provides fault tolerance but not load balancing.

All examples below use 2 NICs (enp0s3 and enp0s8) on the same network (192.168.1.X).

Before configuring bonding of any type, there are some necessary preliminary steps.

To use NIC bonding, the ifenslave package must be installed:

Terminal window
sudo apt update
sudo apt -y install ifenslave

Ensure the bonding kernel module is loaded:

Terminal window
sudo modprobe bonding

Ensure the kernel module loads at startup:

Terminal window
sudo nano /etc/modules

Add to the end of the file (if it doesn’t already exist):

bonding

Clear the /etc/network/interfaces file:

Terminal window
sudo nano /etc/network/interfaces

Set as below:

source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

Set Name Servers (if not already)

Terminal window
sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

Configure the bond:

Terminal window
sudo nano /etc/network/interfaces.d/bond

Fill as below:

auto enp0s3
iface enp0s3 inet manual
bond-master bond0
auto enp0s8
iface enp0s8 inet manual
bond-master bond0
auto bond0
iface bond0 inet static
address 192.168.1.196/24
netmask 255.255.255.0
network 192.168.1.0
gateway 192.168.1.1
slaves enp0s3 enp0s8
bond-mode active-backup
bond-primary enp0s3

Restart Networking:

Terminal window
sudo systemctl restart networking

Check the status of the bond

Terminal window
sudo cat /proc/net/bonding/bond0

Configure the bond

Terminal window
sudo nano /etc/network/interfaces.d/bond
auto enp0s3
iface enp0s3 inet manual
bond-master bond0
auto enp0s8
iface enp0s8 inet manual
bond-master bond0
auto bond0
iface bond0 inet static
address 192.168.1.196/24
netmask 255.255.255.0
network 192.168.1.0
gateway 192.168.1.1
bond-slaves enp0s3 enp0s8
bond-mode balance-rr

Restart networking:

Terminal window
sudo systemctl restart networking

Check the status of the bond:

Terminal window
sudo cat /proc/net/bonding/bond0

Configure the bond

Terminal window
sudo nano /etc/network/interfaces.d/bond
auto enp0s3
iface enp0s3 inet manual
bond-master bond0
auto enp0s8
iface enp0s8 inet manual
bond-master bond0
auto bond0
iface bond0 inet static
address 192.168.1.196/24
netmask 255.255.255.0
network 192.168.1.0
gateway 192.168.1.1
bond-slaves enp0s3 enp0s8
bond-mode balance-xor

Restart networking:

Terminal window
sudo systemctl restart networking

Check the status of the bond:

Terminal window
sudo cat /proc/net/bonding/bond0

Configure the bond:

Terminal window
sudo nano /etc/network/interfaces.d/bond
auto enp0s3
iface enp0s3 inet manual
bond-master bond0
auto enp0s8
iface enp0s8 inet manual
bond-master bond0
auto bond0
iface bond0 inet static
address 192.168.1.196/24
netmask 255.255.255.0
network 192.168.1.0
gateway 192.168.1.1
bond-slaves enp0s3 enp0s8
bond-mode broadcast

Restart networking:

Terminal window
sudo systemctl restart networking

Check the status of the bond:

Terminal window
sudo cat /proc/net/bonding/bond0