Smart Systems for Home

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

Ubuntu Server Network Config Made Simple

· 5 min read · Home Networking

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Advanced Network Configurations
  6. Troubleshooting
  7. Frequently Asked Questions

Configuring the network on an Ubuntu Server enables it to communicate with other devices, access the internet, and accept remote connections. This guide provides a step-by-step approach to setting up both static and dynamic IP configurations using Netplan, the modern default tool in Ubuntu 18.04 and newer. It assumes you have terminal access, either directly or via SSH, and basic technical knowledge of IP addressing and network infrastructure.

What You'll Need

  • Ubuntu Server installed (20.04 or 22.04 LTS recommended)
  • Terminal access (SSH or console)
  • Static IP details (optional): IP address, netmask, gateway, DNS servers
  • Sudo or root privileges

Step-by-Step

  1. Open a terminal on your Ubuntu Server. If you are connecting remotely, use SSH: ssh username@server_ip
  2. Identify your network interface name by running ip a or ip addr. Look for the interface with an IP address assigned (e.g., eth0, ens33, enp0s3). Avoid using lo (loopback) or docker0 (virtual interfaces).
  3. Locate the Netplan configuration file. Run ls /etc/netplan to see available files. The filename may vary depending on the installation source and version (e.g., 01-netcfg.yaml, 50-cloud-init.yaml).
  4. Edit the Netplan configuration using a text editor. For example: sudo nano /etc/netplan/01-netcfg.yaml
  5. To configure a static IP, use the following format (customise the values for your network):
    network: version: 2 ethernets: ens33: addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: - 8.8.8.8 - 8.8.4.4 This sets a static IPv4 address, a gateway, and two public Google DNS servers. Ensure the addresses field uses CIDR notation (e.g., 192.168.1.100/24).
  6. To configure DHCP, use the following format:
    network: version: 2 ethernets: ens33: dhcp4: true This allows the server to automatically request an IP address from your network's DHCP server.
  7. Save the file and exit the editor (Ctrl+O to save, Enter to confirm, Ctrl+X to exit in Nano).
  8. Validate the Netplan configuration using sudo netplan try. This command applies the new configuration and waits for confirmation. If you cancel it within 60 seconds, the previous settings are restored. Otherwise, the changes take effect permanently.
  9. Alternatively, apply the configuration immediately with sudo netplan apply. This is useful in test environments or when you know the configuration is correct.
  10. Verify the new settings with ip a show eth0 (replace eth0 with your actual interface name). Confirm the IP address, gateway, and DNS are as expected.

Common Mistakes

  • Incorrect YAML indentation: YAML files are sensitive to spacing. Each level of indentation must be consistent. Misaligned text can cause Netplan to fail silently or with cryptic errors.
  • Using the wrong interface name: Make sure the interface name in the configuration matches the one from ip a. Mismatches will prevent the settings from being applied.
  • Not validating before applying: Using netplan apply without try can cause a network outage if the configuration is incorrect. Always validate first in production environments.
  • Confusing IPv4 and IPv6: Unless you're using IPv6, configure only dhcp4 or addresses for IPv4. Netplan supports IPv6 as well, but it requires a separate configuration.
  • Forgetting to specify a gateway or DNS: A server may connect to the network but fail to reach external resources without a gateway or DNS server configured.

Tips

  • Backup your Netplan file before editing: sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak. This lets you restore the previous configuration if needed.
  • Use a YAML validator: Paste your Netplan configuration into an online YAML validator or run yaml-lint (if installed) to catch syntax errors before applying the file.
  • Test changes in a virtual or staging environment first: Avoid applying untested configurations on live servers to prevent network downtime.
  • Double-check IPv4 subnet masks: A CIDR notation like /24 means 255.255.255.0 in subnet mask terms. Make sure your static IP is within the correct range for your network.
  • Keep DNS servers public or internal: Use public DNS servers like 8.8.8.8 or 1.1.1.1 for external access, or internal DNS for domain resolution in private networks.

Advanced Network Configurations

  • Multiple interfaces: To configure more than one network interface, add each under the ethernets section. For example:
    eth1: dhcp4: true This allows you to set up a dual-homed server with one interface for internal and one for external traffic.
  • IPv6 Support: If your network uses IPv6, add dhcp6: true or a static IPv6 address in the same format as IPv4. Example:
    addresses: - 2001:db8:1234::1/64 Check with your network administrator to confirm your IPv6 subnet and gateway.
  • Bonding interfaces: For redundancy or performance, you can set up NIC bonding. This requires additional configuration in Netplan and hardware support. Example:
    network: version: 2 bonds: bond0: interfaces: - eth0 - eth1 addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 Ensure the network-manager service is disabled if using bonding.

Troubleshooting

  • If the server loses network connectivity after applying changes, try sudo netplan apply again or restore the backup YAML file using sudo cp /etc/netplan/01-netcfg.yaml.bak /etc/netplan/01-netcfg.yaml
  • Check for syntax errors with sudo netplan apply or sudo netplan try. These commands will provide detailed error messages if the configuration is invalid.
  • Confirm the interface name matches the one in the configuration file. Mismatches are a common cause of configuration failure.
  • Make sure the gateway and DNS servers are correctly configured and reachable on your network. Run ping 192.168.1.1 and nslookup google.com to test connectivity.
  • If you're unsure about any settings, use ip a to check the current configuration and compare it to your changes. This helps identify what's not working as expected.
  • If your server is in a cloud environment, ensure that the instance's metadata or cloud-init files are not overriding your Netplan settings. Some providers manage network settings automatically, which can conflict with custom configurations.

Frequently Asked Questions

Can I use DHCP and static IP on the same interface?

No, Netplan does not support both on the same interface. Choose either static IP or DHCP4, not both.

How do I know which YAML file to edit?

Run ls /etc/netplan to list the files. The most recent or commonly used one is typically the one to edit, but check the content first with cat /etc/netplan/01-netcfg.yaml to confirm.

What if I can't connect after applying the changes?

Try sudo netplan apply again to reapply the configuration. If that fails, restore the previous YAML file or use a physical console to fix the issue.

Do I need to reboot the server after changing the network config?

No, netplan apply or netplan try applies the changes without requiring a reboot.

Can I configure multiple network interfaces?

Yes. Add another interface under the ethernets section with its own settings. For example:
eth1: dhcp4: true

How do I check if DNS is working correctly?

Run nslookup google.com or dig google.com. If it resolves to an IP address, DNS is working. Otherwise, check the nameservers in your configuration.