Set Static IP Address on Ubuntu Server
-
To set a static IP address, we are going to use
netplan.
First make sure that it is installed using this command:
sudo apt install openvswitch-switch-dpdk
Next we need to figure out what interface our server is using. To do that we need to run:
ip route showAfter we run the command we are looking for the interface. When I ran it, I got this:
default via 192.168.1.1 dev ens18 proto dhcp src 192.168.1.110 metric 100 192.168.1.0/24 dev ens18 proto kernel scope link src 192.168.1.110 metric 100 192.168.1.1 dev ens18 proto dhcp scope link src 192.168.1.110 metric 100We need to find the line with
192.168.1.0/24. So my interface is going to beens18
Next we need to make the configuration file. I am going to use
nanoto do this.sudo nano /etc/netplan/99_config.yamlIn the configuration file we need to add this:
network: version: 2 renderer: networkd ethernets: ens18: dhcp4: no addresses: - 192.168.1.5/24 routes: - to: default via: 192.168.1.1 nameservers: addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]Be sure to change the line under
ethernetsto your interface like I have here. Under addresses, this is where we are going to put the IP address we want to use:192.168.1.5/24. If you are using nano, pressCtrl+XthenEnterto save the file.
After we make the configuration file, we need to set the permissions for the file. To do that we use this command:
sudo chmod 600 /etc/netplan/*This will set all the configuration files in that directory to have the correct permissions.
Now all we need to do is run this command:
sudo netplan applyThis will apply the configuration, now your IP address is set.
One last thing that I like to do, is to remove this
192.168.1.1 dev ens18 proto dhcp scope link src 192.168.1.110 metric 100configuration. To do that we need to run:sudo ip route del 192.168.1.1This will delete that configuration. Now everything should be set.