Adding a Public IPv4 Address to a Linux Server (Ubuntu)

For Cloud Servers

This article explains how to add additional, public IPv4 and IPv6 addresses on a cloud server running Ubuntu or Debian.

Warning

These instructions are only valid for Cloud Servers. You can learn how to add IPv4 and IPv6 addresses on Dedicated Servers in the following articles:

Adding a Public IPv4 Address on a Dedicated Server (Ubuntu 20.04)

Adding a Public IPv6 Address on a Dedicated Server (Ubuntu 20.04)

How to configure additional public IPv4 and IPv6 addresses in Ubuntu and Debian:

Requirements
  • You have assigned one or more additional public IPv4 and/or IPv6 addresses to your server in the Cloud Panel.

  • You have logged on to the server.

  • You have taken note of the IPv4 and IPv6 addresses of the server.

Showing Name Servers

  • To display the DNS servers used, enter the following command:
    [root@localhost ~]# cat /etc/resolv.conf
    The DNS servers used are then displayed in the nameserver entry. Example:
    [root@localhost ~]# cat /etc/resolv.conf
    # Generated by NetworkManager
    nameserver 212.227.123.16
    nameserver 212.227.123.17

  • Make a note of these DNS servers.

Adding IPv4 and IPv6 Addresses

  • Open the file /etc/network/interfaces with the editor vi.
    [root@localhost ~]# vi /etc/network/interfaces

  • Replace the existing entries with the following information:

    source /etc/network/interfaces.d/*

    # The loopback network interface
    auto lo <Interface_Device>
    iface lo inet loopback

    # The primary network interface
    allow-hotplug <Interface_Device>

    iface <Interface_Device> inet static
        address Main-IPv4-Address/32
        gateway 10.255.255.1
        up route add -net 10.255.255.1 netmask 255.255.255.255 dev <Interface_Device> 

    iface <Interface_Device> inet6 static
        accept_ra 0
        address Main-IPv6-Address/64
        gateway fe80::1

    Replace the placeholder with the interface device ens192. Then enter the main IPv4 address and main IPv6 address.

    To configure the additional IPv4 address(es), insert the following entry(s):
    iface <interface_device> inet static
        address <Additional-IPv4-Address 1>/32

    iface <interface_device> inet static
        address <Additional-IPv4-Address 2>/32

    Replace the part with the desired additional IPv4 addresses.

Notes
  • 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]. When you use command mode, your keyboard input is interpreted as a command.

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

  • To configure additional IPv6 addresses, insert the following entry:
    iface <interface_device> inet6 static
        accept_ra 0
        address <Additional-IPv6-Address>/64

    Replace the part with the desired additional IPv6 address. To configure multiple additional IPv6 addresses, enter them one after the other. Use a space to separate the IPv6 addresses.

    The following example is a complete entry that configures the main IPv4 address, the main IPv6 address, and one additional IPv4 address and one additional IPv6 address.
    source /etc/network/interfaces.d/*

    # The loopback network interface
    auto lo ens192
    iface lo inet loopback

    # The primary network interface
    allow-hotplug ens192

    iface ens192 inet static
    address 82.165.103.128/32
    gateway 10.255.255.1
    up route add -net 10.255.255.1 netmask 255.255.255.255 dev ens192

    iface ens192 inet static
    address 212.227.193.228/32

    iface ens192 inet6 static
    accept_ra 0
    address 2001:8d8:1800:334::1/64
    gateway fe80::1

    iface ens192 inet6 static
    accept_ra 0
    address 2001:8d8:1800:334::2/64

Adding DNS Servers

To add the DNS servers, do the following:

Debian
  • Open the /etc/resolv.conf file with the vi editor.
    [root@localhost ~]# vi /etc/resolv.conf

  • Then enter the DNS servers in the following format:
    nameserver <DNS-Server1>
    nameserver <DNS-Server1> <DNS-Server2>

 

Ubuntu 16.04
  • Open the file /etc/resolvconf/resolv.conf.d/base with the editor vi.
    [root@localhost ~]# vi /etc/resolvconf/resolv.conf.d/base

  • Then enter the DNS servers in the following format:
    nameserver <DNS-Server1>
    nameserver <DNS-Server1> <DNS-Server2>

  • Then run the following command:
    [root@localhost ~]# resolvconf -u

Ubuntu 18.04 and Ubuntu 20.04
  • Open the file /etc/systemd/resolved.conf with the editor vi.
    [root@localhost ~]# vi /etc/systemd/resolved.conf

  • Then enter the DNS servers in the following format:
    DNS=<DNS-Server1> <DNS-Server2> <DNS-Server3>

  • Then execute the following command:
    [root@localhost ~]# systemctl restart systemd-resolved

Restarting the Server and Checking the Configuration

  • To reboot the server, enter the following command:
    [root@localhost ~]# systemctl reboot

  • Log in to the server again.

  • To verify that the desired IPv4 and IPv6 addresses have been configured correctly, enter the ip addr show command:
    [root@localhost ~]# ip addr show

Please Note

If the server becomes unreachable due to a configuration error, you can use the KVM console to log in to the server and correct the configuration.