Skip to main content

Operating System Network

In most cases, you don't need any special settings after creating your instance for it to be connected to the internet or a network, because after the instance is created and boots, the operating system has the correct, necessary information through DHCP and the settings applied by cloud-init. But sometimes you may want several interfaces, set IPs on interfaces manually, or make changes depending on your environment. This guide covers the operating system's network settings.

Interface and Network Settings in Linux Distributions​

How interfaces and network settings are configured can differ slightly between Linux distributions. This article tries to explain the basic network settings in most Linux distributions.

When images are prepared at Arvancloud, or operating systems are made cloud-ready, a package named cloud-init is installed on the operating system, which is generally responsible for providing a set of metadata to the instance. This package can inject your SSH key, the instance name, and much other data into the instance at boot. What you need to watch out for when manually configuring some components of your operating system is that this package can also set standard network configurations, among many other things. If you're going to configure the instance's interfaces or network settings manually and want these settings to persist after a reboot, be sure to create the 99-custom-networking.cfg file with the following commands and configure cloud-init with it:

sudo nano /etc/cloud/cloud.cfg.d/99-custom-networking.cfg

Set the file's content to the following:

network: {config: disabled}

DNS Settings​

If your instance is going to be a DNS client, DNS queries need to be sent to a server. In all distributions, the /etc/resolv.conf file is used to set which server is queried. In some newer distributions, this file may itself be a link to another file, and its content may be changed by other network management tools. For example, in new versions of Debian and Ubuntu, this file may be managed indirectly by systemd-resolved, or sometimes by initscripts or NetworkManager. In such cases, manual changes to the file don't persist after a reboot.

Note: Even if the above don't exist in a distribution, your operating system may receive DNS information from DHCP and overwrite the previous setting. A simple solution for most distributions is to replace the symbolic link with a new file with the content you want, and finally make that file read-only. To do this, you can use the following command:

sudo rm -f /etc/resolv.conf
sudo nano /etc/resolv.conf

At this step, fill the file with the nameservers you want, for example:

nameserver 8.8.8.8
nameserver 8.8.4.4

Then make the file read-only with the following command:

sudo chattr +i /etc/resolv.conf

In the future, to apply changes, be sure to first make the file writable again with the following command:

sudo chattr -i /etc/resolv.conf

Ubuntu and Debian​

In Ubuntu and Debian, besides the solution described above, you can also use the resolvconf package. If this package isn't installed on your operating system, you can install it with the following commands:

sudo apt update
sudo apt install resolvconf

If you get a name resolution error during installation, you can temporarily edit the file to point to a working nameserver and then install the package.

To set your nameservers, edit the following file:

sudo nano /etc/resolvconf/resolv.conf.d/head

And add these lines to it:

nameserver 8.8.8.8
nameserver 8.8.4.4

Save the file and then restart the service with the following commands:

sudo systemctl restart resolvconf.service
sudo systemctl restart systemd-resolved.service

Now you can check the service's status to make sure it's active:

sudo systemctl status resolvconf.service

After this, you should see the changes in the /etc/resolv.conf file.

CentOS and RHEL​

In Red Hat–based distributions, each time you reboot or restart the network service, the content of resolv.conf may be overwritten by NetworkManager and DHCP when the following scripts run.

/etc/sysconfig/network-scripts/ifup-post
/etc/sysconfig/network-scripts/ifdown-post

The first solution can solve your problem, but if you want NetworkManager not to overwrite this file, you can do the following:

sudo nano /etc/NetworkManager/conf.d/90-dns-none.conf

Fill the file with the following values:

[main]
dns=none

Then reload NetworkManager:

sudo systemctl reload NetworkManager

Note that cloud-init may already have created this file, or a file with similar content, in that directory, so to be sure, check for it first and then create the file. Finally, you can edit resolv.conf and, to be extra sure, make it read-only with the chattr command mentioned above.

Checking and Configuring the Network with iproute2​

In various Linux distributions, you can use the default iproute2 package to check interface settings and even change them, but the changes aren't permanent and are erased after every reboot.

1. Checking the Overall Status of Interfaces​

ip addr show

2. Adding and Removing an Interface IP​

sudo ip addr add 192.168.50.5/24 dev eth0

Replace eth0 with the name of your interface, and the IP and subnet with your own information. Note that this command doesn't remove the previous IP information; the IP is added to the interface as a second IP.

To remove it, you can use the following command:

sudo ip addr del 192.168.50.5/24 dev eth0
sudo ip addr flush dev eth0

3. Showing Interface Status​

ip link show
ip link set eth0 down
ip link set eth0 up

5. Showing the Operating System's Default Route Table​

ip route show

6. Adding or Removing a Route​

For example, we use the eth0 interface for the 10.10.20.0/24 network, and the next hop, the interface the traffic is delivered to, has the address 192.168.50.100. To delete it, simply enter the range. If several routes are registered for a range, you can delete only that one by giving more details, such as the interface name.

sudo ip route add 10.10.20.0/24 via 192.168.50.100 dev eth0
sudo ip route del 10.10.20.0/24
sudo ip route del 10.10.20.0/24 dev eth0

Getting Information from DHCP at Runtime​

Sometimes, while the operating system is running, we need to get IP, gateway, and other information through DHCP. The simplest way is to use the dhclient tool. Before using it, we need to make sure the interface we want is active. We covered how to check this earlier. We can use the tool as follows:

sudo ip link set dev ethX up
sudo ip addr flush dev ethX
sudo ip route flush dev ethX
sudo dhclient ethX -v

Note that you need to replace ethX with the name of your interface.

Configuring Instance Interfaces in Linux​

In each distribution, interface configuration files may be in a different location in the operating system.

If you're going to change the public interface's configuration from DHCP to static or manual, be sure to first save the information received from DHCP somewhere for reuse.

RHEL/CentOS​

For instances with a Red Hat–based operating system, you can edit these files:

  • Global settings
/etc/sysconfig/network
  • Settings for each interface
/etc/sysconfig/network-scripts/ifcfg-[interfacename]
  • Route table and routes for each interface
/etc/sysconfig/network-scripts/route-[interface-name]

The first file has some global settings. After the instance's cloud-init creates these files, changing them is useless without disabling cloud-init's network settings, and they are erased after a reboot. If you're going to configure the settings manually, make sure cloud-init is disabled for this part.

Make sure the following values exist in this file.

NETWORKING=yes
NETWORKING_IPV6=no
IPV6INIT=no
NOZEROCONF=yes

NETWORKING specifies whether networking is enabled and can have the values Yes or No. The next two entries disable IPv6, and NOZEROCONF disables a technique that automatically creates a 169.254.0.0/255.255.0.0 route in the system's routes.

The next file is for configuring each interface on the instance.

/etc/sysconfig/network-scripts/ifcfg-[interfacename]

In this file, the interface name is at the end of each file name. For example, if a second interface has been added to the instance for the private network, you can create a separate file for it and put its settings in it.

On an instance, this file has default values such as BOOTPROTO, DEVICE, HWADDR, MTU, ONBOOT, TYPE, and USERCTL. Each entry controls something different. For example:

  • BOOTPROTO can contain the values dhcp, bootp, or none.
  • DEVICE specifies the interface to be configured.
  • HWADDR specifies the MAC address of the interface to be configured.
  • MTU has a numeric value; 1500 is appropriate for public interfaces and 1450 for private network interfaces.
  • ONBOOT determines whether the interface is activated at boot.
  • TYPE specifies the interface type.
  • USERCTL specifies whether non-root users are allowed to change this interface.
  • GATEWAY specifies that this interface has a gateway, and its value is the default gateway IP.
  • IPADDR specifies the interface's IP address.
  • NETMASK specifies the network's subnet mask.

The configuration file doesn't necessarily need all the entries above, but by default you can consider them for an interface that will be configured manually. You can find more values in the documentation for your distribution.

For example, suppose our instance has a private and a public interface, and we want the public interface to receive its settings from DHCP and the Arvancloud infrastructure, while the private interface is configured manually.

Note: If needed, first create the interface's file, and if you don't know an interface's name, first find it with a command such as ip link, and then create the file with that name.

Public interface:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

Content of the public interface file:

BOOTPROTO=dhcp
DEVICE=eth0
MTU=1500
ONBOOT=yes
TYPE=Ethernet
USERCTL=no

Private interface:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth1

Content of the private interface file:

BOOTPROTO="none"
DEVICE=eth1
MTU=1450
IPADDR="192.68.0.100"
NETMASK="255.255.255.0"
ONBOOT=yes
TYPE=Ethernet
USERCTL=no

Note: If the instance doesn't have a floating IP and you use the public interface for internet access, you don't need to configure GATEWAY.

Finally, after defining the configuration, we can activate the interface with the new settings with the following commands:

sudo ifdown eth0
sudo ifup eth0

Or restart the network service:

sudo systemctl restart network

Debian and Ubuntu​

To configure a static interface in Debian-based distributions, simply edit the following file:

/etc/network/interfaces

On an instance, by default this file may not contain anything specific, and interface configuration files may be read separately from the following directory:

/etc/network/interfaces.d

For manual configuration, you can do this step:

sudo rm -f /etc/network/interfaces.d/*
sudo nano /etc/network/interfaces

To set it to DHCP mode, set the file's content to the following values:

auto ens3
iface ens3 inet dhcp

Or:

auto ens3
iface ens3 inet static
address 194.5.193.25
netmask 255.255.254.0
gateway 194.5.192.1

For example, for an instance with a public and a private interface where the private network is configured as STATIC, the file contains an iface ... inet dhcp block for the public interface and an iface ... inet static block with address and netmask for the private interface.

Finally, after configuring, you can reboot or use ifdown and ifup to activate the interface with the new configuration.

sudo ifdown ens3
sudo ifup ens3

Ubuntu 18.04 / Netplan Configuration​

Starting with version 18.04, Ubuntu began using netplan to configure network cards and their settings. In netplan, settings are written in a YAML file. When you write settings in this file, netplan uses a renderer to generate the appropriate settings based on which system the operating system uses for network configuration.

Netplan configuration files are in the /etc/netplan/*.yml directory. On Ubuntu instances you get from Arvancloud, as mentioned before, default settings exist, but for manual configuration you can do the following:

Note that you must first configure cloud-init so it doesn't change the network settings and the settings persist after a reboot.

The YAML file has a hierarchical structure, and the relationship between parameters is defined with spaces. So when writing this file, don't use tabs to separate parameters, or you'll get an error.

Go to the /etc/netplan/ directory and open the existing file with an editor for your own configuration, or delete the previous file and create a new one:

sudo nano /etc/netplan/*.yml

The file has a structure similar to the following:

network:
version: 2
renderer: networkd
ethernets:
DEVICE_NAME:
dhcp4: yes/no
addresses: [IP_ADDRESS/NETMASK]
gateway4: GATEWAY

The configuration above shows a simple template for configuring a network card. Some of its items and parameters are explained below.

Note that DEVICE_NAME is the name of the interface to be configured. You can find this name with the ip link command.

  • The dhcp4 parameter enables or disables DHCP version 4 for the interface.
  • The addresses parameter sets IPs on the interface; you can enter one or more IPs here.
  • The gateway parameter sets a gateway on this interface, which is required for instances' public interfaces.
  • The renderer parameter specifies your backend, which on Ubuntu Server is systemd-networkd.

For an instance connected to a public network and a private network, the file defines both interfaces under ethernets: the public one with dhcp4: true, and the private one with dhcp4: false, its static addresses, and its match: macaddress and mtu.

The mtu parameter sets the link's MTU and must be set together with the macaddress parameter. The macaddress parameter is the MAC address of the device to be configured, and if the MAC address is wrong, the settings aren't applied to the device; in effect, it is a parameter for precisely identifying the interface being configured.

After entering the settings, to apply them simply run the command:

sudo netplan apply -d

If there's an error in the YAML syntax, you'll see an explanation because of the -d switch. If the file is correct, the settings are applied without errors. Finally, you can restart the network service:

sudo systemctl restart systemd-networkd.service

Interface and Network Settings in Windows​

To check and change the network settings of Windows Server, first enter ncpa.cpl in the Run window to open the Network Connections page. Then right-click the network card and click Properties.

On the next page, first select Internet Protocol Version 4 and click Properties again.

On the page that opens, you can change information such as the IP, subnet mask, and gateway, and finally save the settings.

To check the network card's settings, double-click the network card and select Details; the next page shows the network settings.