Skip to content

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:

  1. The need for a separate permanent IP address for a service. For example, a web server, mail server, etc.

  2. 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.

  3. Provide static IP addresses to virtual machines or containers deployed on a physical server.

  4. 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.

  1. It is necessary to check the name of the network adapter with the command ip a and remember it:

    You can find the name of the connection profile for this adapter with the nmcli connection show command (for example, System ens1, cloud-init ens1 or Wired connection 1).

  2. Then run the command nmtui and select Edit a connection:

  3. Select the existing connection of the main interface in the list and press <Edit...>.

  4. 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.

  5. Exit nmtui and apply the changes with the command:

    nmcli connection up "System ens1"
    

    where System ens1 is the profile name from the nmcli connection show output.

  6. Use the ip a command 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.

  1. Check the interface parameters with the ip a command:

  2. Open the netplan configuration file in the /etc/netplan/ directory (for example, 50-cloud-init.yaml) with any text editor:

  3. In the description of the main interface, where dhcp4: true is already specified, add the addresses parameter with the additional IP address. You do not need to create a separate VLAN:

    network:
      version: 2
      ethernets:
        ens1:
          dhcp4: true
          addresses:
            - 203.0.113.10/25
    

    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.cfg file with the line network: {config: disabled}.

  4. Save the changes and close the editor. Then apply the configuration using the sudo netplan try command (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 the ip a command.

Debian

These instructions apply to Debian 12 and 13. Check the interface name (for example, ens1, eno1, ens18) with the ip a command.

  1. Check the interface parameters with the ip a command:

  2. Open the network configuration file /etc/network/interfaces with any text editor:

  3. Add commands for adding and removing the additional address to the description of the main interface. No gateway is specified for the additional address:

    auto ens1
    iface ens1 inet dhcp
        post-up ip addr add 203.0.113.10/25 dev ens1
        pre-down ip addr del 203.0.113.10/25 dev ens1
    
  4. 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:

    ip addr add 203.0.113.10/25 dev ens1
    

    Then check the network settings with the ip a command.

    Attention

    The systemctl restart networking command 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.

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×