Configuring Static IPv6 Address Using NetPlan on Ubuntu 18.04 and Up

Note: This article applies only to Ubuntu versions 18.04, 19.04, 19.10, 20.04, 21.04, 21.10, and newer.

From Ubuntu 18.04 onwards, Ubuntu switched over the method used to configure networking over to using NetPlan, which uses configuration files written in YAML. In this article, we will learn how to configure IPv6 networking in these newer versions of Ubuntu using netplan,

To configure IPv6, you would ideally need to configure the file /etc/netplan/01-netcfg.yaml which is in YAML.

Before we head out to configuring, we need to know what interface the server is using. To find out, run

ip a

Here's my IP

In this example, we have an interface called ens3. This may be different for you.

Configuring static IPv6 using NetPlan.

A side note on this, YAML is strict on indentation so we will use 2 spaces throughout the guide.

To find out the IPv6 Address assigned to you, login to CrownPanel and navigate to Manage Network tab.
For instruction Click here

You will find an IPv6 address similar to 2604:6600:158:5171::/64. If you do not find your IPv6 allocation, please open a Support Ticket.

While adding the address you would need to change the prefix of the IP to the netmask. (In this example we change prefix from 64 to 48)

To edit the configuration file.

vi /etc/netplan/01-netcfg.yaml

Initially, the network is configured to DHCP.

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes

Make the following changes to it now,

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      dhcp6: no
      addresses:
        - Your-VPS-IPv4-Address/IPv4-Netmask
        - Your-VPS-IPv6-Address/IPv6-Netmask
      gateway4: IPv4-Gateway
      gateway6: IPV6-Gateway
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
          - 2001:4860:4860::8888
          - 2001:4860:4860::8844

Example,

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      dhcp6: no
      addresses:
        - 193.29.57.59/24
        - 2604:6600:158:5171::/48
      gateway4: 193.29.57.1
      gateway6: 2604:6600:158::1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4
          - 2001:4860:4860::8888
          - 2001:4860:4860::8844

For nameservers, we are using google's public DNS.

8.8.8.8
8.8.4.4
2001:4860:4860::8888
2001:4860:4860::8844

Once you exit from the configuration. Apply the changes by running the following command,

netplan apply

or

netplan try

Note: If everything is good it will ask to press Enter before 120 sec if you didn't confirm by hitting Enter it will timeout and revert the changes made on the config file.

Output,

root@vps:~# netplan try
Warning: Stopping systemd-networkd.service, but it can still be activated by:
  systemd-networkd.socket
Do you want to keep these settings?

Press ENTER before the timeout to accept the new configuration

Changes will revert in 118 seconds
Configuration accepted.
root@vps:~#

Let's see if we can now Ping google's IPv6,

root@vps:~# ping6 ipv6.google.com
PING ipv6.google.com(lax17s05-in-x0e.1e100.net (2607:f8b0:4007:808::200e)) 56 data bytes
64 bytes from lax17s05-in-x0e.1e100.net (2607:f8b0:4007:808::200e): icmp_seq=1 ttl=57 time=1.24 ms
64 bytes from lax17s05-in-x0e.1e100.net (2607:f8b0:4007:808::200e): icmp_seq=2 ttl=57 time=0.720 ms
64 bytes from lax17s05-in-x0e.1e100.net (2607:f8b0:4007:808::200e): icmp_seq=3 ttl=57 time=0.849 ms

Configuring static IPv6 with routes

If when running netplan apply if you see the message, "gateway6 has been deprecated, use default routes instead..." then use the below configuration style,

network:
 version: 2
 renderer: networkd
 ethernets:
   ens3:
     dhcp4: true
     dhcp6: false
     addresses:
       - Your-VPS-IPv6-Address/IPv6-Netmask
     routes:
         - on-link: true
           to: ::/0
           via: IPV6-Gateway
     nameservers:
       addresses:
         - 2001:4860:4860::8888
         - 2001:4860:4860::8844

Example,

network:
 version: 2
 renderer: networkd
 ethernets:
   ens3:
     dhcp4: true
     dhcp6: false
     addresses:
       - 2604:6600:158:5171::/48
     routes:
         - on-link: true
           to: ::/0
           via: 2604:6600:158::1
     nameservers:
       addresses:
         - 2001:4860:4860::8888
         - 2001:4860:4860::8844

Once you exit from the configuration file, be sure to run netplan apply,

netplan apply

You have now static configured IPv6 Addresses on your server.

If you can not configure and face technical difficulties, kindly reach out to us via Support Ticket.