The use of a static IP address in Ubuntu is essential when you need to maintain a consistent IP address for a server, network device, or any other system on your network. This will also boost network security as it is hard to access by unauthorized users. While it is possible to set a static IP address using the GUI, using the command line interface (CLI) provides more control and flexibility over your network settings.
In this article, two different methods will be discussed to set static ID using the command line in Ubuntu:
- Using the Netplan Configuration File
- Using the Network Manager Command Line Interface (NMCLI)
Method 1: Using the Netplan Configuration File
A user can set a static IP Address by modifying the netplan configuration file. For this, follow the below-mentioned steps:
Step 1: Locate Network Interface Name
The first a user needs to do is to locate the network interface name, which can be done by typing the below command:
$ ip a
It can be seen from the output that “ens33” is the name of the network interface. A user can also find the network interface name by executing another command which is mentioned below:
$ nmcli d
Step 2: Modify the Netplan Configuration File
A user can do network modification using the netplan configuration file that can be accessed by executing the below command:
$ sudo nano /etc/netplan/01-network-manager-all.yaml
It opens a file using a nano text editor where a user needs to provide the information like static and default gateway IP address as shown below:
ethernets: ens33: dhcp4: no addresses: - 192.168.1.10/24 gateway4: 192.168.1.1 nameservers: addresses: [126.96.36.199, 188.8.131.52]
Save and exit the editor.
Note: A user needs to take care of the alignment and indentation just like shown in the above image or else they can face errors.
Step 3: Apply Configuration Changes
A user can apply the newly made changes to the netplan configuration file by executing the below command:
$ sudo netplan apply
Step 4: Verify the Configuration Changes
These new configurations changes can be verified by executing the below command:
$ ip a
It can be seen in the output that the IPv4 address has now been changed to the provided address.
Similarly, a user can also verify the default gateway address by executing the below command:
$ ip route
In the output the default gateway address has now also been changed.
Method 2: Using the Network Manager Command Line Interface (NMCLI)
The nmcli can be used to modify your network connection by following the steps mentioned below:
Step 1: Create a Static Connection
A user can create a new static connection by executing the command mentioned below:
$ sudo nmcli con add type ethernet con-name 'static-ip' ifname ens33 ipv4.method manual ipv4.addresses 192.168.1.10/24 gw4 192.168.1.1
In this code ‘static-ip’ is the name of the new static connection, and then the information regarding the network interface name, IPv4, and gateway address has also been provided.
This command will provide a new static IP address to the interface ‘ens33’, which will be 192.168.1.10/24 with the gateway IP address of 192.168.1.1.
Step 2: Set the DNS Server for the Static IP
To set the DNS for the already provided IPv4, the below command is used:
$ sudo nmcli con mod static-ip ipv4.dns 192.168.1.1
This means that any network traffic that goes through this connection will use the specified DNS server to resolve domain names into IP addresses.
Step 3: Activate the Static Connection
A user can activate the static connection by executing the command mentioned below:
$ sudo nmcli con up id 'static-ip'
Step 4: Static IP Verification:
The below command can be executed if the new changes have been applied or not by typing:
$ ip a
Similarly, default gateway address can also be verified using the below command:
$ ip route
The newly made changes to set the static IP have been applied and activated.
Setting a static IP address in Ubuntu using the command line is a powerful way to ensure that your devices are reliably and consistently connected. A user can set the static ip address in Ubuntu either by modifying the netplan configuration file or by using the nmcli command. Both methods have been discussed in detail with step-by-step guides for better understanding.