47

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
  • 15,657
final20
  • 471

11 Answers11

36

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]
  • 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). – Stuart Rossiter Aug 08 '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). – Stuart Rossiter 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 https://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
  • I wrote "Then you may end up with a file name /etc/netplan/01-netcfg.yaml". Isn't it the same thing you propose ? – Ludovic Kuty Dec 13 '21 at 16:04
  • 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
  • I have 20+ servers running for 4 years with regular upgrades and maintenance. Never once, 50-cloud-init.yaml has been reset or overwritten. From what I have noticed, cloud-init (as the name says) is run once, at server creation and then it won't be automatically reissued again. – Dario Fumagalli Mar 02 '22 at 03:35
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.
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.

zx485
  • 2,426
veperr
  • 81
  • 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 07 '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/

6

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

HubbleT
  • 61
  • 1
  • 3
3

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.

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

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

The installer/system did that. Now you need to override it.

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

To correctly update the 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

(Note: My network device is ens160 - not eth0 - adjust as needed.)
Save.
Then do a sudo netplan apply.
Make sure your network interface looks right and is working (ip ad / ping).
Then do a reboot. Retest.

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.

Generally these installed 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 (usually) "work", but you should avoid using them when possible.

B. Shea
  • 1,188
  • 14
  • 17
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
  • 4
1

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

  • 1
    I wouldn't do that since that file is generated by CloudInit. – Ludovic Kuty Mar 13 '19 at 07: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
  • 1
    Wrong. DO NOT EDIT 50-cloud-init.yaml, or any existing system generated files! – B. Shea Dec 13 '21 at 16:42
  • @B.Shea I have 20+ servers running for 4 years with regular upgrades and maintenance. Never once, 50-cloud-init.yaml has been reset or overwritten. For what I have noticed, cloud-init (as the name says) is run once, at server creation and then it won't be automatically reissued again. – Dario Fumagalli Mar 02 '22 at 03:34
  • 1
    @DarioFumagalli Only 4 years? You're using broken logic: Just because nothing "broke" doesn't mean you are doing it the proper way. You should NEVER edit system installed config files if there is a way to avoid it. And there is clearly a way to avoid it in this case.. – B. Shea Mar 02 '22 at 13:07
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/