Configuring a Static IP with Netplan

This document outlines how to configure a static IPv4 address for a network interface using Netplan. This configuration is standard for modern Linux distributions using the systemd-networkd renderer.

1. The Configuration File

Netplan configurations are stored in .yaml files within /etc/netplan/. Common filenames include 01-netcfg.yaml or 50-cloud-init.yaml.

2. Standard Configuration Template

Below is the corrected version of your configuration. Note that in modern Netplan, gateway4 is deprecated in favor of the explicit routes block.

YAML

network:
  version: 2
  renderer: networkd
  ethernets:
    ens1:
      dhcp4: false
      addresses:
        - 10.11.17.5/24
      nameservers:
        addresses:
          - 8.8.8.8
          - 1.1.1.1
      routes:
        - to: default
          via: 10.11.17.1

3. Key Parameters Explained

Parameter

Description

ens1

The name of the physical network interface. Use ip link to verify your interface name.

dhcp4: false

Disables automatic IP assignment from a DHCP server.

addresses

Your static IP address in CIDR notation (e.g., /24 represents a subnet mask of 255.255.255.0).

routes

Defines the default gateway (via) to allow the system to communicate outside the local network.

nameservers

Specifies DNS servers (in this case, Google Public DNS).


4. Applying the Changes

To avoid losing connection due to a syntax error, follow these three steps:

Step A: Test the configuration

This command will check for errors and automatically roll back if you don't confirm the changes within 120 seconds.

Bash

sudo netplan try

Step B: Apply the configuration

If the test is successful, apply the settings permanently:

Bash

sudo netplan apply

Step C: Verify the status

Confirm that the IP address has been assigned to the interface:

Bash

ip addr show ens1

Important Note on YAML Formatting:

Netplan is extremely sensitive to indentation. Always use spaces, never tabs. Ensure that each level of the hierarchy is indented by exactly two spaces relative to the level above it.

Updated on