The Easiest Guide to Setting a Static IP on Linux !

The Easiest Guide to Setting a Static IP on Linux !

In the art of networks, a static IP is like a fortress—steadfast, unwavering. The wise tech masters know: control the address, and you control the access.

~Sun Tzu

Introduction

Ever tried to SSH into your server, only to find its IP has changed again? It’s like chasing a moving target! Configuring a static IP on your server can make life a whole lot easier, especially if you’re hosting services or need consistent access. This guide will walk you through setting up a static IP on your Linux server using Netplan—a tool that makes network configuration simpler and (almost) error-proof.

Whether you’re on Ubuntu, a Raspberry Pi, or any Linux server setup, this guide has you covered. I’ll be using my own Raspberry Pi, but these steps apply to most Ubuntu-based systems with Netplan.

Why Set a Static IP?

A static IP is a permanent IP address assigned to your server, as opposed to a dynamic IP which changes every time the device restarts or reconnects. Here’s why a static IP is crucial:

  • Reliable Remote Access: Once set, your server will always be reachable at the same IP address, making it easy to SSH in, set up web services, or connect to databases.
  • Local Network Stability: For services that need to be accessed by other devices on the same network, a static IP keeps things straightforward.
  • Avoid IP Conflicts: Setting a fixed IP helps avoid network issues by preventing IP conflicts with other devices.

Introduction to Netplan

Netplan is the network configuration tool used by recent Ubuntu versions, replacing the old /etc/network/interfaces file. It uses simple YAML files to set up network interfaces, including static IPs.

What’s with YAML? YAML files are easy to read and edit, but they’re also picky. Just one extra space in the wrong place can break the configuration, so be precise!

Step-by-Step Guide to Setting Up a Static IP with Netplan

Step 1: Connect to Your Server

First, connect to your server. If it’s remote, log in via SSH:

ssh username@your-server-ip

Note: Replace username and your-server-ip with the actual username and IP address of your server. If it’s a fresh setup, you might be using the default user (like pi for a Raspberry Pi).

Step 2: Identify Your Network Interface

Let’s figure out which network interface you’re setting up. Run:

ip a

Look for the network interfaces in the output. You’ll likely see something like eth0 (Ethernet), ens4, or wlan0 (Wi-Fi for Raspberry Pi or laptops). Note the name of the interface you want to set up with a static IP.

Step 3: Open Your Netplan Configuration File

Netplan configuration files are stored in /etc/netplan/. Let’s locate and open one:

ls /etc/netplan/

You’ll likely see a file named 00-installer-config.yaml or similar. Open this file with a text editor:

sudo nano /etc/netplan/00-installer-config.yaml

Pro Tip: Back up this file before editing it. One typo in YAML and things can get a bit, well… messy.

Step 4: Configure the Static IP

Now, let’s add the configuration for a static IP. Here’s an example template to modify:

network:
  version: 2
  ethernets:
    eth0:                    # Replace with your interface name
      dhcp4: no              # Turn off DHCP for a static IP
      addresses: [192.168.1.100/24]  # Assign your static IP and subnet
      gateway4: 192.168.1.1         # Define your network gateway
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]  # Optional DNS servers

Quick Explanation of Each Line:

  • dhcp4: no: This disables dynamic IP assignment, allowing us to specify a fixed IP.
  • addresses:: Here, you add the desired static IP and subnet in CIDR notation (like 192.168.1.100/24).
  • gateway4:: This is the IP address of your router (or network gateway). Typically, it’s something like 192.168.1.1.
  • nameservers:: Add any DNS servers if needed. Google DNS (8.8.8.8 and 8.8.4.4) is a common choice.

Careful with Spaces! YAML is space-sensitive. Make sure each level is indented by exactly two spaces.

Step 5: Apply the Netplan Configuration

Once you’ve made your edits, save and exit the editor (in Nano, that’s Ctrl + O, Enter, then Ctrl + X).

Now, apply the configuration with:

sudo netplan apply

If you don’t see any error messages, your static IP should now be set. If you do get an error, it’s likely due to incorrect indentation or a typo in the YAML file.

Step 6: Verify the Configuration

Use ip a again to confirm that your network interface now has the static IP address. You can also test connectivity by pinging the gateway:

ping -c 4 192.168.1.1  # Replace with your gateway IP

Seeing replies? Great! Your server is now reachable at the static IP you configured.

Wrapping Up

Configuring a static IP might feel like a small step, but it makes a big difference when you need reliable access to your server. Now, whether you’re running a Raspberry Pi home server, a cloud instance, or any Linux-based server, you have the knowledge to pin down its location on your network.

By Chief Geek

Hello Geeks, I'm Chief Geek, your self-appointed Chief Geek(duh) at Weekend Geeks. By day, I’m a tech enthusiast, but by night, I morph into a caffeinated zombie on steroids type super-geek, diving headfirst into the world of gadgets, games, and all things nerdy.If something plugs in, powers up, or requires a firmware update, I’m on it faster than you can sneeze.From vintage arcade classics to the latest console crazes, I’ve rekt my way to the Chicken Dinner (not to mention a few aimbots here and there! ) . Python, Java, C++ , BrainFek– you name it, I’ve probably debugged it at 3 AM while munching on cold rocks .I talk to my Raspberry Pi more than I do to actual humans. My terminal is my therapist.When I’m not buried under a pile of intel manuals, you’ll find me blogging about the latest geek trends, reverse engineering the universe, and curating the ultimate playlist of 8-bit games and keygen soundtracks. My mission? To make Weekend Geeks your go-to destination for all things geeky, quirky, and downright fun.Fun Fact: I once stayed up 48 hours straight trying to perfect my high score in Flappy-Bird (True Story). Spoiler alert: I didn’t, but I did discover the perfect ratio of caffeine to snacks.Let's make the world a little geekier, one blog post at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Facebook
  • X (Twitter)
  • LinkedIn
  • More Networks
Copy link