How to Configure Static IP Address on Ubuntu Linux

This post shows students and new users how to configure a static or fixed IP address on Ubuntu Linux. Generally, IP addresses are assigned dynamically and you probably would not need to assign fixed IP addresses for your computers.

However, in certain situations, you may need to setup static IP addresses on some machines that you don’t want having random IP addresses. In many environments, static IP addresses are assigned by a router or DHCP servers using reservations. An IP reservation is a process where the same IP addresses are reserved and assigned only to computers with corresponding MAC addresses.

The computer which has its MAC address reserved to a particular address will always receive that IP address.

If you’re not going to be using a dedicated system for IP management, then configuring individual system with unique IP address may be your next option.

Since the release of Ubuntu 17.10, Netplan is now the default network configuration tool to manage network settings replacing the configuration file /etc/network/interfaces that was used in previous versions.

Netplan currently supports two renderers NetworkManager and Systemd-networkd. NetworkManager is mostly used on Desktop machines while the Systemd-networkd is used on servers without a GUI.

The new interfaces configuration file now lives in the /etc/netplan directory. There are two renderers. NetworkManager and networkd.

When you use NetworkManager as the renderer, you will use the NetworkManager GUI to manage the interfaces. Ubuntu uses a ‘Predictable Network Interface Names’ that, by default, start with en[letter][number].

Netplan configuration files are stored in the /etc/netplan directory and have the extension .yaml. You’ll probably find one or two YAML files in this directory.

The network configuration file will differ from setup to setup. Some may be named 01-netcfg.yaml, 50-cloud-init.yaml, etcs.

Below is a sample file for a network interface using networkd as renderer using DHCP. Networkd uses the command line to configure the network interfaces.

sudo nano /etc/netplan/*.yaml

You should see a similar DHCP server for servers like the one below:

network: 
  ethernets: 
    enp0s3: 
      dhcp4: yes
  renderer: networkd
  version: 2

On Desktops, you may see something like the one below:

network: 
  renderer: NetworkManager
  version: 2

How to configure IP address with networkd

To configure a static IP address using the new Netplan tool on Ubuntu server, you’ll need to edit the *.yaml file in the /etc/netplan/ directory.

If your Ubuntu cloud instance is provisioned with cloud-init, you’ll need to disable it before setting a static IP address.

To do so create the following file by running the commands below:

sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

With the file opens, paste the line below and save.

network: {config: disabled}

After editing the file above, run the commands below to open the network configuration file for the network interfaces. The device type can be ethernetsbondsbridges, or vlans.

sudo nano /etc/netplan/*.yaml

Then change the dhcp4 value to no, and configure the static IP address details, including DNS and Gateway addresses as shown below.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        - 192.168.1.0/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8, 8.8.4.4]

When you’re done editing the file, save it and exit.

You will want to make sure that the file meets YAML code indent standards. If not probably indented, you’ll get an error.

Run the commands below to apply your changes.

sudo netplan apply

To validate that your changes are apply, run the commands below to view the IP address configuration detals.

ip addr show dev enp0s3

It should display similar lines like the one below:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e0:e9:4d brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3
       valid_lft 976sec preferred_lft 976sec
    inet6 fe80::2aa0:522f:4f82:8d5b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

How to setup static IPs on Ubuntu desktop

To setup static IP addresses on Ubuntu desktops, click the network icon in the top menu, the select Wired Connected –> Wired Settings.

This will open the GNOME Network configuration settings. Click on the cog icon.

Then choose Manual for IPv4 Method, and setup the IP, Network, Gateway and/or DNS addresses. Click the Apply to save your changes.

This how how to configure static IP addresses on Ubuntu systems.

For more about Netplan, visit this site.

Conclusion:

This post showed you how to set up static IP addresses on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.