Add static route on Ubuntu 14.04 server

15,112

post-up route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1 remove post-up and change it to "up"

up route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1 after that, ifdown em1:0 and ifup em1:0 or u can restart networking.service cheer.

Share:
15,112

Related videos on Youtube

James
Author by

James

Updated on September 18, 2022

Comments

  • James
    James over 1 year

    I have set up Ubuntu 14.04 server on a machine and configured /etc/network/interfaces with two gateways. The default one is em1 which is fine, but I need traffic to a specific IP address on the internet to be sent via the other gateway. The following command works from the command line:

    sudo route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1
    

    but adding it to /etc/network/interfaces does not work on reboot:

    # 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 em1
    iface em1 inet static
        address 10.0.2.251
        netmask 255.255.255.0
        gateway 10.0.2.1
        dns-nameservers 8.8.8.8 8.8.4.4
    
    auto em1:0
    iface em1:0 inet static
        address 172.16.1.6
        netmask 255.255.0.0
        gateway 172.16.0.1
        dns-nameservers 8.8.8.8 8.8.4.4
    
    post-up route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1
    

    Can anyone help?

    • Eddie Dunn
      Eddie Dunn over 8 years
      Just for grins can you try it with a 30 bit (255.255.255.252) mask? I just found this: askubuntu.com/questions/560616/… that indicates it may be a problem with the 32 bit mask specifically. Not a fix but maybe more focus on the problem.
    • James
      James over 8 years
      No dice. I changed the netmask but route complained route: netmask doesn't match route address
    • Eddie Dunn
      Eddie Dunn over 8 years
      Well you would need to make it match for the network too. without knowing more of the IP I can;t tell what that should be.
    • Zoredache
      Zoredache over 8 years
      Try putting it in the iface em1:0 block, and try changing your post-up to an up. You might also want to try the iproute syntax of ip route add x.x.x.x/32 via 172.16.0.1 instead.