# How to Set Up Static IP Address and Custom Routing on Ubuntu: The Complete Guide

Configuring a static IP address and custom routes is an essential skill for anyone managing Ubuntu servers, workstations, or cloud instances. This comprehensive guide will walk you through every step of the process, explain key concepts behind network routing, and provide troubleshooting tips to ensure your configuration is robust and reliable.

* * *

## **Why Configure a Static IP and Custom Routes?**

*   **Static IP:** Ensures your system always has the same address, which is crucial for remote access, web hosting, and network services.
    
*   **Custom Routes:** Control how traffic moves through your network, optimize connections, or access specific remote hosts.
    

* * *

## **Step 1: Identify Your Network Interface**

The first step is to determine which network interface you want to configure. Interface names on Ubuntu can be `eth0`, `ens3`, `enp0s3`, etc.

```bash
ip link show
```

Look for an interface that is **UP** and connected. Example output:

```bash
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ...
```

Here, `ens3` is our interface.

* * *

## **Step 2: Prepare Your Static IP Details**

Before configuring, gather the following information (using demo values):

*   **IP Address**: `192.0.2.10`
    
*   **Subnet Mask/CIDR**: `/24`
    
*   **Gateway**: `192.0.2.1`
    
*   **DNS Server(s)**: `8.8.8.8` (Google Public DNS)
    

* * *

## **Step 3: Configure Netplan (Ubuntu 18.04+)**

Ubuntu uses **Netplan** for network configuration. Netplan files are found in `/etc/netplan/`. List them:

```bash
ls /etc/netplan/
```

Open the appropriate file (e.g., `01-netcfg.yaml`) for editing:

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

Add or update with your configuration (**using demo IPs**):

```bash
network:
  version: 2
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.0.2.10/24
      gateway4: 192.0.2.1
      nameservers:
        addresses: [8.8.8.8]
      routes:
        - to: 192.0.2.1/32
          scope: link
          via: 0.0.0.0
          on-link: true
        - to: default
          via: 192.0.2.1
          on-link: true
```

**Explanation:**

*   **dhcp4: no:** Disables DHCP, enables static IP.
    
*   **addresses:** Sets the IP and subnet mask.
    
*   **gateway4:** Sets the gateway for outbound traffic.
    
*   **nameservers:** DNS servers for resolving domain names.
    
*   **routes:**
    
    *   **to:** Destination for the route (single IP or default).
        
    *   **scope:** `link` means route is directly reachable on the local interface.
        
    *   **via:** Next hop; `0.0.0.0` means direct, no gateway.
        
    *   **on-link:** Tells the system this address is directly reachable.
        
    *   **default route:** For all traffic not matched by other routes, use the specified gateway.
        

* * *

## **Step 4: Apply Your Configuration**

Run the following to activate your changes:

```bash
sudo netplan apply
```

* * *

## **Step 5: Verify Your Network Configuration**

Check your IP address:

```bash
ip a
```

Check your routing table:

```bash
ip route
```

Sample output:

```bash
192.0.2.1 dev ens3  scope link
default via 192.0.2.1 dev ens3
```

Check your DNS:

```bash
cat /etc/resolv.conf
```

Test connectivity:

```bash
ping -c 4 8.8.8.8
ping -c 4 example.com
```

* * *

## **Understanding Routes in Detail**

### **What is a Route?**

A **route** tells your system how to reach networks or hosts. The routing table contains rules that determine which interface and gateway traffic uses.

### **Key Route Fields**

*   **scope: link:** The destination is directly reachable on the local network (no gateway needed).
    
*   **to:** The target IP or network (e.g., `192.0.2.1/32` for one host, or `default` for all traffic).
    
*   **via:** The next hop (gateway) for reaching the destination. `0.0.0.0` means direct (no gateway).
    
*   **on-link:** Marks the destination as directly reachable on the local segment.
    

#### **Example 1: Link-Scoped Route**

```bash
- to: 192.0.2.1/32
  scope: link
  via: 0.0.0.0
  on-link: true
```

*   Packets destined for `192.0.2.1` are sent directly via `ens3`, with no gateway.
    

#### **Example 2: Default Route**

```bash
- to: default
  via: 192.0.2.1
  on-link: true
```

*   All traffic not matching a more specific rule goes to the gateway `192.0.2.1`.
    

* * *

## **Summary Table: Route Fields**

| **Field** | **Example Value** | **Meaning** |
| --- | --- | --- |
| scope | link | Route is valid on the local link/interface |
| to | 192.0.2.1/32 | Target IP address (single host) |
| via | 0.0.0.0 / gateway IP | Next hop (gateway) for traffic, 0.0.0.0 = direct |
| on-link | true | Target is directly reachable on local network |
| to | default | This route applies to all destinations not matched elsewhere |

* * *

## **Visual Example (Routing Table)**

Imagine your routing table looks like this:

```bash
Destination        Gateway         Genmask         Flags Iface
192.0.2.1          0.0.0.0         255.255.255.255 UGH   ens3
0.0.0.0            192.0.2.1       0.0.0.0         UG    ens3
```

*   **First row:** Direct route to `192.0.2.1` via `ens3` (no gateway).
    
*   **Second row:** Default route—send everything else via `192.0.2.1`.
    

* * *

## **Troubleshooting**

*   **Network unreachable:** Double-check IP, gateway, and interface name.
    
*   **YAML errors:** Make sure you use spaces (not tabs) and correct indentation in Netplan files.
    
*   **DNS not working:** Some systems may overwrite `/etc/resolv.conf`. Use Netplan to set DNS or configure `systemd-resolved`.
    
*   **Connectivity issues:** Use `ip a`, `ip route`, and `ping` to diagnose.
    

* * *

## **Advanced: Temporary Network Changes (No Reboot Needed)**

If you want to test settings without editing Netplan, use these commands (**using demo IPs**):

```bash
sudo ip addr flush dev ens3
sudo ip addr add 192.0.2.10/24 dev ens3
sudo ip link set ens3 up
sudo ip route add 192.0.2.1 dev ens3
sudo ip route add default via 192.0.2.1
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
```

**Note:** These changes are lost after reboot.

* * *

## **Conclusion**

Configuring a static IP address and custom routes on Ubuntu is straightforward once you understand the concepts and configuration syntax. Whether you’re running a server, a cloud VM, or a desktop, these steps ensure your system is reliably reachable and communicates efficiently on your network.

### CONTACT:

I’m Kumar Bishojit Paul, the Founder and CEO of [BIKIRAN](https://www.bikiran.com/). If you need further assistance, please leave a comment. I’m interested in helping you.

**Key Takeaways:**

*   Know your network interface name.
    
*   Use Netplan for permanent configuration.
    
*   Understand how routes work—especially link scope and default routes.
    
*   Test and verify your setup for reliability.
    

Have questions or want to learn more about advanced routing? Comment below or explore Ubuntu’s [official networking documentation](https://ubuntu.com/server/docs/network-configuration)!


---

## 🏢 About Bikiran

**[Bikiran](https://bikiran.com/)** is a software development and cloud infrastructure company founded in 2012, headquartered in Khulna, Bangladesh. With 15,000+ clients and over a decade of experience, Bikiran builds and operates a suite of products spanning domain services, cloud hosting, app deployment, workflow automation, and developer tools.

| SL  | Topic        | Product                                                              | Description                                             |
| --- | ------------ | -------------------------------------------------------------------- | ------------------------------------------------------- |
| 1   | Website      | [Bikiran](https://bikiran.com/)                                      | Main platform — Domain, hosting & cloud services        |
| 2   | Website      | [Edusoft](https://www.edusoft.com.bd/)                               | Education management software for institutions          |
| 3   | Website      | [n8n Clouds](https://n8nclouds.com/)                                 | Managed n8n workflow automation hosting                 |
| 4   | Website      | [Timestamp Zone](https://www.timestamp.zone/)                        | Unix timestamp converter & timezone tool                |
| 5   | Website      | [PDFpi](https://pdfpi.bikiran.com/)                                  | Online PDF processing & manipulation tool               |
| 6   | Website      | [Blog](https://blog.bikiran.com/)                                    | Technical articles, guides & tutorials                  |
| 7   | Website      | [Support](https://support.bikiran.com/)                              | 24/7 customer support portal                            |
| 8   | Website      | [Probackup](https://probackup.bikiran.com/)                          | Automated database backup for SQL, PostgreSQL & MongoDB |
| 9   | Service      | [Domain](https://www.bikiran.com/domain)                             | Domain registration, transfer & DNS management          |
| 10  | Service      | [Hosting](https://www.bikiran.com/services/hosting/web)              | Web, app & email hosting on NVMe SSD                    |
| 11  | Service      | Email & SMS                                                          | Bulk email & SMS notification service                   |
| 12  | npm          | [Chronopick](https://www.npmjs.com/package/@bikiran/chronopick)      | Date & time picker React component                      |
| 13  | npm          | [Rich Editor](https://www.npmjs.com/package/@bikiran/editor)         | WYSIWYG rich text editor for React                      |
| 14  | npm          | [Button](https://www.npmjs.com/package/@bikiran/button)              | Reusable React button component library                 |
| 15  | npm          | [Electron Boilerplate](https://www.npmjs.com/package/create-edx-app) | CLI to scaffold Electron.js project templates           |
| 16  | NuGet        | [Bkash](https://www.nuget.org/packages/Bikiran.Payment.Bkash)        | bKash payment gateway integration for .NET              |
| 17  | NuGet        | [Bikiran Engine](https://www.nuget.org/packages/Bikiran.Engine)      | Core .NET engine library for Bikiran services           |
| 18  | Open Source  | [PDFpi](https://github.com/bikirandev/pdfpi)                         | PDF processing tool — open source                       |
| 19  | Open Source  | [Bikiran Engine](https://github.com/bikirandev/Bikiran.Engine)       | Core .NET engine — open source                          |
| 20  | Open Source  | [Drive CLI](https://github.com/bikirandev/DriveCLI)                  | CLI tool to manage Google Drive from terminal           |
| 21  | Docker       | [Pgsql](https://github.com/bikirandev/docker-pgsql)                  | Docker setup for PostgreSQL                             |
| 22  | Docker       | [n8n](https://github.com/bikirandev/docker-n8n)                      | Docker setup for n8n automation                         |
| 23  | Docker       | [Pgadmin](https://github.com/bikirandev/docker-pgadmin)              | Docker setup for pgAdmin                                |
| 24  | Social Media | [LinkedIn](https://www.linkedin.com/company/bikiran12)               | Bikiran on LinkedIn                                     |
| 25  | Social Media | [Facebook](https://www.facebook.com/bikiran12)                       | Bikiran on Facebook                                     |
| 26  | Social Media | [YouTube](https://www.youtube.com/@bikiranofficial)                  | Bikiran on YouTube                                      |
| 27  | Social Media | [FB n8nClouds](https://www.facebook.com/n8nclouds)                   | n8n Clouds on Facebook                                  |
