How to Configure a Static IP on Ubuntu Server
Configuring a static IP address on an Ubuntu Server ensures your
machine maintains a consistent network identity, which is essential for
hosting services, SSH access, and predictable network management. Modern
Ubuntu Server versions utilize Netplan, a YAML-based
network configuration abstraction utility, to manage network settings
instead of the traditional /etc/network/interfaces file.
This guide provides a straightforward, step-by-step walkthrough to
locate your network interface, modify the Netplan configuration file
with your desired static IP parameters, and safely apply the
changes.
Step 1: Identify Your Network Interface
Before changing any settings, you need to know the logical name of the network interface you want to configure. You can find this by running the following command in your terminal:
ip aLook for your primary network interface in the output. It typically
starts with en or eth (for example,
eth0 or enp0s3). Note this name down, as you
will need it for the configuration file.
Step 2: Locate the Netplan Configuration File
Netplan stores its configuration files in the
/etc/netplan/ directory. These files use the
.yaml extension. To see the exact name of your
configuration file, list the contents of the directory:
ls /etc/netplan/Common filenames include 01-netcfg.yaml,
50-cloud-init.yaml, or
00-installer-config.yaml.
Step 3: Edit the Configuration File
Open the identified YAML file with a text editor like
nano. You will need root privileges to edit this file:
sudo nano /etc/netplan/01-netcfg.yamlImportant Note on YAML Formatting: YAML files are highly sensitive to indentation. Always use spaces for indentation, never use the Tab key, and ensure alignment is perfectly consistent.
Modify the file to reflect your static network requirements. A standard static IP configuration template looks like this:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
addresses:
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4Be sure to replace enp0s3 with your actual interface
name, 192.168.1.50/24 with your desired static IP and
subnet mask in CIDR notation, 192.168.1.1 with your gateway
IP, and the nameserver IPs with your preferred DNS servers (such as
Google DNS or Cloudflare DNS).
Once the edits are complete, save and close the file (in Nano, press
CTRL+O, Enter, then CTRL+X).
Step 4: Test and Apply the Changes
Netplan allows you to safely test your configuration before committing to it. This prevents you from being permanently locked out of your server if a typo was made. Run the test command:
sudo netplan tryIf the configuration is valid, Netplan will prompt you to press Enter to accept the changes. If you lose connection or do not confirm within the countdown limit, Netplan will automatically roll back to your previous working settings.
If you are confident in your configuration and want to apply it directly without a trial period, use the apply command:
sudo netplan applyStep 5: Verify the Static IP Configuration
To confirm that your Ubuntu Server is successfully using the new static IP address, run the IP address command once more:
ip a show enp0s3Verify that the output displays your newly assigned static IP address. Finally, test external network connectivity and DNS resolution by pinging an external website:
ping -c 3 google.comIf the ping responses return successfully, your Ubuntu Server is fully configured with a stable, static IP address.