Thursday, February 13, 2025

How to Step-by-Step Guide to Setting a Static IP on Ubuntu Server with Netplan

If you're running an Ubuntu server, especially the latest 24.04 LTS release, assigning a static IP address is a fundamental step for ensuring reliable network connectivity. Unlike dynamic IPs assigned by DHCP which can change, a static IP remains constant, crucial for servers that need to be consistently accessible for services, websites, or remote management.

For those of us who've spent time managing CentOS or Rocky Linux servers, Ubuntu Server's network configuration might seem a little different at first. Since Ubuntu 18.04, netplan has been the default network configuration tool. If you're like me and need a quick refresher, or if you're setting up your Ubuntu 24.04 LTS server for the first time, this guide will walk you through setting a static IP address using netplan.

Step-by-Step Guide to Setting a Static IP

  1. Access Your Server and Navigate to Netplan Directory:

    First, log in to your Ubuntu Server 24.04 LTS. Open your terminal and navigate to the netplan configuration directory:

    Bash
    cd /etc/netplan
    
  2. List Netplan Configuration Files:

    List the files in this directory. You'll typically find a YAML file, often named something like 50-cloud-init.yaml. The exact filename might vary.

    Bash
    ls /etc/netplan/
    

    You should see output similar to:

    50-cloud-init.yaml
    
  3. Edit the Netplan Configuration File:

    Now, you need to edit this YAML file using a text editor like nano or vi. Important: Make sure you use sudo as you'll need administrative privileges to modify this file.

    Bash
    sudo nano /etc/netplan/50-cloud-init.yaml
    

    Note: If your filename is different from 50-cloud-init.yaml, replace it in the command above.

  4. Configure Static IP Settings:

    Inside the YAML file, you'll need to modify or add the following configuration. Carefully replace the example IP addresses, gateway, and DNS servers with your network's specific details.

    Here's a template you can adapt:

    YAML
    network:
      version: 2
      renderer: networkd
      ethernets:
        ens160:  # Replace 'ens160' with your actual interface name (e.g., eth0, enp0s3)
          dhcp4: no
          addresses:
            - 172.16.129.135/24  # Your desired static IP address and subnet mask (e.g., 192.168.1.10/24)
          routes:
            - to: default
              via: 172.16.129.1    # Your network's gateway IP address (e.g., 192.168.1.1)
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4] # Your preferred DNS server addresses (e.g., Google Public DNS)
    

    Understanding the Configuration:

    • network: The root element for network configuration.
    • version: 2: Specifies the Netplan configuration version.
    • renderer: networkd: Indicates that networkd will be used as the network renderer. This is the standard renderer for Ubuntu Server.
    • ethernets:: Defines Ethernet interface configurations.
    • ens160:: Replace this with your actual network interface name. To find your interface name, you can use the command ip link show or ifconfig -a. Common names include eth0, ens3, ens160, enp0s3, etc.
    • dhcp4: no: Disables DHCP for IPv4 on this interface, ensuring a static IP.
    • addresses:: Specifies the static IPv4 address and subnet mask in CIDR notation (e.g., 192.168.1.10/24).
      • 172.16.129.135/24: Example IP address. Change this to your desired static IP. /24 represents a subnet mask of 255.255.255.0.
    • routes:: Defines network routes.
      • to: default: Specifies the default route (for all traffic not on the local network).
      • via: 172.16.129.1: Replace this with your gateway IP address. This is the IP address of your router or the device that connects your network to the internet.
    • nameservers:: Defines DNS server settings.
      • addresses: [8.8.8.8, 8.8.4.4]: Replace these with your preferred DNS server IP addresses. 8.8.8.8 and 8.8.4.4 are Google Public DNS servers. You can use other public DNS servers or your ISP's DNS servers.
  5. Apply the Netplan Configuration:

    After saving and closing the configuration file, apply the changes using the following command:

    Bash
    sudo netplan apply
    

    This command will apply the new network configuration.

  6. Verify the Static IP Address:

    To confirm that your static IP address has been successfully set, use the ip addr show command, replacing ens160 with your interface name:

    Bash
    ip addr show ens160
    

    Look for the inet line within the output for your interface. You should see your newly configured static IP address. For example, you might see output similar to this, confirming the IP 172.16.129.135 is set:

    ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.16.129.135  netmask 255.255.255.0  broadcast 172.16.129.255
            inet6 fe80::20c:29ff:fea2:528e  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:a2:52:8e  txqueuelen 1000  (Ethernet)
            RX packets 34914  bytes 9756627 (9.7 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 17552  bytes 1901185 (1.9 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            device interrupt 44  memory 0x3fe00000-3fe20000
    

Congratulations! You have successfully configured a static IP address on your Ubuntu Server 24.04 LTS. Your server will now have a consistent IP address, making it reliably accessible for all your server needs.

Troubleshooting Tips:

  • Incorrect Interface Name: Double-check that you've used the correct network interface name in the Netplan configuration file.
  • YAML Syntax Errors: YAML files are sensitive to spacing and indentation. Ensure your file is correctly formatted. Use spaces for indentation, not tabs. You can use online YAML validators to check for syntax errors.
  • Network Connectivity Issues: If you lose network connectivity after applying the configuration, verify your IP address, subnet mask, gateway, and DNS server settings are correct and compatible with your network.

Let me know in the comments if you have any questions or run into any issues! Happy server managing!

0 comments:

Post a Comment