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/network@.service
[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 network@eth0.service
systemctl start network@eth0.service
Interface's name passed as argument into if-up.d scripts. So you can write: case "$IFACE" in eth0) # do smth ;; esac – Roman Sklyarov – 2017-06-05T14:46:25.593
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 – 2013-12-16T16:00:05.757
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 – 2013-12-16T17:43:41.207
Well, if it work with you then it should do fine for me. Thank you! – Tiana987642 – 2013-12-17T14:34:51.057