Static IP Configuration in Ubuntu using Netplan
Netplan is a network setup tool released in Ubuntu 17.10 that allows users to customize network settings such as IP addresses, gateways, and DNS servers in a YAML-based configuration file. Users may quickly set up and maintain their network connections with Netplan, including the assignment of static IP addresses. This can be especially helpful when a device needs a constant IP address to connect to other networked devices or to offer services like file sharing or web hosting. In this situation, setting up a static IP address in Ubuntu using Netplan can offer a dependable and effective network connection.
To configure IP address in Ubuntu using netplan, follow below simple steps:
NOTE: This steps were tested on Ubuntu 20.04 and Ubuntu 22.04, they can also work on Ubuntu 18.04
- Open the terminal
- Navigate to the Netplan configuration directory by entering the command below:
cd /etc/netplan
- Check the current configuration file by entering the command below:
ls
- Create a new configuration file or edit the available configuration file by entering the command below:
sudo nano 01-netcfg.yaml
- Within the configuration file, add/edit the following lines:
- Replace
STATIC_IP
with the static IP address you want to use,GATEWAY_IP
with the IP address of your network gateway, andDNS_SERVER_1
andDNS_SERVER_2
with the IP addresses of your DNS servers. Note that you should replaceeth0
with the name of the network interface you want to configure (you can find the name by using theip a
command).
Below is a an example of a fully configured interface:network: version: 2 renderer: networkd ethernets: enp3s0: addresses: - 192.168.0.201/24 routes: - to: default via: 192.168.0.1 nameservers: addresses: [192.168.0.1, 8.8.8.8]
- Save the changes to the configuration file by pressing
Ctrl + X
, thenY
, thenEnter
. - Apply the changes by entering the command below:
sudo netplan apply
.
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [STATIC_IP/24]
routes:
- to: default
via: GATEWAY_IP
nameservers:
addresses: [DNS_SERVER_1, DNS_SERVER_2]
Static IP Configuration in Ubuntu by editing /etc/network/interfaces
Prior to Ubuntu 17.10, the usual approach for managing network interfaces was to configure a static IP address by modifying the /etc/network/interfaces file. Hence, modifying the /etc/network/interfaces file is still the suggested method for creating a static IP address if you are using Ubuntu 16 or a previous version. With the help of this technique, you may easily and successfully set up a dependable network connection with a constant IP address, gateway, and DNS settings.
Follow below simple steps to achieve this:
- Open the terminal and enter the command below to open the network interfaces file.
- Find the section for the network interface you want to set a static IP for (usually "eth0" or "wlan0"). It should look something like this:
iface eth0 inet dhcp
- Change "dhcp" to "static" and add the following lines below it:
address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1
NOTE:Change the IP addresses to the desired static IP, subnet mask, and default gateway for your network.
- Save and exit the file.
- Restart the network interface with the command below.
$ sudo nano /etc/network/interfaces
$ sudo ifdown eth0
$ sudo ifup eth0
You should now be able to access the network with the static IP you set.
Enjoy!!