Uncover the infinite in IT

Table of Contents
< All Topics

Setting Up Static and Dynamic IP Addresses

Tutorial Overview

Learn how to configure a static IP address manually on a Debian-based system using the Netplan tool and revert to DHCP (dynamic IP address) configuration when needed.

Prerequisites

  • Root or sudo access.

Steps

Step 1: Identify Your Network Interface

1. Open your terminal.

2. Run the following command to list network interfaces:

ip a
  • This command lists all available network interfaces. Typically, you’ll see interfaces named something like eth0, eth1 (Ethernet) or wlan0, wlan1 (Wi-Fi).
  • Look for your active interface, usually listed under “state UP.”

3. Note the name of the active network interface. For example, assume it’s eth0 for Ethernet.

Step 2: Set a Static IP Address Using Netplan

1. Open the Netplan configuration file for your network interface. Typically, it will be found in /etc/netplan/.

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

2. Update the configuration to set a static IP. Replace eth0 with your actual interface name. Here’s an example configuration:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.10/24
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
  • dhcp4: no disables DHCP, enabling static configuration.
  • addresses specifies the desired static IP address (192.168.1.10) and subnet mask (/24 for 255.255.255.0).
  • gateway4 defines the default gateway, usually your router’s IP.
  • nameservers points to DNS servers, here set to Google’s DNS servers (8.8.8.8 and 8.8.4.4).

3. Apply the changes:

sudo netplan apply

4. Verify the IP address with:

ip a

You should now see the assigned static IP address for your network interface.

Step 3: Revert to DHCP (Dynamic IP Address)

1. Open the Netplan configuration file again:

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

2. Change dhcp4: no to dhcp4: yes, then delete the addresses, gateway4, and nameservers sections:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: yes

3. Apply the changes:

sudo netplan apply

4. Verify that DHCP is now enabled by checking the IP address:

ip a