Sign In
Sign In

Configure a Static IP Address

Updated on 29 August 2024

If your server sometimes becomes unavailable without any apparent reason, the issue might be related to the server losing its IP address. 

By default, the server is assigned a dynamic IP address, which has a lease time. Before this lease time expires, the server must request a new IP address, and then DHCP will automatically assign the same IP. 

However, some Linux distributions request the IP address slightly later than needed (after the lease time has expired), causing the IP address to be temporarily lost.

To fix this issue, you need to configure a static IP on your server.

Required Parameters

To follow the steps below, you will need the parameters: 

  • IP Address: This is the IP address of your server. We will use 192.0.2.70 as an example. You need to replace it with your actual IP address.

  • Subnet Mask: Add /24 to your IP address. In our example, it will be 192.0.2.70/24.

  • Gateway: This is the first address in the network; you just need to change the last number of the IP address to 1. In our example: 192.0.2.1.

  • Interface: The interface name will vary depending on the distribution. For example, it could be eth0 or ens3. You can check it using the command ip addr.

To continue with the guide, connect to your server via SSH as root. 

Ubuntu 18.04 and later

  1. Create the /etc/netplan/99-ipv4.yaml file using a text editor:
nano /etc/netplan/99-ipv4.yaml
  1. Update it with the following directives, removing the previous ones. Replace 192.0.2.70/24 and 192.0.2.1 with your actual IP address and gateway:
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - "192.0.2.70/24"
      routes:
        - to: "0.0.0.0/0"
          via: "192.0.2.1"
      nameservers:
        addresses:
          - "1.1.1.1"
          - "1.0.0.1"
  1. Restrict read permissions on this file to everyone except root:
chmod 600 /etc/netplan/99-ipv4.yaml

Disabling Previous Settings in Ubuntu

  1. Check the /etc/netplan/ directory; there may be other interface settings that could conflict with yours.
ls -a /etc/netplan/
  1. Normally, it will have the 50-cloud-init.yaml file. Rename it so it doesn't end with .yaml:
mv /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml-backup-$(date +"%Y%m%d")
  1. Do the same to other files in the /etc/netplan/, except 99-ipv4.yaml, if it exists.
  2. To prevent cloud-init from creating new files, make the following file:
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
  1. And add this line to it:
network: {config: disabled}

Listening on an Address in Ubuntu

After rebooting the server, an address might not be added in time when other services want to listen on it. Therefore, we allow binding to addresses that are not yet available on the interface.

  1. Open the /etc/sysctl.conf file:
nano /etc/sysctl.conf
  1. Add this line to it:
net.ipv4.ip_nonlocal_bind = 1
  1. Reload the sysctl configuration:
sysctl -p /etc/sysctl.conf

Applying Settings in Ubuntu

  1. Apply the netplan configuration:
netplan --debug apply
  1. Check that the address has been added to the interface:
ip addr

Reboot the server and verify that the address is working correctly.

Ubuntu Configuration Example

For Ubuntu network settings, all configuration files are located in /etc/netplan/. You can see your final configuration by running:

netplan get

Here is an example of the configuration if you follow each step in this guide to configure the 166.1.227.252 IP address:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
      - "166.1.227.252/24"
      nameservers:
        addresses:
        - "1.1.1.1"
        - "1.0.0.1"
      dhcp4: false
      routes:
      - to: "0.0.0.0/0"
      via: "166.1.227.1"

Debian 9 and later

  1. Open the /etc/network/interfaces file using a text editor:
nano /etc/network/interfaces
  1. Comment out or remove the current eth0 settings: 
#allow-hotplug eth0
#iface eth0 inet dhcp
  1. Add the following lines, replacing 192.0.2.70/24 and 192.0.2.1 with your actual IP address and gateway:
auto eth0
allow-hotplug eth0
iface eth0 inet static
	address 192.0.2.70/24
gateway 192.0.2.1

Disabling Previous Settings in Debian

  1. Check if the directory /etc/network/interfaces.d/ has other interface settings that could conflict with yours.
ls -a /etc/network/interfaces.d/
  1. Normally, it will have the 50-cloud-init file. Move it to another directory:
mv /etc/network/interfaces.d/50-cloud-init /root/50-cloud-init-backup-$(date +"%Y%m%d")
  1. To prevent cloud-init from adding new files, create the 99-disable-network-config.cfg file:
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
  1. And add the following line to it:
network: {config: disabled}

Listening on an Address in Debian

After rebooting the server, an address might not be added in time when other services want to listen on it. Therefore, we allow binding to addresses that are not yet available on the interface.

  1. Open the /etc/sysctl.conf file:
nano /etc/sysctl.conf
  1. Add the following line to it:
net.ipv4.ip_nonlocal_bind = 1
  1. Reload the sysctl configuration:
sysctl -p /etc/sysctl.conf

Applying Settings in Debian

  1. Restart the networking service:
systemctl restart networking.service
  1. Check that the address has been added to the interface:
ip addr

The IP address you configured should be listed under the inet section of the eth0 block.

Reboot the server and verify that the address is working correctly.

Debian Configuration Example

Debian uses the /etc/network/interfaces file for network settings.

Here is an example of the configuration if you follow each step in this guide to configure the 166.1.227.252 IP address:

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet static
    address 166.1.227.252/24
    gateway 166.1.227.1

CentOS 7 and later

CentOS uses NetworkManager for network configurations. Instead of editing configuration files, you can use the nmcli or nmtui utilities.

  1. Check the connection name:
nmcli connection show

You need the value from the NAME column — in our case, it is ens3. This name will be used in the following commands:

NAME  UUID                                  TYPE      DEVICE
ens3  xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  ethernet  ens3

If your connection name differs, do not forget to replace ens3 in the commands below with the correct connection name.

  1. Add the address to the connection:
nmcli connection modify ens3 ipv4.addresses 192.0.2.70/24
  1. Add the gateway:
nmcli connection modify ens3 ipv4.gateway 192.0.2.1
  1. Copy and execute these commands. They set DNS configurations and specify manual configuration:
nmcli connection modify ens3 ipv4.dns 1.1.1.1
nmcli connection modify ens3 +ipv4.dns 1.0.0.1
nmcli connection modify ens3 ipv4.method manual

Disabling Previous Settings in CentOS

  1. Check the /etc/sysconfig/network-scripts/ directory; there may be other interface settings that could conflict with yours. 
ls -a /etc/sysconfig/network-scripts/
  1. Normally, there will be an ifcfg-ens3 file. Move the file to another directory, such as /root/:
mv /etc/sysconfig/network-scripts/ifcfg-ens3 /root/ifcfg-ens3-backup-$(date +"%Y%m%d")
  1. To preserve the changes after the server reboots, create the following file: 
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
  1. And add this line to it:
network: {config: disabled}

Listening on an Address in CentOS

After rebooting the server, an address might not be added in time when other services want to listen on it. Therefore, allow binding to addresses that are not yet available on the interface.

  1. Open the /etc/sysctl.conf file:
nano /etc/sysctl.conf
  1. Add the following line:
net.ipv4.ip_nonlocal_bind = 1
  1. Reload the sysctl configuration:
sysctl -p /etc/sysctl.conf

Applying Settings in CentOS

  1. Restart the NetworkManager service:
systemctl restart NetworkManager.service
  1. Check that the addresses have been added to the interface:
ip addr

Reboot the server and verify that the addresses are working correctly.

CentOS Configuration Example

In the latest versions of CentOS, NetworkManager saves its settings in the /etc/NetworkManager/system-connections/ directory. 

Here is an example of the final configuration content in /etc/NetworkManager/system-connections/ when configuring the 166.1.227.252 IP address with this guide:

[connection]
id=ens3
uuid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
type=ethernet
interface-name=ens3
timestamp=1719197216

[ethernet]

[ipv4]
address1=192.0.2.70/24,192.0.2.1
dns=1.1.1.1;1.0.0.1;
method=manual

[proxy]
Was this page helpful?
Updated on 29 August 2024

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us