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.
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 :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 usedpkg-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 thenetworkd
renderer instead ofNetworkManager
because the configuration is on a server.This is set a static IP instruction in Ubuntu-Server 18.04 and 20.04
Then replace your configuration, for example, the following lines:
Apply changes:
In case you run into some issues execute:
[NOTE]:
/24
is equivalent with255.255.255.0
ens160
is your ethernet name, you can get it using$ ifconfig
I've found another way using cloud-init.
/etc/cloud/cloud.cfg.d/50-curtin-networking.cfg
- the contents seem to be the same as they would be in /etc/netplan.clean, reboot and re-initialize
cloud-init
with this command: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 withifconfig
.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; theoptional: 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 isnetworkd
so there is no need to declare it. Taking the information from your post, it should be like this:Once you save the file, run
sudo netplan --debug apply
thedebug
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/
Config file is in YAML format: Don't use TAB when configuring the file. It only works with SPACE.
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:
(Note: My network device is
ens160
- noteth0
- 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.
This is the setting what make it work.
restart the server
change eth0 to your adapter, find out your adapter using ifconfig.
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
Edit / create the cloud-init network configuration file
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:
You can find more configuration options at https://netplan.io/examples
Reload the cloud-init configuration. This will reboot your server.
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:
Then use command
$sudo netplan apply
to apply the changes.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
Apply changes:
$ sudo netplan apply