Quantcast
Channel: Computing For Geeks
Viewing all articles
Browse latest Browse all 78

Different ways of Configuring Static routes in Linux

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

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 eth1
Adding 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/interfaces 
Add:
auto eth1
iface eth1 inet static
address 192.168.1.10
netmask 255.255.255.0
up route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
        up route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.1
 or
auto eth1
iface eth1 inet static
address 192.168.1.10
netmask 255.255.255.0
up ip route add 172.16.0.0/24 via 192.168.1.1 || true 
Explanation:
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-eth1
 vi /etc/sysconfig/network-scripts/route-eth1
Add:
GATEWAY0=192.168.1.1
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  
Save and close the file.


Restart networking:
service network restart
You can also use:
ifdown eth1;ifup eth1
NOTE: 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

Simple way to configure static ip address on CentOS 6.x and CentOS 7 Server

How to Install Latest Kamailio SIP Server on CentOS 7

 






Viewing all articles
Browse latest Browse all 78

Trending Articles