> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Set a Static IP with Netplan on Ubuntu

> Netplan static IP on Ubuntu: set addresses, default route and nameservers in /etc/netplan/*.yaml, test with sudo netplan try, then keep it on Ubuntu 24.04.

To set a static IP with Netplan on Ubuntu, edit the YAML file in `/etc/netplan/`, set `dhcp4: false`, list your address under `addresses` (for example `203.0.113.10/24`), add a default route with `routes: - to: default via: 203.0.113.1` and your DNS under `nameservers`. Run `sudo netplan try` to test with automatic rollback, then press **Enter** to keep it.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 10 minutes | Ubuntu 22.04 / 24.04 LTS, and Debian images that use Netplan | sudo access and console access in case you lose the connection |

## Before you start

* **Netplan** is Ubuntu's network configuration tool: you describe the network in a YAML file and Netplan hands it to `systemd-networkd` or NetworkManager.
* Get your address, prefix length, gateway and DNS servers. On a VPS they're on the [Network tab of the VPS Panel](https://help.freakhosting.com/portals/vps-panel/how-to-view-and-manage-network-settings.md). A netmask of `255.255.255.0` is prefix `/24`.
* Open the [VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md) before you start. If the network breaks, you can still log in there.
* On AlmaLinux and Rocky Linux, NetworkManager is used instead: see [add IP addresses on Linux](https://help.freakhosting.com/portals/other/how-to-add-additional-ip-addresses-on-linux.md) for `nmcli` commands.

> [!WARNING]
>
> A wrong network file disconnects the server. Always use `sudo netplan try` first: it rolls back automatically after 120 seconds unless you confirm. Keep the VNC console open as a fallback.

## How to set a static IP with Netplan

**1. Find the interface name**

```bash theme={null}
ip -br link
```

Note the name of the interface that is `UP`, for example `eth0`, `ens3` or `enp1s0`.

**2. Find and back up the Netplan file**

```bash theme={null}
ls /etc/netplan/
sudo cp /etc/netplan/50-cloud-init.yaml /root/50-cloud-init.yaml.bak
```

Use the file name you see. Keep the backup outside `/etc/netplan/`, because Netplan reads every `.yaml` file in that folder.

**3. Edit the file**

```bash theme={null}
sudo nano /etc/netplan/50-cloud-init.yaml
```

Replace the interface section with your values. Use spaces, never tabs:

```yaml theme={null}
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 203.0.113.10/24
      routes:
        - to: default
          via: 203.0.113.1
      nameservers:
        addresses: [1.1.1.1, 9.9.9.9]
```

**4. Set safe permissions**

```bash theme={null}
sudo chmod 600 /etc/netplan/*.yaml
```

Netplan warns that the configuration is "too open" if other users can read the file.

**5. Test the configuration**

```bash theme={null}
sudo netplan try
```

Netplan applies the change and shows `Press ENTER before the timeout to accept the new configuration`. Open a **new** SSH session to the server. If it works, press **Enter** in the first one. If you can't connect, do nothing: the old configuration comes back after 120 seconds.

**6. Apply permanently (if needed)**

If you accepted in `netplan try`, the change is already active. After editing the file again later, you can apply directly with:

```bash theme={null}
sudo netplan apply
```

## How to check it worked

```bash theme={null}
ip -br addr show eth0
ip route show default
resolvectl status eth0
```

```text theme={null}
eth0             UP             203.0.113.10/24
default via 203.0.113.1 dev eth0 proto static
```

Then test the internet and name lookups with `ping -c 3 1.1.1.1` and `ping -c 3 ubuntu.com`.

## What if the gateway is outside my subnet?

Some networks give a single address (`/32`) or a gateway that isn't inside your address range. Netplan then needs `on-link: true` on the route so the server knows the gateway is directly reachable:

```yaml theme={null}
          addresses:
            - 203.0.113.10/32
          routes:
            - to: default
              via: 198.51.100.1
              on-link: true
```

Only use this when your provider's details show such a gateway.

## Why does Netplan say `gateway4` is deprecated?

Older guides use `gateway4: 203.0.113.1`. Current Netplan still reads it but warns: "`gateway4` has been deprecated, use default routes instead". Replace it with the `routes: - to: default` block shown above. Don't use both: Netplan then reports conflicting default routes and refuses the file.

## Will cloud-init overwrite my changes?

If the file is called `50-cloud-init.yaml` and starts with a comment saying it was generated by cloud-init, cloud-init may write it again. To stop cloud-init from managing the network, create a file in `/etc/cloud/cloud.cfg.d/` with this content, as described in the [cloud-init network documentation](https://docs.cloud-init.io/en/latest/reference/network-config.html):

```bash theme={null}
echo 'network: {config: disabled}' | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
```

## Frequently asked questions

### Where is the Netplan config file on Ubuntu?

In `/etc/netplan/`. The name varies, for example `50-cloud-init.yaml`, `00-installer-config.yaml` or `01-netcfg.yaml`. Netplan reads all `.yaml` files there in alphabetical order.

### What is the difference between `netplan try` and `netplan apply`?

`netplan try` applies the change and reverts it automatically unless you confirm within the timeout (120 seconds by default; change it with `--timeout`). `netplan apply` applies it immediately with no rollback.

### How do I add IPv6 or a second IP?

Add more lines under `addresses`, for example `- 203.0.113.11/24` or `- "2001:db8::10/64"`. See [add IP addresses on Linux](https://help.freakhosting.com/portals/other/how-to-add-additional-ip-addresses-on-linux.md) and [configure IPv6 on Linux](https://help.freakhosting.com/portals/other/how-to-configure-ipv6-on-linux.md).

### Netplan gives "Invalid YAML". What's wrong?

Usually indentation. YAML needs consistent spaces (no tabs), and each level indented further than its parent. Compare with the example above, or run `sudo netplan generate` to see the line with the error.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Add more IP addresses](https://help.freakhosting.com/portals/other/how-to-add-additional-ip-addresses-on-linux.md)
>
> Netplan, NetworkManager and ifupdown.

> [card] [Configure IPv6](https://help.freakhosting.com/portals/other/how-to-configure-ipv6-on-linux.md)
>
> Add an IPv6 address and route.

> [card] [Change DNS resolvers](https://help.freakhosting.com/portals/other/how-to-change-dns-resolvers-on-linux.md)
>
> Set the nameservers your server uses.

> [card] [VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md)
>
> Log in when the network is down.

***

> [!NOTE]
>
> **Checked against the official [Netplan documentation](https://netplan.readthedocs.io/en/stable/using-static-ip-addresses/) and [Ubuntu Server networking guide](https://ubuntu.com/server/docs/explanation/networking/configuring-networks/)** on 4 November 2026; the YAML examples were validated with `netplan generate` (Netplan 1.1.2) on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.