If you are a regular Linux user or a system administrator, you might require to set up networking on your system. Unlike the desktop systems where you can use dynamic configurations, you will have to do specific configurations on servers depending upon your network environment. Dynamic configurations keep changing upon the server reboot so in some cases it becomes necessary to have static configurations for instance when a server needs to be remotely administered, or when a server is running an application or service that needs constant access. So, In this article, we will explain how you can set up a basic network in a Debian-based Linux OS. The basic setup includes setting a static IP, gateway, DNS, and hostname.
We have used Debian 11 OS for describing the procedure mentioned in this article.
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
You can also run ifconfig command to view the IP address.
$ ifconfig
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
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
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
Once done, you can test your configuration by running the ifconfig command as follows:
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
In this method, we 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 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.
Please note that the address, netmask and gateway line must start with leading whitespace! In case, you want to dynamically assign the address, use the following lines:
auto eth0 iface eth0 inet dhcp
Defining the (DNS) Nameservers
To add DNS server information, we will need to edit the /etc/resolv.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.
Once done, you can verify the IP address using ip a or ifconfig 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.
Go to IPv4 tab. Choose Manual and enter the IP address, netmask, gateway, and DNS.
In case you want to dynamically assign the IP address, choose the Automatic (DHCP) option and enter the DNS information.
Once done, click on Apply to save the changes.
Setting up Hostname
Just 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
To change the hostname of the system, 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.
To permanently change the host name, you perform will need to edit hostname file located at /etc/hostname. Enter the below command to do so:
$ sudo nano /etc/hostname
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 be used to test connectivity between two systems on aLAN or WAN. To test connectivity to a device, type ping followed by IP or host name 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.