35

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.

  • 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? – WinEunuuchs2Unix 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. – EODCraft Staff Jan 12 '19 at 10:25
22

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]
| improve this answer | |
  • 3
    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 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). – Stuart Rossiter Aug 27 '19 at 10:26
14

This is set a static IP instruction in Ubuntu-Server 18.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.
| improve this answer | |
  • not able to ping after assigning static IP address – user2763554 Mar 3 '19 at 14:55
  • OK....I'm able to ping after doing service networking restart – user2763554 Mar 3 '19 at 15:42
  • 4
    I wouldn't do that since that file is generated by CloudInit – Ludovic Kuty 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. – sailfish009 Mar 17 at 3:40
  • @sailfish009 I mentioned it before in the NOTE section in my answer. – Benyamin Jafari Mar 17 at 6:11
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.

| improve this answer | |
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/

| improve this answer | |
5

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

This was my issue.

| improve this answer | |
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
| improve this answer | |
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.

| improve this answer | |
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.

| improve this answer | |
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

| improve this answer | |
  • 1
    I wouldn't do that since that file is generated by CloudInit. – Ludovic Kuty 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

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.