How to Configure Interface and Network Settings in Various Linux Distributions
The way of interface configuration and network settings in Linux may be a little different. So, let's spare some time and read the following lines about the basic network settings in most Linux distributions. On most occasions, after creating the instance, you require not to set particular settings to connect it to the internet. Be noted that when you generate the instance, the operating system employs the DHCP and the settings put by the cloud-init to gain the correct and required information. However, sometimes you'd like to have multiple interfaces, or put the IP on the interfaces manually, or make some changes in accordance with the desired environment.
The objectives of this article are:
- Setting the DNS,
- Basic commands of the network checking and setting via IPROUTE2,
- Receiving the network specifications via DHCP again,
- Configuring the interfaces in various Linux distributions manually.
Hint: A package named cloud-init will be installed on the operating system while preparing the images with ArvanCloud or Cloud Ready the operating systems. The package duty is to provide a collection of metadata for instances. It can inject your SSH Key, the instance name, and other data into the instance. However, be noted that this package can set the network standard configurations as well as many other things when you are configuring some of your operating system components. If you are supposed to set the instance interface or the instance network manually and make the settings persistent after rebooting, employ the following command to config the cloud-init:
And put the file content in front of the following command:
If your machine is supposed to work as a DNS client, then you should arrange a server to respond to the DNS questions. To set a server for this purpose, you should specify the etc/resolv.conf/ in all distributions. In some newer distributions, the file may itself be a link to another one, the content of which may be increased or decreased from the management ancillary tools. For example, it may be indirectly managed by the system-resolved, initscripts, or NetworkManager in Debian and Ubuntu new versions. In such a case, you will lose the file's manual changes after rebooting the system.
Hint: Be noted that even if the preceding points do not exist in distribution, your operating system may receive the DNS data from the DHCP and cause the previous one to overwrite.
One of the easy solutions for most distributions can be to replace the Symbolic Link with a new file containing the desired content and finally set it on read-only mode. Apply the following command to do so:
In this step, fill the file content with the nameservers. For example, see the following figure:
Apply the following command to change the read-only mode for future changes:
Ubuntu / Debian
Besides the mentioned solution, you can also employ the resolvcont package to do the settings in Ubuntu and Debian. Use the following command to install it if required:
If you encounter the name resolution error during installation, you can temporarily edit the file to a correct nameserver and then install the package.
Edit the following file to set your nameservers:
Then, use the following commands to activate it:
Now, check the service status to ensure it is activated:
In the end, save the file.
Centos / RHEL
In RHEL based distributions, when you reboot or restart the network service, the NetworkManager and the DHCP may overwrite the resolv.conf file content by implementing the following scripts:
The first solution can troubleshoot the problem. However, if you want the NetworkManager not to overwrite this file, complete the following steps:
Fill the file content with the following values:
Then, reload the NetworkManager:
Notice that this file or another, with similar content, might be in the indicated directory through the cloud-init. Therefore, first, check it out and then create the file.
In the end, edit the resolv.conf file and use the chattr command (which we mentioned earlier) to set it on the read-only mode.
The Basic Commands of Network Checking and Setting by IPROUTE2
In various Linux distributions, you can check the interface settings and even use the IPROUTE2 default package to change them. However, the changes are not persistent; it means, after rebooting the system, they will be removed.
We are going to explain some useful commands in the following lines:
- Reviewing the general status of the interfaces
- Deleting and adding to the interface
Replace the eth0 with the interface name and then the IP and the subnet with your data. Be noted that these data do not remove the previous IP; indeed, the system will add it to the interface as the second IP.
Apply the following command to remove it:
- Displaying the status of the interfaces
- Activating or deactivating the links
- Displaying the operating system default route table
- Increasing or decreasing a route
For example, we apply the eth0 interface to the 10.10.20.0/24 network. On the other hand, the next-hop or the interface to which the traffic should be delivered has the 192.168.50.100 address. To remove it, you should only insert the range. If there are several routes registered for a range, you can add more details (for example, the interface name) to delete only the same one.
Getting Data from DHCP at Runtime
Sometimes, when running the operating system, it is required to get the IP, Gateway, and ... data from the DHCP. The easiest way to do so is to employ the dhclient tool. Before using this tool, make sure of setting the intended interface on Active mode - you have learned how to check it beforehand. Apply the introduced tool in the following way:
Notice that you should replace the ethX with the interface name.
How to Set the Instance Interfaces on Various Linux Distributions
In every distribution, the configuration of the interface files may be found in a different position on the operating system. In this article, we explain the interface settings of RHEL and Debian based distributions. We also review the Netplan settings in Ubuntu 18.04. Be noted that they have made some changes in this version settings, and you should write the settings in the YAML format to add the intended config file.
Hint: If you aim to change the general interface config from the DHCP mode to the Static or Manual mode, you have to, first, write down the information received from the DHCP to reuse it.
RHEL / Centos
Edit the following files for the instances with RHEL based operating systems:
The first file has some global settings which have the following values by default:
As you see in the figure, when the cloud-init created the file instance, it would be useless for the network to deactivate the cloud-init settings - they will be removed whenever rebooting the system. Make sure that the cloud-init is deactivated for this part if doing the settings manually.
Make sure that the file includes the following values:
- The NETWORKING phrase determines whether the network is active or inactive; it may hold Yes or No values,
- The next two phrases are for deactivating the IPV6,
- The NOZEROCONF phrase is for deactivating the technique that creates a 169.254.0.0/255.255.0.0 route in the system routing table.
The next file is for setting the interface on the instance:
Be noted that the end of every file is the interface name. For example, if the second interface has been added to the private network instance, you can create a separate file and put its settings there.
This file, for example, by default, has the above values on an instance. Each phrase controls some different features; for example:
- The BOOTPRO phrase can have the None, Bootp, or DHCP value.
- The DEVICE phrase determines the interface that should be configured.
- The HWADDR phrase defines the mackaddress of the interface that should be set.
- The MTU phrase has a numeric value; 1500 is appropriate for public interfaces and 1450 for private interfaces.
- The ONBOOT phrase causes the interface to activate or not when booting.
- The TYPE phrase defines the interface type.
- The USERCTL phrase determines whether not routed users are allowed to change the interface or not.
- The GATEWAY determines that there is a gateway on this interface, and its values are equal to the default gateway IP.
- The IPADDR phrase defines the interface IP address.
- The NETMASK phrase is for determining the network subnetmask.
There is no need to have all the above phrases in a config file, but you can, by default, consider them for the interface that you are going to configure manually. Read more values from the relevant distribution documents.
For example, suppose that your interface has two private and public interfaces. In this case, you want the public interface to receive the specifications from the DHCP and the ArvanCloud infrastructure. However, the private interface is configured manually.
Hint: Notice that you can first create the interface file if required. And, if you do not know the interface name, use a command such as the IP Link to find it before creating the file according to name.
The file content
The file content
Tip: If the instance does not have a FLOAT IP and you are using the public interface for the internet, there is no need to config the GATEWAY.
In the end, activate the interface with new settings after defining the config by the following command:
Debian / Ubuntu
To config the static interface of the Debian based distributions, it is enough to edit the following file:
By default, this file may include no specific content on the instance, and it may read the interface config files from the following directory separately:
Do this step to set it manually:
Equal the file content to the following command to set it on the DHCP mode:
Or the Static mode.
For example, there is an instance with two public and private interfaces that the private network config is on Static mode, and the content of the settings is as follows:
Finally, after doing the settings, reboot the system or use ifdown and ifup to activate the interface with the config.
Ubuntu 18.04 / Netplan Configuration
From its 18.04 version, Ubuntu began using Netplan to network card configuration and settings. In the Netplan, the settings are in the YAML format. When writing the settings in this file, the Netplan employs the Render to generate the appropriate settings based on the fact that what system the operating system uses to network settings.
The Netplan config files are in etc/netplan/*.yaml directory. As mentioned earlier, there are default settings for the Ubuntu instances provided on ArvanCloud. However, apply the following way to do the settings manually:
Hint: Notice that you should first set the cloud-init to prevent the network settings from changing it. This way, the settings also remain persistent after rebooting the system.
Hint: The YAML files have a cascade structure, so space determines the relationship between parameters. Hence, be noted that you should not use Tab to separate parameters in writing this file because you will encounter an error.
Go to the etc/netplan/ directory and use an editor to open the file for personal setting, or delete the previous file and create a new one:
The file structure is something like the following:
The above config shows a simple sample template of the network card settings; we are going to explain some of its items and parameters. Notice that the DEVICE_NAME is the name of the interface that should be set. Apply the IP link command to find the name.
The dhcp4 parameter causes the DHCP 4th version to be active or inactive for the interface.
The Address parameter is for setting the IP on the interface; it can have one or more IPs.
The Gateway parameter is for setting a gateway on the interface; it is necessary for the instance public interfaces.
The Render parameter determines your backend that is systemd-networkd on the Ubuntu server.
Keep on reading to get familiar with how to set an instance connected to a public or private network:
The MTU parameter is for setting the MTU link that must set with the macaddress parameter. The macaddress parameter is a device that is supposed to be set, and if the macaddress be wrong, the system will not apply the settings to the device. Indeed, a parameter for determining the exact interface is setting.
Use the following command to apply the settings:
If there is an error in the YAML syntax structure, you will see a comment because of the d- switch. However, if the file is correct, the system applies the settings without error. In the end, you can restart the Network.