48

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.

Pablo Bianchi
  • 17,371
final20
  • 481

11 Answers11

38

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]
Ludovic Kuty
  • 488
  • 4
  • 9
16

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.
Benyamin Jafari
  • 4,234
  • 4
  • 27
  • 37
7

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.

zx485
  • 2,865
veperr
  • 79
6

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/

6

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

HubbleT
  • 161
6

Writing a new answer as so many are just wrong.

https://netplan.readthedocs.io/en/stable/howto/ :

"To configure Netplan, save configuration files in the /etc/netplan/ directory with a .yaml extension (e.g. /etc/netplan/config.yaml), then run sudo netplan apply."

The installation/system may have already added a file (such as 50-cloud-init) to this area.

50-cloud-init.yaml, 00-installer-config.yaml should be overridden not edited:


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

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

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

Note: My network device is "ens160" - not "eth0" - adjust as needed.

Save new file.
Then run sudo netplan apply.
Make sure your network interface looks right and is working using ip ad / ping.
Reboot and retest. The host should be set to a static address. DONE.

Simply delete, or rename your file and rerun 'netplan apply' to restore previous settings.


More Information

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

Adding your own file 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 (when possible) instead of editing any installer and/or 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.

Generally these YAML files will NOT get overwritten, but that isn't valid logic to not follow the conf.d "standard" of using custom files to override and avoid editing any installed files. It doesn't take any extra time. Drop a file in netplan. Done. So, there's no excuse as I have witnessed in comments of "well, it's worked so far..".

So, editing the default netplan *.yaml(s) will technically "work", but you should create your own when possible.

B. Shea
  • 1,252
2

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
Ryan
  • 25
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.

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.

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

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/