Sometimes you may need to configure static IP address in Linux. In this article, we will look at how to set up static IP in Ubuntu using Netplan utility. You can use it to change IP address permanently from command line.
How To Set Up Static IP in Ubuntu
Here are the steps to set up static IP in Ubuntu using Netplan.
Netplan allows you to easily configure network interface using YAML files to describe the interface. Once you have created the YAML (.yaml) file, Netplan will generate configuration files for the renderer tool of your choice. Netplan supports two renderers – NetworkManager and Systemd-networkd. We will use networkd renderer for our example.
You can set a single static IP, multiple static IP addresses, or an IP range. We will set static IP range 192.168.100.0-192.168.100.255 in this article.
1. Identify Ethernet Interface
Run the following command to identify the ethernet interface you want to configure.
$ ip link
You will see an output like the following, which lists all available network interfaces – one loopback interface and the rest are ethernet interfaces.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff
For our example, we will use ens2 interface. lo is a loopback interface and cannot be used for this purpose.
Also read : How to Enable Key Based Authentication in SSH
2. Edit Netplan Configuration File
When you install Netplan, it will automatically create several YAML files (.yaml) at /etc/netplan.
If there are no YAML files at /etc/netplan on your system, run the following command to generate them.
$ sudo netplan generate
You can edit any of the available files at /etc/netplan. They are all read by Netplan.
We will edit 01-netcfg.yaml
$ sudo vim /etc/netplan/01-netcfg.yaml
Also read : How to Run Cron Job Every 5,10,15 minutes
Add the following configuration lines in bold under ethernet section. Replace ens2 with the name of your ethernet interface, that you want to update.
network: version: 2 renderer: networkd ethernets: ens2: dhcp4: no dhcp6: no addresses: - 192.168.100.21/24 gateway4: 192.168.100.1 nameservers: addresses: [220.127.116.11, 18.104.22.168]
You need to specify an IP range in CIDR notation. For example, we have specified 192.168.100.21/24 for IP range 192.168.100.0 – 192.168.100.255.
You can use a third-party tool to obtain CIDR notation of your IP range.
Now let us look at each of the added lines.
- dhcp4: no and dhcp6: no disable DHCP
- addresses: Specify the static IP address range under
addresses:you can add one or more IPv4 or IPv6 IP addresses to be assigned to the network interface. You can also mention an IP range.
- gateway4: Specify the gateway
- nameservers: Set the IP addresses of the nameservers.
Save and close the file.
Also read : How to Use rsync Command in Linux
3. Apply changes
Run the following command to apply changes.
$ sudo netplan apply
Verify the changes by running the following command. Replace ens2 with your ethernet interface that you want to be modified.
ip addr show dev ens2
You will see the following output. The part in bold indicates the static IP range configured for your network interface.
2: ens2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff inet 192.168.121.221/24 brd 192.168.121.255 scope global dynamic ens3 valid_lft 3575sec preferred_lft 3575sec inet6 fe80::5054:ff:feb0:f500/64 scope link valid_lft forever preferred_lft forever