How to Configure Static IP on Ubuntu
Tags: discuss, ubuntu
Published: 2022-02-21 07:19:09

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8txfhf9hj7i9ko8akzz.PNG)
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
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzjsmf3vm7w7f2sq71ic.PNG)
By default the file looks like the one below:
![Step 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mhqtrnqsmxmkc0nlrwf.PNG)
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]
```
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 use` ctrl + 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe1hf0q1fos8218gwq6a.PNG)

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