1

I have a CentOS 7 machine on private address space, IP is 10.10.0.2. I'd like to have internet access via a gateway with a public IP which is connected to the same switch, lets call it 50.50.50.50. I had it working at some point, but have now broken it and seem to not be able to fix it. I've also installed net-tools because I find it much easier to wrap my head around the format for it.

[root@node002 ~]# ip route show
10.0.0.0/8 dev ens785 proto kernel scope link src 10.10.0.2 metric 100 
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785

So how can I add a route for everything else, with 50.50.50.50 as the gateway?

I've tried a few things such as [root@node002 ~]# ip route add 0.0.0.0/0 dev ens785 but that doesn't add anything to the routing table at all. No error message either. I've also tried adding a route to the host like this ip route add 50.50.50.50 dev ens785 which gave me an extra line in the routing table.

[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785
50.50.50.50     0.0.0.0         255.255.255.255 UH    0      0        0 ens785

I've also tried other variations such as [root@node002 ~]# ip route add 0.0.0.0/0 via 50.50.50.50 but this deleted my route to host as this is how it looks immediately after running that:

[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785

I've also tried:

[root@node002 ~]# ip route add default via 50.50.50.50/0
Error: ??? address is expected rather than "50.50.50.50/0".

And even:

[root@node002 ~]# ip route add default dev ens785
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785
50.50.50.50     0.0.0.0         255.255.255.255 UH    0      0        0 ens785

Every other machine has a default route that has 0.0.0.0 as the destination and the gateway. Like my desktop for instance, which makes sense - traffic on same network goes to the local network without a gateway, traffic to anything else goes via the router.

root@desktop ~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         100.100.100.1   0.0.0.0         UG    100    0        0 eth0
100.100.100.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0

I don't quite know how I broke this or how to fix it. Any pointers greatly appreciated!

EDIT

First of all, I gave the gateway machine a private address on the same interface. It now is 10.1.1.1, which I think means with a subnet mask of 255.0.0.0 means it's the same network. The address is ping-able. But I can't add it as a default either.

[root@node002 ~]# ip route add default via 10.1.1.1
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785
50.50.50.50     0.0.0.0         255.255.255.255 UH    0      0        0 ens785

I can temporarily add a public IP to the node and THEN I can add a default gateway:

[root@node002 ~]# ip a add 50.50.50.51 dev ens785
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785
50.50.50.50     0.0.0.0         255.255.255.255 UH    0      0        0 ens785
[root@node002 ~]# ip route add default via 10.1.1.1
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.1.1.1        0.0.0.0         UG    100    0        0 ens785
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785
50.50.50.50     0.0.0.0         255.255.255.255 UH    0      0        0 ens785
50.50.50.51     0.0.0.0         255.255.255.255 UH    100    0        0 ens785

I can then remove the public IP address, but still keep the default route:

[root@node002 ~]# ip a del 50.50.50.51/32 dev ens785
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.1.1.1        0.0.0.0         UG    100    0        0 ens785
10.0.0.0        0.0.0.0         255.0.0.0       U     100    0        0 ens785
50.50.50.50     0.0.0.0         255.255.255.255 UH    0      0        0 ens785

So why do I have to add a public IP to be able to add a gateway, if I can then remove the public IP and keep the gateway?

EDIT 2 I still cannot get a default route to add using the ip commands unless I briefly add a public IP.

More confusingly, I can achieve things by bypassing NetworkManager and using ifcfg files (and swapping to a static assignment rather than DHCP).

[root@node002 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens785 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
NM_CONTROLLED=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens785
DEVICE=ens785
ONBOOT=yes
MTU=9000
HWADDR=00:11:22:33:44:55
IPADDR=10.10.0.2
NETMASK=255.0.0.0
GATEWAY=10.1.1.1
DNS1=10.1.1.1
[root@node002 ~]# systemctl stop NetworkManager 
[root@node002 ~]# ifdown ens785 && ifup ens785
[root@node002 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use 
Iface
0.0.0.0         10.1.1.1        0.0.0.0         UG    0      0        0 ens785
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens785
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 ens785
  • Even if your gateway router has public IP address 50.50.50.50, if it's connected to the 10.0.0.0/8 segment, it also has an 10.x.x.x IP address on that segment (if it doesn't, fix your networking setup). You need to use that address as the gateway, not 50.50.50.50. – dirkt Dec 6 '17 at 16:48
4

First you can't add default gateway via IP and you don't have an interface has an IP from the same range of the gateway. So to add a default gateway for example 50.50.50.50/24 you must have an IP from the same range for example 50.50.50.100/24.

If you have only one interface and it has private IP address and you need to access the internet you need a router so he can make NAT from local IP to Public ip.

You have only switch, so to fix this issue you can do a trick which is add a sub(alias) interface to the same interface that has the private IP with IP from the public IP range for example 50.50.50.100/24 and then add the default gateway like this ip route add default via 50.50.50.50

  • I kind of follow, but don't understand it. If the gateway is physically on the same switch, it can be seen at layer 2 and I see it in the arp cache. So why can't I add it? Updated original with further info. – Biggles Oct 20 '17 at 15:36
2

Routing is always done at layer 3 (IP). It is not enough to have a default route pointing to 50.50.50.50, your routing table must also contain an entry that shows how to reach that host. To the IP layer some random address is unreachable, even if it is on the same link; the IP layer does not know that. Only when the IP layer has determined from the routing table where the IP packet should go, it is ready to hand it over to layer 2.

Note that is not strictly necessary for your host to have an IP in the same range (say 50.50.50.51); the important thing is that you have route to the subnet. However, adding a secondary address (ip addr add 50.50.50.51/24 dev ens785) automatically adds a route to 50.50.50.0/24.

Having said that, I think the correct way to do this is what you tried to do: add a secondary address to the router in the 10.0.0.0/8 range and add a default route to that address (10.1.1.1 in your example). I don't know why adding the route didn't work for you. Did you get any error message from ip route add default via 10.1.1.1?

If you want to reach the Internet from the hosts on you 10.0.0.0/8 network, the router needs to perform NAT between the private and public addresses, like @WissamRoujoulah said above.

  • No errors with the ip route add default via 10.1.1.1. Output is as first post - it just did nothing. – Biggles Oct 25 '17 at 9:27
  • Maybe Networkmanager and DHCP is interfering with what you are trying to do? What happens if you try the ip route add default via 10.1.1.1 command after disabling NetworkManager and DHCP like you did in EDIT2 above. – Johan Myréen Oct 25 '17 at 18:15
  • @Biggles, having similar issue,ip route add default via 10.1.1.1 does not add any default route (Ubuntu 17.10). – Te Ri Jun 5 '18 at 5:58
1

I think this is a bug in the automated network configuration manager. If you turn off the config manager by putting:

NM_CONTROL=no

in your ifcfg-ens785 and restart networking, it should let you manually add the default route.

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.