Configure a Static IP Address
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 Copy link
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.70as an example. You need to replace it with your actual IP address. -
Subnet Mask: Add
/24to your IP address. In our example, it will be192.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
eth0orens3. You can check it using the commandip addr.
To continue with the guide, connect to your server via SSH as root.
Ubuntu 18.04 and later, Debian 12 Copy link
- Create the
/etc/netplan/99-ipv4.yamlfile using a text editor:
nano /etc/netplan/99-ipv4.yaml- Update it with the following directives, removing the previous ones. Replace
192.0.2.70/24and192.0.2.1with 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"
- Restrict read permissions on this file to everyone except
root:
chmod 600 /etc/netplan/99-ipv4.yamlDisabling Previous Settings in Ubuntu Copy link
- Check the
/etc/netplan/directory; there may be other interface settings that could conflict with yours.
ls -a /etc/netplan/- Normally, it will have the
50-cloud-init.yamlfile. 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")- Do the same to other files in the
/etc/netplan/, except99-ipv4.yaml, if it exists. - To prevent
cloud-initfrom creating new files, make the following file:
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg- And add this line to it:
network: {config: disabled}Listening on an Address in Ubuntu Copy link
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.
- Open the
/etc/sysctl.conffile:
nano /etc/sysctl.conf- Add this line to it:
net.ipv4.ip_nonlocal_bind = 1- Reload the
sysctlconfiguration:
sysctl -p /etc/sysctl.confApplying Settings in Ubuntu Copy link
- Apply the netplan configuration:
netplan --debug apply- Check that the address has been added to the interface:
ip addrReboot the server and verify that the address is working correctly.
Ubuntu Configuration Example Copy link
For Ubuntu network settings, all configuration files are located in /etc/netplan/. You can see your final configuration by running:
netplan getHere 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, 10, 11 Copy link
- Open the
/etc/network/interfacesfile using a text editor:
nano /etc/network/interfaces- Comment out or remove the current
eth0settings:
#allow-hotplug eth0
#iface eth0 inet dhcp- Add the following lines, replacing
192.0.2.70/24and192.0.2.1with 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.1Disabling Previous Settings in Debian Copy link
- Check if the directory
/etc/network/interfaces.d/has other interface settings that could conflict with yours.
ls -a /etc/network/interfaces.d/- Normally, it will have the
50-cloud-initfile. Move it to another directory:
mv /etc/network/interfaces.d/50-cloud-init /root/50-cloud-init-backup-$(date +"%Y%m%d")- To prevent
cloud-initfrom adding new files, create the99-disable-network-config.cfgfile:
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg- And add the following line to it:
network: {config: disabled}Listening on an Address in Debian Copy link
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.
- Open the /etc/sysctl.conf file:
nano /etc/sysctl.conf- Add the following line to it:
net.ipv4.ip_nonlocal_bind = 1- Reload the
sysctlconfiguration:
sysctl -p /etc/sysctl.confApplying Settings in Debian Copy link
- Restart the networking service:
systemctl restart networking.service- Check that the address has been added to the interface:
ip addrThe 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 Copy link
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.1CentOS 7 and later Copy link
CentOS uses NetworkManager for network configurations. Instead of editing configuration files, you can use the nmcli or nmtui utilities.
- Check the connection name:
nmcli connection showYou 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 ens3If your connection name differs, do not forget to replace ens3 in the commands below with the correct connection name.
- Add the address to the connection:
nmcli connection modify ens3 ipv4.addresses 192.0.2.70/24- Add the gateway:
nmcli connection modify ens3 ipv4.gateway 192.0.2.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 manualDisabling Previous Settings in CentOS Copy link
- Check the
/etc/sysconfig/network-scripts/directory; there may be other interface settings that could conflict with yours.
ls -a /etc/sysconfig/network-scripts/- Normally, there will be an
ifcfg-ens3file. Move the file to another directory, such as/root/:
mv /etc/sysconfig/network-scripts/ifcfg-ens3 /root/ifcfg-ens3-backup-$(date +"%Y%m%d")- To preserve the changes after the server reboots, create the following file:
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg- And add this line to it:
network: {config: disabled}Listening on an Address in CentOS Copy link
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.
- Open the
/etc/sysctl.conffile:
nano /etc/sysctl.conf- Add the following line:
net.ipv4.ip_nonlocal_bind = 1- Reload the
sysctlconfiguration:
sysctl -p /etc/sysctl.confApplying Settings in CentOS Copy link
- Restart the
NetworkManagerservice:
systemctl restart NetworkManager.service- Check that the addresses have been added to the interface:
ip addrReboot the server and verify that the addresses are working correctly.
CentOS Configuration Example Copy link
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]