1

I have a machine that I want to force all traffic to go through the default gateway. Even traffic on the local network.

I have made it to work by removing the auto generated route for this interface, but is it a way to do it permanently in RHEL/Centos by the network scripts?

Default routing table used to look like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

But I want it to be after a network restart:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
1
  • Put every machine on its own /30 ? You can get a million /30's in 10.x.y.z.
    – MSalters
    Jun 30 '15 at 13:54
2

At least the route to the gateway need to be direct, i.e. you need

Kernel IP routing table
Destination     Gateway         Genmask           Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0           UG    0      0        0 eth0
192.168.1.1     0.0.0.0         255.255.255.255   U     0      0        0 eth0

The best way is to modify the netmask of your local network (from /24 = 255.255.255.0 to /32 = 255.255.255.0 and set the network address to "192.168.1.1". Then all traffic should go through the gateway.

2
  • 1
    If you assign a /32 on the local interface, the default gateway will be outside the network, and the most systems does not like that.
    – toed
    Jun 30 '15 at 11:07
  • @toed That depends which is the network address. If it is 192.168.1.0, its definitely outside the network, but you can also choose 192.168.1.1 as network adress. I will clarify my answer.
    – jofel
    Jun 30 '15 at 11:32
1

It looks like there is no method to remove the route that I want by the network-scripts. I guess it is not a very common use-case, but I really don't want servers on the same network being able to talk to each other.

But I did found one script that runs after the network is initialized.

The script /etc/sysconfig/network-scripts/ifup-post calles /sbin/ifup-local at the end, if it exists.

So I did create that one with this content:

#!/bin/bash
ip route delete 192.168.1.0/24

After a network restart, I did not have this local network route.

1
  • You can also add this ip statement to the up hook on your interface config file to delete this route after the interface comes up. Another handy way to statically route traffic.
    – 111---
    Jun 30 '15 at 13:19
0

To make your changes stick in rhel/centos, create the file /etc/sysconfig/static-routes with a line beginning with the keyword any followed by the arguments you gave to route add.

In the file /etc/sysconfig/network-scripts/ifcfg-<interface> replace or add a line DEFROUTE="no".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.