Ubuntu Server Network Config Made Simple
In this article
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
- Open a terminal on your Ubuntu Server. If you are connecting remotely, use SSH:
ssh username@server_ip - Identify your network interface name by running
ip aorip addr. Look for the interface with an IP address assigned (e.g.,eth0,ens33,enp0s3). Avoid usinglo(loopback) ordocker0(virtual interfaces). - Locate the Netplan configuration file. Run
ls /etc/netplanto see available files. The filename may vary depending on the installation source and version (e.g.,01-netcfg.yaml,50-cloud-init.yaml). - Edit the Netplan configuration using a text editor. For example:
sudo nano /etc/netplan/01-netcfg.yaml - 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.4This sets a static IPv4 address, a gateway, and two public Google DNS servers. Ensure theaddressesfield uses CIDR notation (e.g.,192.168.1.100/24). - To configure DHCP, use the following format:
network: version: 2 ethernets: ens33: dhcp4: trueThis allows the server to automatically request an IP address from your network's DHCP server. - Save the file and exit the editor (Ctrl+O to save, Enter to confirm, Ctrl+X to exit in Nano).
- 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. - Alternatively, apply the configuration immediately with
sudo netplan apply. This is useful in test environments or when you know the configuration is correct. - Verify the new settings with
ip a show eth0(replaceeth0with 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 applywithouttrycan 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
dhcp4oraddressesfor 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
/24means 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
ethernetssection. For example:
eth1: dhcp4: trueThis 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: trueor a static IPv6 address in the same format as IPv4. Example:
addresses: - 2001:db8:1234::1/64Check 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.1Ensure thenetwork-managerservice is disabled if using bonding.
Troubleshooting
- If the server loses network connectivity after applying changes, try
sudo netplan applyagain or restore the backup YAML file usingsudo cp /etc/netplan/01-netcfg.yaml.bak /etc/netplan/01-netcfg.yaml - Check for syntax errors with
sudo netplan applyorsudo 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.1andnslookup google.comto test connectivity. - If you're unsure about any settings, use
ip ato 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.