How to Set a Static IP Address on Ubuntu | Beginner’s Guide with Troubleshooting

1. What is a Static IP Address?

Basics of a Static IP Address

An IP address is a unique number used to identify each device on a network. In most network environments, IP addresses are dynamically assigned using DHCP (Dynamic Host Configuration Protocol). However, in certain situations, using a “static IP address” can be beneficial. A static IP address ensures that a device retains the same IP address even after reconnection, allowing for consistent access.

Benefits of a Static IP Address

The main advantages of using a static IP address include:

  • Stable Connection: Since the same address is used even after reconnection or reboot, devices such as servers, printers, and network-attached storage (NAS) can maintain a stable connection.
  • Simplified Access Management: Setting up remote access and port forwarding becomes easier. For example, when configuring SSH or Remote Desktop connections, using the same IP address simplifies the setup process.
  • Enhanced Network Security: By utilizing a static IP address, it becomes easier to restrict access to specific devices, improving overall security management.

2. Preparing to Set a Static IP Address on Ubuntu

Checking Your Ubuntu Version

First, check your Ubuntu version since the steps for configuring a static IP address may vary depending on the version. Use the following command to check your version:

lsb_release -a

Starting from Ubuntu 17.10, Netplan has been introduced as the default network configuration tool. Netplan simplifies network settings using YAML configuration files.

Checking and Installing Netplan

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

netplan --version

If Netplan is not installed, you can install it using this command:

sudo apt install netplan.io

With this, you are now ready to configure a static IP address.

3. Configuring a Static IP Address with Netplan

Creating a YAML Configuration File

To configure a static IP using Netplan, first, create a configuration file. This file is typically stored in /etc/netplan/, and it is recommended to use a filename like “99-config.yaml” with numbers for better organization. Use the following command to create and open the file in a text editor:

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

Editing the YAML Configuration File

Next, add the static IP settings to the newly created YAML file:

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. To check the correct interface name, use the command ip addr.
  • addresses specifies the static IP address and subnet mask (e.g., 192.168.1.100/24).
  • gateway4 defines the router’s IP address.
  • nameservers lists the DNS server addresses. Using Google’s DNS (8.8.8.8, etc.) is a common practice.

Saving and Applying the Configuration

After saving the file, apply the settings using the following command:

sudo netplan apply

This will restart the network, applying the new static IP address.

4. Verifying the Configuration and Troubleshooting

How to Verify the Configuration

To check if the static IP address has been correctly applied, use this command:

ip addr show enp3s0

This will display the assigned IP address for the enp3s0 interface, confirming whether the configuration was successful.

Common Errors and How to Fix Them

Indentation Errors
YAML files are highly sensitive to indentation (leading spaces). If you encounter an error message like “Error in network definition,” check to ensure that spaces are correctly placed at the beginning of each line.

Unstable Network Connection
If your network connection becomes unstable after setting a static IP, it is often due to an IP address conflict. Verify that no other device is using the same IP address and change the assigned address if necessary.

5. Advanced Setup: Multiple Network Interfaces and Bridge Connections

Configuring Multiple Interfaces

In some network environments, you may need to assign different IP addresses to multiple network interfaces. With Netplan, you can configure multiple interfaces simultaneously. Here is an 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 IP addresses to both enp3s0 and enp4s0 interfaces.

Setting Up VLANs and Bridge Connections

Bridge connections and VLAN (Virtual Local Area Network) settings are particularly useful in virtual machine and container environments. Netplan makes it easy to set up bridge connections. Here’s an example of a bridge configuration:

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 Considerations When Setting a Static IP Address

Avoiding IP Address Conflicts

When setting a static IP address, ensure that it does not conflict with other devices on the network. If two devices share the same IP address, communication issues can arise, making the network connection unstable. To prevent conflicts, verify the existing IP addresses before assigning a new one.

Checking Network Configuration

Subnet masks and gateway settings depend on the network structure. If the subnet mask is incorrectly configured, devices within the same network may fail to communicate. Consult your router settings or network administrator to confirm the correct values.

年収訴求