Network Configuration on Debian

As a Linux user or system administrator, configuring networking on your system is a critical task, especially for server environments. Unlike desktop systems, which often rely on dynamic network configurations, servers frequently require specific, static settings to suit your network's needs. Dynamic configurations may change with each server reboot but are not always suitable for server scenarios. This is particularly true for servers that need to be managed remotely or host applications and services requiring consistent network access. In this guide, we'll walk you through the process of establishing a basic network setup on a Debian-based Linux operating system, ensuring your server maintains a reliable and stable network connection. The basic setup includes setting a static IP, gateway, DNS, and hostname.

We have tested this on Debian 11 and Debian 12.

View current network configuration

To view current network configurations, run the following command in Terminal. It will show the output for each interface in a separate section.

$ ip a

View network configuration

You can also run ifconfig command to view the IP address.

$ ifconfig

Show network configuration with ifconfig command

Run the below command in Terminal to find DNS server IP:

$ cat /etc/resolv.conf

Change network configuration

Basic network configuration includes setting a static or dynamic IP address, adding a gateway, DNS server information. There are different ways to configure the network on Debian OS.

Method 1: Use ifconfig and route command

In this method, we will see how to configure network settings. However, remember, these settings will not be permanent. Once you reboot your system, the settings will be removed.

1. Assign an IP address to the interface

We will use ifconfig to assign an IP address to our network interface. Below is the syntax of the command:

$ sudo ifconfig <interface> <IP_address> netmask <subnetmask> up

In the following example, the command assigns the IP address 192.168.72.165 to the network interface eth0. The network mask is 24 (255.255.255.0) bits.

$ sudo ifconfig eth0 192.168.72.165 netmask 255.255.255.0 up

Assign IP address

2. Set the Default Gateway

The default gateway is the address used to communicate with the outside network. To configure the default gateway, use the following command syntax:

$ sudo route add default gw <IP_address> <interface>

In the following example, I am using 192.68.72.2 as my default gateway address.

$ sudo route add default gw 192.168.72.2 eth0

Set default gateway

3. Set Your DNS server

DNS server resolves a domain name to an IP address so the browser can load Internet resources. To configure the DNS name server address, use the following command syntax:

$ echo “nameserver <IP_address>” > /etc/resolv.conf

In the following example, I am setting Google’s public DNS IP address as my nameservers address that is 8.8.8.8.

$ echo “nameserver 8.8.8.8” > /etc/resolv.conf

Set DNS servers

Once done, you can test your configuration by running the ifconfig command as follows: View changed network config

Remove IP address from a network interface

To remove an IP address from a network interface, run the following command in Terminal:

$ ip address del <IP_address> dev <interface>

Method 2: Change network settings by using the interfaces file

This method will configure permanent network settings that your system will remember even after a reboot. For that, we will have to edit /etc/network/interfaces file using any text editor. Run the following command in the terminal to do so:

$ sudo nano /etc/network/interfaces

Then add the following lines in it:

auto eth0

iface eth0 inet static

address 192.168.72.165

netmask 255.255.255.0

gateway 192.168.72.2

Now press Ctrl+O and then Ctrl+X to save and exit the file.

Add a static IP address

Please note that the address, netmask, and gateway line must start with leading whitespace! In case, you want to assign the address, use the following lines dynamically:

auto eth0
iface eth0 inet dhcp

Defining the (DNS) Nameservers

We will need to edit the /etc/resolv to add DNS server information.conf file. Run the following command to do so:

$ nano /etc/resolv.conf

I am adding here two Nameservers. One is Google's public DNS server address and the other is my router’s IP address.

nameserver 8.8.8.8
nameserver 192.168.72.2

Now press Ctrl+O and then Ctrl+X to save and exit the file.

Set nameserver in resolv.conf file

Once done, you can verify the IP address using ip a or ifconfig command.

Check changed config with ip command

Method 3: Change network configuration through Debian GUI

In this method, we will use the graphical way for configuring the basic network settings.

To do so, hit the windows button on your keyboard, then in the search bar type settings. From the results that appear, open the Settings. Then on the left sidebar, click on the Network tab. After that, click on the gear icon of the interface that you want to configure.

Debian network manager

Go to IPv4 tab. Choose Manual and enter the IP address, netmask, gateway, and DNS.

IPv4 tab

In case you want to dynamically assign the IP address, choose the Automatic (DHCP) option and enter the DNS information.

DHCP

Once done, click on Apply to save the changes.

Setting up Hostname

Like the IP address, a unique hostname is also used to recognize a system on a network. To find the current hostname of your system, run the below command in Terminal:

$ hostname

Set hostname

To change the system's hostname, you can run the below command. But once you reboot your system, your original hostname will be restored.

$ hostname host_name

I am changing here my hostname from Debian to Debian10.

Set a new host name

To permanently change the host name, you will need to edit hostname file located at /etc/hostname. Enter the below command to do so:

$ sudo nano /etc/hostname

Edit hostname file

This file contains only the hostname of the file. Change the old name to your desired name, and then press Ctrl+O and Ctrl+X to save and exit.

Some other useful commands you might require while setting up a network in a Debian OS:

Ping

It can test connectivity between two systems on aLAN or WAN. To test connectivity to a device, type ping followed by the IP or hostname of that device:

$ ping <IP or hostname>

Arp:

Arp is used to translate IP addresses into Ethernet addresses. To print arp table, type:

$ arp –a

Route

It is used to display the routing table of a Linux system.

$ route

Host

It translates host names to IP addresses and vice versa.

To find IP against a specified domain:

$ host domain_name

To find a domain name against the specified IP address.

$ host IP_address

Enable and disable the interface

To enable up the interface, use:

$ ifup <interface>

To bring down the interface, use:

$ ifdown <interface>

That is all there is to it! In this article, we have explained how to setup a basic network in Debian OS. We have discussed different methods including the graphical and command-line based. You can choose the one that you find more easy and convenient.