Add specific route via start-up script
Solution 1
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
#
my_dev=
TARGET_IP=192.168.1
for d_name in ${dev_names}
do
echo "Checking device name $d_name ..."
status=$(/sbin/ifconfig $d_name|grep ${TARGET_IP})
if [ ! -z "$status" ]
then
echo "$d_name contains the target IP $TARGET_IP"
my_dev=$d_name
fi
done
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 10.1.0.0/16 dev eth0:B 192.168.1.1
Use crontab or another method to have the script run at startup.
Note: This won't work if the interface is a wireless connection.
Solution 2
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 10.1.0.0/16 gw 192.168.1.1 dev eth0:B
pre-down route delete -net route add -net 10.1.0.0/16 gw 192.168.1.1 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.
Edit:
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:
/etc/conf.d/network@eth0
address1=192.168.0.15
netmask1=24
broadcast1=192.168.0.255
gateway1=192.168.0.1
address2=10.0.0.17
netmask2=24
broadcast2=10.0.0.255
gateway2=10.0.0.1
These configurations for the two interfaces are purely conjectural, you should substitute your own. And now /etc/systemd/system/[email protected]
[Unit]
Description=Network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device
[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/network@%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
[Install]
WantedBy=multi-user.target
and you enable and start the service as usual,
systemctl enable [email protected]
systemctl start [email protected]
Related videos on Youtube
Tiana987642
Updated on September 18, 2022Comments
-
Tiana987642 over 1 year
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
andeth0:B
on ethernet card).route add -net 10.1.0.0/16 dev eth0:B 192.168.1.1
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? -
Tiana987642 over 10 yearsHi; 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.
-
canadmos over 10 yearsI 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.
-
Tiana987642 over 10 yearsI use OpenSUSE 12.1 32 bit. What a pity, I think this is a nice approach. THank you!
-
Tiana987642 over 10 yearsWell, if it work with you then it should do fine for me. Thank you!
-
MariusMatutiae over 10 years@Tiana987642 In systemd distros it is even easier.
-
Tiana987642 over 10 yearsCan you write it down here? It can be useful for me or someone else :)
-
MariusMatutiae over 10 years@Tiana987642 Done.
-
Roman Sklyarov almost 7 yearsInterface's name passed as argument into if-up.d scripts. So you can write: case "$IFACE" in eth0) # do smth ;; esac