Configure Static Ip Linux Ubuntu

Oct 27, 2024 · 2 mins read
Configure Static Ip Linux Ubuntu

Configuring a Static IP Address in Ubuntu Using Netplan

This guide details how to configure a static IP address on Ubuntu using Netplan, the default network configuration tool. Along with a troubleshooting section to help resolve common issues.


Summary of CLI Steps:

# Open the Netplan configuration file
sudo nano /etc/netplan/01-netcfg.yaml

# Add the static IP configuration to the file
network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
            addresses:
                - 192.168.1.10/24
            nameservers:
                search: [mydomain, otherdomain]
                addresses: [192.168.0.10, 1.1.1.1]
            routes:
                - to: default
                  via: 192.168.1.1

# Apply the configuration
sudo netplan apply

Configuring a Static IP Using the Command Line (CLI)

  1. Identify Your Network Interface

    • Use ip link or ifconfig to list network interfaces and identify the correct name (e.g., eth0 or enp3s0).
    ip link
    
  2. Edit the Netplan Configuration File

    • Open the Netplan configuration file, typically located at /etc/netplan/01-netcfg.yaml:
    sudo nano /etc/netplan/01-netcfg.yaml
    

    Note in most recent Ubuntu distros, the default file name is /etc/netplan/50-cloud-init.yaml, If you are not planning to use cloud-init, feel free to rename it to /etc/netplan/01-netcfg.yaml:

    sudo mv /etc/netplan/50-cloud-init.yaml /etc/netplan/01-netcfg.yaml
    
  3. Add Static IP Configuration

    • Modify the file to include your static IP configuration. Replace placeholders with your specific details:
     network:
         version: 2
         renderer: networkd
         ethernets:
             enp3s0:     # Interface_name
                 addresses:
                     - 192.168.1.10/24   # Static_IP
                 nameservers:
                     search: [mydomain, otherdomain]
                     addresses: [192.168.0.10, 1.1.1.1]  # DNS
                 routes:
                     - to: default
                       via: 192.168.1.1  # Gateway
    
    • <interface_name>: Replace with the network interface name.
    • <your_static_ip>: Set the static IP, e.g., 192.168.1.100.
    • <your_gateway>: Define the gateway IP, e.g., 192.168.1.1.
    • <dns_server>: Optional. Add a DNS server, e.g., 8.8.8.8.
  4. Save and Close the File

    • Save changes by pressing CTRL + O and exit with CTRL + X.
  5. Apply the Configuration

    • Use Netplan to apply the configuration:
    sudo netplan apply
    
  6. Verify Configuration

    • Confirm the new IP by checking the interface with:
    ip a show <interface_name>
    

Troubleshooting

  • Issue: Configuration doesn’t persist after reboot.
    Solution: Verify that the Netplan file syntax is correct. You can validate it with sudo netplan try.

  • Issue: Network connectivity loss after applying changes.
    Solution: Ensure IP, subnet mask, and gateway values are correct and that they don’t conflict with other devices.

  • Issue: No internet connection.
    Solution: Verify the gateway address and add DNS servers in /etc/netplan/01-netcfg.yaml, if missing.

  • Issue: Errors when applying Netplan configuration.
    Solution: Run sudo netplan try to test configuration without applying it permanently. If there are errors, check file syntax.

Share this article!