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, ordefault
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
- 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 viaens3
, 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
viaens3
(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 configuresystemd-resolved
.Connectivity issues: Use
ip a
,ip route
, andping
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.
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!
Subscribe to my newsletter
Read articles from KUMAR BISHOJIT directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
