Step-by-Step Guide: How to Change DHCP to Static IP Address on Ubuntu Using the CLI



Introduction:

In this tutorial, we'll walk you through the process of changing your Ubuntu machine's dynamic DHCP-assigned IP address to a static one using the command-line interface (CLI). By assigning a static IP address, you gain more control over your network configuration and ensure consistent connectivity. Let's get started!


**Prerequisites:**

- Ubuntu system with administrative privileges (sudo access).

- Basic familiarity with the Ubuntu command line.


**Step 1: Check Your Network Interface**

Open a terminal by pressing `Ctrl + Alt + T` or searching for "Terminal" in the applications menu. First, identify the network interface you want to configure. You can use the `ifconfig` or `ip addr` command to list available interfaces. Typically, it's named something like "eth0" or "ens33."


**Step 2: Back Up Your Current Network Configuration**

Before making any changes, it's wise to back up your current network configuration. Use the following command to create a backup of your network configuration file:

```bash

sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak

```


**Step 3: Edit the Netplan Configuration File**

Open the Netplan configuration file in your preferred text editor (e.g., nano or vim). In this example, we'll use nano:

```bash

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

```

You should see a YAML configuration file. Modify the file to set a static IP address, gateway, and DNS servers. Replace the content with the following, adjusting the values to match your network configuration:


```yaml

network:

  version: 2

  renderer: networkd

  ethernets:

    <YourInterfaceName>:

      dhcp4: no

      addresses:

        - <YourStaticIPAddress>/24  # Example: 192.168.1.100/24

      gateway4: <YourGatewayIPAddress>  # Example: 192.168.1.1

      nameservers:

        addresses: [<PrimaryDNS>, <SecondaryDNS>]  # Example: [8.8.8.8, 8.8.4.4]

```


**Step 4: Apply the Configuration**

Save the changes (in nano, it's `Ctrl + O`, then press `Enter`) and exit (`Ctrl + X`). Now, apply the new configuration:

```bash

sudo netplan apply

```


**Step 5: Verify Your New Configuration**

Check if the static IP address is applied successfully:

```bash

ip addr show <YourInterfaceName>

```


**Step 6: Test Your Network Connection**

Ensure that your network connection is functioning correctly by pinging a website or another device on your network:

```bash

ping www.example.com

```


**Step 7: Reboot (Optional)**

To make sure your changes persist after a system reboot, consider restarting your machine:

```bash

sudo reboot

```


Congratulations! You've successfully changed your Ubuntu machine's DHCP-assigned IP address to a static one using the CLI. Your system will now maintain the specified IP configuration even after reboots.

Comments