How to Configure Static IP on Ubuntu from the terminal

How to Configure Static IP on Ubuntu from the terminal

Hi. In this article, we'll be learning how to configure a Static IP address on a Linux (Ubuntu) machine both from the terminal and the GUI using Ubuntu's default network configuration tool called "Netplan".

What is Netplan

Netplan is the default network configuration tool for managing networks in Ubuntu. It uses YAML files that are located in /etc/netplan/. It supports two renderers to control network interfaces on Ubuntu-based systems which are NetworkManager and systemd(networkd). NetworkManager is mostly used on the client's desktop machine as it is GUI based while systemd(networkd) is used on servers without a GUI.

A. Configuring Static IP using NetworkManager (GUI)

By default, Ubuntu uses NetworkManager as the renderer to control network interfaces as shown in the image below

To configure the static IP through the GUI which uses NetworkManager behind the scenes:

  1. Go to your Network settings as shown in the image below

  1. Select IPV4

  2. Enter the IP details and click Apply as shown in the image below

B. Configuring Static IP from the terminal using systemd(networkd)

The network interface can be managed either by NetworkManager or networkd, but it cannot be managed by both at the same time. Therefore, to configure a network interface using networkd, the NetworkManager renderer has to be stopped and disabled.
NOTE: Most of the commands have to be run as root (sudo)

  • Step 1:

stop and disable NetworkManager using "systemctl stop" and "systemctl disable"

  • Step 2:

Create a YAML file

Using your editor of choice (vim, nano etc), create the YAML file and save it in /etc/netplan

  1. version - version of the network configuration format

  2. renderer - make ubuntu use networkd as the renderer

  3. ethernets - device type (an ethernet card in this case. A wireless card will be wifis)

  4. enp0s3 - the network interface to be statically configured

  5. dhcp4 - false. Disable dynamic ip configuration

  6. addresses - configure the ip address

  7. gateway4 - configure the default gateway

  8. nameservers - configure the dns servers

  • Step 3:

Apply the changes "netplan apply"

Step 4:

Verify the IP "ifconfig"

Verify the default gateway "route -n"

Voila! QED!