For VPSs purchased after 16 May 2023

If you assign an IPv4 address and one or two IPv6 addresses to a VPS, these IP addresses are automatically configured in the operating system.

This article explains how to manually configure public IPv4 and IPv6 addresses in the operating system of a VPS on which Ubuntu 20.04, Ubuntu 22.04 or Debian 12 is installed. This is necessary if you assign two IPv4 addresses and at least one IPv6 address to a VPS.

The network configuration is completed in the images of IONOS for Ubuntu 20.04, Ubuntu 22.04 and Debian 12 via netplan. The network configuration is controlled via files with the file extension .yaml, which are located in the directory /etc/netplan. If there are several files in this directory, they are loaded in lexical order.

How to configure an additional, public IPv4 address in Ubuntu 20.04, Ubuntu 22.04 and Debian 12:

Prerequisites
  • You already assigned an additional, public IPv6 address to your server in the Cloud Panel.

  • You logged in to the server.

  • You made a note of the IPv6 addresses of the server.

Checking Whether netplan is Used for the Network Configuration

To check whether your server is already using netplan for the network configuration, proceed as follows:

  • Change to the /etc/netplan directory.

    [root@localhost ~]# cd /etc/netplan

  • Check whether there is already a configuration file in this directory. To do this, enter the following command:

    [root@localhost ~]# ls

Displaying the Network Interface

Determine the file name of the network interface. To do this, enter the following command:

[root@localhost ~]# ip addr

In this example, the name of the network interface is ens6:

root@localhost:/etc/netplan# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 02:01:e4:5c:32:4e brd ff:ff:ff:ff:ff:ff
    altname enp0s6
    inet 82.165.34.29/32 metric 100 scope global dynamic ens6
       valid_lft 429sec preferred_lft 429sec
    inet6 2a02:247a:21f:9c00::/128 scope global dynamic noprefixroute
       valid_lft 3598sec preferred_lft 2598sec
    inet6 2a02:247a:21f:9c00::1/128 scope global dynamic noprefixroute
       valid_lft 3598sec preferred_lft 2598sec
    inet6 fe80::1:e4ff:fe5c:324e/64 scope link
       valid_lft forever preferred_lft forever

Displaying the Default DNS Server

  • To display the DNS servers used, enter the following command:

    [root@localhost ~]# resolvectl --no-pager |grep Server

    The DNS servers used are then displayed at the end of the output.

    Example:

    [root@localhost ~]# resolvectl --no-pager |grep Server
    Current DNS Server: 212.227.123.16
           DNS Servers: 212.227.123.16 212.227.123.17

  • Make a note of the DNS servers.

Displaying the Gateway for IPv4 and IPv6

  • To display the default gateway of the active interface, enter the following commands:

    IPv4:

    [root@localhost ~]# ip route show | grep 'default'

    IPv6:

    [root@localhost ~]# ip -6 route show | grep 'default'

  • Note the IP addresses of the gateways. These are listed directly after the default via part. Examples:

    IPv4 gateway

    root@ubuntu:~# ip route show | grep 'default'
    default via 82.165.34.1 dev ens6 proto dhcp src 195.20.234.94 metric 100

    IPv6 Gateway

    root@localhost:~# ip -6 route show | grep 'default'
    default via fe80::1 dev ens192 proto ra metric 100 expires 4sec pref high

    In this example, fe80::1 is the IPv6 gateway.

Adding IPv4 and IPv6 Addresses

  • Check whether there is already a configuration file in this directory. To do this, enter the following commands:

    [root@localhost ~]# cd /etc/netplan
    [root@localhost ~]# ls /etc/netplan

  • If there is a configuration file in this directory, rename it. To do this, enter the following command and replace the placeholders.

    root@localhost ~]# mv OLD_CONFIGURATIONFILE.yaml OLD_CONFIGURATIONFILE.yaml.old

    Example:

    [root@localhost ~]# mv 50-cloud-init.yaml 50-cloud-init.yaml.old

Attention!

If the directory contains the file 00-Public_network.yaml, do not rename it, as this file is required for the public network.

  • Create the file /etc/netplan/01-netcfg.yaml with the vi editor. To do this, enter the following command:

    [root@localhost ~]# vi etc/netplan/01-netcfg.yaml

Please Note

The vi editor has an insert mode and a command mode. You can enable insert mode with the [i] key. In this mode, the characters entered are immediately inserted into the text. To enable command mode, press [ESC]. If you use command mode, your keyboard input is interpreted as a command.

  • Insert the following information:

    network:
      version: 2
      renderer: networkd
      ethernets:
        NETWORKINTERFACE:
          addresses:
            - MAIN IPv4 ADDRESS/32
            - ADDITIONAL IPv4 ADDRESS/32
            - MAIN IPv6 ADDRESS/64
            - ADDITIONAL IPv6 ADDRESS_1/64 
     
          nameservers:
            addresses:
              - IP ADDRESS OF NAME SERVER 1
              - IP ADDRESS OF NAME SERVER 2  
          routes:
            - on-link: true
              to: default
              via: STANDARD GATEWAY OF THE ACTIVE INTERFACE
      version: 2

    Example:

    network:
      version: 2
      renderer: networkd
      ethernets:
        ens6:
          addresses:
            - 82.165.34.29/32
            - 195.20.236.17/32
            - 2a02:247a:21f:9c00::1/64
            - 2a02:247a:021f:9c00:0000:0000:0000:0000/64 
          nameservers:
            addresses:
              - 212.227.123.16
              - 212.227.123.17  
          routes:
            - on-link: true
              to: default
              via: 82.165.34.1
      version: 2

  • Replace the NETWORKINTERFACE placeholder with the network interface that you have determined. Then enter the main IPv4 address, the additional IPv4 address and the IPv6 addresses. To do this, replace the appropriate placeholders.

Attention

The indentations are part of the syntax of the YAML format. Therefore, make sure that these are adhered to correctly. Otherwise the configuration cannot be applied correctly and the server may no longer be accessible. Always use the space bar for the indentations.

  • Replace the placeholders IP ADDRESS OF NAME SERVER 1 and IP ADDRESS OF NAME SERVER 2 with the determined name servers.

  • Replace the placeholder STANDARD GATEWAY OF ACTIVE INTERFACE with the determined IPv4 gateway.

  • To exit vi and save the file, enter the command :wq and then press Enter.

  • To change the permissions of the 01-netcfg.yaml file so that the file can only be read and changed by the root account, enter the following command:

    root@localhost:~# sudo chmod 600 /etc/netplan/01-netcfg.yaml

  • To check the configuration file, enter the following command:

    root@localhost:~# netplan --debug apply

Attention

The changes you have made in the file /etc/netplan/01-netcfg.yaml remain and must be changed by you if they are not correct. Otherwise the operating system will attempt to load them after a restart.

  • Restart the server so that the changes are applied. To do this, enter the following command:

    root@localhost:~# systemctl reboot

  • To check whether the additional IPv4 address has been configured correctly, enter the following command:

    root@localhost:~# ip addr

Please Note

If the server is no longer accessible due to a configuration error, you can log in to the server using the remote console and correct the configuration.

Additional Information

You can find more information about netplan in the following articles:

http://manpages.ubuntu.com/manpages/bionic/man5/systemd.network.5.html

https://netplan.io/examples/