How to Configure a Static IP Address in Ubuntu 18.04 Server

Ubuntu 18.04 Static IP Address

In Ubuntu 18.04 network configuration is managed by Netplan and it’s causing a lot of confusion and frustration for people who aren’t aware of the change.

Most people just want the short answer so here it is, you’ll need to know the IP address, gateway and nameservers.

The network config file in /etc/netplan should be named something like 50-cloud-init.yaml so open it up and configure your network like this.

Important: Tab indentation is forbidden in YAML files, you must use spaces instead.

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.1.70/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]

Once you’re done with editing the file run this command:

sudo netplan apply

You’re done, now check ifconfig and test the network.

If it doesn’t work then run this command:

sudo netplan --debug apply

This will give you hints about what might be wrong.

And here’s how to configure a static IP address and bridge for an LXD containers host.

Step 1. Finding the new configuration files in Ubuntu 18.04

Prior to 18.04 network configuration was done in /etc/network/interfaces but in Bionic Beaver the new config files are found in /etc/netplan:

[email protected]:~$ ls -lh /etc/netplan
total 4.0K
-rw-r--r-- 1 root root 388 Jan  5 22:15 01-netcfg.yaml

and here’s how it looks when properly configured for an LXD host:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
      dhcp6: no

  bridges:
    br0:
      interfaces: [ens33]
      dhcp4: no
      addresses: [192.168.1.101/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1]

As you can see my physical network is reported as ens33 and I’ve disabled all DHCP because I’m using static addressing for this machine, the br0 bridge settings are pretty straightforward and easy to understand.

After editing the config file you should apply the changes like this

[email protected]:/etc/netplan# netplan --debug apply
** (generate:1774): DEBUG: Processing input file //etc/netplan/01-netcfg.yaml..
** (generate:1774): DEBUG: starting new processing pass
** (generate:1774): DEBUG: br0: setting default backend to 1
** (generate:1774): DEBUG: ens33: setting default backend to 1
** (generate:1774): DEBUG: Generating output files..
** (generate:1774): DEBUG: NetworkManager: definition br0 is not for us (backend 1)
** (generate:1774): DEBUG: NetworkManager: definition ens33 is not for us (backend 1)
DEBUG:netplan generated networkd configuration exists, restarting networkd
DEBUG:no netplan generated NM configuration exists
DEBUG:device ens33 operstate is up, not replugging
DEBUG:netplan triggering .link rules for ens33
DEBUG:device br0 operstate is up, not replugging
DEBUG:netplan triggering .link rules for br0
DEBUG:device lo operstate is unknown, not replugging
DEBUG:netplan triggering .link rules for lo

Once you’re comfortable with the syntax you can omit the –debug switch and just use netplan apply but I recommend using the debug switch until then.

Check your configuration is working with

[email protected]:/etc/netplan# ifconfig -a
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.101  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::64df:7dff:fe1c:8b03  prefixlen 64  scopeid 0x20
        ether 69:dd:7d:1c:8d:01  txqueuelen 1000  (Ethernet)
        RX packets 12172  bytes 2051194 (2.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 504  bytes 55974 (55.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0d:29:c4:01:d3  txqueuelen 1000  (Ethernet)
        RX packets 2673456  bytes 1747246330 (1.7 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 501  bytes 57198 (57.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 46  bytes 3661 (3.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 46  bytes 3661 (3.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

and

[email protected]:/etc/netplan# networkctl status -a
● 1: lo
       Link File: /lib/systemd/network/99-default.link
    Network File: n/a
            Type: loopback
           State: carrier (unmanaged)
         Address: 127.0.0.1
                  ::1

● 8: ens33
       Link File: /lib/systemd/network/99-default.link
    Network File: /run/systemd/network/10-netplan-ens33.network
            Type: ether
           State: carrier (configured)
            Path: pci-0000:02:01.0
          Driver: e1000
          Vendor: Intel Corporation
           Model: 82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter)
      HW Address: 00:0d:29:c4:0b:d4

● 9: br0
       Link File: /lib/systemd/network/99-default.link
    Network File: /run/systemd/network/10-netplan-br0.network
            Type: ether
           State: routable (configured)
          Driver: bridge
      HW Address: 61:df:71:1b:8b:02
         Address: 192.168.1.101
                  fe80::64df:7dff:fe1c:8b03
         Gateway: 192.168.1.1 (ARADOR CORPORATION)
             DNS: 192.168.1.1

And check your connectivity with pings and such.

WARNING: If you made changes to /etc/network/interfaces you must undo them or the above won’t work and you’ll have all kinds of weird stuff going on that makes no sense to you. I hope this helped and if you’re still having problems leave a comment below and I’ll try to help.

Next: Cloud Hosting and a Powerful New AI called Maxine

7 COMMENTS

  1. How to make it work with LXD bridge?
    I have similar config, but with lxdbrd0 and after netplan apply it works like a charm, but after reboot it for some reason sets broadcast as 0.0.0.0 for bridge. After another netplan apply it works again,,

    • The main issues I had getting this to work was with DHCP but once I ensured there was nothing in my /etc/network/interfaces and that the Netplan config had dhcp4:no and dhcp6:no for both network and bridge then the problems went away, although I do recall having to manually configure the routing on one of my boxes and reboot my router but I believe that was due to a /etc/network/interfaces conflict.

  2. Another useful command of netplan is try. This allows it to read the config file and, if there are errors, roll back to the previous config. Very cool if you are trying this remotely. Also adding the –debug to the try command gives details of the internals of how it is reading the config file and where it is tripping up.

    netplan try and netplan –debug try

  3. I’m using Vi to edit the file, and when I add the “:” it changes the color of the text to blue and the “:” to red. What am I doing wrong? (I’m a newbee to the UNIX command line, but a DOS veteran.) I basically know how to open in Vi and to save my changes; that’s about all.

LEAVE A REPLY

Please enter your comment!
Please enter your name here