---
title: "Configure a Static IP Address on Your Server | Hostman Docs"
description: "Learn how to set up and configure a static IP address on Ubuntu using Hostman’s secure step-by-step server guide."
---

> For the complete documentation index for AI agents, see [llms.txt](https://hostman.com/llms.txt).

If your [server](https://hostman.com/cloud-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, Debian 12

1.  Create the `/etc/netplan/99-ipv4.yaml` file using a text editor:

```shell
nano /etc/netplan/99-ipv4.yaml
```

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

```yml
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"
```

3.  Restrict read permissions on this file to everyone except `root`:

```shell
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.

```shell
ls -a /etc/netplan/
```

2.  Normally, it will have the `50-cloud-init.yaml` file. Rename it so it doesn't end with `.yaml`:

```shell
mv /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml-backup-$(date +"%Y%m%d")
```

3.  Do the same to other files in the `/etc/netplan/`, except `99-ipv4.yaml`, if it exists.
4.  To prevent `cloud-init` from creating new files, make the following file:

```shell
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
```

5.  And add this line to it:

```shell
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:

```shell
nano /etc/sysctl.conf
```

2.  Add this line to it:

```shell
net.ipv4.ip_nonlocal_bind = 1
```

3.  Reload the `sysctl` configuration:

```shell
sysctl -p /etc/sysctl.conf
```

### Applying Settings in Ubuntu

1.  Apply the netplan configuration:

```shell
netplan --debug apply
```

2.  Check that the address has been added to the interface:

```shell
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:

```shell
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:

```yml
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

> [!NOTE]
> Starting from Debian 12, Debian uses Netplan for network configuration. If you have Debian 12, use the [previous section](https://hostman.com/docs/cloud-servers/configure-static-ip/#ubuntu-18-04-and-later) of this article.

1.  Open the `/etc/network/interfaces` file using a text editor:

```shell
nano /etc/network/interfaces
```

2.  Comment out or remove the current `eth0` settings: 

```shell
#allow-hotplug eth0
#iface eth0 inet dhcp
```

3.  Add the following lines, replacing `192.0.2.70/24` and `192.0.2.1` with your actual IP address and gateway:

```shell
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.

```shell
ls -a /etc/network/interfaces.d/
```

2.  Normally, it will have the `50-cloud-init` file. Move it to another directory:

```shell
mv /etc/network/interfaces.d/50-cloud-init /root/50-cloud-init-backup-$(date +"%Y%m%d")
```

3.  To prevent `cloud-init` from adding new files, create the `99-disable-network-config.cfg` file:

```shell
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
```

4.  And add the following line to it:

```shell
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:

```shell
nano /etc/sysctl.conf
```

2.  Add the following line to it:

```shell
net.ipv4.ip_nonlocal_bind = 1
```

3.  Reload the `sysctl` configuration:

```shell
sysctl -p /etc/sysctl.conf
```

### Applying Settings in Debian

1.  Restart the networking service:

```shell
systemctl restart networking.service
```

2.  Check that the address has been added to the interface:

```shell
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:

```shell
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:

```shell
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.

2.  Add the address to the connection:

```shell
nmcli connection modify ens3 ipv4.addresses 192.0.2.70/24
```

3.  Add the gateway:

```shell
nmcli connection modify ens3 ipv4.gateway 192.0.2.1
```

4.  Copy and execute these commands. They set DNS configurations and specify manual configuration:

```shell
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. 

```shell
ls -a /etc/sysconfig/network-scripts/
```

2.  Normally, there will be an `ifcfg-ens3` file. Move the file to another directory, such as `/root/`:

```shell
mv /etc/sysconfig/network-scripts/ifcfg-ens3 /root/ifcfg-ens3-backup-$(date +"%Y%m%d")
```

3.  To preserve the changes after the server reboots, create the following file: 

```shell
nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
```

4.  And add this line to it:

```shell
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:

```shell
nano /etc/sysctl.conf
```

2.  Add the following line:

```shell
net.ipv4.ip_nonlocal_bind = 1
```

3.  Reload the `sysctl` configuration:

```shell
sysctl -p /etc/sysctl.conf
```

### Applying Settings in CentOS

1.  Restart the `NetworkManager` service:

```shell
systemctl restart NetworkManager.service
```

2.  Check that the addresses have been added to the interface:

```shell
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:

```shell
[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]
```
