Mastering static IP configuration on Debian
Configuring a static IP address is a fundamental task for any server administrator. A predictable IP address is essential for services, network shares, and remote access. While Debian's installer typically defaults to DHCP, setting a static IP is straightforward.
This guide covers two primary methods: the modern systemd-networkd approach (default in many minimal installs and other distributions) and the traditional "legacy" method using the /etc/network/interfaces file.
Before you begin: Always back up
Before editing any system configuration, create a backup. A simple typo can lock you out of your server, especially if you're working remotely.
Identify which files you're about to change. If you plan to use the legacy method, back up the interfaces file:
# Backup for the legacy method
xinit@localhost:~$ sudo cp /etc/network/interfaces /etc/network/interfaces.bak
If you're using or migrating to systemd-networkd, back up its configuration directory (even if it's empty):
# Backup for the modern method
xinit@localhost:~$ sudo cp -a /etc/systemd/network /etc/systemd/network.bak
The modern method: systemd-networkd
systemd-networkd is a powerful and flexible networking daemon that is part of the systemd init system. It's the recommended approach for modern Debian systems.
Step 1: Identify your network interface
First, find the name of the network interface you want to configure.
xinit@localhost:~$ ip a
You'll see an output like this. Look for your main ethernet interface (e.g., ens18, eth0, enp0s3).
1: lo: <LOOPBACK,UP,LOWER_UP> ...
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ...
link/ether bc:24:11:d6:61:90 brd ff:ff:ff:ff:ff:ff
inet 10.0.255.112/16 ...
In this example, our interface is ens18.
Step 2: Create a .network configuration file
systemd-networkd configurations live in /etc/systemd/network/. We'll create a simple file to define our static IP. You can name it anything ending in .network, but a numbered prefix helps control the order of application.
Create the file:
xinit@localhost:~$ sudo nano /etc/systemd/network/10-static.network
Add the following content, replacing the interface name (ens18) and IP details with your own:
[Match]
# Match the interface name from 'ip a'
Name=ens18
[Network]
# Define the static IP and subnet
Address=10.0.255.112/16
# Define the gateway
Gateway=10.0.0.1
# Define DNS servers (space-separated)
DNS=10.0.0.1 8.8.8.8 8.8.4.4
- Address: The IP address followed by the subnet mask in CIDR notation (/16 is equivalent to 255.255.0.0).
- Gateway: Your network's default gateway.
- DNS: One or more DNS servers.
Step 3: Enable and apply settings
First, ensure the legacy networking service is disabled and systemd-networkd is enabled to start on boot:
xinit@localhost:~$ sudo systemctl disable networking.service
xinit@localhost:~$ sudo systemctl enable systemd-networkd.service
Now, restart the systemd-networkd daemon to apply your new configuration:
xinit@localhost:~$ sudo systemctl restart systemd-networkd.service
You can verify the new IP address using ip a.
The "legacy" method: /etc/network/interfaces
This is the traditional method used by Debian for many years, relying on the ifupdown package. It's still perfectly functional, especially if you have an older or existing setup.
Step 1: Identify your interface
Just as in the modern method, use ip a to find your interface name (e.g., ens18).
Step 2: Edit /etc/network/interfaces
Open the configuration file with a text editor:
xinit@localhost:~$ sudo nano /etc/network/interfaces
By default, it's likely configured for DHCP:
# The primary network interface
allow-hotplug ens18
iface ens18 inet dhcp
You need to change this. Comment out or delete the DHCP lines for your interface and add the static configuration:
# The primary network interface
auto ens18
allow-hotplug ens18
iface ens18 inet static
address 10.0.255.112
netmask 255.255.0.0
gateway 10.0.0.1
# For DNS, you must add the nameservers here
dns-nameservers 10.0.0.1 8.8.8.8 8.8.4.4
- auto ens18: Brings the interface up automatically at boot.
- iface ens18 inet static: Defines the interface as statically configured.
- address, netmask, gateway: Your core network details.
- dns-nameservers: This line is crucial. If you don't set DNS here, your server won't be able to resolve domain names. This requires the resolvconf package to be installed (sudo apt install resolvconf).
Step 3: Apply the configuration
To apply the changes, you can restart the networking service:
xinit@localhost:~$ sudo systemctl restart networking.service
Alternatively, you can try to bring the interface down and up again (this can be risky over SSH):
xinit@localhost:~$ sudo ifdown ens18 && sudo ifup ens18
Conclusion
While both methods achieve the same goal, systemd-networkd is the recommended path for new Debian installations due to its flexibility and integration with the systemd ecosystem. The "legacy" /etc/network/interfaces method remains a simple and reliable option for existing systems or if you prefer its straightforward syntax.