Take Control: Setting a Static IP and Custom DNS on Debian 12

Tired of your Debian 12 system grabbing a new IP address every time you reboot? Want the reliability of a fixed IP for your server or development machine? Or perhaps you need to use specific DNS servers for privacy or network configuration reasons?

Fear not! While NetworkManager often handles network configurations seamlessly, manually setting a static IP address and custom DNS servers in Debian 12 is a straightforward process. This article will guide you through the steps, giving you greater control over your network settings.

Understanding the Configuration Files

In Debian 12, network configuration is primarily handled by the networkd service. Its configuration files reside in the /etc/systemd/network/ directory. You’ll likely need to create or modify a .network file within this directory to define your static IP and DNS settings.

Step 1: Identify Your Network Interface

Before diving into configuration, you need to know the name of your network interface. Open your terminal and use the following command:

# ip a

Look for an active Ethernet or Wi-Fi interface. Common names include eth0, enp0s3, wlan0, or wlp2s0. Make a note of the interface name you want to configure.

Step 2: Create or Modify the Network Configuration File

Navigate to the network configuration directory:

# cd /etc/systemd/network/

You might find an existing .network file (e.g., 10-dhcp.network). If so, you can modify it. If not, create a new file. It’s a good practice to name your file based on your interface name (e.g., eth0.network or enp0s3.network). You’ll need sudo privileges to create or edit files in this directory:

# sudo nano your_interface_name.network

Replace your_interface_name with the actual name of your network interface.

Step 3: Configure the Static IP Address

Inside the .network file, add the following content, adjusting the values to match your desired static IP configuration:

Ini, TOML

[Match]
Name=your_interface_name

[Network]
Address=192.168.1.10/24
Gateway=192.168.1.1
DNS=8.8.8.8 8.8.4.4

Let’s break down each line:

  • [Match]: This section specifies which network interface this configuration applies to.
    • Name=your_interface_name: Replace your_interface_name with the actual name of your network interface (e.g., eth0).
  • [Network]: This section defines the network settings.
    • Address=192.168.1.10/24: This sets the static IP address.
      • 192.168.1.10: Replace this with your desired static IP address.
      • /24: This specifies the subnet mask (in CIDR notation). /24 corresponds to a subnet mask of 255.255.255.0. Adjust this based on your network setup.
    • Gateway=192.168.1.1: This sets the default gateway (your router’s IP address). Replace 192.168.1.1 with the correct gateway for your network.
    • DNS=8.8.8.8 8.8.4.4: This specifies the DNS servers to use. Here, we’ve set Google’s public DNS servers. You can replace these with your preferred DNS servers, separated by spaces. You can add multiple DNS servers.

Step 4: Apply the New Configuration

After saving and closing the .network file, you need to apply the new configuration. First, restart the systemd-networkd service:

# sudo systemctl restart systemd-networkd

Then, you might also need to restart the systemd-resolved service, which handles DNS resolution:

# sudo systemctl restart systemd-resolved

Step 5: Verify Your Configuration

To confirm that your static IP and DNS settings have been applied correctly, use the following commands:

  • Check IP Address: Baship a Look for your configured network interface and verify that the IP address listed matches the one you set in the .network file.
  • Check DNS Servers: Bashresolvectl status Examine the output for your network interface. Under the “Current DNS Server” and “DNS Servers” sections, you should see the DNS servers you configured.
  • Test Connectivity:Try pinging an external website to ensure your network connection and DNS resolution are working correctly: Bashping google.com

Troubleshooting Tips

  • Typos: Double-check your .network file for any typos in the interface name, IP address, gateway, or DNS server entries.
  • File Permissions: Ensure the .network file has the correct permissions (usually owned by root).
  • Conflicting Configurations: If you had previous network configurations, ensure they are removed or disabled to avoid conflicts.
  • NetworkManager Interference: If you’re still having issues, NetworkManager might be interfering. You can try disabling it and enabling systemd-networkd as the sole network management tool (this is generally recommended for server environments but be cautious on desktop systems).

Conclusion

Manually configuring a static IP address and custom DNS servers in Debian 12 provides you with greater control and predictability over your network setup. By understanding the systemd-networkd configuration files, you can tailor your network settings to your specific needs. Remember to verify your configuration after making changes and troubleshoot any issues methodically. Happy networking!

Leave a Reply

Your email address will not be published. Required fields are marked *