Howto add permanent static routes in Ubuntu
Sponsored Link
Advantages of Static Routes
- Easy to configure
- No routing protocol overhead
Disadvantages of Static Routes
- Network changes require manual reconfiguration
- Network outages cannot be automatically routed around
- Does not scale well in large networks.
Add a Static route using "route" command
route add [-net|-host] <IP/Net> netmask <Mask> gw <Gateway IP> dev <Int>X
Example
route add -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
route add -host 10.10.1.1 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
This adds the route immediatly to the Kernel IP routing table. To confirm the route has been successfully, simply type the "route" command with no arguements:
route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.254 * 255.255.255.0 U 0 0 0 eth0
localnet * 255.255.255.0 U 0 0 0 eth0
10.10.10.0 * 255.255.255.0 U 0 0 0 eth0
10.10.1.1 * 255.255.255.0 U 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
Use
netstat -rn
to print the Kernel IP Routing table.
To keep the Static Route persistent or you want to add the route entries to the network script files (not using the route command) then all you need to do is to edit the file
/etc/network/interfaces
and the static routes in the following format:
up route add [-net|-host] <host/net>/<mask> gw <host/IP> dev <Interface>
Example
up route add -net 172.20.11.0/16 gw 172.20.10.254 dev eth1
And the file will like the following
sudo cat /etc/network/interfaces
The output should show something like this
sudo cat /etc/network/interfaces
The output should show something like this
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0 eth1
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
# dns-* options are implemented by the resolvconf package, if installed
iface eth1 inet static
address 172.20.10.1
netmask 255.255.255.0
broadcast 172.20.10.255
gateway 172.20.10.254
# static route
up route add -net 172.20.11.0/16 gw 172.20.10.254 dev eth1
The above has 2 Ethernet interfaces and the static route is added to the interface eth1.
For the change to /etc/network/interface to take effect. please restart the "networking" service as follows:
sudo /etc/init.d/networking restart
NOTE: If you added the route already using the "route" then there is no need to restart the networking service because, the next time server is restarted this takes effect.
Is the
up
stuff there a typo or a sudo-like command?@llaurén
No you need to add the syntax in /etc/networ/interfaces file
It does not work:
$ cat /etc/network/interfaces
auto lo
iface lo inet loopback
iface eth0 inet dhcp
auto eth0
up route add -net 172.20.11.0/16 gw 172.20.10.254 dev eth0
Renders…
$ sudo /etc/init.d/networking stop
* Deconfiguring network interfaces…
/etc/network/interfaces:13: misplaced option
ifdown: couldn’t read interfaces file “/etc/network/interfaces”
[fail]
AND
$ sudo /etc/init.d/networking start
* Configuring network interfaces…
/etc/network/interfaces:13: misplaced option
ifup: couldn’t read interfaces file “/etc/network/interfaces”
[fail]
You need to put the up route option after the iface line it is attached to. You’re putting it above all your iface lines.
Or rather, after the iface static line. You’re using DHCP and I didn’t notice that line there. You may need to configure the route in your dhcp server instead. I don’t think that the iface dhcp line accepts options in the same way as the iface static.
I have found your statement to be true – if using DHCP, then the up command is ignored. So if your using a weak DHCP server like W2003’s version that only partially adds static routes – is there any other place I can add the route add command? so at boot up the route gets added to the routing table?
You could add it to rc.local which gets run last. The only problem with this option is that anything that loads before rc.local will not be able to use this route.
For me it didn’t work until I put it at the end of the the inet eth0 section, as I’m using eth0 and eth1 is not used. It probably worked in the howto example because that interface was eth1 and was interpreted to be part of the eth1 configuration
Thank you for this information. I had to edit the route add statement since I only wanted to add the default gateway. However, your information ended several hours of problem solving and wrong solutions.
Your “Adding a static route” example isn’t very clear.
What is the example attempting to do?
Why are there two “route add” commands?
Is the order of entries in the route table important or not?
If you don’t want to explain these things in this article a link elsewhere would good.
@josvazg – My educated guess would be that 172.20.11.0/16 should actually be 172.20.11.0/24 (as in /24 which is 255.255.255.0 for your subnet, not /16 which is 255.255.0.0)
Hope that helps.
Thank you all, the informations helped me alot! Had the same error while booting debian. Thanks alot!
How did you fix that error while booting? I tries nearly everything without success. me <- lost Any idea what could be wrong?
up route add -net 172.25.0.0/16 gw 172.25.6.254 dev eth0
up: command not found
route add -host 10.10.1.1 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
NICE ONE DUDE !
and i mean … REALLY
netmask for host is 255.255.255.255 … PERIOD !!!
Wow just the info that i was looking, thanks
I have system with static address on eth0 like the example (converted from dhcp).
I added second device eth1 and setup on subnet 10.0.0.0/24
this interface is intended to provide dhcp for that small subnet as well as be the gateway to the other subnet.
……….
iface eth0 inet static
address 192.168.1.69
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
iface eth1 inet static
address 10.0.0.2
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
gateway 10.0.0.2
up route add -net 10.0.0.0/24 gw 10.0.0.2 dev eth1
……………
When I try to restart the network, it indicates Failed to bring up eth0
but the route seems to be setup.
Yup I second that. It doesn’t like it. It’s either 172.20.11.0/24 or 172.20.0.0/16
You might be on ssh session!.. You are trying to kill the network PID while you are connected to the same PID … use console & try
Is it possible to set a static route for an unnumbered interface? I was hoping to do something like this but with no luck:
ip route add via dev
Otherwise the unnumbered interface winds up using the default gateway for the server rather than the network I need it to run on
tried everything but it still seems that I am not able to make routes persistent even after adding the lines in /etc/interfaces:
#Persistent static routes
up route add -net 10.10.0.0/16 gw 10.10.138.1 dev eth1
up route add -net 10.47.0.0/16 gw 10.10.138.1 dev eth1
Remove the gateway on eth1 ( gateway 10.0.0.2)