Linux Set Static Route


Linux


Description:


This article will go through the proper way to set a static route on Linux.


RHEL RHEL   &   CentOS CentOS Installations:


1.    Get Interface Name:
Run an ifconfig or ip addr in order to grab the interface name, that you want to set the route traffic to go through.


ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:40:78:ec
          inet addr:10.0.0.24  Bcast:10.79.0.255  Mask:255.255.255.0
          inet6 addr: 2606:a000:4f85:ac00:a00:27ff:fe40:78ec/64 Scope:Global
          inet6 addr: fe80::a00:27ff:fe40:78ec/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:177 errors:0 dropped:30 overruns:0 frame:0
          TX packets:82 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:17929 (17.5 KiB)  TX bytes:9417 (9.1 KiB)


2.    Create Route File:
Create a new file named /etc/sysconfig/network-scipts/route-eth0 This file is titled route-eth0 to correspond to the adapter that we want to send the through in order to get to the selected route.

  • The contents of the file will simply be the ip subnet that you want to get to, and the IP address of the interface that you want to send the traffic to in order to get to the specified subnet.
10.2.0.0/16 via 10.0.0.24


3.    Restart the Network Services:

systemctl restart network.service


4.    Verify the route:

route -n


Debian Debian   &   Ubuntu Ubuntu Installation:


1.    Get Interface Name:
Run an ifconfig or ip addr in order to grab the interface name, that you want to set the route traffic to go through.


ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:40:78:ec
          inet addr:10.0.0.24  Bcast:10.79.0.255  Mask:255.255.255.0
          inet6 addr: 2606:a000:4f85:ac00:a00:27ff:fe40:78ec/64 Scope:Global
          inet6 addr: fe80::a00:27ff:fe40:78ec/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:177 errors:0 dropped:30 overruns:0 frame:0
          TX packets:82 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:17929 (17.5 KiB)  TX bytes:9417 (9.1 KiB)


2.    Edit the interfaces file:
Open the /etc/network/interfaces file, and add the route statements to the end of the interface block.

auto eth0
iface eth0 inet Static
address 10.0.0.24
netmask 255.255.0.0
gateway 10.0.0.1
up route add -net 10.2.0.0 netmask 255.255.0.0 gw 10.0.0.24


3.    Restart the Network Services:

systemctl restart networking.service


4.    Verify the route:

route -n


References: