Netplan static IP on Ubuntu configuration

In this tutorial, we will discuss a netplan static IP configuration on Ubuntu Linux. Netplan allows for straightforward network IP address configuration using human-readable data-serialization language YAML. The article will also discuss a default Netplan network settings and the location of the Netplan configuration file.

You have two options when configuring the IP address on your Ubuntu system, and that is either a static IP address or DHCP. A static IP address allows you to manually select your IP address by configuring it on the Linux system, whereas DHCP relies on the router or DHCP server to lease you an IP address – either a reserved one or the next available one that is currently free, depending on the setup.

In this tutorial you will learn how to:

  • Use netplan to set static IP on Ubuntu Server
  • Configure netplan to set static IP on Ubuntu Server
Netplan static ip on Ubuntu configuration
Netplan static ip on Ubuntu configuration

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any version of Ubuntu Linux system
Software Netplan.io
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Configure static IP address using Netplan




Netplan network configuration had been first introduced starting from Ubuntu 18.04, hence Netplan is available to all new Ubuntu from this version and higher. Ubuntu uses Netplan to configure static IP addesses, which utilizes a YAML syntax. This makes it easy to configure a static IP and change minute details in the future if necessary. Let’s get started with some basic understating on how netplan works on Ubuntu.

Netplan allows network configuration via both: networkd daemon or NetworkManager. networkd daemon is mainly used for server configuration, whereas NetworkManager is used by GUI users. To switch between both you need to specify renderer explicitly via netplan configuration file.

NOTE
If no renderer is specified within the netplan’s configuration file, then the default handler for the network configuration on this particular device will be networkd daemon.

The netplan configuration file location is set to /etc/netplan/ directory. Other possible locations are /lib/netplan/ and /run/netplan/. Depending on your Ubuntu installation the actual Netplan configuration file can take one of the following three forms:

  • 01-netcfg.yaml
  • 01-network-manager-all.yaml
  • 50-cloud-init.yaml

In case you cannot find your configuration file, you may attempt to generate the new netplan config by executing the below command:

$ sudo netplan generate
DID YOU KNOW YOU CAN CONFIGURE STATIC IP USING DHCP SERVER?
Most likely your current Ubuntu system uses DHCP server to configure its networking settings. Hence, the configuration of your IP address is dynamic. In many scenarios, simply configuring your router or local DHCP server is a preferred way to set a static address to any host regardless of the operating system in use. Check your router manual and assign the static IP address to your host based on its MAC address using the DHCP service.

Netplan static ip step by step instructions

Ubuntu Server

  1. To configure a static IP address on your Ubuntu server you need to find and modify a relevant netplan network configuration file. See the above section for all possible Netplan configuration file locations and forms.For example you might find there a default netplan configuration file called 01-netcfg.yaml with a following content instructing the networkd deamon to configure your network interface via DHCP:
    # This file describes the network interfaces available on your system
    # For more information, see netplan(5).
    network:
      version: 2
      renderer: networkd
      ethernets:
        enp0s3:
          dhcp4: yes
    
  2. To set your network interface enp0s3 to static IP address 192.168.1.222 with gateway 192.168.1.1 and DNS server as 8.8.8.8 and 8.8.4.4 replace the above configuration with the one below.
    WARNING
    You must adhere to a correct code indent for each line of the block. In other words, the number of spaces before each configuration stanza matters. Otherwise you may end up with an error message similar to:
    Invalid YAML at //etc/netplan/01-netcfg.yaml line 7 column 6: did not find expected key
    # This file describes the network interfaces available on your system
    # For more information, see netplan(5).
    network:
      version: 2
      renderer: networkd
      ethernets:
        enp0s3:
         dhcp4: no
         addresses: [192.168.1.222/24]
         gateway4: 192.168.1.1
         nameservers:
           addresses: [8.8.8.8,8.8.4.4]
    
  3. Once ready apply the new Netplan configuration changes with the following commands:
    $ sudo netplan apply
    

    In case you run into some issues execute:

    $ sudo netplan --debug apply
    


Ubuntu Desktop

This is a preferred way of setting the static IP address on Ubuntu Desktop. It is important to note the if your Ubuntu system is using Netplan to configure network on your hosts you need to set the renderer within the Netplan’s configuraiton file to renderer:NetworkManager.

Having the Netplan’s renderer set to networkd daemon will result in the Wired Unmanaged error.

  1. Click on top right network icon and select settings corresponding to the network interface you wish to assign with the static IP address.

    Select wired or Wifi network settings
    Select wired or Wifi network settings
  2. Next, click on the gear box icon next to your network connection you wish to configure. This could be wired or wireless connection.
    Click on the network you wish to configure
    Click on the network you wish to configure
  3. Select IPv4 from the top menu.
    Select IPv4 to start configuring a new IP address
    Select IPv4 to start configuring a new IP address
  4. Choose Manual for the IPv4 settings, enter your desired configuration and hit Apply.

    Set your desired static IP address
    Set your desired static IP address
  5. Restart your network by ON/OFF switch.

    Restart to apply new network settings
    Restart to apply new network settings
  6. Check your new static IP address configuration.

    Check you new static IP configuration
    Check you new static IP configuration

Closing Thoughts




In this tutorial, you saw how to configure a static IP address on an Ubuntu Linux system. We have covered a GUI and command line method here, so that users with a desktop system or server (without GUI) will be able to follow along. Your static IP address settings will persist after reboots, and you will need to manually reconfigure the IP address or revert to DHCP in order to stop using it.