Ubuntu 18.04 Server - Set Static IP With Netplan
Netplan is the new networking system that is taking over from editing the /etc/networking/interfaces file. The most common thing you will want to do is set a static IP. You can do that by simply editing the /etc/netplan/01-netcfg.yaml file.
sudo vim /etc/netplan/01-netcfg.yaml
The file will look like below:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens2:
dhcp4: yes
Here is an example configuration to set a static IP of 192.168.1.100 with your router/gateway being at 192.168.1.1 and wanting to use OpenDNS dns servers:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens2:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 208.67.222.222
- 208.67.220.220
Before I could paste, I had to fix an "issue" with vim first with the following commands:
echo "set mouse=" > .vimrc
sudo echo "set mouse=" | sudo tee -a /root/.vimrc Ubuntu Blog - Ubuntu Bionic: Netplan
Once you have successfully edited your configuration file, you can apply it with:
sudo netplan apply
Now your server should be using the new IP.
Testing Configuration Before Applying
The following command will check the changes before applying them and waiting for 2 minutes. During this time, one can press enter/return on the keyboard to keep the changes. If one does not do this then the changes are reverted. This is particularly useful in case you mess up your network, and can't send any SSH commands to undo what you have just done.
sudo netplan try
Extra Info
Netplan is actually pretty powerful and you can split your configuration up across multiple files in multiple locations as well as configuring bridges/bonding etc. I recommend reading these two posts for more information:
- Ubuntu Blog - Ubuntu Bionic: Netplan
- websiteforstudents.com - Configuring Static IP Addresses On Ubuntu 17.10 Servers
DHCP With Manually Set DNS Nameservers
If you want to use DHCP for setting your IP address, but you prefer to manually set your DNS nameserver, then you would do something like below:
network:
version: 2
renderer: networkd
ethernets:
ens18:
dhcp4: true
dhcp4-overrides:
use-dns: no
nameservers:
addresses:
- 192.168.0.222
192.168.0.222
.
Appendix
PHP Code To Generate Static IP YAML File
<?php
$interfaceName = "enp1s0";
$staticIp = "192.168.0.3";
$cidr = 16;
$gateway = "192.168.0.1";
$nameserverIps = array("8.8.8.8", "8.8.4.4");
$networkConfig = array(
'network' => array(
"version" => "2",
"renderer" => "networkd",
"ethernets" => array(
$interfaceName => array(
"addresses" => array(
"{$staticIp}/{$cidr}",
),
"gateway4" => "$gateway",
"nameservers" => array(
"addresses" => $nameserverIps,
),
),
),
),
);
print yaml_emit($networkConfig) . PHP_EOL;
References
- TechRepublic - How to create a bridge network on Linux with Netplan
- Ask Ubuntu - Can netplan configured nameservers supersede (not merge with) the DHCP nameservers?
First published: 16th August 2018