Configure a Static IP with Netplan
A comprehensive guide to configuring static IP addresses on Linux using Netplan, covering YAML syntax, common scenarios, DNS, gateways, and troubleshooting.
Netplan, introduced in Ubuntu 17.10, is a YAML-based network configuration abstraction tool designed to simplify network setup across different Linux distributions. It generates configuration files for various renderers like NetworkManager or systemd-networkd, providing a consistent interface for network management. This article details how to configure a static IPv4 address using Netplan, covering essential parameters such as IP addresses, subnet masks, default gateways, and DNS servers.
While Netplan is primarily associated with Ubuntu and its derivatives, its YAML-based approach offers a streamlined way to manage network settings that can be adapted for other systems utilizing compatible network renderers.
Understanding the Netplan Configuration File
Netplan configuration files are typically located in the /etc/netplan/ directory and usually have a .yaml or .yml extension. Multiple files can exist in this directory; Netplan processes them in lexicographical order. The most common filename is 01-netcfg.yaml or a similar numeric prefix to control processing order.
A basic Netplan configuration for a static IP address defines the network interface, its addressing scheme, gateway, and DNS servers. All parameters are case-sensitive and rely on correct indentation, using spaces (never tabs) for hierarchy.
Key YAML Structure Elements
network:: The top-level key for all network configurations.version: 2: Specifies the Netplan configuration file format version. Version 2 is the current stable version.renderer:: (Optional) Specifies which backend Netplan should use (e.g.,networkdorNetworkManager). If omitted, Netplan often attempts to autodetect or usesnetworkdby default on server installations.ethernets:: Defines configurations for Ethernet interfaces. Wi-Fi interfaces usewifis:.[interface_name]:: The name of your network interface (e.g.,ens33,eth0). You can find this using commands likeip aorifconfig -a.dhcp4: no: Explicitly disables IPv4 DHCP for this interface. For static IP, this must beno.dhcp6: no: Explicitly disables IPv6 DHCP (optional, but good practice if not used).addresses:: A list of IPv4 or IPv6 addresses with their subnet masks in CIDR notation (e.g.,[192.168.1.50/24]). Multiple addresses can be specified.routes:: (Optional) Used for defining static routes, including the default gateway.gateway4:: (Deprecated in Netplan 0.106+, preferroutes:) The default IPv4 gateway address.nameservers:: Configures DNS servers.addresses:(undernameservers:): A list of DNS server IP addresses (e.g.,[1.1.1.1, 8.8.8.8]).search:(undernameservers:): A list of domain suffixes to search for hostnames (e.g.,[example.com, mycompany.local]).
Example Configuration: Static IPv4
Let's configure a common scenario: assigning a static IPv4 address to an interface named ens33.
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd # Explicitly specify systemd-networkd as the renderer
ethernets:
ens33:
dhcp4: no
dhcp6: no # Disable IPv6 DHCP as well if not using
addresses: [192.168.1.50/24] # Static IP address with subnet mask (CIDR)
routes:
- to: default
via: 192.168.1.1 # The default gateway
nameservers:
addresses: [1.1.1.1, 8.8.8.8, 208.67.222.222] # Primary, secondary, tertiary DNS servers
search: [home.local, mycorp.com] # Optional: DNS search domains
In this example:
- The interface
ens33will not obtain an IP address via DHCP. - It will be assigned
192.168.1.50with a subnet mask of255.255.255.0(equivalent to/24). - The default gateway for outbound traffic will be
192.168.1.1. Note the use ofroutes:which is the preferred method for defining gateways in recent Netplan versions (0.106+). If you're on an older Netplan version,gateway4: 192.168.1.1would be used directly underens33:. - DNS queries will first go to Cloudflare (
1.1.1.1), then Google (8.8.8.8), then OpenDNS (208.67.222.222). - When resolving hostnames like
myhost, the system will first trymyhost.home.local, thenmyhost.mycorp.com.
Applying the Configuration
After creating or modifying the Netplan YAML file, two commands are typically used:
- Test the configuration:
sudo netplan tryThis command attempts to apply the configuration and reverts to the previous working state if connectivity is lost or if there's a syntax error. It provides a 120-second window (default) for you to confirm the changes or for it to automatically roll back.
- Apply the configuration permanently:
sudo netplan applyThis command applies the configuration directly. It's recommended to use
netplan tryfirst, especially for remote systems, to prevent losing connectivity due to misconfigurations.
You can also generate the backend configuration files without applying them using sudo netplan generate. This can be useful for debugging or inspection.
Verifying the Configuration
After applying the changes, verify that the IP address, gateway, and DNS settings are correctly configured:
- Check IP address:
ip a show ens33Look for the
inetaddress corresponding to your static IP. - Check default route:
ip rVerify that the
default viaentry points to your configured gateway. - Check DNS resolvers:
cat /run/systemd/resolve/resolv.confOn systems using
systemd-resolved(common withrenderer: networkd), this file contains the effective DNS servers. Alternatively,resolvectl statusprovides detailed DNS information.resolvectl status - Test connectivity:
ping -c 3 8.8.8.8 # Ping a public IP to test internet connectivity ping -c 3 stackoverflow.com # Ping a hostname to test DNS resolution
Advanced Configuration Options (Briefly)
Netplan supports various other configurations, including:
- IPv6 Static Configuration: Similar to IPv4, using
addresses: [2001:db8::1/64]androutes: - to: default via: 2001:db8::fe. - Bonding (Link Aggregation): Grouping multiple network interfaces for redundancy or increased bandwidth.
- Bridges: Creating software bridges for virtualization or other networking setups.
- VLANs: Configuring Virtual Local Area Networks on top of physical interfaces.
- MAC Address: You can explicitly set the MAC address for an interface using
macaddress: 00:1a:2b:3c:4d:5eunder the interface definition.
Common Pitfalls and Troubleshooting
- YAML Syntax Errors: The most common issue. Ensure correct indentation (spaces, not tabs), proper colons, and valid key names. Netplan will usually output an error message indicating the line number if there's a syntax issue during
netplan tryornetplan apply. - Incorrect Interface Name: Double-check the interface name (e.g.,
ens33,eth0) usingip a. Typos will prevent the configuration from applying. - Conflicting Configurations: If you have multiple Netplan files, ensure they don't define the same interface with conflicting settings. The last processed file (lexicographically) typically wins for overlapping settings. Also, ensure no old
/etc/network/interfacesconfigurations are active if you're transitioning. gateway4vs.routes: Be aware of the Netplan version. If you are on Netplan 0.106 or newer, use theroutes:syntax for the default gateway. Older versions usegateway4:directly under the interface. Using the wrong one can lead to no default route.- DNS Not Working: Verify
nameservers: addresses:are correct and reachable. Check/run/systemd/resolve/resolv.conforresolvectl statusfor the actual resolvers being used. Firewalls might block DNS traffic (UDP port 53). - Permissions: Ensure the YAML file has appropriate permissions (e.g.,
-rw-r--r--, owned by root) to prevent accidental modification or security issues. - NetworkManager vs. systemd-networkd: If you're on a desktop system,
renderer: NetworkManagermight be more appropriate. For servers,renderer: networkdis standard. Using the wrong renderer can lead to unexpected behavior or configurations not being applied.