A Guide to Setting Up a Static IP Address on Ubuntu Linux

|

|

The post provides instructions on how to configure a static IP address on Ubuntu Linux using Netplan, the default network configuration tool as of Ubuntu 17.10. It details how to disable dynamic IP assignment and set up a static IP address via terminal commands and by manipulating .yaml configuration files. Additionally, it offers a guide…

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 set up static IP addresses on some machines that you don’t want to have 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 with its MAC address reserved for a particular address will always receive that IP address. If you’re not using a dedicated system for IP management, configuring an individual system with a unique IP address may be your next option.

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

Netplan currently supports two renderers, NetworkManager and Systemd-network. NetworkManager is mostly used on Desktop machines, while Systemd-network 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 ‘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, etc.

Below is a sample file for a network interface using the network as a renderer using DHCP. The network 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 the Ubuntu server, you must 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 open, 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 ensure 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 applied, run the commands below to view the IP address configuration details.

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 set up static IP addresses on Ubuntu desktops, click the network icon in the top menu, then 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 set up the IP, Network, Gateway, and DNS addresses. Click Apply to save your changes.

This shows 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. Please use the comment form below if you find any errors above or have something to add.

Like this:



2 responses to “A Guide to Setting Up a Static IP Address on Ubuntu Linux”

  1. Catt Avatar
    Catt

    Does this work on a ubuntu on Virtualbox as well or is it just for actual machines?

  2. Eric Avatar
    Eric

    Near the top of the article it says:

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

    It appears there was an autocorrect typo. The error being the second renderer is “Networkd” not “Network” without the D.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.