Routes file in linux

Configure Static Routes In Debian or Red Hat Enterprise Linux

S tatic routes improve the overall performance of your network (especially bandwidth saving). They are also useful in stub networks (i.e. there is only one link to the network). For example, each LAN (located at different offices) is connected to HQ IDC (Internet data center) using single T1/LL/Wan links.

For example under Red Hat/Fedora Linux you can add a static route for eth0 network interface by editing /etc/sysconfig/network-scripts/route-eth0 file. Under Debian Linux add static route by editing /etc/network/interface file.

Display Current Routing Table Using ip command

By using the ip command, you can set up and view static route. For example, to display the current routing table you can type the command:
# ip route show
Sample output:

You can add static route using following command:
ip route add via dev
For example network 192.168.55.0/24 available via 192.168.1.254:
# ip route add 192.168.55.0/24 via 192.168.1.254 dev eth1
Alternatively, you can use old good route command too:
# route add -net 192.168.55.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1

Linux Persistence Routes

The drawback of ‘ip’ or ‘route’ command is that when Linux system reboots it will forget static routes. So store them in a configuration file. Static routing describes a system that does not implement adaptive routing. In these systems routes through a data network are described by fixed paths (statically). These routes are usually entered into the router by the system administrator

Red Hat (RHEL) / CentOS / Fedora Linux Persistence Static Routing

You need to open /etc/sysconfig/network-scripts/route-eth0 file to define static routes for eth0 interface:
# cat /etc/sysconfig/network-scripts/route-eth0
Sample Output:

A note about CentOS 7.x/RHEL 7.x and permanent static route

On a modern version of CentOS 7 or RHEL 7, you need to create a file named /etc/sysconfig/network-scripts/route-eth0 and update as follows:
# vi /etc/sysconfig/network-scripts/route-eth0
Add a permanent static route:
172.16.1.0/24 via 10.10.8.254 dev eth0
Save and close the file. You must restart networking service:
# systemctl restart network
OR
# service restart network

How do I define static routing for network 10.0.0.0/8 via 10.9.38.65 router?

Open /etc/sysconfig/network-scripts/route-eth0:
# vi /etc/sysconfig/network-scripts/route-eth0
Append following line:
10.0.0.0/8 via 10.9.38.65
Save and close the file. Restart networking:
# service network restart
Verify new routing table:
# route -n

Debian / Ubuntu Linux Persistence Static Routing

Open configuration file /etc/network/interfaces to configure static routes in Debian or Ubuntu Linux:
# cat /etc/network/interfaces
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Debian / Ubuntu Linux static routing for two interfaces:

Here is an updated config file named /etc/network/interfaces:

Conclusion

Please note that the route command has been deprecated. Please use the ip command or see the following tutorials:

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

The explanation on static route is good.
but i found that there is also a file under /etc/sysconfig/network-scripts/static-route.
If this file is present then what is its use.
By default there is no file named route-eth0 under /etc/sysconfig/network-scripts/ .

If i have to add static route in /etc/sysconfig/network-scripts/static-route file then how to do that.

with regards
rohit
india
9433248393

I’ve been trying to figure our how to add a static multicast route.

I know how to do it manually via the route command.
e.g.

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
which give you the following route.

Destination Gateway Genmask Flags Metric Ref Use Iface
224.0.0.0 * 240.0.0.0 0 0 0 eth 0

But i would like to add the route to route-eth0 so i don’t need to apply it manually everytime the server reboots.

I’ve seen an example format for the file as

192.168.0.0/24 via 152.3.182.5

But i don’t know what the equivalent is for the following route
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

What do i put after ‘via’ for the above multicast route.

Open /etc/rc.local:
vi /etc/rc.local

Append command:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

Reboot system:
reboot
Verify route
route -n

I find this method preferable because the syntax doesn’t chance. Same route command.

And yes you can use:
224.0.0.0/4 via YOUR-ROUTER-IP-ADDRESS

For example my subnet 10.0.0.0/8 routed via 10.5.123.1, so I have following line in my route-eth0
10.0.0.0/8 via 10.5.123.1

GATEWAY0=192.168.55.0
NETMASK0=255.255.255.0
ADDRESS0=192.168.1.254

…shouldn’t that be

GATEWAY0=192.168.1.254
NETMASK0=255.255.255.0
ADDRESS0=192.168.55.0

thanks for the heads up!

Thanks for the info – why can’t they include info about the ./network-scripts/route-eth0 file in the man file for route? :-S

hi this is venkateshwarlu. iam working in sysadmin in media. heare is using dell linux server. i have give the public ip and private ip.
but not corctly understanding in
” static routing ”

Static routing is some thing which are not ging to change. If there is one inbound port one outbound port every thing is static one.

It says you are defining a rule for a network to go outside world.
So it will never be broadcasted to search a path.
It will be fast and optimum bandwidth utilisation.

Anywat you if donot know how to write rules, can make a linux box as transparent routing.

224.0.0.0/4 via YOUR-LOCALHOST-IP-ADDRESS

Actually script recognize 2 formats of route- files. The older one is just continued command line pararameters for ‘ip route add’ command and new one use

ADDRESS6
GATEWAY4
NETMASK6

triples
The simplest way is to figure out what ‘ip’ command will do a trick and put it without ‘ip route add’ into /etc/sysconfig/network-scripts/route-eth0
i.e.
224.0.0.0/4 dev eth1
or
to multicast 224.0.0.0/4 dev eth1

192.168.55.0/24 via 192.168.1.254

by the way to see all you routes use
ip route list table all
command
for more information issue
ip route help
for example if you have a 2 internet lines and want to do load balance route then do not set default gateway in configuration, but add in one of the route files (let’s say /etc/sysconfig/network-scripts/route-ppp1) something like:
default dev ppp0 nexthop ppp1

If i have to add static route in /etc/sysconfig/network-scripts/static-route file then how to do that.

eth0 host 1.2.3.4
eth0 net 192.168.2.0 netmask 255.255.255.0 gw 192.168.9.6

The first line provide a host route to an interface for proxy arp.
The second line provides a VPN gateway.

Can you help me with this, how to configure Red Hat Linux to work with a load balancer router? I’m not familiar with the Red Hat Linux. Thanks.

Thank’s, is very pretty info.

thanks, bydefualt route-eth0 file will not be there, need to be created

I have a pptp connection and have added the following in /etc/sysconfig/network-scripts/route-ppp0

ADDRESS0=172.16.0.0
GATEWAY0=192.168.10.1
NETMASK0=255.255.0.0
ADDRESS1=192.168.0.251
GATEWAY1=192.168.10.50
NETMASK1=255.255.255.0

when the tunnel is established subnet 172.16.0.0/16 is reachable but not 192.168.0.251 as it tries to go out the def gw

192.168.0.251 become reachable if i manually add by (but looses route if pptp drops) :

route add 192.168.0.251 gw 192.168.10.1

I must be messing something up here….anyone can shed light pls ? 🙂

@Stan: create /etc/ppp/ip-up.d/route-traffic and add your route command. For example, I router 10.0.0.0/8 via ppp0 so my file is as follows:

hey guys ,,
I have a problem, I’m trying to configure a Debian board (Olimex) to do the routing but all my trys failed even though it seems my network is connected well

I can ping from my pc the both interfaces which the board are connected to but the problem I can’t ping any IP outside .

anyone can help and let me know what missing .

How command can I use to list the multicast coming out of my redhat server?

i like the redhat and i trying to learn the redhat window so pleas help me in this ok thanks

Hi
I would like to know how I can add permanently a dyndns host in my routing table(I have two network interfaces…). I have some hosts with fixed ip addresses in my /etc/sysconfig/network-scripts/route-eth0 that I have added with
1.2.3.4/32 via 192.168.1.1
but if I can not ad in this file
myhost.dyndns.org/32 via 192.168.1.1

Can’t be done. Otherwise your host would have to resolve the dyndns name each time it routed a packet !!

Find out what range your ISP is allocating the dyndns host within and add a route for that.

I done it in a way. I have a crontab every minute do something like
ping -c1 my_adress.dyndns.org | grep from | cut -d’ ‘ -f 4 | cut -d’:’ -f1′ > /tmp/new_ip
diff /tmp/old_ip /tmp/new_IP && route del default gw 192.168.1.254 eth0 route add -net `echo /tmp/new_ip` -mask 255.255.255.0 &&cat /tmp/old_ip > /tmp/old_ip && route add default gw 192.168.1.254 eth0 route add -net `echo /tmp/new_ip` -mask 255.255.255.0

If you read /etc/init.d/network (rhel),you can find this:
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep “^any” /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi
so, /etc/sysconfig/static-routes is your best chose!
vi /etc/sysconfig/static-routes
any -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254
any -host 192.168.100.1 gw 192.168.1.254
Thanks!

does 224.0.0.0/4 via IP dev eth0 in the route-eth0 script route all mutlicast addresses? For example I need to multicast on 224 and 235 addresses …if not..how would I enable it in static routing in Red Hat Linux?

Hi,
My server ABC has got two nics connected to different subnets. How do I add static route to send traffic to particular interface?

For example, I have 5 another servers with ip-address 10.216.98.XX and all have their own gateway because they all are at different locations. Now I want to ass route which sends/receive all traffic from these servers to eth0 on my ABC server.

Thanks in advance.

Thx for the tutorial

I have a problem with my static routes.

I have a route to a 10.0.0.0/8 network through both of my NICs which are configured for seperate /24 networks.

My eth0 has a default gateway. So I need to add the static route for Eth1.

10.0.0.0/8 via 172.21.32.3

I restart network services and see the route in netstat -nr.

However, when I unplug the Eth1 cable, the route is STILL IN THE TABLE. Which means the packets will not go out Eth1 even when there is a route. When I issue the ifdown eth1 command, finally the route is removed from the routing table and pings can go out eth0.

How can I get this to happen automagically?

I have 3 servers in cluster and want to update the ntp server. I am running Red Hat on all the servers.
Do I need to stop the cluster, update the ntp server and start the ntpd service in each server and then bring the cluster up or I can update the ntp server with out disturbing the cluster? Would really appreciate some inputs.

hi I went help to config centOS 0.4 to Linux routing for network WAN

It would be useful to stick with the listed subnets for the example.
All possibilities are in vain:

# ip route show
192.168.70.0/24 dev eth0 proto kernel scope link src 192.168.70.1
192.168.14.0/24 dev eth1 proto kernel scope link src 192.168.14.162
169.254.0.0/16 dev eth0 scope link
default via 192.168.14.1 dev eth1

# ip route add 192.168.70.0/24 via 192.168.14.1 dev eth1
RTNETLINK answers: File exists

# ip route add 192.168.70.0/24 via 192.168.14.1 dev eth0
RTNETLINK answers: Network is unreachable

# ip route add 192.168.14.0/24 via 192.168.70.1 dev eth0
RTNETLINK answers: File exists

# ip route add 192.168.14.0/24 via 192.168.70.1 dev eth1
RTNETLINK answers: Network is unreachable

# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.70.0 * 255.255.255.0 U 0 0 0 eth0
192.168.14.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.14.1 0.0.0.0 UG 0 0 0 eth1

on debian, the config can be also on

Thanks for this tutorials, it’s help me a lot. I’m configuring 2 network using Debian Linux as router in Network1 dan using Mikrotik in Network2.

Regards,
TKJ Pekalongan
TKJ SMK Gondang

after reboot
this routing automatic remove
why ?

hi,
I have entered ips, netmask, gw in /etc/sysconfig/network-scripts/enp130s0f2 then save it and restart the service. But when i route -n, it is not showing the changes.
Could u please help me ?

Hello @sanjib, @ricky. The utility used for configuring static routes in Debian has changed. I am not sure about RHEL/CentOS. Since Debian 9, static routes are added by the means of iproute2 utility( https://www.cyberciti.biz/faq/linux-ip-command-examples-usage-syntax/ ). So, the syntax is now as follows:

I am using a VPN. By default all of my internet traffic is routed through that. However, for work I use an SSH server that probably has some IP based filtering and doesn’t allow me to connect via the VPN. Therefore I would like to route the IP adres of the SSH server outside the VPN.

I’ve added the following route where 1.2.3.4 is the IP of the SSH server and wlp1s0 is my wifi interface.
ip route add 1.2.3.4 via 192.168.0.1 dev wlp1s0

However this doesn’t seem to work. Any ideas? I’m unable to do a traceroute on the SSH server’s IP however ping seems to work.

Источник

Читайте также:  Установка dns сервер linux
Оцените статью