Ga naar de hoofdinhoud

How to add additional static IP’s

Lincoln de Simone
Gepubliceerd op November 28, 2019

Identify the main interface

If you want to add a secondary IP, this will be added in addition to your main interface. We will need to identify the main interface to setup additional IP addresses.

Locate the interface with your current main IP address. Use command ip a to show all the network interfaces that are currently in use. Assume that 192.168.1.50 is your main IP and you want to add 192.168.1.51.

root@worldtream:~# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether aa:aa:0a:a1:11:ee brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.50/24 brd 192.168.1.255 scope global eno2
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN glen 1000
    link/ether aa:aa:0a:a1:11:ed brd ff:ff:ff:ff:ff:ff

In the above example we see that eth0 is currently using 192.168.1.50, this is our main interface.

On the next pages you can find examples of a secondary IP address added, sorted by operating system.

CentOS 6.x / 7.x
Ubuntu 14.x / Ubuntu 16.x
Ubuntu 17.x / 18.x / 20.x

Centos 6 / 7

CentOS works with an interface file per physical connection. You can find the interface files in: /etc/sysconfig/network-scripts/ifcfg-xx. Where xx is the name of the interface. If your interface is called eth0, we will create a file for eth0:1. We recommend to use ‘nano’ to edit the file:

nano /etc/sysconfig/network-scripts/ifcfg-eth0:1

Copy all the text below and add this to the opened file, adjust the highlighted so it fits your situation:

DEVICE=eth0:1
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.51
NETMASK=255.255.255.0

Note: the device is called eth0:1, while the file is also called eth0:1. This is intentional. When you exit the text editor you can apply the changes by using the following command:

ifup eth0:1

Should you want to add more static addresses you can follow the logic and add the next static IP on eth0:2.

If you are unsure if your configuration is correct feel free to contact us here.


Ubuntu 14 / 16

Ubuntu works with a single network configuration file. You can find the file at /etc/network/interfaces. Now we will edit the configuration file, we recommend to use ‘nano’ to edit the file:

nano /etc/network/interfaces

Copy the text at eth0:1 and edit the bold text that fits your situation:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.50
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 93.190.136.10 217.23.0.121

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

When you exit the text editor you can apply the changes by using the following command:

ifup eth0:1

Should you want to add more static addresses you can follow the logic by repeating the steps above and use eth0:2 instead.

If you are unsure if your configuration is correct feel free to contact us here.


Ubuntu 17 / 18 / 20

From Ubuntu 17.10 and on the /etc/network/interfaces file is no longer used. It uses Netplan. This configuration file is found at /etc/netplan. Now we will edit the configuration file, we recommend to use ‘nano’ to edit the file:

nano /etc/netplan/01-netcfg.yaml

Adding a static IP in the netplan file is easier than you might think. Just add a comma after the current address and add the new one. We have highlighted this.

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
 version: 2
 renderer: networkd
 ethernets:
   ens3:
     dhcp4: no
     dhcp6: no
     addresses: [192.168.1.50/24,192.168.1.51/24]
     gateway4: 192.168.0.1
     nameservers:
       addresses: [93.190.136.10,217.23.0.121]

To apply the new configuration, apply with the following in the command line:

netplan apply

If you want to add more IP addresses, simply add this after the one that was just added and separate by using a comma. Do not add any spaces in between.

Note: if you receive an error, please check the configuration as netplan is very space and character sensitive. If you don’t use the exact amount of spaces in the above example it will fail.

If you are unsure if your configuration is correct feel free to contact us here.

Learn more about our services: worldstream.com