Setting Network on Ubuntu Server: A Step‑by‑Step Guide
In this article
Yes, you can configure networking on Ubuntu Server; the process involves editing Netplan files, applying the changes, and verifying connectivity. Below is a detailed, step‑by‑step walk‑through that covers both static and DHCP setups, common pitfalls, and troubleshooting tips.
What You’ll Need
- Ubuntu Server 20.04 LTS or newer installed and running.
- Root or sudo privileges on the machine.
- A terminal session (local or SSH). If you’re using SSH, you’ll need a working connection before you change network settings.
- Knowledge of your network’s IP scheme: subnet mask, gateway, and DNS servers. If you’re unsure, ask your network administrator.
- Optional: a text editor such as nano or vim, or the default editor set by the system.
Step‑by‑Step
- Identify the network interface. Run
ip link showand look for the interface that connects to your network. Common names areeth0,ens160, orenp3s0. Note the exact name; Netplan requires it. - Locate the Netplan configuration directory. Netplan YAML files live in
/etc/netplan. List the files withls /etc/netplan. You’ll typically see one file, e.g.,01-netcfg.yaml. - Back up the existing configuration. Before editing, copy the file:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak. This allows you to restore the original if something goes wrong. - Open the Netplan file for editing. Use a text editor:
sudo nano /etc/netplan/01-netcfg.yaml. The file is YAML; indentation matters. Use two spaces per level. - Configure DHCP (default). If you want the server to obtain an IP automatically, ensure the file contains something like:
network: version: 2 renderer: networkd ethernets: ens160: dhcp4: trueReplaceens160with your interface name. - Configure a static IP. For a fixed address, replace the
dhcp4: trueline with the following block, adjusting values to match your network:network: version: 2 renderer: networkd ethernets: ens160: dhcp4: no addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: - 8.8.8.8 - 8.8.4.4Theaddressesline includes the IP and CIDR mask. Thegateway4is your router, andnameserversare the DNS servers. - Save and exit. In nano, press Ctrl+O to write, Enter to confirm, then Ctrl+X to close.
- Validate the YAML syntax. Run
sudo netplan try. This command applies the configuration temporarily and prompts you to confirm. If the network works, press y to keep the changes; if not, press n to revert. This step prevents you from locking yourself out. - Apply the configuration permanently. If
netplan trysucceeded, runsudo netplan applyto enforce the new settings. The command restarts the networking backend. - Verify the interface state. Use
ip a show ens160to confirm the IP address appears. For static IPs, you should see the configured address; for DHCP, you should see an address assigned by the router. - Test connectivity. Ping the gateway:
ping -c 4 192.168.1.1. Then ping an external host:ping -c 4 8.8.8.8. Finally, resolve a domain:nslookup example.com. - Check systemd‑networkd status. If you encounter problems, run
systemctl status systemd-networkdand review logs withjournalctl -u systemd-networkdfor error messages. - Persist DNS settings. Netplan writes to
/etc/resolv.confautomatically. If you need custom search domains, add them undernameservers:in the YAML file. - Optional: Configure IPv6. Add
dhcp6: trueor staticaddresses6under the interface block if your network uses IPv6. - Reboot if necessary. Some changes, especially to the default gateway, may require a reboot:
sudo reboot. Verify after restart.
Common Mistakes
- Using tabs instead of spaces in YAML. Netplan will reject the file and leave networking unchanged.
- Mis‑spelling the interface name. Netplan will create a new interface with no physical device, leading to no connectivity.
- Leaving
dhcp4: nowithout providing anaddressesblock. The interface will remain unconfigured. - Omitting the CIDR mask (e.g., writing
192.168.1.100instead of192.168.1.100/24). Netplan will error out. - Not validating the YAML before applying. A syntax error can lock you out over SSH.
Tips
- If you’re on a cloud instance (AWS, Azure, GCP), the cloud provider may override Netplan settings with its own network configuration. Check provider documentation first.
- Use
netplan generateto see the rendered configuration in/run/systemd/networkbefore applying. - For temporary testing, you can add a
staticinterface in/etc/network/interfacesand useifup, but Netplan is the modern, supported method on Ubuntu Server. - Keep a local console or out‑of‑band management (e.g., IPMI, iLO) handy in case SSH access is lost.
- When editing Netplan, always use
netplan tryfirst; it gives you a 120‑second window to confirm the network works.
Troubleshooting
- No IP address assigned. Check that the interface name matches exactly. Verify
netplan applyoutput for errors. - Cannot reach gateway. Ensure the
gateway4value is correct and that the subnet mask matches the network. Useroute -nto inspect routing tables. - DNS resolution fails. Verify
/etc/resolv.confcontains the correct nameservers. If usingsystemd-resolved, checksystemctl status systemd-resolvedandresolvectl status. - SSH disconnects after applying Netplan. Use
netplan tryto avoid lock‑out. If you lose SSH, log in locally or via console and runnetplan revertto restore the backup. - Systemd‑networkd shows errors. Inspect logs:
journalctl -u systemd-networkd -b. Common messages include “Invalid address” or “Could not read interface.” - IPv6 not working. Verify that the network supports IPv6 and that the interface block includes
dhcp6: trueor a staticaddresses6entry. - Firewall blocking traffic. Check
ufw statusoriptables -Lto ensure rules allow outgoing traffic.
Frequently Asked Questions
Can I use Netplan with NetworkManager on Ubuntu Server?
Ubuntu Server typically uses systemd-networkd as the renderer. NetworkManager is not installed by default. If you install it, you can set renderer: NetworkManager in the YAML file, but this is uncommon for headless servers.
What happens if I forget to add a CIDR mask?
Netplan will reject the file during validation, and the interface will remain unconfigured. Always include the subnet mask (e.g., /24).
Is it safe to edit Netplan files over SSH?
Yes, as long as you use netplan try first. If the configuration fails, you’ll be prompted to revert before the session ends.
How do I restore a previous Netplan configuration?
Copy the backup file back: sudo cp /etc/netplan/01-netcfg.yaml.bak /etc/netplan/01-netcfg.yaml and run sudo netplan apply.
Can I set a static IP without editing Netplan?
On Ubuntu Server, Netplan is the recommended method. Editing /etc/network/interfaces is legacy and may conflict with Netplan.
Will changing the IP affect SSH connections?
If you change the IP to a new address, you’ll need to reconnect using the new address. If you change the gateway or DNS, SSH may still work but name resolution could fail.