Adding a Public IPv4 and IPv6 Address to a Linux Server (Ubuntu and Debian)

For Virtual Private Server (VPS) and Cloud Servers

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

Attention

If you are looking for information on how to add IPv4 addresses and IPv6 addresses on a Dedicated Server instead, please see 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 in the Cloud Panel.

  • You have logged in to the server.

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

Determining the Network Interface

To determine the file name of the network interface, enter the following command:

[root@localhost ~]# ip addr
Example:

[root@localhost ~]# 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: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:2b:2c:68 brd ff:ff:ff:ff:ff:ff
    inet 212.227.209.26/32 brd 212.227.209.26 scope global dynamic ens192
       valid_lft 33737sec preferred_lft 33737sec
    inet6 fe80::250:56ff:fe2b:2c68/64 scope link
       valid_lft forever preferred_lft forever

In this example, the name of the network interface is ens192.

Displaying the 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 the DNS servers.

Installing the Net-tools Package

Install the net-tools package. This package is a collection of important programs for controlling the network subsystem.

To install net-tools, enter the following commands:

[root@localhost ~]# apt-get update
[root@localhost ~]# apt-get upgrade
[root@localhost ~]# apt-get install -y net-tools

Adding the IPv4 and IPv6 Addresses

  • Open the /etc/network/interfaces file with the vi editor.
    [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 in all places with the interface device you determined. Then, enter the main IPv4 address and the main IPv6 address.

  • To configure the additional IPv4 address(es), insert the following entry(s) below the entry for the main IPv4 address:
    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 address. Replace the placeholder with the interface device you determined.

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

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

  • To configure additional IPv6 addresses, add the following entry(s) below the entry for the main IPv6 address:
    iface <interface_device> inet6 static
        accept_ra 0
        address <Additional IPv6 Address 1>/64

    iface <interface_device> inet6 static
        accept_ra 0
        address <Additional IPv6 Address 2>/64

    Replace the part with the desired additional IPv6 address. Replace the placeholder with the interface device you determined.

    The following example shows a complete entry that is used to configure the main IPv4 address, the main IPv6 address, and an additional IPv4 address and an 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 the DNS Server(s)

To add the DNS servers, proceed as follows:

Debian 9
  • 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 Server 1>
    nameserver <DNS Server 2>

 

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 Server 1>
    nameserver <DNS Server 2>

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

 

Ubuntu 18.04, Ubuntu 20.04, Debian 10 and Debian 11
  • 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 Server 1> <DNS Server 2> <DNS Server 3>

    If the # symbol is listed at the beginning of this entry, delete it. Otherwise, the setting will not be applied.

  • Save the file.

  • To apply the setting, run the following command:
    [root@localhost ~]# systemctl restart systemd-resolved

Restarting the Server and Checking the Configuration

  • To restart 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 are 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 log in to the server using the KVM console and correct the configuration.