Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Setting Network on Ubuntu Server: A Step‑by‑Step Guide

· 5 min read · Home Networking

In this article
  1. What You’ll Need
  2. Step‑by‑Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Identify the network interface. Run ip link show and look for the interface that connects to your network. Common names are eth0, ens160, or enp3s0. Note the exact name; Netplan requires it.
  2. Locate the Netplan configuration directory. Netplan YAML files live in /etc/netplan. List the files with ls /etc/netplan. You’ll typically see one file, e.g., 01-netcfg.yaml.
  3. 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.
  4. 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.
  5. 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: true
    
    Replace ens160 with your interface name.
  6. Configure a static IP. For a fixed address, replace the dhcp4: true line 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.4
    
    The addresses line includes the IP and CIDR mask. The gateway4 is your router, and nameservers are the DNS servers.
  7. Save and exit. In nano, press Ctrl+O to write, Enter to confirm, then Ctrl+X to close.
  8. 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.
  9. Apply the configuration permanently. If netplan try succeeded, run sudo netplan apply to enforce the new settings. The command restarts the networking backend.
  10. Verify the interface state. Use ip a show ens160 to 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.
  11. 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.
  12. Check systemd‑networkd status. If you encounter problems, run systemctl status systemd-networkd and review logs with journalctl -u systemd-networkd for error messages.
  13. Persist DNS settings. Netplan writes to /etc/resolv.conf automatically. If you need custom search domains, add them under nameservers: in the YAML file.
  14. Optional: Configure IPv6. Add dhcp6: true or static addresses6 under the interface block if your network uses IPv6.
  15. 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: no without providing an addresses block. The interface will remain unconfigured.
  • Omitting the CIDR mask (e.g., writing 192.168.1.100 instead of 192.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 generate to see the rendered configuration in /run/systemd/network before applying.
  • For temporary testing, you can add a static interface in /etc/network/interfaces and use ifup, 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 try first; it gives you a 120‑second window to confirm the network works.

Troubleshooting

  1. No IP address assigned. Check that the interface name matches exactly. Verify netplan apply output for errors.
  2. Cannot reach gateway. Ensure the gateway4 value is correct and that the subnet mask matches the network. Use route -n to inspect routing tables.
  3. DNS resolution fails. Verify /etc/resolv.conf contains the correct nameservers. If using systemd-resolved, check systemctl status systemd-resolved and resolvectl status.
  4. SSH disconnects after applying Netplan. Use netplan try to avoid lock‑out. If you lose SSH, log in locally or via console and run netplan revert to restore the backup.
  5. Systemd‑networkd shows errors. Inspect logs: journalctl -u systemd-networkd -b. Common messages include “Invalid address” or “Could not read interface.”
  6. IPv6 not working. Verify that the network supports IPv6 and that the interface block includes dhcp6: true or a static addresses6 entry.
  7. Firewall blocking traffic. Check ufw status or iptables -L to 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.