Configure Static IP Addresses on Ubuntu 18.04 LTS Server

I have just started testing Ubuntu 18.04 . The first thing I noticed was how different it handles network interfaces. The way Ubuntu manages network interfaces has completely changed.

Have you heard of NetPlan? Probably not, if you have, then you’re a step ahead of many. NetPlan is a new network configuration tool introduced in Ubuntu 17.10 to manage network settings.

It can be used write simple YAML description of the required network interfaces with what they should be configured to do; and it will generate the required configuration for a chosen renderer tool.

This new tool replaces the static interfaces (/etc/network/interfaces) file that had previously been used to configure Ubuntu network interfaces. Now you must use /etc/netplan/*.yaml to configure Ubuntu interfaces.

The new interfaces configuration file now lives in the /etc/netplan directory. There are two renderers. NetworkManager and networkd.

NetworkManager renderer is mostly used on desktop computers and networkd on servers. If you want NetworkManager to control the network interfaces, use NetworkManager as the renderer, otherwise use networkd.

Netplan configuration files are stored in the /etc/netplan directory and have the extension .yaml. You’ll probably find one or two YAML files in this directory.

The network configuration file will differ from setup to setup. Some may be named 01-netcfg.yaml, 50-cloud-init.yaml, etcs.

Below is a sample file for a network interface using networkd as renderer using DHCP. Networkd uses the command line to configure the network interfaces.

sudo nano /etc/netplan/*.yaml

You should see a similar DHCP server for servers like the one below:

network: 
  ethernets: 
    enp0s3: 
      dhcp4: true
  renderer: networkd
  version: 2

On Desktops, you may see something like the one below:

network: 
  renderer: NetworkManager
  version: 2

Static IP Addresses with Networkd

To configure a static IP address using the new NetPlan tool on Ubuntu server, the file should look similar to the content below.

Run the commands below to open the network configuration file.

sudo nano /etc/netplan/*.yaml

Then change the dhcp4 value to no, then configure the static IP address details, including DNS and Gateway addresses.

network: 
  ethernets: 
    enp0s3: 
      addresses: 192.168.1.2/24
      dhcp4: false
      gateway4: "192.168.1.1"
      nameservers: 
        addresses: 
          - "8.8.8.8"
          - "8.8.4.4"
  renderer: networkd
  version: 2

When you’re done editing the file, save it and exit.

Run the commands below to apply your changes.

sudo netplan apply

To validate that your changes are apply, run the commands below to view the IP address configuration detals.

ip addr show dev enp0s3

It should display similar lines like the one below:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e0:e9:4d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3
       valid_lft 976sec preferred_lft 976sec
    inet6 fe80::2aa0:522f:4f82:8d5b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

You will want to make sure that the file meets YAML code indent standards. If not probably indented, you’ll get an error.

For more about NetPlay, visit this site.

Congratulations! You’ve just successfully configured static IP addresses on Ubuntu servers.

Enjoy!~

You may also like the post below:

37 thoughts on “Configure Static IP Addresses on Ubuntu 18.04 LTS Server

  1. After new clean installation of 18.4 server one gets two networking config files: /etc/netplan/50-cloud-init.yaml and /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg. What is the relation between them?

    1. You need to configure all the networking here:
      /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg
      then cloud-init rewrites /etc/netplan/50-cloud-init.yaml with the new config

      you need to do this:
      cloud-init clean
      in order to make efective the changes.
      Krgrds,
      Guillermo

      1. This is what worked for me:
        EDIT: /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg
        > network:
        > ethernets:
        > enp0s3:
        > addresses: [192.168.1.125/24]
        > gateway4: 192.168.1.1
        > nameservers: [8.8.8.8, 8.8.4.4]
        > dhcp4: false
        > version: 2

  2. I just removed cloud-init after a fresh Ubuntu 18.04 install without any issue, I’ll never know if cloud-init is useful or not…

  3. You know what would be mighty useful? When you say to open something you actually give the command to do it.

    Yes, I could slug through multiple GIS to find out how to do it, but I used GIS to get to this page in the hopes that you would actually give complete instructions.

  4. Nowhere in this how-to article do you specify an actual file name for the new method. Was that intentional because it kinda makes the whole article useless without it.

  5. Точно, бесполезная статья, нигде не описано, как открыть сам файл, с помощью какой команды

  6. How to make it work?
    ##
    network:
    version: 2
    renderer: networkd
    ethernets:
    enp1s0:
    addresses: [ 10.1.10.20/24 ]
    gateway4: 10.1.10.1
    nameservers:
    addresses:
    – “192.168.0.5”
    – “192.168.0.6”
    vlans:
    vlan.115:
    id: 115
    link: enp1s0
    addresses:
    – 10.1.15.5/25
    – 10.1.15.6/25
    – 10.1.15.7/25
    – 10.1.15.8/25
    gateway4: 10.1.15.1

    1. How about you try to follow the instructions? Your edits do not follow the structure of the doc as indicated.

  7. @!robot – are there any plans on fixing this? This website is mentioned in a lot of places and this method is just wrong with the standard Ubuntu server 18.04 install.

    1. Not true. these instructs worked exactly as indicated in a standard fresh installation of Ubuntu 18.04.1.

      1. Not really. It tells you to edit a fill that is not going to be preserved. Comments corrected the above article. It needs to be updated.

  8. Thanks a lot, it works!
    I was wondered by the network-manager and /etc/network/interfaces that wont work well 😀

  9. just went to edit the network address” sudo nano /etc/netplan/01-netcfg.yaml” but the page was blank so nothing to edit and I know the ip is “192.168.1.6” so where do I actually configure the static IP for my server ?

  10. This is all worthless – no explanation on the new lines, if the ‘addresses’ is supposed to be a subnet (although looks like it), and then how to specify ONE ipv4 address per box.

    The search for real instructions continues (maybe), or just scrapping ubuntu for good. This is complete BS the way they keep changing things. Redhell is better than this…

  11. is it me or should the address be addresses: 192.168.1.2/32 … instead of addresses: 192.168.1.2/24 … the latter is a range and we are describing an address for an interface.

    1. You’re not describing the range, you’re describing the netmask of the address. /24 describes it as 192.168.1.2 with netmask 255.255.255.0

  12. Who ever came up with this Format, needs to take a stop and think.
    I liked the KISS format better.

    PS I’ll be surprised if you post this
    Steve

  13. Works a treat, thanks 🙂
    Don’t know what issues all these other people are having. Not going there….

  14. Worked great, once I recalled how sensitive yaml is to white space. An unnecessary change, but it’s easy enough. Thanks for the post!

  15. its more easy with examples :
    /usr/share/doc/netplan.io/examples/windows_dhcp_server.yaml
    /usr/share/doc/netplan.io/examples/source_routing.yaml
    /usr/share/doc/netplan.io/examples/static_multiaddress.yaml
    /usr/share/doc/netplan.io/examples/loopback_interface.yaml
    /usr/share/doc/netplan.io/examples/direct_connect_gateway.yaml
    /usr/share/doc/netplan.io/examples/vlan.yaml
    /usr/share/doc/netplan.io/examples/bonding.yaml
    /usr/share/doc/netplan.io/examples/network_manager.yaml
    /usr/share/doc/netplan.io/examples/bonding_router.yaml
    /usr/share/doc/netplan.io/examples/bridge.yaml
    /usr/share/doc/netplan.io/examples/static.yaml
    /usr/share/doc/netplan.io/examples/bridge_vlan.yaml
    /usr/share/doc/netplan.io/examples/wireless.yaml
    else it is a lot of tricks

  16. Hoje esta assim:
    network:
    ethernets:
    enp4s0:
    addresses:
    – 192.168.0.241/24
    gateway4: 192.168.0.254
    nameservers:
    addresses:
    – 192.168.0.248
    search: []
    optional: true
    version: 2

    Quero deixar um ip adicional e gateway na mesma interface … como devo colocar?
    network:
    ethernets:
    enp4s0:
    addresses:
    – 192.168.0.241/24
    – 192.168.175.241/24
    gateway4:
    – 192.168.0.254
    – 192.168.175.254
    nameservers:
    addresses:
    – 192.168.0.248
    search: []
    optional: true
    version: 2

    Não deu muito certo assim.

  17. Thanks, worked but seems sensitive to tabbings and layout needs to be consistent then it worked for me but cant be bothered to look into it more than that at mo :0)

  18. internet stops working when assigned fixed IP with this
    there are two adopters NAT and host only
    IP shows both the adopters enp0s3 and enp0s8
    ping is working from host to guest but internet from guest stops working. Please suggest steps to keep internet working and have static IP also

Leave a Reply to Earnest Redwood Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.