Static network IP on Ubuntu server
Configuring IP address on Ubuntu server without using any tools other than a text editor
Version 18.04
The new interfaces configuration file now lives in the /etc/netplan directory. There are two renderers: NetworkManager and networkd.
NetworkManager renderer is mostly used on desktop computers and networkd on servers. If you want NetworkManager to control the network interfaces, use NetworkManager as the renderer, otherwise use networkd.
When you use NetworkManager as the renderer, you will use the NetworkManager GUI to manage the interfaces.
- DHCP sample file using networkd
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: ens33: dhcp4: yes dhcp6: yes
- Static configuration
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: ens33: dhcp4: no dhcp6: no addresses: [192.168.1.2/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8,8.8.4.4]
- Apply changes
sudo netplan apply
Examples
- Multiple IP addresses and multiple gateways
network: version: 2 renderer: networkd ethernets: enp3s0: addresses: - 9.0.0.9/24 - 10.0.0.10/24 - 11.0.0.11/24 #gateway4: # unset, since we configure routes below routes: - to: 0.0.0.0/0 via: 9.0.0.1 metric: 100 - to: 0.0.0.0/0 via: 10.0.0.1 metric: 100 - to: 0.0.0.0/0 via: 11.0.0.1 metric: 100
- Wireless
network: version: 2 renderer: networkd wifis: wlp2s0b1: dhcp4: no dhcp6: no addresses: [192.168.0.21/24] gateway4: 192.168.0.1 nameservers: addresses: [192.168.0.1, 8.8.8.8] access-points: "network_ssid_name": password: "**********"
- Static
network: version: 2 renderer: networkd ethernets: enp3s0: addresses: - 10.10.10.2/24 gateway4: 10.10.10.1 nameservers: search: [mydomain, otherdomain] addresses: [10.10.10.1, 1.1.1.1]
Version 16.04 and earlier
Edit network configuration file
sudo vim /etc/network/interfaces
You fill find configuration something like this
# The primary network interface -- use DHCP to find our address auto eth0 iface eth0 inet dhcp
Change it to this
# The primary network interface auto eth0 iface eth0 inet static address 192.168.1.2 gateway 192.168.1.1 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 dns-nameservers 8.8.8.8 8.8.4.4
And finally restart networking service
sudo /etc/init.d/networking restart
You should be able to ping the host on the new IP address
If you need to add static routes, you can do it here by adding the lines
up route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.180.1 dev eth0
And delete them
down route del -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.180.1 dev eth0