How to Set Up Multiple IP Addresses on a Linux Cloud Server?

25-01-2024 02:20:47

When a cloud server is provisioned, it typically comes with one IP address provided for free. Most cloud servers, across various countries and availability zones offered by Chaomo Data, support multiple IP addresses. Due to differences in underlying architecture, some zones require manual configuration of these additional IP addresses in the operating system. This article demonstrates the process for setting up multiple IPv4 and IPv6 addresses on various Linux operating systems.

Note: For cloud servers in the V zone, after setting multiple IPs, a restart (not a simple reboot) from the cloud server management console is necessary for the new IPs to become effective.

CentOS 8

Taking the example of adding 3 IPv4 addresses and 2 IPv6 addresses, the network adapter is named ens3. Modify the content of the file /etc/sysconfig/network-scripts/ifcfg-ens3 as follows.

TYPE="Ethernet"
DEVICE="ens3"
ONBOOT="yes"
BOOTPROTO="none"
IPADDR=192.0.2.101
PREFIX=23
GATEWAY=192.0.2.1
DNS1=192.0.2.200
IPADDR1=192.0.2.102
PREFIX1=32
IPADDR2=192.0.2.103
PREFIX2=32
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6ADDR_SECONDARIES="2001:db8:1000::100 2001:db8:1000::200" 

Restart the network or the operating system.

# nmcli con load /etc/sysconfig/network-scripts/ifcfg-ens3
# nmcli con up 'System ens3'

CentOS 7

For adding 3 IPv4 addresses and 2 IPv6 addresses, with the network adapter named eth0. To add the first IP address, modify the /etc/sysconfig/network-scripts/ifcfg-eth0 file as follows.

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.0.2.101
NETMASK=255.255.254.0
GATEWAY=192.0.2.1
DNS1=192.0.2.200

IPV6INIT=yes
IPV6ADDR="2001:db8:1000::100/64"
IPV6_AUTOCONF="yes"
IPV6ADDR_SECONDARIES="2001:db8:1000::200/64" 
DNS2=2001:db8:1000::1

Set the default route for eth0. Modify the /etc/sysconfig/network-scripts/route-eth0 file as follows.

169.254.0.0/16 dev eth0

To add the second IPv4 address, modify the /etc/sysconfig/network-scripts/ifcfg-eth0:1 file as follows.

DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.0.2.102
NETMASK=255.255.255.255
ONBOOT=yes

To add the third IPv4 address, modify the /etc/sysconfig/network-scripts/ifcfg-eth0:2 file as follows.

DEVICE=eth0:2
BOOTPROTO=static
IPADDR=192.0.2.103
NETMASK=255.255.255.255
ONBOOT=yes

Restart the network or the operating system.

# service network restart

For adding a range of contiguous IP addresses, a quicker method is available. For example, to add the range 192.0.2.0-256, modify the /etc/sysconfig/network-scripts/ifcfg-eth0-range0 file as follows, then restart the network or the operating system.

IPADDR_START=192.0.2.0
IPADDR_END=192.0.2.256
CLONENUM_START=0
NETMASK=255.255.255.0

Note: Using the above method for adding a range of IP addresses requires disabling NetworkManager.

systemctl stop NetworkManager
systemctl disable NetworkManager

Ubuntu 18-20

For adding 3 IPv4 addresses and 2 IPv6 addresses, with the network adapter named ens3. Modify the /etc/netplan/10-ens3.yaml file as follows.

network:
version: 2
renderer: networkd
ethernets:
    ens3:
    dhcp4: no
    addresses: [192.0.2.101/23,192.0.2.102/32,192.0.2.103/32,'2001:db8:1000::100/64','2001:db8:1000::200/64']
    gateway4: 192.0.2.1
    nameservers:
        addresses: [192.0.2.200]
    routes:
    - to: 169.254.0.0/16
        via: 192.0.2.1
        metric: 100

Restart the network or the operating system.

# netplan apply

For adding a range of contiguous IP addresses or a subnet, a quicker method is available. For example, to add the range 192.0.2.0/24, modify the /etc/network/interfaces file as follows, then restart the network or the operating system.

iface eth0 inet static
address 192.0.2.0/24

Debian 9, Debian 10

For adding 3 IPv4 addresses and 2 IPv6 addresses, with the network adapter named ens3. Modify the /etc/network/interfaces file as follows.

auto lo
iface lo inet loopback

auto ens3
iface ens3 inet static
    address 192.0.2.101
    netmask 255.255.254.0
    gateway 192.0.2.1
    dns-nameservers 192.0.2.200
    post-up ip route add 169.254.0.0/16 dev ens3

iface ens3 inet6 static
    address 2001:db8:1000::100
    netmask 64
    up /sbin/ip -6 addr add dev ens3 2001:db8:1000::200
    dns-nameservers 2001:db8:1000::1

auto ens3:1
iface ens3:1 inet static
    address 192.0.2.102
    netmask 255.255.255.255

auto ens3:2
iface ens3:2 inet static
    address 192.0.2.103
    netmask 255.255.255.255

Restart the network or the operating system.

# systemctl restart networking.service

For adding a range of contiguous IP addresses or a subnet, a quicker method is available. For example, to add the range 192.0.2.0/24, modify the /etc/network/interfaces file as follows, then restart the network or the operating system.

iface eth0 inet static
address 192.0.2.0/24

Fedora 29-32

For adding 3 IPv4 addresses and 2 IPv6 addresses, with the network adapter named ens3. Modify the /etc/network/interfaces file as follows.

# nmcli con add con-name public-net ifname ens3 type ethernet ipv4.method 'manual' ipv4.addresses '192.0.2.101/23' ipv4.gateway '192.0.2.1' ipv4.dns ''
# nmcli con mod public-net +ipv4.addresses '192.0.2.102'
# nmcli con mod public-net +ipv4.addresses '192.0.2.103'
# nmcli con mod public-net +ipv4.dns '192.0.2.200'

Set dynamic IPv6 and add an IPv6 address; the method for adding additional IPv6 addresses is similar.

# nmcli con mod public-net ipv6.method 'auto' ipv6.addresses ''
# nmcli con mod public-net +ipv6.addresses '2001:db8:1000::200/128'

Restart the network or the operating system.

# nmcli con up public-net

FreeBSD

For adding 3 IPv4 addresses and 2 IPv6 addresses, with the network adapter named vtnet0. Modify the /etc/rc.conf file as follows.

static_routes="linklocal"
route_linklocal="-net 169.254.0.0/16 -interface vtnet0"
ifconfig_vtnet0="inet 192.0.2.101 netmask 255.255.254.0"
defaultrouter="192.0.2.1"
ifconfig_vtnet0_alias0="192.0.2.102 netmask 255.255.255.255"
ifconfig_vtnet0_alias1="192.0.2.103 netmask 255.255.255.255"

rtsold_enable="YES"
ipv6_activate_all_interfaces="YES"
rtsold_flags="-aF"
ifconfig_vtnet0_ipv6="inet6 2001:db8:1000::100 prefixlen 64"
ifconfig_vtnet0_alias0="inet6 2001:db8:1000::200 prefixlen 64" 

Restart the network or the operating system.

# service rtsold start
# reboot