How to configure the network on Linux

If you are a regular Linux user or a system administrator, you may require network setup on your system. Unlike desktop systems where you can use dynamic configurations, you will have to perform specific configurations on the servers depending on your network environment.

Dynamic configurations are constantly changing when the server is restarted, so in some cases a static configuration is required, such as when the server needs to be administered remotely or when the server is running an application application or service that requires constant access. So, in this article, I will explain how you can set up basic networking in a Ubuntu-based Linux operating system. Basic setup includes setting static IP, Gateway, DNS and hostname.

We have used Ubuntu 20 operating system to describe the process mentioned in this article.

1. View the current network configuration.

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

$ ip a


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

$ ifconfig

 

2. Change network

configuration Basic network configuration includes setting static or dynamic IP address, adding Gateway, DNS server information. There are different ways to configure the network on Ubuntu Operating System.

3. Method 1: Using the ifconfig and route commands

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

3.1. Assign an IP address to the network interface.

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

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

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

$ ifconfig eth0 192.168.225.66 netmask 255.255.255.0 up

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

3. 2. Set default Gateway.

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

$ route add default gw <IP_address> <interface>

In the following example, I am using 192.68.225.1 as my default Gateway address.

$ route add default gw 192.168.225.1 eth0

3.3. Set your DNS server.

DNS servers resolve domain names to IP addresses so that browsers can load Internet resources. To configure the DNS nameserver address, use the following command syntax:

$ echo "nameserver <IP_address>" > /etc/resolv.conf

In the following example, I will set the Google DNS IP address as my name server address to 8.8.8.8.

$ echo "nameserver 8.8.8.8" > /etc/resolv.conf
3.4. Remove the IP address from the network interface

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

$ ip address del <IP_address> dev <interface>

4. Method 2: Change the network settings using the network interface file.

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

$ nano /etc/network/interfaces

Then add the following lines to it:

auto eth0

iface eth0 inet static

address 192.168.225.10

netmask 255.255.255.0

gateway 192.168.225.1

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

Please note that the address, network mask, and Gateway line must begin with a leading space

5. Specifying DNS Servers

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

$ nano /etc/resolv.conf

I will add here two DNS Servers. One is Google’s DNS server address and the other is my router’s IP address.

nameserver 8.8.8.8
nameserver 8.8.4.4

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

6. Set the Hostname.

Just like an IP address, a unique hostname is also used to identify a system on the network. To find your system’s current hostname, run the command below in Terminal:

$ hostname

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

$ hostname host_name

Here I am changing my hostname from Ubuntu to Ubuntu20

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

$ nano /etc/hostname


This file contains only the hostname, change the old name to the name you want, then press Ctrl + O and Ctrl + X to save and exit.

                  Good Luck!