Date created: 07/01/13 16:57:54. Last modified: 04/05/18 16:00:56

IP routing tables and namespaces

Two sub-interface in different Network Namespaces (on Linux each NetNS is a full IP stack so these are implicitly seperate routing tables):

sudo ip netns list
sudo ip link add link ens2f0 name ens2f0.10 type vlan id 10
sudo ip link set up dev ens2f0.10
sudo ip a a 10.0.1.2/30 dev ens2f0.10
sudo ip r a 10.0.2.0/30 via 10.0.1.1
ip r

sudo ip netns add qos
sudo ip link add link ens2f0 name ens2f0.20 type vlan id 20
sudo ip link set ens2f0.20 netns qos
sudo ip netns exec qos ip link set up dev ens2f0.20
sudo ip netns exec qos ip link show
sudo ip netns exec qos ip a a 10.0.2.2/30 dev ens2f0.20
sudo ip netns exec qos ip r a 10.0.1.0/30 via 10.0.2.1
sudo ip netns exec qos ip r

 

Adding  a custom routing table which also has a default route, to be used for example with an OOB connection;

Show currnet routing tables;

cat /etc/iproute2/rt_tables

Add new routing table, ID 20 called "OOB";

echo "20 OOB" >> /etc/iproute2/rt_tables

Add the rest to /etc/rc.local or similar start up script:

# Bring up our secondary interface which is connected to the OOB network
/sbin/ip link set dev eth1 up
# Add the IP address to this interface /sbin/ip addr add 192.0.2.10/24 dev eth1
# Add a default route on this interface in this custom IP routing table /sbin/ip route add 0/0 via 192.0.2.254 table OOB
ip route show table OOB
# Add a rule to route any traffic that comes from our OOB IP to be routed according # to the routes in the OOB routing table (which simply contains a default route, out via # the same OOB network) /sbin/ip rule add from 192.0.2.10 table OOB

Previous page: 'iptables' - Notes
Next page: Linux Network Tuning