How to Setup Static IP in Ubuntu

How to Setup Static IP in Ubuntu

By default, most systems use DHCP server to automatically assign dynamic IP address to the system every time it logs into the network. But sometimes you may want your system to have the same fixed static IP address that does not change every time you reboot your system. Such requirements are common, if other systems connect to your system by using its IP address, or if you have web domains linked to a specific IP address. In this article, we will learn how to setup static IP in Ubuntu.

How to Setup Static IP in Ubuntu

There are two ways to setup static IP in Ubuntu – via command line and via GUI. We will look at both these approaches.

Setup Static IP in Ubuntu via Command Line

In this case, we will use netplan utility to set static IP address in Ubuntu.

1. Check network configuration

Open terminal and run the following command to get information about present network configuration.

$ ip a

The above command will display a lot of useful information about our network configuration including current IP, subnet mask, network adapter name.

In our case, the current network adapter is eth0.

6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:df:c3:ad brd ff:ff:ff:ff:ff:ff
    inet 172.23.199.129/20 brd 172.23.207.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fedf:c3ad/64 scope link
       valid_lft forever preferred_lft forever

The ip 172.23.199.129 is dynamically assigned and IP 172.23.207.255 is broadcast address.

Run the following command to note the subnet name.

$ ifconfig -a

For our example, IP is 172.23.199.129 and subnet mask is 255.255.240.0

2. Change Configuration

By default, Linux systems use netplan utility to manage network configuration. When you install netplan, it will automatically create a few .yaml files for configuration at /etc/netplan. If they are not present, you can create them using the following command.

$ sudo netplan generate 

You can edit any of them as they are all read by netplan.

$ sudo vim /etc/netplan/01-netcfg.yaml

Add the following setting to it to set the network configuration to use version 2.

network:
 version: 2

Then we will add a network renderer and set it to NetworkManager. By default it is set to systemd-networkd.

network:
 version: 2
 renderer: NetworkManager

Next, we will add the ethernet adapter details we noted earlier, and set dhcp4 to no to disable dynamic addresses.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no

Next, we will also specify the IP address that you want to set static IP address to.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no
     addresses: [172.23.207.254/20]
     gateway4: 192.168.1.1
     nameservers:
         addresses: [8.8.8.8,8.8.8.4]

Save and close the file to apply the changes.

Test the changes by running the following command.

$ sudo netplan try

Once again run the following command to see the new settings.

$ sudo netplan try

Setup Static IP using GUI

Alternatively, you can also set static IP using GUI. Search for settings. Click Network or Wifi depending on whose settings you want to modify. To open interface settings, click gear icon next to interface name.

Select Manual in IPv4 tab and enter your static IP address, netmask and gateway. Click Apply button.

In this article, we have learnt how to setup static IP address. It is very useful if you want to fix the IP address of your system, instead of keeping it dynamic.

Also read:

How to Setup Automatic Updates in Ubuntu
How to Disable Automatic Updates in Ubuntu
How to Change SSH Port in Ubuntu
How to Allow Only SSH Using Iptables
How to Disable Functions in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *