For VPSs purchased after May 16, 2023

This article explains how to add another public IPv4 address on a VPS that has Ubuntu 20.04, Ubuntu 22.04, or Debian 12 installed.

The network configuration is done through netplan in the images of IONOS for Ubuntu 20.04 and Ubuntu 22.04. Network configuration is no longer controlled by the /etc/network/interfaces file but by files with the .yaml file extension that are located in the /etc/netplan directory. If there are multiple files in this directory, they are loaded in lexical order.

To configure another public IPv4 address in Ubuntu 20.04 and 22.04:

Requirements
  • You assigned an additional, public IPv4 address to your server in the Cloud Panel.

  • You logged in to the server.

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

Checking if netplan is used for network configuration

To check if your server is already using netplan for network configuration, enter the following command:

  • Change to the /etc/netplan directory.

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

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

    [root@localhost ~]# ls

Please Note

If the /etc/netplan directory does not exist, netplan has not been installed. In this case, do not install netplan but instead follow the steps described in the following article:

Adding Public IPv4 and IPv6 Addresses on a Linux Server (Ubuntu and Debian)

Displaying the network interface

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

[root@localhost ~]# ip addr

In this example, the network interface name is ens6:

root@ubuntu:/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:95:f5:d9 brd ff:ff:ff:ff:ff:ff
    inet 195.20.234.94/32 scope global dynamic ens6
       valid_lft 463sec preferred_lft 463sec
    inet6 fe80::1:e4ff:fe95:f5d9/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

Then, the DNS servers used are 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

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

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

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

    IPv4 gateway

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

Adding an IPv4 address

  • Check if there is not 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, type the following command and replace the wildcards (OLD_CONFIG_FILE).

    root@localhost ~]# mv OLD_CONFIG_FILE.yaml OLD_CONFIG_FILE.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 because this file is needed for the public network.

  • Create the /etc/netplan/01-netcfg.yaml file using 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 enter the insert mode by pressing the [i] key. In this mode, the entered characters are immediately inserted into the text. To enter the command mode, press [ESC] afterwards. If you use the command mode, your keyboard inputs are interpreted as a command

  • Include the following information:

    network:
      version: 2
      renderer: networkd
      ethernets:
        NETWORKINTERFACE:
          addresses:
            - MAIN-IPv4-ADDRESS/32
            - ADDITIONAL IPv4-ADDRESS/32 
          nameservers:
            addresses:
              - IP ADDRESS FROM NAMESERVER 1
              - IP ADDRESS FROM NAMESERVER 2  
          routes:
            - on-link: true
              to: default
              via: STANDARD-GATEWAY OF ACTIVE INTERFACE
      version: 2

    Example:

    network:
      version: 2
      renderer: networkd
      ethernets:
        ens6:
          addresses:
            - 195.20.234.94/32
            - 195.20.234.86/32
          nameservers:
            addresses:
              - 212.227.123.16
              - 212.227.123.17  
          routes:
            - on-link: true
              to: default
              via: 195.20.234.1
      version: 2

  • Replace the placeholder NETWORKINTERFACE with the interface device ens192. Then enter the main IPv4 address and the additional IPv4 address. Replace the placeholders for this.

Attention
  • The indentations belong to syntax of YAML format. Therefore, make sure that they are observed correctly. Otherwise, the configuration cannot be applied correctly and the server may no longer be accessible.

  • Always use the spacebar for the indentations.

  • Replace the placeholder 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, type the command :wq and then press Enter.

  • To change the permissions of the 01-netcfg.yaml file so that only the root account can read and modify the file, 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 /etc/netplan/01-netcfg.yaml file remain and must be changed by you if they are not correct. Otherwise, the operating system will try to load them after a reboot.

  • Reboot the server to apply the changes. To do this, enter the following command:

    root@localhost:~# systemctl reboot

  • To verify that the additional IPv4 address is configured correctly, enter the following command:

    root@localhost:~# ip addr

Please Note

If the server becomes unreachable 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 here:

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

https://netplan.io/examples/