How to add additional IP (Secondary IP) in Ubuntu

Adding an additional or secondary IP is routine task for Linux system administrator. When we setup a new server, ideally it will have an IP address, which is the server main IP address linked with the server hostname.

You may be wondering why do we need to add more than one IP address in server? why do we need to add this to a single network card? We will answer these questions in the this article.

Additional IPs are required for several reasons, some of them are listed below, which are best practices in the industry:

  • To host application in a separate IP
  • To install SSL certificate on website
  • To configure name servers

Linux systems allow users to add multiple virtual IP addresses on a single network interface card without adding an additional network adapter.

Suggested Read:

We can add IP address in the same interface or create sub interface on the same device, followed by adding an IP to that. By default interface name comes in the format: ethX (eth0) till Ubuntu 14.04 LTS. But from Ubuntu 15.10 onwards, network interfaces names have been changed from ethX to enXXXXX (For server ens33 & For desktop enp0s3).

In this article we will teach you how to add additional IP address in Ubuntu and it’s derivative.

1) How to add secondary IP address permanently via Netplan in Ubuntu 20.04 LTS

Configuring an IP address on an Ubuntu 20.04 system is different than the older version of Ubuntu because Ubuntu 17.10 and later uses ‘Netplan’ as the default network management tool.

Additional IP address can be permanently added to your system by editing the file ‘/etc/netplan/50-cloud-init.yaml‘ or ‘/etc/netplan/01-netcfg.yaml‘.

Configured existing IP address can be viewed by IP command as shown below:

# 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.201/24 brd 192.168.56.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe97:132e/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:12:e8:c1 brd ff:ff:ff:ff:ff:ff
    inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
       valid_lft 86353sec preferred_lft 86353sec
    inet6 fe80::344b:6259:4dbe:eabb/64 scope link
       valid_lft forever preferred_lft forever

Similaraly, you can also see your existing network configuration details from the following file:

# cat /etc/netplan/50-cloud-init.yaml

network:
version: 2
renderer: networkd
ethernets:
cnp0s3:
dhcp4: no
addresses: [192.168.56.201/24]
gateway4: 192.168.56.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]

To add a new IP address, open Netplan configuration file '/etc/netplan/50-cloud-init.yaml' in your preferred text editor, and add a new IP address in the IP address column as shown below:

Once changes are made, save and close the file.

# vi /etc/netplan/50-cloud-init.yaml

network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
addresses: [192.168.56.201/24, 192.168.56.202/24]
gateway4: 192.168.56.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]

Execute the following command to enable the new configuration.

# netplan apply

Run the below command to check whether the new IP is reflecting:

# 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.56.201/24 brd 192.168.56.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.56.202/24 brd 192.168.56.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe97:132e/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:12:e8:c1 brd ff:ff:ff:ff:ff:ff
inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
valid_lft 86353sec preferred_lft 86353sec
inet6 fe80::344b:6259:4dbe:eabb/64 scope link
valid_lft forever preferred_lft forever

1.a) Adding secondary IP address temporarily via Netplan in Ubuntu

Run the following IP command to add an additional IP address temporarily to your system:

# ip addr add 192.168.56.202/24 dev enp0s3 label enp0s3:1

2) How to add secondary IP address permanently in Ubuntu 16.04 LTS

As discussed in the beginning of this article, network interface names have been changed
from ‘ethX’ to ‘enXXXX’ (enp0s3) for Ubuntu version 15.10 onwards. While trying out the below exercise make sure to replace the interface name with yours.

Let me check the IP information on my system before performing this. See below:

# ifconfig
or
# ip addr

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.201 netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe97:132e  prefixlen 64  scopeid 0x20
        ether 08:00:27:97:13:2e  txqueuelen 1000  (Ethernet)
        RX packets 7  bytes 420 (420.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 294  bytes 24747 (24.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.3.15  netmask 255.255.255.0  broadcast 10.0.3.255
        inet6 fe80::344b:6259:4dbe:eabb  prefixlen 64  scopeid 0x20
        ether 08:00:27:12:e8:c1  txqueuelen 1000  (Ethernet)
        RX packets 1  bytes 590 (590.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 97  bytes 10209 (10.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
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 325  bytes 24046 (24.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 325  bytes 24046 (24.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

To add secondary IP address permanently on Ubuntu system, edit '/etc/network/interfaces' file and add the required details.

We should not add secondary IP address after ‘dns-nameservers’ because it wont work and add the IP details in format specified below.

Moreover we don’t need to add sub interface, which were done in Ubuntu 14.04 LTS.

# vi /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet static
address 192.168.56.201
netmask 255.255.255.0

iface enp0s3 inet static
address 192.168.56.202
netmask 255.255.255.0

gateway 192.168.56.1
network 192.168.56.0
broadcast 192.168.56.255
dns-nameservers 8.8.8.8 8.8.4.4
dns-search      2daygeek.local  

Save and close the file then restart the network interface:

# systemctl restart networking
or
# ifdown enp0s3 && ifup enp0s3

Run the below command to check if the new IP has been assigned:

# 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.201/24 brd 192.168.56.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.56.151/24 brd 192.168.56.255 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe97:132e/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:12:e8:c1 brd ff:ff:ff:ff:ff:ff
    inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
       valid_lft 86353sec preferred_lft 86353sec
    inet6 fe80::344b:6259:4dbe:eabb/64 scope link
       valid_lft forever preferred_lft forever

let me double check this by pinging the new IP address:

# ping 192.168.56.202 -c 4
PING 192.168.56.202 (192.168.56.202) 56(84) bytes of data.
64 bytes from 192.168.56.202: icmp_seq=1 ttl=64 time=0.019 ms
64 bytes from 192.168.56.202: icmp_seq=2 ttl=64 time=0.087 ms
64 bytes from 192.168.56.202: icmp_seq=3 ttl=64 time=0.034 ms
64 bytes from 192.168.56.202: icmp_seq=4 ttl=64 time=0.042 ms

--- 192.168.56.202 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3068ms
rtt min/avg/max/mdev = 0.019/0.045/0.087/0.026 ms

2.a) Adding secondary IP address temporarily in Ubuntu

Run the following IP command to add an additional IP address temporarily to your Ubuntu 16.04 LTS system:

# ip addr add 192.168.56.202/24 broadcast 192.168.56.255 dev enp0s3

Run the below command to check if the new IP is assigned. This will go off when you reboot the machine.

# 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.201/24 brd 192.168.56.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.56.202/24 brd 192.168.56.255 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe97:132e/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:12:e8:c1 brd ff:ff:ff:ff:ff:ff
    inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
       valid_lft 86353sec preferred_lft 86353sec
    inet6 fe80::344b:6259:4dbe:eabb/64 scope link
       valid_lft forever preferred_lft forever

3) How to add secondary IP address permanently in Ubuntu 14.04 LTS

Before adding IP address in the system, just verify the main IP address of the server by running any of the below commands:

# ifconfig
or
# ip addr

eth0      Link encap:Ethernet  HWaddr 08:00:27:98:b7:36 
          inet addr:192.168.56.150  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe98:b736/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:902 (902.0 B)  TX bytes:16423 (16.4 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:6a:cf:d3 
          inet addr:10.0.3.15  Bcast:10.0.3.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe6a:cfd3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:80 errors:0 dropped:0 overruns:0 frame:0
          TX packets:146 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:8698 (8.6 KB)  TX bytes:17047 (17.0 KB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:25 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:1730 (1.7 KB)  TX bytes:1730 (1.7 KB)

From the above output, we can see that the server main IP address is 192.168.56.150 and we will be going to assign next IP address 192.168.56.151 as a secondary IP.

To add secondary IP address permanently on Ubuntu system, edit the /etc/network/interfaces file and add the required IP details.

# vi /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.56.150
        netmask 255.255.255.0
        network 192.168.56.0
        broadcast 192.168.56.255
        gateway 192.168.56.1

auto eth0:1
iface eth0:1 inet static
        address 192.168.56.151
        netmask 255.255.255.0

Save and close the file, then restart the network interface:

# service networking restart
or
# ifdown eth0:1 && ifup eth0:1

Verify the newly added IP address:

# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:98:b7:36 
          inet addr:192.168.56.150  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe98:b736/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:84 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:962 (962.0 B)  TX bytes:11905 (11.9 KB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:98:b7:36 
          inet addr:192.168.56.151  Bcast:192.168.56.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth1      Link encap:Ethernet  HWaddr 08:00:27:6a:cf:d3 
          inet addr:10.0.3.15  Bcast:10.0.3.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe6a:cfd3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4924 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3185 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4037636 (4.0 MB)  TX bytes:422516 (422.5 KB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

3.a) Configuring Additional IP address temporarily in Ubuntu

Run the following IP command to add an additional IP address temporarily to your Ubuntu 14.04 LTS system:

# ip addr add 192.168.56.151/24 broadcast 192.168.56.255 dev eth0 label eth0:1

Use the IP command to check the newly added IP address. If you reboot the system then the newly added IP address is lost as it was added temporarily.

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:b7:36 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.150/24 brd 192.168.56.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.56.151/24 brd 192.168.56.255 scope global secondary eth0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:b736/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:6a:cf:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.3.15/24 brd 10.0.3.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe6a:cfd3/64 scope link
       valid_lft forever preferred_lft forever

Conclusion

In this guide, you have learnt how to add additional IP or Secondary IP on a single network interface in Ubuntu system, from Ubuntu 14.04 LTS to Ubuntu 20.04 LTS.

If you have any questions or feedback, feel free to comment below and we will get back to you as soon as we can.

About Prakash Subramanian

Prakash Subramanian is a Linux lover and has 3.5+ years of experience in linux server administration with major Linux distribution such as (RHEL, CentOS, Ubuntu). He is currently working as a Senior L2 Linux Server administrator.

View all posts by Prakash Subramanian

2 Comments on “How to add additional IP (Secondary IP) in Ubuntu”

  1. Hi, I want to use multi IP for load balancing. (use gobetween)
    For example, I already use 10.1.1.10 for some load balance service. I add IP(10.1.1.11) for my ubuntu, is required restart network for apply the network.
    If I restart the network(netplan apply), Is there no issue at service 10.1.1.10 network? I think 10.1.1.10 network disconnecting for a while and session will be closed.

Leave a Reply

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