Add Secondary IP Address on Ubuntu 24.04|22.04|20.04

In this tutorial, I’ll teach you how to install a secondary IP address on Ubuntu 24.04|22.04|20.04, both temporarily and permanently. Transmission Control Protocol/Internet Protocol (TCP/IP) is a set of protocols that allows computers to communicate with one another. Despite its name, the stack includes a variety of protocols, including IP, TCP, UDP, ICMP, DNS, SMTP, ARP, and many others. The IP protocol is in charge of a host’s logical addressing, which allows packets to be transferred from one host to another.

Each device on the network is given a unique IP address for this purpose, and many addresses might be issued to the same device. In addition to the IP addressing protocol at the network layer, a protocol at the transport layer, such as the TCP and UDP protocols, will be required for a host to access a service offered on another host. This communication is carried out via these protocols through network ports. To access a service, in addition to establishing a source and destination IP, source and destination ports will be used.

Need for Secondary IPs in a Linux system?

Here are the reasons why you need multiples IP addresses:

  • To set up name servers.
  • Assign separate IP addresses to different services.
  • Installing an SSL certificate on a website and hosting numerous SSL sites without the use of virtual hosts is possible.
  • Separate IP addresses are used to host programs.

There are three main options to configure IP addresses on Ubuntu 24.04|22.04|20.04

1. Configuring Secondary IP Address Permanently

  1. Using Netplan
  2. Configuring Manually
  3. Using NMCLI

2. Configuring Secondary IP Addresses Temporarily

You can temporarily add a secondary IP address to your Ubuntu 24.04|22.04|20.04 system using the syntax below. When you use this approach to install a secondary IP, it will persist until the next system reboot.

Syntax:

ip addr add <ip-address> dev <interface-name>

Delete the above IP as follows:

sudo ip addr del <ip-address> dev <interface-name>

Configuring Secondary IP Addresses Permanently

Configuring IP addresses permanently on Ubuntu can be achieved through the following ways:

Option 1: Configure Secondary IP Address using Netplan

When using Netplan, an static or secondary IP address can be added to your Ubuntu system by editing /etc/netplan/50-netcfg.yaml file.

First of all, issue the following command to check current IP address of the system:

$ ip a
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: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.10/24 brd 192.168.33.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee0:af44/64 scope link 
       valid_lft forever preferred_lft forever

Or you can also use the following:

$ ifconfig
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.33.10  netmask 255.255.255.0  broadcast 192.168.33.255
        inet6 fe80::a00:27ff:fee0:af44  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:e0:af:44  txqueuelen 1000  (Ethernet)
        RX packets 16  bytes 1400 (1.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 51  bytes 4286 (4.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 306  bytes 31120 (31.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 306  bytes 31120 (31.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As you can see from the output above, the current IP address of the system is 192.168.33.10 on NIC (Network Interface Controller) enp0s8.

If you don’t have Netplan tool installed on your system, you can the following command to install it:

sudo apt install netplan.io

Now you can use vim editor or any other editor of your choice to edit /etc/netplan/50-netcfg.yaml file. If the vim editor is not installed you can also install as shown below:

#install vim editor
sudo apt install vim

#edit the file
sudo vim /etc/netplan/01-netcfg.yaml

Now you can add the new IP address as shown below. In this guide, I will add secondary IP 192.168.33.15.

network:
    version: 2
    renderer: networkd
    ethernets:
        enp0s8:
            addresses:
                - 192.168.33.10/24
                - 192.168.33.15/24
            nameservers:
                addresses: [8.8.8.8, 1.1.1.1]

Then apply the changes:

sudo netplan try
sudo netplan apply

Now, confirm if the IP address has been added:

$ ip a
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: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.10/24 brd 192.168.33.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet 192.168.33.15/24 brd 192.168.33.255 scope global secondary enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee0:af44/64 scope link 
       valid_lft forever preferred_lft forever

Option 2: Configure Secondary IP Address using NMCLI

On an Ubuntu 24.04/22.04/20.04 system, we can also utilize the Network Manager CLI tool to add a secondary IP address.

Run the following to install NMCLI tool:

sudo apt  install network-manager

Examine the network connection profiles that are already in place:

$ nmcli device show
GENERAL.DEVICE:                         enp0s8
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         02:70:58:87:E8:B4
GENERAL.MTU:                            1500
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         10.0.2.15/24
IP4.GATEWAY:                            10.0.2.2
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 10.0.2.2, mt = 100
IP4.ROUTE[2]:                           dst = 10.0.2.0/24, nh = 0.0.0.0, mt = 0
IP4.ROUTE[3]:                           dst = 10.0.2.2/32, nh = 0.0.0.0, mt = 100
IP6.ADDRESS[1]:                         fe80::70:58ff:fe87:e8b4/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 256

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = ::1/128, nh = ::, mt = 256

After locating the interface for adding a secondary IP address, we proceed to add the IP address as shown below:

sudo nmcli con add con-name "Network2" \
    type ethernet \
    ifname enp0s8 \
    ipv4.address 192.168.33.20/24 \
    ipv4.gateway 192.168.33.1 \
    ipv4.method manual \
    connection.autoconnect yes

Now check network creation:

$ nmcli connection show
NAME      UUID                                  TYPE      DEVICE 
Network2  35d0b0a5-edbc-4aec-93c3-41326c4ee8dd  ethernet  -- 

The secondary IP is assigned using the +ipv4.addresses option:

sudo nmcli con mod Network2 +ipv4.addresses 192.168.33.20/24

For the IP address to be added, restart the network:

sudo nmcli connection down "Network2" && sudo nmcli connection up "Network2"

Check updated IP addresses:

$ ip a
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: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e0:af:44 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s8
       valid_lft forever preferred_lft forever
    inet 192.168.33.20/24 brd 192.168.33.255 scope global secondary enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee0:af44/64 scope link 
       valid_lft forever preferred_lft forever

Conclusion

That concludes the discussion. You’ve added a secondary IP address to your Ubuntu. I hope you found this information to be useful.

Cool Guides you should also check;

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

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

Related Post

One of the most effective control panels for web hosting that is used to host websites and administer web servers […]

Beekeeper Studio is an open-source completely free cross-platform SQL editor and database manager. It is available for Mac, Linux, and […]

OpenResty® is a full-featured web platform that uses our improved Nginx core to scale online applications and services. Its purpose […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.