Adding a fixed IP address on an Ubuntu Server

Tiempo de lectura: 2 minutos

Configuring a fixed IP in Ubuntu is relatively simple, but the method differs depending on whether you are using the desktop version or the server version.

Since Ubuntu 18.04 onwards, the server version uses a tool called Netplan. Here is the step-by-step guide for both cases.

This is the fastest way if you have a graphical environment:

If you are using the server version or prefer the terminal, Netplan uses configuration files in YAML.

Run the following command to see how your interface is named (look for something like eth0, enp3s0, etc.):

Bash

ip a

2. Edit the configuration file

Netplan files are located in /etc/netplan/. Check what yours is named with ls /etc/netplan/ (it will be something like 01-netcfg.yaml or 50-cloud-init.yaml).

Open it with the editor nano (you’ll need admin privileges):

Bash

sudo nano /etc/netplan/01-netcfg.yaml

3. Modify the content

Modify the file so that it has the structure of the following example. Watch out for spaces! The YAML format does not allow tabs, use only spaces for indentation:

YAML

network: version: 2 renderer: networkd ethernets: enp3s0: # Change this to your network card name dhcp4: no addresses: - 192.168.1.150/24 # Your fixed IP and subnet (/24 equals 255.255.255.0) routes: - to: default via: 192.168.1.1 # Your router's IP nameservers: addresses: [8.8.8.8, 1.1.1.1] # DNS servers

4. Apply the changes

To save in nano, press Ctrl + O, then Enter, and to exit Ctrl + X.

Before applying blindly, test that the syntax is correct:

Bash

html

If everything goes well, click on the confirmation button or apply changes definitively with:

Bash

sudo netplan apply

Leave a Comment