Setting additional static IP address to interface with DHCP¶
In this article
Adding an additional static IP address to an interface that already receives a primary IP via DHCP can be useful in a number of cases:
-
The need for a separate permanent IP address for a service. For example, a web server, mail server, etc.
-
Separate traffic from different services or applications using IP addresses on the same interface. For example, a separate IP for web traffic and a separate IP for databases.
-
Provide static IP addresses to virtual machines or containers deployed on a physical server.
-
Provide redundancy - assigning a secondary IP address to switch to if the primary address is unavailable.
Information
An additional IP address is ordered separately - see Is it possible to purchase an additional static ip address?. Use the address and mask (prefix) issued to you for the new IP. You do not need to specify a gateway for the additional address: the default route stays on the primary address received via DHCP.
Attention
An error in the network settings can make the server unreachable via SSH. It is recommended to make changes while having access to the remote access console in Invapi.
Rocky/Alma/CentOS¶
These instructions apply to AlmaLinux and Rocky Linux 8-10, CentOS Stream, and also CentOS 7 / CentOS 8 if the network is managed by NetworkManager. The additional address is added to the existing connection profile of the main interface - you do not need to create a separate interface or VLAN.
-
It is necessary to check the name of the network adapter with the command
ip aand remember it:
You can find the name of the connection profile for this adapter with the
nmcli connection showcommand (for example,System ens1,cloud-init ens1orWired connection 1). -
Then run the command
nmtuiand select Edit a connection:
-
Select the existing connection of the main interface in the list and press <Edit...>.
-
In the IPv4 CONFIGURATION section, press <Show>. Leave the <Automatic> mode unchanged, then in the Addresses field press <Add...> and enter the additional IP address with its prefix, for example
203.0.113.10/25. Do not change the Gateway and DNS servers fields. Press <OK>.Note
If the primary address is configured manually (<Manual> mode), simply add one more line to the Addresses list.
-
Exit
nmtuiand apply the changes with the command:where
System ens1is the profile name from thenmcli connection showoutput. -
Use the
ip acommand to make sure both addresses are present on the interface.
You can get the same result without nmtui with a single command:
nmcli connection modify "System ens1" +ipv4.addresses 203.0.113.10/25 && nmcli connection up "System ens1"
Note
In CentOS 7 / CentOS 8 without NetworkManager (the NM_CONTROLLED=no parameter is set or the network service is used), the additional address is set in the /etc/sysconfig/network-scripts/ifcfg-<interface_name> file with the IPADDR1= and PREFIX1= parameters, after which you run the systemctl restart network command.
Ubuntu (netplan)¶
These instructions apply to Ubuntu 22.04, 24.04 and 26.04.
-
Check the interface parameters with the
ip acommand:
-
Open the
netplanconfiguration file in the/etc/netplan/directory (for example,50-cloud-init.yaml) with any text editor:
-
In the description of the main interface, where
dhcp4: trueis already specified, add theaddressesparameter with the additional IP address. You do not need to create a separate VLAN:Note
If the file was created by cloud-init, your changes may be overwritten. In this case, create the
/etc/cloud/cloud.cfg.d/99-disable-network-config.cfgfile with the linenetwork: {config: disabled}. -
Save the changes and close the editor. Then apply the configuration using the
sudo netplan trycommand (if the connection is lost, the settings are automatically rolled back after 120 seconds; if they are applied successfully, press Enter) and make sure the settings are correct using theip acommand.
Debian¶
These instructions apply to Debian 12 and 13. Check the interface name (for example, ens1, eno1, ens18) with the ip a command.
-
Check the interface parameters with the
ip acommand:
-
Open the network configuration file
/etc/network/interfaceswith any text editor:
-
Add commands for adding and removing the additional address to the description of the main interface. No gateway is specified for the additional address:
-
To avoid restarting the network while connected via SSH, add the address to the current session manually - after a reboot it will be assigned from the configuration file:
Then check the network settings with the
ip acommand.Attention
The
systemctl restart networkingcommand may drop the SSH connection, and an error in the file may leave the server without network access. Run it only if you have access to the console.