Here we are going to learn on different ways to configure static routes on a Linux system. be it Ubuntu, Kali Linux, CentOS, Fedora, Linux Mint or any other Linux system. If you have a new installation of Linux System, i recommend you read any of below articles depending on the Linux distribution you are running:
Mostly default gateway is for any and all traffic which is not destined for the local network and for which no preferred route is specified in the routing table. The default gateway is traditionally a dedicated network router.
You can configure static route by various methods, one of them being manually specifying the route on network configuration script.Other methods includes using commands such as:
route add
ip route
A point to note on use of both "route add" and "ip route" commands is that they configure routing on the runtime and doesn’t persist the configuration after a reboot.
We are going to talk about different ways to set static route.Both temporarily and setting up permanent static route.
Adding static route with route add command
Syntax:
route add -net <IP>/<MASK> <GW> dev <ethX>
Let's add route for interface eth1, network 192.168.1.0/24.
Syntax:
ip route add <IP>/<MASK> via <GW> dev <ethX>
To get to network 192.168.0.0/24 and 172.16.0.0/16, use default gw 192.168.1.1
Restart networking:
Checking static routes:
![]()
Top Things to do after fresh installation of CentOS 7 minimal
Top things to do after Fresh Installation of Fedora 23
Top Ten Must Do Things After Installing Kali Linux
What to Do after Installing Ubuntu 14.04, 12.04, 13.0
A static route can be defined as a pre-determined path that network information must follow to reach a specific host or network. Static route is normally important for traffic that must pass through an encrypted VPN tunnel or traffic that should take a specific route for reasons of cost or security.Mostly default gateway is for any and all traffic which is not destined for the local network and for which no preferred route is specified in the routing table. The default gateway is traditionally a dedicated network router.
You can configure static route by various methods, one of them being manually specifying the route on network configuration script.Other methods includes using commands such as:
route add
ip route
A point to note on use of both "route add" and "ip route" commands is that they configure routing on the runtime and doesn’t persist the configuration after a reboot.
We are going to talk about different ways to set static route.Both temporarily and setting up permanent static route.
Adding static route with route add command
Syntax:
route add -net <IP>/<MASK> <GW> dev <ethX>
Let's add route for interface eth1, network 192.168.1.0/24.
route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth1Adding static route with ip route command
Syntax:
ip route add <IP>/<MASK> via <GW> dev <ethX>
ip route add 192.168.0.0/24 via 192.168.1.1 dev eth1
Adding Static persistent route
Ubuntu/Debian based Systems:
vi /etc/network/interfacesAdd:
auto eth1or
iface eth1 inet staticaddress 192.168.1.10up route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.1
netmask 255.255.255.0
up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
auto eth1Explanation:
iface eth1 inet staticaddress 192.168.1.10up ip route add 172.16.0.0/24 via 192.168.1.1 || true
netmask 255.255.255.0
To get to network 192.168.0.0/24 and 172.16.0.0/16, use default gw 192.168.1.1
CentOS 7, RHEL and OpenSUSE
Static route configuration can be stored per-interface in a /etc/sysconfig/network-scripts/route-interface file. For example, static routes for the eth1 interface would be stored in the /etc/sysconfig/network-scripts/route-eth1vi /etc/sysconfig/network-scripts/route-eth1Add:
GATEWAY0=192.168.1.1Save and close the file.
NETMASK0=255.255.255.0
ADDRESS0=192.168.0.0
GATEWAY1=192.168.1.1
NETMASK1= 255.255.0.0
ADDRESS1=172.16.0.0
Restart networking:
service network restartYou can also use:
ifdown eth1;ifup eth1NOTE: Subsequent static routes must be numbered sequentially, and must not skip any values. For example, ADDRESS0, ADDRESS1, ADDRESS2, and so on.
Checking static routes:
ip route show
netstat -nr
route -n
