How to set a Static IP in Ubuntu

Prior to 17.10

First, we need to shutdown the interface:

sudo ifdown eth0

To specify a static connection we need to change the Ethernet's interface configuration in /etc/network/interfaces:

iface eth0 inet static
address 192.168.0.101
gateway 192.168.0.254
netmask 255.255.255.0
dns-nameservers 8.8.8.8 8.8.4.4

If you are using IPv6 static addresses, add an additional block it /etc/network/interfaces:

iface eth0 inet6 static
address 2001:db8:ff01:6fe::1e3a
netmask 64
gateway 2001:db8:ff01:6fe::1
dns-nameservers 2001:4860:4860::8888 2001:4860:4860::8844

Keep in mind that only 3 name servers will end up in the resolv.conf file if you use both the IPv4 block and the IPv6 block above. This is an issue related to the libc resolver limitation of a maximum of 3 name servers…

Finally, restart the interface to put the new settings into effect:

sudo ifup eth0

17.10 and after

cat > /tmp/01-netcfg.yaml << EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
     dhcp4: no
     dhcp6: no
     accept-ra: no
     addresses:
     - 192.168.1.222/24
     - fd35:cf89:7706:1e92::222/64
     gateway4: 192.168.1.1
     gateway6: fd35:cf39:7706:1e92:1
     nameservers:
       addresses:
       - 8.8.8.8
       - 8.8.4.4
       search:
       - lab.example.com
       - home.example.com
EOF
 
sudo mv /tmp/01-netcfg.yaml /etc/netplan/
sudo rm /etc/netplan/50-cloud-init.yaml

Once ready apply changes with:

sudo netplan --debug apply