Setting Up IP Addressing

LinuxBeginner
Practice Now

Introduction

In computer networking, an IP (Internet Protocol) address is a numerical label assigned to each device connected to a computer network. This address is used for two main functions: host or network interface identification and location addressing.

There are two primary ways an IP address can be assigned:

  • DHCP (Dynamic Host Configuration Protocol): An IP address is automatically assigned by a server on the network. This is the most common method for client devices.
  • Static IP: The IP address is manually configured and does not change. This is often used for servers, printers, or other devices that need a consistent address.

In modern Ubuntu systems, network configuration is managed by a utility called netplan. netplan uses simple YAML files to describe network interfaces.

In this lab, you will learn how to switch from a dynamic (DHCP) IP address to a static IP address and then revert the changes. This is a fundamental skill for any Linux system administrator.

Inspect the Current Network Configuration

In a real-world scenario, changing the primary IP address of a remote server can lock you out. To avoid this, a common practice is to convert the existing DHCP-assigned IP address into a static one. In this step, you will find the current network settings of your virtual machine.

First, display the network configuration for the eth0 interface using the ip command:

ip addr show eth0

You will see output similar to this. Note the inet line, which contains your current IPv4 address and subnet prefix (the number after the /).

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:16:3e:0d:25:f1 brd ff:ff:ff:ff:ff:ff
    altname enp0s5
    altname ens5
    inet 172.16.50.173/24 metric 100 brd 172.16.50.255 scope global dynamic eth0
       valid_lft 1892159962sec preferred_lft 1892159962sec
    inet6 fe80::216:3eff:fe0d:25f1/64 scope link
       valid_lft forever preferred_lft forever

Next, find your default gateway. This is the router that your VM uses to communicate with other networks.

ip route show

The line starting with default via shows you the gateway address.

default via 172.16.50.253 dev eth0 proto dhcp src 172.16.50.173 metric 100
172.16.50.0/24 dev eth0 proto kernel scope link src 172.16.50.173 metric 100

From the output above, the IP address is 172.16.50.173, the prefix is 24, and the gateway is 172.16.50.253. Your values might be different, so be sure to use the ones from your own terminal output in the next step.

Configure a Static IP Address

Now you will modify the network configuration file to assign a static IP address. This involves turning off DHCP and specifying the IP address, gateway, and DNS servers manually, using the values you just found.

First, open the configuration file with nano:

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

Now, modify the file to use your current IP, prefix, and gateway as static values. YAML files are very sensitive to indentation, so be sure to use spaces, not tabs, and match the indentation level exactly as shown.

Replace the placeholders YOUR_IP/PREFIX and YOUR_GATEWAY with the actual values you found in the previous step.

## This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - YOUR_IP/PREFIX
      routes:
        - to: default
          via: YOUR_GATEWAY
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
  version: 2

For example, if your IP was 172.16.50.173/24 and your gateway was 172.16.50.253, your file would look like this:

## This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 172.16.50.173/24
      routes:
        - to: default
          via: 172.16.50.253
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
  version: 2

Here's a breakdown of the configuration:

  • dhcp4: false: This disables the automatic DHCP client for IPv4.
  • addresses: This sets the static IP address and subnet.
  • routes: This section defines the default gateway.
  • nameservers: This sets the DNS servers. Here, we are using Google's public DNS servers.

After making the changes, save the file by pressing Ctrl+O, then press Enter to confirm the filename. Finally, exit nano by pressing Ctrl+X.

Apply Changes with netplan apply Command

In this step, you will apply the new network configuration. Simply saving the configuration file is not enough; you must tell netplan to read the new configuration and apply it to the system.

This is done with the netplan apply command. This command will parse all the .yaml files in /etc/netplan/, translate them into a format the system understands, and make the changes live.

Because this command modifies the system's network state, it requires sudo privileges.

Run the following command in your terminal:

sudo netplan apply

Note: After running this command, your connection to the LabEx VM may briefly disconnect and then automatically reconnect. If it does not reconnect on its own, please refresh your browser page.

If the command is successful and your YAML syntax is correct, it will execute without any output. If you see an error, it's likely due to an indentation or syntax mistake in your 01-netcfg.yaml file. If that happens, re-open the file and carefully check your changes against the example in the previous step.

Verify the Static IP Configuration

In this step, you will verify that the static IP address has been successfully applied. Since we used the same IP address that the VM already had, your connection should not have been interrupted.

The best way to check the current IP addresses on a Linux system is with the ip command. Run the ip addr show command again for the eth0 interface.

ip addr show eth0

You should see output that is nearly identical to what you saw in Step 1. Look for the inet line, which shows the IPv4 address. Notice that keywords like dynamic and metric are now gone, and valid_lft is set to forever.

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:16:3e:0d:25:f1 brd ff:ff:ff:ff:ff:ff
    altname enp0s5
    altname ens5
    inet 172.16.50.173/24 brd 172.16.50.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::216:3eff:fe0d:25f1/64 scope link
       valid_lft forever preferred_lft forever

The key difference is that the IP address is now configured statically by netplan rather than being leased from a DHCP server. This confirms your static IP configuration was successful.

Revert to DHCP

In this final step, you will revert the network configuration back to using DHCP. This is useful when you no longer need a static IP or want the network to manage addresses automatically. The process is the reverse of what you did before.

First, open the configuration file again with nano:

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

Now, delete the static configuration lines and set dhcp4 back to true. The entire file should look like the simple, original version:

## This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      dhcp4: true
  version: 2

Save the file (Ctrl+O, Enter) and exit nano (Ctrl+X).

Next, apply the changes just as you did before:

sudo netplan apply

Note: After running this command, your connection to the LabEx VM may briefly disconnect and then automatically reconnect. If it does not reconnect on its own, please refresh your browser page.

Finally, verify that the system is now using an IP address assigned by DHCP. Run the ip addr show eth0 command again:

ip addr show eth0

The output will now show the same inet address as before, but this time it has been assigned automatically by the network's DHCP server, indicated by the return of the dynamic keyword and a specific lease time (valid_lft). This confirms you have successfully switched back to DHCP.

Summary

Congratulations on completing the lab! You have successfully configured network settings on a modern Ubuntu system.

In this lab, you learned how to:

  • Locate the netplan configuration file in /etc/netplan/.
  • Use the nano editor with sudo to modify system configuration files.
  • Understand the basic YAML syntax for setting a static IP address versus using DHCP.
  • Apply new network configurations using the sudo netplan apply command.
  • Verify the current IP address of a network interface with the ip addr show command.

These are essential skills for managing any Linux server or desktop, giving you control over how your system identifies itself on a network.