If you’re running Ubuntu Server and need to configure a static IP address, you might have seen guides mentioning /etc/network/interfaces
or resolvconf
. However, these methods are outdated. The recommended way today is to use netplan
.
In this guide, you’ll discover how to set a static IP in Ubuntu and define custom DNS settings, including nameservers and search domains. Additionally, we’ll explain how to keep DHCP while specifying DNS servers for better control.
Why Should You Set a Static IP on Ubuntu Server?
Assigning a static IP ensures your server retains the same address across reboots. This reliability is essential for servers running web services, databases, or acting as internal network resources.
Step 1: Identify Your Ubuntu Server Network Interface
To begin, list your network interfaces:
ip link
You’ll usually see names like eth0
, ens33
, or enp0s3
.
Step 2: Edit the Netplan Configuration
Netplan configurations are stored in /etc/netplan/
. View the files with:
ls /etc/netplan/
Next, edit the YAML file (replace with your actual file name):
sudo nano /etc/netplan/50-cloud-init.yaml
Here’s an example static IP configuration for Ubuntu Server:
network:
version: 2
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
search: [yourdomain.local]
addresses:
- 1.1.1.1
- 1.0.0.1
Replace eth0
with your interface name. Adjust the IP, gateway, and DNS to match your network.
Important: Some older guides might mention using the
gateway4
parameter. However,gateway4
has been deprecated. It’s better to use theroutes
section, as demonstrated above, for better compatibility with future Ubuntu versions.
Step 3: Apply the Static IP Ubuntu Configuration
Once you have finished editing, apply the changes with:
sudo netplan apply
To confirm your new settings, run:
ip a
This command will display your active IP address. To confirm your DNS configuration is working, you can run:
apt update
This will refresh the built-in software repositories, and as long as it’s successful you know that your DNS configuration is working.
Alternative: Keep DHCP but Configure DNS in Ubuntu Server
If you prefer to use DHCP for IP assignment but still want to control DNS servers, use this configuration:
network:
version: 2
ethernets:
eth0:
dhcp4: yes
dhcp4-overrides:
use-dns: no
nameservers:
search: [yourdomain.local]
addresses:
- 1.1.1.1
- 1.0.0.1
This method allows the server to receive its IP address from the DHCP server, while your specified DNS servers handle name resolution.
Conclusion
To sum up, netplan
is the modern, recommended tool for configuring a static IP Ubuntu setup. You should avoid older methods like resolvconf
or editing /etc/network/interfaces
, as they are deprecated in the latest Ubuntu versions. Whether you need a full static IP or simply want to control your DNS while keeping DHCP, netplan
makes the process clear and manageable.
If you would like to learn about all the configuration options for netplan
, you can read the official Netplan documentation.
If you would prefer to view this guide in video form, I’ve created a video explaining these instructions on the TechOpt.io YouTube channel, which you can watch below:
Leave a Reply