See all Debian/Ubuntu Linux related FAQ
An IP alias on Debian, Ubuntu, or any other Linux distros refers to assigning multiple IP addresses to a single network interface. A single network interface card (NIC) can respond to multiple IP addresses and configurations. IP aliases are commonly used for various purposes, such as network segmentation, virtual hosting, load balancing, VPN, split DNS, network testing, and more. Let us see how to add IPv6 and IPv4 IP addresses to your AWS EC2 or Lightsail instance using the CLI without disturbing the IPv4/IPv6 addresses assigned by the AWS DHCP server. These instructions will also work with all other cloud providers, as most providers use DHCP to give you a public IPv4/IPv6 address.

There are two methods to set IP aliases on Debian and Ubuntu Linux, depending on your system’s version: DHCP-based and Netplan-based. Please choose one method and avoid using both simultaneously.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements none|Linux terminal
Category Network Management
OS compatibility Debian Linux Mint Pop!_OS Ubuntu
Est. reading time 4 minutes

Sample IPv4 and IPv6

For demo purpose I’m using the following IP address in private range:

  • IPv4 : 10.8.0.1/24
  • IPv6 : fd5a:dadf:8d6d::1/48

Step 1 – Finding out your interface

The default interface name will be eth0, but it can sometimes be different. Here is how to list your network interfaces in Linux. For example, try the ip command:
$ ip link show
Finding out EC2 Lightsail Instance Networking Interface Name on Debian or Ubuntu Linux
I have ‘ens5’ as the network interface name. To list or show assigned IP address on Linux, type:
$ sudo ip addr list ens5
$ sudo ip addr show ens5

Step 2 – Adding an IP alias to an EC2 instance on Debian/Ubuntu Linux using DHCP hooks

DHCP hooks based method works on both Debian 11/12 and Ubuntu 20.04/22.04/22.04 LTS.

You need to place a simple script in the “/etc/dhcp/dhclient-exit-hooks.d” directory that will run by the DHCP client upon exiting. Most cloud providers do not configure static networking using the /etc/netplan/50-cloud-init.yaml on Debian 12/Ubuntu 22.04 LTS or /etc/network/interfaces on Debian 11/Ubuntu 20.04 LTS. Here is how the configuration looks on a Debian Linux 12 EC2 instance:
$ sudo cat /etc/netplan/50-cloud-init.yaml
Outputs indicating that EC2 box will get IPv4/IPv6 using DHCP:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        ens5:
            dhcp4: true
            dhcp6: true
            match:
                macaddress: 02:26:14:f3:44:83
            set-name: ens5
    version: 2

On a Debian 11 file /etc/network/interfaces will look as follows:

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
 
# Cloud images dynamically generate config fragments for newly
# attached interfaces. See /etc/udev/rules.d/75-cloud-ifupdown.rules
# and /etc/network/cloud-ifupdown-helper. Dynamically generated
# configuration fragments are stored in /run:
source-directory /run/network/interfaces.d

Which will call for the interface file named ens5:
$ cat /run/network/interfaces.d/ens5
Outputs for Debian 11 indicating that EC2 instance will us DHCP for IPv6/IPv4:

auto ens5
allow-hotplug ens5
 
iface ens5 inet dhcp
 
#iface ens5 inet6 manual
iface ens5 inet6 dhcp
  try_dhcp 1

Please do not edit these configuration files. The will get updated. Instead, add a script in the “/etc/dhcp/dhclient-exit-hooks.d” directory to perform configurations after the DHCP client has finished its operation, such as updating network routing tables, configuring network interface, etc. This directory allows administrators to extend the functionality of the DHCP client by adding scripts to be run at the end of the DHCP lease process. This directory is perfect for adding IP aliases script to an EC2 instance on Debian/Ubuntu Linux.

Example

Create a new file named /etc/dhcp/dhclient-exit-hooks.d/add-custom-ip-alias as follows using a text editor such as vim/emacs/nano:
$ sudo vim /etc/dhcp/dhclient-exit-hooks.d/add-custom-ip-alias
Append the following ip command for ens5 inteface:

/sbin/ip -4 addr add 10.8.0.1/24 dev ens5
/sbin/ip -6 addr add fd5a:dadf:8d6d::1/48 dev ens5

Save and close the file. That is all. Reboot the Linux instance using the reboot command for testing purposes:
$ sudo reboot
Another option is just to source the script using the source command without rebooting the box:
$ sudo source /etc/dhcp/dhclient-exit-hooks.d/add-custom-ip-alias

Step 3 – Verification

Verify new IP alias using the ip command and ping command $ sudo ip addr show ens5
$ sudo ip addr show ens5 | grep -E '10.8.0.1|fd5a:dadf:8d6d::1'
$ ping 10.8.0.1
# For ipv6 try ping6 command #
$ ping6 fd5a:dadf:8d6d::1

A note about private IPv4 and IPv6 address

Prerequisite
By default, subnetcalc and ipcalc command may not be installed on your system. Hence, use the apk command on Alpine Linux, dnf command/yum command on RHEL & co, apt command/apt-get command on Debian, Ubuntu & co, zypper command on SUSE/OpenSUSE, pacman command on Arch Linux to install the subnetcalc and ipcalc.
The IPv4 and IPv6 used are private but should not conflate with existing VPC, VPN, or VLAN CIDRs. You can use the subnetcalc command or ipcalc command to make private CIDR. For example, for IPv6, try:
$ subnetcalc fd5a:: 48 -uniquelocal
Private IPv4 networks fall within these ranges:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16

Hence, you can use something as follows:
$ subnetcalc 192.168.1.0/24
$ ipcalc 10.8.0.1/24

Unique Local Addresses (ULA) are the recommended private IPv6 addresses, similar to IPv4’s private ranges. They start with fd00::/8. Please note that IANA reserves FC00::/7 but RFC 4193 requires the 8th bit of these addresses to be set to 1. Hence, unique local addresses use FD00::/8.

A note about adding IP alias to an EC2 or Lightsail instance on Debian/Ubuntu Linux using netplan

Debian Linux version 12+ and Ubuntu Linux 20.04/22.04/24.04 LTS, you can simply set up a new netplan configuration with IPv4 and IPv6 addresses as follows. Create a new file named:
{admin@debian-12:~}$ sudo vim /etc/netplan/60-secondary-ip-alias.yaml
Append the following config (replace ens5 and IPv4/IPv6 as per your needs. This config file follows YAML syntax):

network:
    version: 2
    renderer: networkd
    ethernets:
        ens5:
            addresses:
                - 10.8.0.1/24 
                - fd5a:dadf:8d6d::1/48
Save and close the file by pressing Esc+:x! when using vim. Set correct permissions using the chmod command:
{admin@debian-12:~}$ sudo chmod 0600 /etc/netplan/60-secondary-ip.yaml
Test it:
{admin@debian-12:~}$ sudo netplan try
If no errors, mean you can type:
{admin@debian-12:~}$ sudo netplan apply
Of course, you can view network service configuration on a Debian 12 or Ubuntu Linux 20.04/22.04/24.04 LTS as follows:
{admin@debian-12:~}$ sudo systemctl restart systemd-networkd.service
{admin@debian-12:~}$ sudo systemctl status systemd-networkd.service

Outputs:

 systemd-networkd.service - Network Configuration
     Loaded: loaded (/lib/systemd/system/systemd-networkd.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-05-14 20:28:57 UTC; 3min 0s ago
TriggeredBy:  systemd-networkd.socket
       Docs: man:systemd-networkd.service(8)
             man:org.freedesktop.network1(5)
   Main PID: 862 (systemd-network)
     Status: "Processing requests..."
      Tasks: 1 (limit: 515)
     Memory: 1.5M
        CPU: 41ms
     CGroup: /system.slice/systemd-networkd.service
             └─862 /lib/systemd/systemd-networkd

May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: ens5: Link UP
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: ens5: Gained carrier
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: lo: Link UP
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: lo: Gained carrier
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: ens5: Gained IPv6LL
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: Enumeration completed
May 14 20:28:57 ip-172-26-11-135 systemd[1]: Started systemd-networkd.service - Network Configuration.
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: ens5: Configuring with /run/systemd/network/10-netplan-ens5.network.
May 14 20:28:57 ip-172-26-11-135 systemd-networkd[862]: ens5: DHCPv4 address 172.26.11.135/20, gateway 172.26.0.1 acquired from 172.26.0.1
May 14 20:28:58 ip-172-26-11-135 systemd-networkd[862]: ens5: DHCPv6 address 2406:da1a:4bc:a300:d2cd:e720:cfc4:f468/128 (valid for 7min 29s, preferred for 2m>
lines 1-24/24 (END)

Wrapping up

You’ve learned how to assign IP aliases to an EC2 or any other cloud instance powered by Debian or Ubuntu Linux using either the DHCP or netplan method. The DHCP-based method works on all Debian and Ubuntu Linux versions, while netplan requires a supported version of Debian or Ubuntu Linux. For more information, please refer to the following documentation:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

2 comments… add one
  • Tranch May 14, 2024 @ 22:39

    There is a space missing between ‘directorythat’.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.