42

I've seen some people saying the file to set static ip is still /etc/network/interfaces

And I've seen other people saying that in 18.04 it's now on /etc/netplan (which people seem unhappy about)

I've tried putting this:

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

In my /etc/netplan/50-cloud-init.yaml and doing sudo netplan apply but that just kills the servers connection to the internet.

7
  • Is it a desktop or a server?
    – user68186
    Apr 29 '18 at 5:10
  • Is this a fresh 18.04 install or upgrade from another version? Apr 29 '18 at 5:16
  • Sorry I should've said this in the text, its a fresh install of 18.04 server.
    – final20
    Apr 29 '18 at 5:25
  • The most simple solution for me was, to specify a static IPv4 address right during installation (together with subnet, gateway, etc.). Simply fill out some wizard fields, no messing with configuration files.
    – Uwe Keim
    Oct 9 '18 at 10:01
  • You can also do this on routers. Steps are self-explanatory in the router config. Jan 12 '19 at 10:25

11 Answers 11

32

All the answers telling you to directly edit /etc/netplan/50-cloud-init.yaml are wrong since CloudInit is used and will generate that file. In Ubuntu 18.04.2 it is clearly written inside the file :

$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eno1:
            dhcp4: true
    version: 2

So you should not edit that file but the one under /etc/cloud/cloud.cfg.d/ if you still want to use CloudInit.

Another way is to completely disable CloudInit first by creating an empty file /etc/cloud/cloud-init.disabled (see https://cloudinit.readthedocs.io/en/latest/topics/boot.html) and then the other answers are OK. Under Ubuntu 18.04.2 I had to use dpkg-reconfigure cloud-init to let it take into account the file /etc/cloud/cloud-init.disabled. I think this is a little bit weird.

I suggest you to rename the file (not the right name since 50-cloud-init.yaml let us think it still uses CloudInit).

Then you may end up with a file name /etc/netplan/01-netcfg.yaml which contains the configuration below. Note the use of the networkd renderer instead of NetworkManager because the configuration is on a server.

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: no
      addresses: [192.168.1.246/24]
      gateway4: 192.168.1.1
      nameservers:
         addresses: [192.168.1.1]
6
  • 4
    It works great. This should be the best answer. 50-cloud-init.yaml as stated shouldn't be modified.
    – Relic
    Jun 26 '19 at 13:11
  • 3
    If still using CloudInit, you need to do a sudo cloud-init clean -r to get the change to take, as per veperr's answer (at least for me on Ubuntu Server 18.04.3). Aug 8 '19 at 11:59
  • 1
    ...plus the renderer line is no longer valid it seems (and is missing in the base version of the file that you edit). Aug 27 '19 at 10:26
  • Simply adding a custom file under /etc/netplan/##-*.yaml works as well. Such as: "/etc/netplan/99-custom-network.yaml". This also follows the netplan.io/examples instructions. In "conf.d" type config areas you should always opt for a custom/new file if possible instead of editing installed package files. It's why we have these "numbered file" areas. These default #'ed files can frequently get overwritten. And YES: If you have a "50-cloud-init.yaml", it should NOT be used. It is system generated as noted in comments. Why people keep posting this as an answer is anyone's guess?
    – B. Shea
    Dec 13 '21 at 15:59
  • 1
    Yep. But, you can add it directly to /etc/netplan. Any new files under this area will persist through reboots. I gave you +1 for 1: saying not to edit cloud-init, and 2: Creating a new file in a numbered area (& not editing ANY preexisting/system ones). Note: You do not even necessarily need to disable anything so long as you are updating the same network interface. A higher numbered yaml will override all previous repeated directives (or should). So "50-" will be read in AFTER "01-". If you renamed your file "99-" you wouldn't have to disable "50-" - if that makes sense. See my answer
    – B. Shea
    Dec 13 '21 at 16:34
15

This is set a static IP instruction in Ubuntu-Server 18.04 and 20.04

$ sudo nano /etc/netplan/50-cloud-init.yaml

Then replace your configuration, for example, the following lines:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:  # Your ethernet name.
     dhcp4: no
     addresses: [192.168.1.137/24]
     gateway4: 192.168.1.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]

Apply changes:

$ sudo netplan apply

In case you run into some issues execute:

$ sudo netplan --debug apply

[NOTE]:

  • /24 is equivalent with 255.255.255.0
  • ens160 is your ethernet name, you can get it using $ ifconfig
  • Ubuntu 16.04 and 14.04 network-interface configuration have a different method.
  • The file is in YAML format: Use spaces, no tabs.
7
  • not able to ping after assigning static IP address
    – gjois
    Mar 3 '19 at 14:55
  • OK....I'm able to ping after doing service networking restart
    – gjois
    Mar 3 '19 at 15:42
  • 5
    I wouldn't do that since that file is generated by CloudInit Mar 13 '19 at 7:48
  • only works if your ethernet is "ens160", check your ethernet "ls /sys/class/net/" and replace "ens160" with it. Mar 17 '20 at 3:40
  • @sailfish009 I mentioned it before in the NOTE section in my answer. Mar 17 '20 at 6:11
8

I've found another way using cloud-init.

  1. Edit the file /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg - the contents seem to be the same as they would be in /etc/netplan.
  2. clean, reboot and re-initialize cloud-init with this command:

    sudo cloud-init clean -r
    
  3. That's it! Your system will reboot, cloud-init will re-initialize and pickup the change in /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg and apply them to /etc/netplan/50-cloud-init.yaml and all will be well. Verify with ifconfig.

1
  • 1
    2 things. -> 1. Better to create your own "99-myconfig.cfg" file under /etc/cloud/cloud.cfg.d/ so it's read in last and will overwrite any previous configs and will not be overwritten by possible system updates. 2. ifconfig is defunct. Use ip command now. For example ip addr will show same thing as ifconfig use to.
    – B. Shea
    Dec 7 '21 at 16:11
7

Ubuntu 18.04 uses now Netplan to configure the network interfaces, so the configuration must be done in the file /etc/netplan/50-cloud-init.yaml, the documentation advises not to mess anymore with the old file /etc/network/interfaces. I have used this configuration with my Ubuntu Server virtual machine and it works so far, just make sure the info is correct; the optional: true setting supposedly speeds up the booting time by not verifying if the interface is connected or not, this is default, also there is no need to declare values not used, for example DHCP, if they are absent they are taken as disabled, also the default renderer in Ubuntu Server is networkd so there is no need to declare it. Taking the information from your post, it should be like this:

network:
    ethernets:
        eht0:
            addresses:
            - 192.168.1.9/24
            gateway4: 192.168.1.1
            nameservers:
                addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]
            optional: true
    version: 2

Once you save the file, run sudo netplan --debug apply the debug flag will output more info and can help to detect any errors. Check the ethernet cable, if in virtual review the VM configuration. If using a WLAN I have read that it is a bit more tricky to setup but I haven't yet set up a machine connected to WiFi with this server version.

If you want more info about Netplan there is a website, it has some basic configuration examples.

https://netplan.io/

1
  • Wrong. DO NOT EDIT 50-cloud-init.yaml, or any existing system generated files!
    – B. Shea
    Dec 13 '21 at 16:40
6

Config file is in YAML format: Don't use TAB when configuring the file. It only works with SPACE.

2

Writing a new answer as so many are just wrong.

Do not edit 50-cloud-init.yaml, 00-installer-config.yaml or ANY system generated files/package files.
Again: DO NOT USE THESE.

From https://netplan.io/examples/ :

"To configure netplan, save configuration files under /etc/netplan/ with a .yaml extension (e.g. /etc/netplan/config.yaml) .."

It does NOT say edit files already placed inside it. The system did that.

Also consult man netplan-generate for the rules governing how the network configurations are read from /etc/netplan/*.yaml (and elsewhere).

To correctly update netplan area to use a static IP over the default DHCP:

Edit/Create a new file (the prepended number+dash and .yaml extension are important):

sudo nano /etc/netplan/99-custom-network.yaml

Add your properly formatted YAML to this file. A static IP example:

network:
  ethernets:
    ens160:
      dhcp4: false
      addresses: [10.10.10.5/24]
      gateway4: 10.10.10.1
      nameservers:
        addresses: [10.10.10.100,8.8.8.8,8.8.4.4]
  version: 2

Save. (Note: My network device is ens160 - not eth0 - adjust as needed.)

Preferably reboot at this point, or do a sudo netplan apply.

Test/Done.

This follows the netplan.io instructions as well as the general rule of not editing any existing/installed files when possible. In /etc/netplan/ and similar conf.d/ type config areas you should always opt for a high numbered custom/new file (if possible) instead of editing any installed package files.

It's why they have numbered files in these configuration areas (in /etc/netplan/ and others). The higher the number on the file equates to when it is read in.

Therefore, something with "99-" prepended on it will generally be read in last and OVERRIDE anything that repeated before it. Therefore, if a network interface is set to DHCP in "00-installer-config.yaml", and/or "50-cloud.init.yaml", the settings for the same interface in a "99-*.yaml" file will override everything else read in previously.

The default yaml files can get overwritten if system generated (such as "50-cloud-init.yaml"). So, editing the default *.yaml(s) will technically "work", but you should avoid using them.

1

Network configuration in 18.04 is managed via netplan and configured with cloud-init. To change your network configuration edit the 50-curtin-networking.cfg file in /etc/cloud/cloud.cfg.d/. If this file does not exist then create it.

Find your interface name

ip address show

Edit / create the cloud-init network configuration file

sudo nano /etc/cloud/cloud.cfg.d/50-curtin-networking.cfg

To set a static IP address, use the addresses key, which takes a list of (IPv4 or IPv6), addresses along with the subnet prefix length (e.g. /24). Gateway and DNS information can be provided as well:

network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 192.168.1.9/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]

You can find more configuration options at https://netplan.io/examples

Reload the cloud-init configuration. This will reboot your server.

sudo cloud-init clean -r
0

This is the setting what make it work.

$sudo nano /etc/netplan/50-cloud-init.yaml

network:
   ethernets:
     eth0:          
     addresses:
     - 192.168.1.9/24
     dhcp: false
     gateway4: 192.168.1.1
     nameservers:
        addresses:
        - 192.168.1.1
        - 8.8.8.8
        - 8.8.4.4
        search: []
  version: 2  

$sudo netplan apply

restart the server

change eth0 to your adapter, find out your adapter using ifconfig.

1
  • Wrong. DO NOT EDIT 50-cloud-init.yaml, or any existing system generated files!
    – B. Shea
    Dec 13 '21 at 16:41
0

To find available ethernet interfaces use ip link show

Then edit the 50-cloud-init.yaml file using $sudo nano /etc/netplan/50-cloud-init.yaml

Add the configuration for available interfaces like eth0: and eth1:

network:
   ethernets:
     eth0:          
     addresses:
     - 192.168.1.9/24
     dhcp: false
     gateway4: 192.168.1.1
     nameservers:
        addresses:
        - 192.168.1.1
        - 8.8.8.8
        - 8.8.4.4
        search: []
     eth0:
     addresses:
     - 192.168.1.9/24
     dhcp: false
  version: 2  

Then use command $sudo netplan apply to apply the changes.

1
  • Wrong. DO NOT EDIT 50-cloud-init.yaml, or any existing system generated files!
    – B. Shea
    Dec 13 '21 at 16:41
0

How to setup a static IP on Ubuntu Server 18.04

Then edit the 50-cloud-init.yaml file using$sudo vim /etc/netplan/50-cloud-init.yaml


network:
  ethernets:
    eno1:
        addresses:
        - 10.0.1.10/24
        dhcp4: false
        gateway4: 10.0.1.1
        nameservers:
            addresses:
            - 10.0.1.2
            search: []
version: 2

Apply changes:

$ sudo netplan apply

3
  • 1
    I wouldn't do that since that file is generated by CloudInit. Mar 13 '19 at 7:47
  • 1
    Why oh why is every guide to setting a static IP on 18.04 telling me to edit a yaml file that says it is a dynamically created file that will not persist? Another cruel joke from the Ubuntu developers that think it is ok to just break things by default...
    – Bigtexun
    Mar 20 '19 at 18:13
  • Wrong. DO NOT EDIT 50-cloud-init.yaml, or any existing system generated files!
    – B. Shea
    Dec 13 '21 at 16:42
0

This worked for me:

dhcp4: no
            dhcp6: no
            addresses: [172.23.4.2/24, ]
            gateway4: 172.23.4.254
            nameservers:
                    addresses: [172.23.4.1, ]

[172.23.4.2/24, ] is the additional thing I did on the yaml file.

Reference: https://serverspace.io/support/help/how-to-configure-static-ip-address-on-ubuntu-18-04/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.