November 18

Linux: Setting up a static route

Linux add a default route using route command

Route all traffic via gateway connected via eth0 network interface:
# route add default gw eth0
Linux add a default gateway (route) using ip command

Route all traffic via gateway connected via eth0 network interface:
# ip route add dev eth0

RHEL/CentOS/Fedora/Scientific Linux persistent routing configuration

Edit /etc/sysconfig/network and set default gateway IP address:
# vi /etc/sysconfig/network

Sample outputs:

## setup default gateway ##

You can add additional static route for eth0 by editing /etc/sysconfig/network-scripts/route-eth0 file as follows: via dev eth0

The above config sets static routing for network via router.

The address is the IP address leading to the remote network. It is preferably the next hop address but the address of the exit interface will work. The “next hop” means the remote end of a link, for example a gateway or router. The dev option can be used to specify the exit interface interface but it is not required. Add as many static routes as required. 

Debian / Ubuntu Linux persistence static routing configuration

Edit /etc/network/interfaces file, enter:
# vi /etc/network/interfaces

Append the following in eth0 section:

up route add -net netmask gw
down route del -net netmask gw

Save and close the file.

Copyright 2021. All rights reserved.

Posted November 18, 2021 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B