Ubuntu Network Config File: How to Edit and Troubleshoot
In this article
Ubuntu allows you to configure network settings either through Netplan on recent releases (18.04+) or via the legacy /etc/network/interfaces file on older systems. Editing the correct file and applying the changes correctly will give you a static IP, custom DNS, or any other network behavior you need. The process is straightforward once you know which file to edit and how to validate the syntax.
What You’ll Need
- A terminal window (Ctrl+Alt+T).
- sudo privileges to edit system files.
- A text editor such as nano, vim, or gedit.
- Knowledge of your network interface name (e.g., eth0, enp0s3).
- Optional: a backup of the original config file.
Step‑by‑Step
- Open a terminal and identify the active network interface:
ip link | grep state
The interface in the "UP" state is the one you will configure. Keep its name for later steps. - For Ubuntu 18.04+ (Netplan):
sudo nano /etc/netplan/01-netcfg.yaml
If the file does not exist, create it or edit an existing one in /etc/netplan/. The file should contain a structure like:network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: trueReplacedhcp4: truewith your static configuration as shown in the next step. - Configure a static IP in the Netplan file. For example:
network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: no addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: - 8.8.8.8 - 8.8.4.4Save the file (Ctrl+O, Enter, Ctrl+X in nano). - Validate the syntax before applying:
sudo netplan try
The system will apply the configuration for 120 seconds. If the network works, confirm by pressingEnter. If not, it will revert automatically. This step prevents a broken network that could lock you out. - Apply the configuration permanently:
sudo netplan apply
Verify connectivity withping -c 4 8.8.8.8andping -c 4 google.com. If DNS fails, double‑check thenameserverssection. - For older Ubuntu releases (pre‑18.04) using /etc/network/interfaces:
sudo nano /etc/network/interfaces
Add or modify a stanza such as:auto enp0s3 iface enp0s3 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8 8.8.4.4Restart networking:sudo systemctl restart networking
or reboot to apply changes.
Common Mistakes
- Using the wrong interface name (e.g., eth0 instead of enp0s3). Verify with
ip linkbefore editing. - Leaving a stray colon or indentation error in the YAML file. Netplan is strict about spaces; two spaces per level is recommended.
- Forgetting to set
renderer: networkdorrenderer: NetworkManagerwhen you want NetworkManager to manage the interface. - Applying
netplan trybut not confirming, causing the system to revert to the old config. - Editing /etc/network/interfaces on a system that uses Netplan; the changes will be ignored.
Tips
- Always back up the original file before editing:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak. - Use
sudo netplan --debug applyto see detailed output if the apply fails. - When configuring multiple interfaces, keep each under its own key in the YAML file to avoid conflicts.
- If you use NetworkManager for Wi‑Fi, keep
renderer: NetworkManagerand edit the Wi‑Fi settings via the GUI instead of Netplan. - For IPv6, add a
addresses:block with a /64 prefix and agateway6:entry.
Troubleshooting
When the network does not come up after editing, follow these steps:
- Check the syntax with
sudo netplan tryorsudo netplan --debug apply. Errors will be printed to the console. - Review the system journal for networkd messages:
journalctl -u systemd-networkdorjournalctl -u NetworkManager. - Verify the interface is not blocked by a firewall rule or
ufw. Disable temporarily withsudo ufw disableto test. - If the interface shows as
DOWN, check the cable or switch port. Usesudo dmesg | grep enp0s3for hardware errors. - For DHCP failures, confirm the DHCP server is reachable and not denying the MAC address. Use
sudo dhclient -v enp0s3to force a request. - If DNS does not resolve, test with
dig @8.8.8.8 google.comto see if the nameserver is reachable. - When unsure, revert to the backup file and run
sudo netplan applyagain.
Frequently Asked Questions
How do I find my interface name?
Run ip link | grep state or ifconfig -a to list all interfaces. The one marked UP is usually the active one.
Can I use Netplan with NetworkManager?
Yes, set renderer: NetworkManager in the YAML file. NetworkManager will then manage the interface while Netplan handles the initial configuration.
What happens if I forget to run netplan apply?
The changes are not applied until the command is executed. The system will continue using the previous configuration.
Is it safe to edit /etc/network/interfaces on 20.04?
No. Ubuntu 20.04 and newer use Netplan by default. Editing /etc/network/interfaces will have no effect unless Netplan is disabled.
How do I revert a broken Netplan configuration?
Use the backup file you created earlier: sudo cp /etc/netplan/01-netcfg.yaml.bak /etc/netplan/01-netcfg.yaml and then sudo netplan apply.