DEV Community

Cover image for How to Configure Static IP on Ubuntu
Jeff Odhiambo
Jeff Odhiambo

Posted on

How to Configure Static IP on Ubuntu

Get the name of the network interface and the default gateway.
We can use ifconfig that will display the interfaces with their IP address, to be able to use ifconfig you’ll be required to install net-tools in some OS.
Run sudo apt-get install -y net-tools. or run ip link.
ifconfig
First time
In my case the interphase where ill configure the static ip is enp0s3.
Note: Your interphase might be different from mine.
In order to assign a static IP address, we will modify a file in /etc/netplan. We will cd into /etc/netplan. Run the command below



cd /etc/netplan


Enter fullscreen mode Exit fullscreen mode

Then ls to list its content, the name of the file might be different depending on the system configuration. In my case, it is named 01-network-manager-all.yaml
Second time
By default the file looks like the one below:
Step 3
Each Netplan Yaml file starts with the network key that has at least two required elements. The first required element is the version of the network configuration format, and the second one is the device type. The device type can be ethernets, bridges, bonds, or vlans. In this case we will be using the ethernet.
Under the device type in the case of ethernets is where we can configure our interphases e.g the enp0s3 that can be configured to obtain IP addresses from DHCP server by setting dhcp4 to yes or true e.g dhcp4: yes or dhcp4: true.
The configuration above also includes the renderer type. If you installed Ubuntu in server mode, the renderer is configured to use networkd as the back end, but in my case, it is set to NetworkManager since am using ubuntu desktop in this illustration not ubuntu server.

To assign a static IP address to enp0s3 interface, edit the file as follows:
Set DHCP to dhcp4: no. This is done so it doesn't take ip addresses from the DHCP.
Specify the static IP address of your choice, in my case I'll use 192.168.10.10/24. Under addresses: you can add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface.
Specify the gateway gateway4: 192.168.10.1
Under nameservers, set the IP addresses of the nameservers if any, addresses: [8.8.8.8, 1.1.1.1]
The final file should be similar to the one below.



network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        [192.168.10.10/24]
      gateway4: 192.168.10.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]


Enter fullscreen mode Exit fullscreen mode

Note

  • Make sure to follow the YAML code indent standards when editing .yaml file.
  • If there are syntax errors in the configuration, the changes will not be applied.

Once done save and close the file.
While using nano usectrl + X followed by Y then enter to exit.
Then apply the changes using:
sudo netplan apply
Verify the changes by typing:

ip addr show dev ens3 or ifconfig then you'll be able to see the changes
Final

In the next article, we will be able to configure DNS.

Top comments (3)

Collapse
 
smartjeff profile image
Jeff Odhiambo

Hello fellow DEVs, are there better alternatives on how to configure static IP address

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
smartjeff profile image
Jeff Odhiambo

that's actually more easy, how about if the server has no graphical interphase