Website Vidya
Linux & ServersStep-by-Step Guide

How to Set a Static IP Address on Ubuntu Server and Desktop

Learn how to set a static ip address on ubuntu 24.04 and 22.04 with Netplan YAML configuration, NetworkManager GUI, and DNS routing setup.

RR
Rajesh RayLead Systems Educator
Published:
6 min read
PDF Cheat Sheet
How to Set a Static IP Address on Ubuntu Server and Desktop
AI Executive SummaryWebsiteVidya AI

6 min read

Learn how to set a static ip address on ubuntu 24.04 and 22.04 with Netplan YAML configuration, NetworkManager GUI, and DNS routing setup.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Identifying Active Network Interface Names
  • Step 2: Locating Existing Netplan Configuration Files
Grounded in full engineering runbookWebsiteVidya AI Engine

When an Ubuntu computer connects to a local network, a Dynamic Host Configuration Protocol (DHCP) server usually assigns it a temporary dynamic IP address.

While DHCP is ideal for laptops and workstations, production servers, database nodes, hypervisors, and network appliances require unchanging static IP addresses.

A predictable IP address ensures that DNS records, SSH bookmarks, firewall rules, and API endpoints remain permanently reachable across reboots.

Ubuntu utilizes Netplan as its default network configuration abstraction utility, parsing YAML files to configure backend renderers like systemd-networkd or NetworkManager.

Advertisement

In this guide, we demonstrate how to set a static ip address on ubuntu both on headless servers via Netplan and on Desktop installations via graphical controls.

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

Advertisement
  • A server or desktop computer running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A non-root user account with sudo administrative privileges.
  • Network parameters ready: target Static IP, Subnet Mask (CIDR notation), Gateway IP, and DNS Nameservers.
  • Console or physical terminal access in case network connectivity drops during reconfiguration.

Step 1: Identifying Active Network Interface Names

Before configuring a static address, identify the logical name of your physical or virtual network interface.

Modern Linux kernels assign predictable interface names (such as eth0, ens3, ens33, or enp3s0) based on hardware bus topology:

Run the ip link or ip addr command to list all network interfaces:

# List active network interfaces:
ip -br link
# Example output:
# lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
# eth0             UP             52:54:00:12:34:56 <BROADCAST,MULTICAST,UP,LOWER_UP>

# Identify current dynamic IP and default gateway:
ip route show default
# Example output: default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.150 metric 100

Step 2: Locating Existing Netplan Configuration Files

Netplan stores network configuration files in YAML format inside /etc/netplan/.

Advertisement

File names typically end in .yaml (such as 50-cloud-init.yaml, 01-netcfg.yaml, or 00-installer-config.yaml):

Inspect the contents of /etc/netplan/ to identify your active configuration:

# List files in the Netplan directory:
ls -la /etc/netplan/
# Output: 00-installer-config.yaml

# Create a backup before making modifications:
sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak

Step 3: Configuring Static IP via Netplan YAML (Server)

Open your Netplan configuration file in a text editor like nano.

YAML syntax is strictly indentation-sensitive; use spaces (not tabs) with 2-space indentation levels.

Configure your network interface with a static IP address, subnet CIDR prefix (such as /24), default gateway route, and DNS nameservers:

Save the following configuration structure into /etc/netplan/00-installer-config.yaml:

# /etc/netplan/00-installer-config.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses:
        - 192.168.1.50/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8
        search:
          - localdomain

Netplan Routing Syntax Evolution: gateway4 vs routes

In older Ubuntu versions (like 18.04 and early 20.04), Netplan accepted gateway4: 192.168.1.1.

Canonical has fully deprecated gateway4 in favor of the standardized routes list shown above.

Using gateway4 on Ubuntu 24.04 triggers deprecation warnings during validation.

Step 4: Testing and Applying Netplan Configuration Safely

Applying invalid YAML indentation or typographical errors over SSH can sever your remote connection permanently.

Netplan provides a safety mechanism called netplan try.

This command applies the new configuration temporarily and waits for user confirmation; if you do not press Enter within 120 seconds, Netplan automatically rolls back to the previous working configuration:

Execute the safe test and apply your changes:

# Test configuration with automatic 120-second rollback safety:
sudo netplan try
# Output:
# Do you want to keep these settings?
# Press ENTER before the timeout to accept the new configuration
# Changes will revert in 120 seconds

# Press Enter to confirm.

# Alternatively, apply settings directly:
sudo netplan apply

Step 5: Verifying Network Connectivity and DNS Resolution

Confirm that the network interface adopted the new static IP address and that gateway routing and DNS resolution function correctly:

Run diagnostic commands to verify your network state:

# Verify IP address on eth0:
ip addr show dev eth0
# Output confirms: inet 192.168.1.50/24 brd 192.168.1.255 scope global eth0

# Verify default gateway routing:
ip route
# Output confirms: default via 192.168.1.1 dev eth0 proto static

# Test DNS resolution and external internet reachability:
ping -c 3 1.1.1.1
ping -c 3 ubuntu.com

Step 6: Setting Static IP on Ubuntu Desktop via NetworkManager GUI

If you run Ubuntu Desktop, managing network configuration graphically through NetworkManager is faster and avoids editing system configuration files manually:

Follow these graphical steps to assign a static IP address:

Step NumberAction in Ubuntu Desktop GUIDetails to Enter
Step 1Open SettingsNavigate to Network (for Ethernet) or Wi-Fi
Step 2Click the Gear IconOpens properties for your active connection profile
Step 3Select the IPv4 TabChange IPv4 Method from Automatic (DHCP) to Manual
Step 4Enter AddressesAddress: 192.168.1.50, Netmask: 255.255.255.0, Gateway: 192.168.1.1
Step 5Configure DNSDisable Automatic DNS toggle, enter 1.1.1.1, 8.8.8.8
Step 6Click Apply & ReconnectToggle the network switch off and on to initialize the static IP

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

Invalid YAML: mapping values are not allowed in this context

Cause: Indentation error or tab characters used instead of spaces in Netplan YAML file.

Resolution: Replace all tab characters with two spaces and verify indentation alignment.

sudo netplan generate

WARNING: gateway4 has been deprecated, use default routes instead

Cause: Using deprecated gateway4 directive in modern Netplan on Ubuntu 24.04.

Resolution: Replace gateway4: 192.168.1.1 with routes: [{to: default, via: 192.168.1.1}].

sudo netplan apply

Network unreachable after applying Netplan configuration

Cause: Incorrect gateway IP or invalid subnet mask prefix specified.

Resolution: Revert to the backup file created in Step 2: sudo cp /etc/netplan/00-installer-config.yaml.bak /etc/netplan/00-installer-config.yaml && sudo netplan apply.

sudo netplan apply

Security Best Practices and Hardening

Ensure your chosen static IP address does not fall inside the active DHCP dynamic lease pool range of your router to prevent duplicate IP collisions.

Protect Netplan configuration files with 600 root-only permissions to prevent unauthorized users from viewing network topology.

Always maintain secondary console access (e.g., IPMI, VNC console) when reconfiguring network interfaces on remote cloud hosts.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
Debian's legacy /etc/network/interfaces has been replaced by Netplan in modern Ubuntu releases to provide a unified declarative YAML schema that configures both systemd-networkd (servers) and NetworkManager (desktops) consistently.
CIDR notation specifies the number of leading bits in the subnet mask. /24 corresponds to the standard subnet mask 255.255.255.0, allowing up to 254 usable host addresses on a class C network.
Edit your Netplan YAML file, set dhcp4: true, remove the addresses, routes, and nameservers sections, and execute sudo netplan apply.
No. netplan apply reconfigures the running backend network daemon (systemd-networkd) dynamically without rebooting the operating system.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout