I need to setup route for one of my computers. I want it permanently so I don't need to enter the command myself every reboots.

However the route command I use ain't a normal one, because I use 2 interfaces on the same physical interface (eth0 and eth0:B on ethernet card).

route add -net dev eth0:B

So I think I can't use the normal way Google tells me.

My question is: if I write a script and add it to $HOME/.kde4/Autostart instead of modify /etc/sysconfig/network/routes, are there any side effects? Are there better ways to do this?


I had to do something similar in a lab at school this past semester. I used a script to determine which interface has a desired IP for the alias to be created, then use that interface to create the alias. Followed by setting the route.

Something like:

#! /bin/bash

# First get all the NIC device names on the system
dev_names=$(/sbin/ifconfig | grep HWaddr| awk '{print $1}')

# Set a loop to loop through each device name

for d_name in ${dev_names}

echo "Checking device name $d_name ..."

status=$(/sbin/ifconfig $d_name|grep ${TARGET_IP})

if [ ! -z "$status" ]
        echo "$d_name contains the target IP $TARGET_IP"
echo "The target NIC device name should be $my_dev"

# Now set the alias
/sbin/ifconfig $my_dev:B {IP_ADDRESS}

# Now add the route
route add -net dev eth0:B

Use crontab or another method to have the script run at startup.

Note: This won't work if the interface is a wireless connection.

  • Hi; I give kudo to you for the script. However I want to make sure there're no side effects, since this computer run this script will act as a server. If you confirm you have no problem with this kind of routing, I will mark this question's answered. – Tiana987642 Dec 16 '13 at 16:00
  • I can't guarantee that it will work for you, but it did work for me. I had the script running on three different machines and it worked on all of them. I'd suggest testing out. You might have to alter the TARGET_IP=, also where it says {IP_ADDRESS} and perhaps the route command. But try it out, it should work. – canadmos Dec 16 '13 at 17:43
  • Well, if it work with you then it should do fine for me. Thank you! – Tiana987642 Dec 17 '13 at 14:34
  • Interface's name passed as argument into if-up.d scripts. So you can write: case "$IFACE" in eth0) # do smth ;; esac – Roman Sklyarov Jun 5 '17 at 14:46

If you are not on a systemd distro (=Arch Linux, OpenSUSE, Fedora), you can do it within your /etc/network/interfaces file, where, I presume, you are setting up your vifs.

Just add to the stanza for interface eth0:B these two lines:

 post-up route add -net route add -net gw dev eth0:B 
 pre-down route delete -net route add -net gw dev eth0:B 

This will do it automatically for you every time you start networking (which is done at boot, or manually) and will also tear down the route if you switch eth0:B off.


at @Tiana987642's request, I will show how to do this in systemd distros. Since I want this to operate at boot, I may as well follow the Wiki, and define a service to be run by systemd. I will modify the Wiki as little as possible:



These configurations for the two interfaces are purely conjectural, you should substitute your own. And now /etc/systemd/system/network@.service

  Description=Network connectivity (%i)


  ExecStart=/usr/bin/ip link set dev %i up
  ExecStart=/usr/bin/ip addr add ${address1}/${netmask1} broadcast ${broadcast1} dev %i
  ExecStart=/usr/bin/ip route add default via ${gateway1}

  ExecStart=/usr/bin/ip link add link $i name $i:B type vlan id 10

  ExecStart=/usr/bin/ip link set dev %i:B up
  ExecStart=/usr/bin/ip addr add ${address2}/${netmask2} broadcast ${broadcast2} dev %i:B
  ExecStart=/usr/bin/ip route add default via ${gateway2}

  ExecStop=/usr/bin/ip addr flush dev %i
  ExecStop=/usr/bin/ip link set dev %i down
  ExecStop=/usr/bin/ip addr flush dev %i:B
  ExecStop=/usr/bin/ip link set dev %i:B down


and you enable and start the service as usual,

  systemctl enable network@eth0.service
  systemctl start network@eth0.service
  • I use OpenSUSE 12.1 32 bit. What a pity, I think this is a nice approach. THank you! – Tiana987642 Dec 17 '13 at 14:31
  • @Tiana987642 In systemd distros it is even easier. – MariusMatutiae Dec 17 '13 at 15:37
  • Can you write it down here? It can be useful for me or someone else :) – Tiana987642 Dec 19 '13 at 13:50
  • @Tiana987642 Done. – MariusMatutiae Dec 19 '13 at 15:53

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.