How to Configure a Static IP Address on Ubuntu Using Netplan: Complete Guide for Reliable Networking

1. What Is a Static IP Address?

Understanding Static IP Addresses

An IP address is a unique identifier assigned to each device on a network. In most environments, IP addresses are assigned dynamically using DHCP (Dynamic Host Configuration Protocol). However, for specific use cases, using a static IP address is beneficial. A static IP address ensures that the same address is assigned to the device even after reconnection, enabling consistent and predictable access.

Benefits of a Static IP Address

The main advantages of using a static IP address include:

  • Stable Connectivity: The same address is retained after reboot or reconnection, allowing stable access to devices such as servers, printers, and network storage (NAS).
  • Simplified Access Management: It makes remote access and port forwarding easier. For example, when setting up SSH or Remote Desktop access, using a consistent IP address simplifies configuration.
  • Improved Network Security: By assigning static IPs, you can restrict access to specific devices, enhancing security management within the network.

2. Preparing to Configure a Static IP Address on Ubuntu

Check Your Ubuntu Version

The procedure for configuring a static IP address may vary depending on your Ubuntu version. You can verify the version using the following command:

lsb_release -a

Since Ubuntu 17.10, Netplan has been introduced as the network configuration tool. Netplan allows network configurations to be written concisely using YAML-based settings.

Checking and Installing Netplan

To verify if Netplan is installed, use the following command:

netplan --version

If Netplan is not installed, you can install it with:

sudo apt install netplan.io

Now you are ready to configure a static IP address.

3. Configuring a Static IP Address Using Netplan

Creating the YAML Configuration File

To configure a static IP address using Netplan, first create a configuration file. The file is typically stored under /etc/netplan/. Although the filename is arbitrary, using a numeric prefix such as 99-config.yaml is recommended. Create and open the configuration file with the following command:

sudo nano /etc/netplan/99-config.yaml

Editing the YAML File

Next, edit the YAML file to define the static IP configuration:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: false
      addresses: [192.168.1.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 1.1.1.1]
  • enp3s0 is the network interface name. Use the ip addr command to confirm the correct interface name.
  • addresses specifies the desired static IP address along with its subnet mask (e.g., 192.168.1.100/24).
  • gateway4 sets the router’s IP address.
  • nameservers lists DNS server addresses. It is common to use public DNS such as Google’s (8.8.8.8).

Saving and Applying the Configuration

Once the file is saved, apply the settings with the following command:

sudo netplan apply

This will reconfigure the network and apply the static IP address.

4. Verifying the Configuration and Troubleshooting

How to Verify the Configuration

To confirm that the static IP has been applied correctly, run:

ip addr show enp3s0

This command displays the IP address assigned to the enp3s0 interface, allowing you to verify successful configuration.

Common Errors and How to Fix Them

Indentation Errors
YAML files rely heavily on proper indentation. Incorrect indentation will result in errors. If you see messages like “Error in network definition,” review your spacing and ensure proper alignment.

Unstable Network Connectivity
If the network becomes unstable after applying a static IP, IP address conflicts are a common cause. Make sure no other device is using the same address and modify the address if necessary.

5. Advanced Usage: Multiple Network Interfaces and Bridge Connections

Configuring Multiple Interfaces

Certain setups require assigning different IP addresses to multiple network interfaces. Netplan can configure multiple interfaces simultaneously. Example:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: false
      addresses: [192.168.1.100/24]
    enp4s0:
      dhcp4: false
      addresses: [192.168.2.100/24]

This configuration assigns different static IP addresses to enp3s0 and enp4s0.

Setting Up VLANs and Bridges

Bridge connections and VLANs are especially useful in virtual machine and container environments. Netplan makes it easy to configure bridges. Example:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
  bridges:
    br0:
      interfaces: [eth0]
      addresses: [192.168.1.50/24]
      gateway4: 192.168.1.1

This configuration connects the eth0 interface to the bridge br0 and assigns a static IP address.

6. Important Notes When Using Static IP Addresses

Avoid IP Address Conflicts

When assigning static IP addresses, ensure no other device uses the same address. Conflicts can cause instability and communication issues. Always review network usage before assigning IPs.

Verify Network Configuration

Subnet masks and gateway settings depend on your network structure. Incorrect subnet masks can prevent devices from communicating within the same network. Consult your router documentation or network administrator for correct values.

年収訴求