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.
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.
/etc/netplan/99-ipv4.yaml file using a text editor:nano /etc/netplan/99-ipv4.yaml
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"
root:chmod 600 /etc/netplan/99-ipv4.yaml
/etc/netplan/ directory; there may be other interface settings that could conflict with yours.ls -a /etc/netplan/
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")
/etc/netplan/, except 99-ipv4.yaml, if it exists.cloud-init from creating new files, make the following file:nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}
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.
/etc/sysctl.conf file:nano /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
sysctl configuration:sysctl -p /etc/sysctl.conf
netplan --debug apply
ip addr
Reboot the server and verify that the address is working correctly.
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"
Starting from Debian 12, Debian uses Netplan for network configuration. If you have Debian 12, use the previous section of this article.
/etc/network/interfaces file using a text editor:nano /etc/network/interfaces
eth0 settings: #allow-hotplug eth0
#iface eth0 inet dhcp
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
/etc/network/interfaces.d/ has other interface settings that could conflict with yours.ls -a /etc/network/interfaces.d/
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")
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
network: {config: disabled}
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.
nano /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
sysctl configuration:sysctl -p /etc/sysctl.conf
systemctl restart networking.service
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 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 uses NetworkManager for network configurations. Instead of editing configuration files, you can use the nmcli or nmtui utilities.
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.
nmcli connection modify ens3 ipv4.addresses 192.0.2.70/24
nmcli connection modify ens3 ipv4.gateway 192.0.2.1
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
/etc/sysconfig/network-scripts/ directory; there may be other interface settings that could conflict with yours. ls -a /etc/sysconfig/network-scripts/
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")
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}
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.
/etc/sysctl.conf file:nano /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
sysctl configuration:sysctl -p /etc/sysctl.conf
NetworkManager service:systemctl restart NetworkManager.service
ip addr
Reboot the server and verify that the addresses are working correctly.
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]