Ubuntu 22.04 uses Netplan to configure network interfaces, allowing you to assign static IP addresses or obtain them via DHCP for both wired and wireless connections. This guide explains how to set up these configurations using the netplan YAML files and apply changes without disrupting active sessions.
Understanding Ubuntu Network Configuration
Modern Ubuntu versions have moved away from the traditional/etc/network/interfaces file in favor of Netplan, a configuration utility that generates backend configurations for either systemd-networkd or NetworkManager. When you edit your network settings, you are primarily modifying YAML files located in the /etc/netplan/ directory. These files define the state of your network interfaces, such as eno1 for Ethernet and wls4 for Wi-Fi. It is crucial to understand that while the title mentions "routing," this process is fundamentally about assigning IP addresses (Layer 3) rather than configuring dynamic routing protocols like OSPF or BGP. For users migrating from older Ubuntu releases or other Linux distributions, understanding this shift is vital to avoid confusion with deprecated tools. If you are managing multiple virtual machines, you might also find it helpful to review How to Configure a Traffic Route from VM1 through Host to VM2 to understand broader traffic flow concepts beyond simple interface assignment.
Configuring a Static IP on Wired Interface
To assign a static IP address to your wired connection, typically identified aseno1, you need to edit the appropriate Netplan configuration file. Open a terminal and use a text editor like nano or vim to modify the file in /etc/netplan/. The filename often ends with .yaml or .yml.
You must ensure the indentation is correct, as YAML is sensitive to whitespace. Use two spaces for each level of nesting; do not use tabs. Below is an example configuration for a static setup:
network:
version: 2
renderer: networkd
ethernets:
eno1:
dhcp4: no
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
In this snippet, dhcp4: no disables automatic IP assignment. The addresses field specifies the IP and subnet mask. The routes section defines the default gateway, which is essential for internet access. Finally, nameservers ensures DNS resolution works correctly. After saving the file, you can apply the changes immediately by running sudo netplan apply. If there are syntax errors, this command will report them, allowing you to fix the file before breaking connectivity.
Setting Up DHCP for Wireless Interface
Wireless interfaces, such aswls4, often benefit from Dynamic Host Configuration Protocol (DHCP) because they may connect to various networks with different IP schemes. Configuring DHCP simplifies mobility and reduces manual intervention. To enable DHCP for your wireless adapter, you can modify the same Netplan configuration file or create a separate one if your system structure supports it.
Here is how the configuration block should look for wls4:
wifis:
wls4:
dhcp4: yes
dhcp6: no
access-points:
"Your_SSID_Name":
password: "your_wifi_password"
This configuration tells the system to automatically request an IP address from the connected router. The access-points section handles the authentication details for your Wi-Fi network. Note that dhcp6: no is included to disable IPv6 if it is not required, which can prevent potential connectivity issues in some environments. Once saved, run sudo netplan apply again. The system will disconnect and reconnect to the wireless network, obtaining a new IP address from the DHCP server.
Distinguishing Between IP Assignment and Routing
It is important to clarify a common misconception: configuring a static IP or enabling DHCP is not the same as setting up dynamic routing. Dynamic routing involves protocols that automatically update routing tables based on network topology changes. In contrast, the configurations above are static. They define how your machine connects to its local network and the internet. If you need to add specific static routes for non-default paths, you can include additional entries in theroutes section of your Netplan file, similar to the default route shown earlier. For more complex scenarios involving internal network segmentation, refer to Troubleshooting WireGuard Client-to-Client Communication Issues to see how tunneling affects routing decisions.
Troubleshooting Common Issues
After applying Netplan configurations, you may encounter connectivity problems. First, verify that the YAML syntax is valid. A single misplaced space can cause the entire configuration to fail. You can check for syntax errors by runningsudo netplan try, which applies the configuration but reverts it after 120 seconds if you do not confirm it works. This safety feature prevents lockouts.
If the wired interface fails to connect, check the physical link status with ip link show eno1. Ensure the interface is in the UP state. For wireless issues, confirm that the SSID and password are correct and that the radio is enabled. You can also check the system logs using journalctl -u systemd-networkd to see detailed error messages related to network initialization. If you suspect a deeper issue with your virtual machine's networking, especially in KVM environments, consulting resources on Ubuntu 22.04 VM on KVM/virt-manager Not Connecting to Internet might provide insights into hypervisor-level restrictions.
Verifying Your Configuration
Once your settings are applied, verify that they are active. Use theip addr show command to list all IP addresses assigned to your interfaces. Confirm that eno1 has the static IP you defined and that wls4 has received a DHCP lease. Additionally, test connectivity by pinging an external host, such as ping 8.8.8.8. If ping fails, check your firewall rules and ensure that the default route is present using ip route show. This final step ensures that your network stack is correctly configured to handle traffic.
Frequently Asked Questions
Can I mix static and DHCP configurations in the same Netplan file?
Yes, you can define both wired and wireless interfaces with different methods in a single YAML file. Each interface is defined under its own key (e.g.,ethernets vs wifis), allowing independent configuration. This approach keeps all network settings centralized and easier to manage.
What happens if I make a typo in the Netplan YAML file?
If there is a syntax error,netplan apply will fail and display an error message indicating the line number and nature of the problem. The previous configuration remains active until a successful apply occurs, preventing accidental loss of connectivity. Always use netplan try to safely test changes.
Does Netplan support IPv6 out of the box?
Yes, Netplan supports IPv6 configuration. You can enable it by settingdhcp6: yes or assigning static IPv6 addresses in the addresses list. However, many users disable it (dhcp6: no) if their network infrastructure does not fully support IPv6 to avoid potential conflicts or delays in connection establishment.
How do I revert to the default network configuration?
To revert to defaults, you can delete or comment out the custom settings in the Netplan YAML files and runsudo netplan generate followed by sudo netplan apply. This regenerates the backend configuration based on the remaining active settings. Be cautious, as this may remove specific static routes or DNS settings you intentionally added.
Is it safe to edit Netplan files while connected remotely?
It is risky to edit Netplan files via SSH because a misconfiguration can lock you out of the server. It is recommended to usenetplan try, which provides a rollback window. Alternatively, configure a console access method or have physical access available before making significant network changes to ensure you can recover the system if needed.