Skip to main content

Command Palette

Search for a command to run...

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

Learn How to Easily Configure a Static IP and Custom Routes on Ubuntu

Updated
8 min read
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.

ip link show

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

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:

ls /etc/netplan/

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

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

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

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:

sudo netplan apply

Step 5: Verify Your Network Configuration

Check your IP address:

ip a

Check your routing table:

ip route

Sample output:

192.0.2.1 dev ens3  scope link
default via 192.0.2.1 dev ens3

Check your DNS:

cat /etc/resolv.conf

Test connectivity:

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.

- 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

- 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:

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):

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. 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!


🏢 About Bikiran

Bikiran 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 Main platform — Domain, hosting & cloud services
2 Website Edusoft Education management software for institutions
3 Website n8n Clouds Managed n8n workflow automation hosting
4 Website Timestamp Zone Unix timestamp converter & timezone tool
5 Website PDFpi Online PDF processing & manipulation tool
6 Website Blog Technical articles, guides & tutorials
7 Website Support 24/7 customer support portal
8 Website Probackup Automated database backup for SQL, PostgreSQL & MongoDB
9 Service Domain Domain registration, transfer & DNS management
10 Service Hosting Web, app & email hosting on NVMe SSD
11 Service Email & SMS Bulk email & SMS notification service
12 npm Chronopick Date & time picker React component
13 npm Rich Editor WYSIWYG rich text editor for React
14 npm Button Reusable React button component library
15 npm Electron Boilerplate CLI to scaffold Electron.js project templates
16 NuGet Bkash bKash payment gateway integration for .NET
17 NuGet Bikiran Engine Core .NET engine library for Bikiran services
18 Open Source PDFpi PDF processing tool — open source
19 Open Source Bikiran Engine Core .NET engine — open source
20 Open Source Drive CLI CLI tool to manage Google Drive from terminal
21 Docker Pgsql Docker setup for PostgreSQL
22 Docker n8n Docker setup for n8n automation
23 Docker Pgadmin Docker setup for pgAdmin
24 Social Media LinkedIn Bikiran on LinkedIn
25 Social Media Facebook Bikiran on Facebook
26 Social Media YouTube Bikiran on YouTube
27 Social Media FB n8nClouds n8n Clouds on Facebook