Linux & Open SourceTechnology

How to Set a Static IP on Ubuntu Server 24.04 Using Netplan

Admin
How to Set a Static IP on Ubuntu Server 24.04 Using Netplan

Ubuntu Server 24.04 is a great choice for a home lab or small business machine, until its address changes and none of your services can be found. Setting a static IP with Netplan fixes that, but only if you edit the right file, use the right spacing, and apply the change in the right order. One wrong indent can leave the server with no network at all. This guide shows the correct method, step by step, with checks before and after.

Quick Answer

Edit the Netplan YAML file in /etc/netplan/, set dhcp4: false for your interface, and add your chosen address with its prefix, a gateway using routes, and DNS servers under nameservers. Test the file with sudo netplan try, then apply it with sudo netplan apply and confirm the result with ip a and a ping test.

How Netplan Works on Ubuntu Server 24.04

Netplan is a simple way to describe the network you want, using YAML files. When you apply it, Netplan hands those instructions to the real backend, which on Ubuntu Server is usually systemd-networkd. That is why a small YAML mistake matters: Netplan cannot pass on instructions it cannot read.

The files live in /etc/netplan/. Common names are 50-cloud-init.yaml and 00-installer-config.yaml. If there are several files, they are read together in order, so check all of them.

Before you touch anything, decide on an address that will not clash. Look at your router’s DHCP range first. If the router hands out addresses from 192.168.1.100 to 192.168.1.200, pick a static address outside that range, such as 192.168.1.50, but still inside your network.

What You Need Before You Start

  • The name of your network interface, such as eth0, ens3, or enp0s3.
  • A free static IP address for the server, for example 192.168.1.50.
  • The subnet prefix, which for most home networks is /24, meaning a mask of 255.255.255.0.
  • Your gateway address, usually your router, for example 192.168.1.1.
  • One or two DNS servers, such as your router address or a public resolver you trust.
  • Local console access if possible. If you change a remote server’s address over SSH, your session will drop when the address changes, which is normal but can be worrying if you are not ready for it.

Step 1: Find Your Interface and Current Settings

  1. Show your interfaces and addresses with ip a. Note the interface that has your current working address.
  2. Show the current route with ip route. The line starting with default tells you the gateway in use now.
  3. List the Netplan files with ls /etc/netplan/.
  4. Read the main file with sudo cat /etc/netplan/50-cloud-init.yaml, adjusting the name to match yours.
  5. Back it up before editing with sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/backup.yaml.bak, or copy it to your home folder where Netplan will not try to load it as a live config.

A file ending in .yaml inside /etc/netplan/ may still be read by Netplan, so store your backup elsewhere with a different ending. If you also manage desktop machines, our guide on how to set a static IP address on Ubuntu step by step covers the desktop method too.

Step 2: Write the Correct Netplan YAML

Open the file in a text editor with sudo nano /etc/netplan/50-cloud-init.yaml. Replace the whole network block for your interface with a clear static setup. The example below uses interface eth0, address 192.168.1.50/24, gateway 192.168.1.1, and two DNS servers. Change every value to match your own network.

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 192.168.1.50/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 1.1.1.1]

Three details matter here. First, indentation must use spaces, never tab characters, and each level must line up exactly. YAML is strict about this. Second, the address needs its prefix, so write /24 at the end, not just the address alone. Third, Ubuntu 24.04 uses the routes method for the default gateway. The older gateway4 setting is deprecated and should not be used in a new file.

Save the file in nano with Ctrl+O, press Enter, then exit with Ctrl+X. Then set safe permissions with sudo chmod 600 /etc/netplan/50-cloud-init.yaml. Newer Ubuntu versions warn if these files are readable by everyone, because they can contain Wi-Fi passwords on some systems.

The full syntax is described in the official Netplan YAML reference documentation, and Ubuntu’s own help is in the Ubuntu Server guide to configuring networks. Check those if your setup has VLANs, bonds, or bridges.

Step 3: Test Safely With Netplan Try

Never apply a network change blind, especially on a server you reach remotely. Netplan has a built-in safety net.

  1. Check the file for basic generation errors with sudo netplan generate. If it reports a line or column, fix that spot first.
  2. Run sudo netplan try. This applies the change for a short countdown and asks you to press Enter to keep it.
  3. If you are at the local console, confirm quickly if the prompt looks correct. If you are remote and the address changes, your SSH session will disconnect, so do not rely on try alone for a remote address change.
  4. For a remote server, consider keeping a console session open through your provider or a local screen, or schedule the change when someone is beside the machine.
  5. Once tested, apply for real with sudo netplan apply.

If netplan try times out without confirmation, it rolls back to the old settings. That rollback has saved many servers from a typo, so use it every time.

Step 4: Verify the New Address Really Works

  1. Run ip a and confirm your interface now shows 192.168.1.50/24, or your chosen address, and no longer shows the old DHCP address as the main address.
  2. Run ip route and confirm the default route points through your gateway.
  3. Ping the gateway with ping -c 4 192.168.1.1.
  4. Ping an outside address by number with ping -c 4 1.1.1.1 to test routing without DNS.
  5. Ping a name with ping -c 4 ubuntu.com to test DNS as well.
  6. Reconnect by SSH, or test your service, using the new static address.
  7. Reboot once with sudo reboot when it is safe, then verify again. A setting that only works until reboot is not finished.

If the number ping works but the name ping fails, your routing is fine and your DNS list is wrong or missing. If even the gateway ping fails, check the address, prefix, cable, and whether another device already owns that address.

Step 5: Handle Cloud-Init and DHCP Clashes

Many Ubuntu servers were installed with cloud-init, and the Netplan file may say it was generated by cloud-init and can be disabled. If your changes keep being overwritten after a reboot, cloud-init may be regenerating the file.

To stop that, edit /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg to contain network: {config: disabled}. Do not disable cloud-init on a cloud virtual machine without checking your provider’s guidance, because it may manage more than networking there.

Also pick an address outside the router’s DHCP pool, or use a DHCP reservation instead. Pick one method per device and note it down.

Common Mistakes and Useful Tips

  • Using tabs instead of spaces: YAML rejects tabs. If your editor inserts them, the file will fail. Keep indentation with spaces only.
  • Still using gateway4: It is deprecated in 24.04. Use the routes block with to: default and via for a file that will keep working.
  • Forgetting dhcp4: false: Without this, the server may keep a DHCP address alongside your static one, causing confusing results.
  • Choosing an address already in use: Ping the address before assigning it, and check the router list. A duplicate address breaks both devices.
  • Editing the wrong file or adding a second live file: Conflicting files create hard-to-find behaviour. Keep one clear file for the interface.
  • Tip — work at the console for the first try: Ten minutes beside the server beats an evening of remote guesswork.
  • Tip — document the change: Record the address, gateway, DNS, file name, and date. Future you will be grateful.

Conclusion

Setting a static IP on Ubuntu Server 24.04 with Netplan is safe when you prepare first: pick a free address outside the DHCP pool, back up the file, write clean YAML with spaces, use dhcp4: false, the /24 prefix, a routes gateway, and DNS servers. Test with sudo netplan try, apply, and verify with ip a, route checks, and pings by number and by name. Finish with a reboot test, and your server will stay exactly where your network expects to find it.

Frequently Asked Questions

Which Netplan file should I edit on Ubuntu Server 24.04?

Edit the YAML file already in /etc/netplan/ that describes your interface, often 50-cloud-init.yaml or 00-installer-config.yaml. List the folder first, read each file, and edit the one that holds your current DHCP settings rather than adding a new conflicting file.

Why should I not use gateway4 anymore?

Because it is deprecated. Current Netplan expects the default gateway to be set as a route, using routes with to: default and via followed by the gateway address. Files using the old key may warn or fail on newer releases.

Will my SSH session disconnect when I apply the new IP?

Yes, if you are connected to the old address. That is normal. Reconnect to the new static address. For that reason, it is safer to make the first change at the local console or when someone can reach the machine if something goes wrong.

What does the /24 after the IP address mean?

It is the subnet prefix length. /24 means the first 24 bits define the network, which matches the common mask 255.255.255.0 used by most home and small office networks. Without the prefix, Netplan does not know the size of your local network.

Admin

Passionate about the intersection of style and storytelling. I simplify fashion trends and industry news into easy-to-follow advice, helping you stay stylish and informed in the fast-paced world of fashion.

One response to “How to Set a Static IP on Ubuntu Server 24.04 Using Netplan”

Leave a Reply

Your email address will not be published. Required fields are marked *