Linux – Default gateway is in different subnet. How to configure in RHEL6.2

linuxnetworkingrhel6

I have two subnets routed to my server from ISP. I have only one gateway ip. The gateway is on the same VLAN as my IP address.
For example netowrk 1 is 1.0.0.0/24 and network 2 is 2.0.0.0/24. Both are routed to eth0 by my ISP. Gateway is 1.0.0.1. My host ip is 2.0.0.1/24 (eth0)
So I can configure default gateway manually with

ip route add default dev eth0
ip route add default via 1.0.0.1

and then internet connection works properly.
How do I configure it in /etc/sysconfig/network-scripts/ifcfg-eth0 ?

I tried to set GATEWAY=1.0.0.1 but it doesn't work. Tried to set GATEWAY and GATEWAYDEV in /etc/sysconfig/network and it does only what first command from listing above do.

Best Answer

You really won't find support for something like this, as it's not something that will pass any sanity checks.

So, think about it a bit. Your computer needs to know the gateway address in order to reach other machines outside it's local subnet. So, if you have a subnet 10.0.1.0/24 and your machine is 10.0.1.12, it would be able to reach any machine from 10.0.1.0-10.0.1.255 without using the gateway.

The gateway is typically required to be within the machine's subnet otherwise the machine has no way to reach it (without some routing tricks, as you are doing). Going back to the example above, if the gateway had an IP of 10.0.2.10, the machines would have no way to reach it (in order to send traffic outside their subnet, they need to reach the gateway, which is outside their subnet).

Are you absolutely certain that you have the correct subnet mask? As another example, if you have an IP in 10.0.1.0/16 with a gateway of 10.0.2.0, this is perfectly valid. If you had 10.0.1.0/24 and a gateway of 10.0.2.0, this is not. I would suspect that your ISP has given you the wrong netmask information.