How to Configure Static IP Address on Ubuntu 18.04

Channel: Linux
Abstract: you can configure a static IP using the interfaces configuration file found in /etc/network/interfaces By defaultthe interface connected to the networ

Sometimes, you may find the need to configure a static IP address on your system. A  perfect example is when you want to make it a server and host services so that it is always reached using a permanent/static IP address. In this new post, we'll take look at how you can configure static IP address on Ubuntu 18.04. There are four main ways of achieving this:

  1. Using Ubuntu Desktop
  2. Using Netplan
  3. Using interfaces file
  4. Using DHCP service
Configure static IP address on Ubuntu 18.04 using Ubuntu Desktop

Using the Ubuntu desktop GUI is one of the easiest and most preferred methods of configuring a static IP. To achieve this, Head out to the top right corner and click on the 'Network' settings icon and select on the interface connected to the network. In my case, I'm connected to the network over the LAN, so I'll head out to 'Wired Connected" the to "Wired settings"

In the next Window, Navigate and Click on "Network" option. To the right, click on the gear icon adjacent to the interface as shown below.

To view current settings, click on 'Details' tab

To configure a static IP address, Click on the IPv4 option and Click on "Manual". Next, type your preferred IP address, netmask, DNS  and default gateway.

Turn off the automatic Toggle

Once satisfied with your settings configuration, Click on the "Apply" button. Next, Restart the network - Turn OFF and ON - for the changes to take effect.

You can now go ahead and verify your new settings.

Configuring Static IP using netplan

Canonical introduced a new tool for network management since the advent of Ubuntu 17.10 . The /etc/network/interfaces file is no longer used and instead a new network management utility called Netplan has taken its place. Netplan's configuration files are found in /etc/netplan/. 

The default configuration file is /etc/netplan/01-netcfg.yaml.

Open the default configuration file using your favorite text editor

nano /etc/netplan/01-netcfg.yaml

Output

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes

To configure a static IP address, where the IP is 192.168.43.245, subnet mask 255.255.255.0, default gateway is 192.168.43.1 and nameservers 192.168.43.1 & 8.8.8.8,  replace this configuration with the configuration shown below

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
     dhcp4: no
     addresses: [192.168.43.245/24]
     gateway4: 192.168.43.1
     nameservers:
       addresses: [192.168.43.1,8.8.8.8]

Save and Exit

Finally, run

sudo netplan apply

Later on, check the IP address using the ifconfig  command to confirm the changes.

Configuring Static IP using interfaces file

Alternatively, you can configure a static IP using the interfaces configuration file found in

/etc/network/interfaces

By default, the configuration file contains the following lines

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

The next step is to identify the network interface that we need to assign a static IP address. To achieve this, run the following command

ip a

This lists all the interfaces attached to your system

Output

ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c0:7f:03 brd ff:ff:ff:ff:ff:ff
    inet 192.168.43.245/24 brd 192.168.43.255 scope global dynamic noprefixroute enp0s3
       valid_lft 2317sec preferred_lft 2317sec
    inet6 fe80::a4ba:e64c:9105:f617/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

Alternatively, you can use

ip link show
1: lo:  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: enp0s3:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:c0:7f:03 brd ff:ff:ff:ff:ff:ff

As seen in the above 2 outputs, the interface connected to the network is enp0s3

To configure the address as a static IP open the /etc/network/interfaces file and append the following lines

auto enp0s3
iface enp0s3 inet static
        address 192.168.43.245
        netmask 255.255.255.0
        gateway 192.168.43.1
        dns-nameservers 192.168.43.1 8.8.8.8
  • auto enp0s3

This enables interface enp0s3

  • iface enp0s3 inet static

This sets the interface to use static addressing.

  • address 192.168.43.245

This is the static IP address

  • gateway 192.168.43.1

This specifies the gateway

  • dns-nameservers 192.168.43.1 8.8.8.8

These are the dns servers

Finally, Save the configuration file, and reboot or restart networking using the commands shown below

ip  flush enp0s3

then restart networking service

systemctl restart netwroking.service

Later on, check your IP configuration to verify the accuracy of the configuration.

How to set DHCP IP on Ubuntu 18.04

To set a dynamic IP address for interface enp0s3, you can leave the default netplan YAML configuration file the way it is, or if a static IP was set, you can configure DHCP the following configuration

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: yes
dhcp6: yes

Next, as root run

netplan apply

restart networking

systemctl restart networking

Check your IP address using ifconfig or ip a

At this point, your system should be able to pick an IP address from the router dynamically.

Read also

  • netctl - Setup Wifi and Static IP on Arch Linux

As we have seen in this post there are many ways you can configure static IP address on Ubuntu 18.04. You can go for the GUI desktop, use the interfaces file or default netplan file. If you want to revert back to DHCP, you can also follow the last step to accomplish that.

Ref From: linoxide
Channels:

Related articles