This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux:two_default_gateways [2021/04/08 14:44] admin created |
linux:two_default_gateways [2021/04/08 15:17] (current) admin |
||
---|---|---|---|
Line 7: | Line 7: | ||
We assume that we have two interfaces: | We assume that we have two interfaces: | ||
- | <color #00a2e8>eth0 | + | |
- | eth1</color> | + | <code bash> |
+ | eth0 | ||
+ | eth1 | ||
+ | </code> | ||
Two networks that should be used are: | Two networks that should be used are: | ||
<code bash> | <code bash> | ||
192.168.1.0/24 | 192.168.1.0/24 | ||
- | |||
10.10.0.0/24 | 10.10.0.0/24 | ||
</code> | </code> | ||
Line 28: | Line 31: | ||
ifcfg-eth1 | ifcfg-eth1 | ||
</code> | </code> | ||
+ | |||
and it looks like this: | and it looks like this: | ||
Line 111: | Line 115: | ||
# ip rule show | # ip rule show | ||
</code> | </code> | ||
+ | |||
+ | ==== CentOS: Start custom script automatically after network startup ==== | ||
+ | |||
+ | Find the ifup-post under /etc/sysconfig/network-scripts. This scipt is called right after any network interface is brought up online. In this script, you will find the following code snippet toward the end. | ||
+ | <code bash> | ||
+ | |||
+ | if [ -x /sbin/ifup-local ]; then | ||
+ | /sbin/ifup-local ${DEVICE} | ||
+ | </code> | ||
+ | |||
+ | In the code snippet above, if ifup-local script exists in /sbin location, then script gets executed with an interface name in argument. Usually no such ecript like ifup-local exists so in order to run a startup script automatically after a network interface is up. Create an executable script called ifup-local in /sbin and put in there any command or script you wish to run. | ||
+ | |||
+ | Here is an example: | ||
+ | |||
+ | <code bash> | ||
+ | if [[ "$1" == "eth0" ]] | ||
+ | then | ||
+ | echo "this part will be executed right after eth0 is up." | ||
+ | echo "so you can put any startup command for eth0 here" | ||
+ | else | ||
+ | #DO_NOTHING | ||
+ | fi | ||
+ | </code> | ||
+ | |||
+ | when script is done, use command to get the script executable. | ||
+ | |||
+ | <code bash> | ||
+ | $ sudo chmod +x /sbin/ifup-local | ||
+ | </code> | ||
+ | |||
+ | <code bash> | ||
+ | [root@localhost ~]# cat /sbin/ifup-local | ||
+ | #!/bin/sh | ||
+ | |||
+ | #ip route add default via 10.5.23.254 dev eth1 table rt2; | ||
+ | #ip rule add from 10.5.23.0/24 table rt2 | ||
+ | |||
+ | ip route add 10.5.23.0/24 dev eth1 src 10.5.23.10 table rt2; | ||
+ | ip route add default via 10.5.23.254 dev eth1 table rt2; | ||
+ | ip rule add from 10.5.23.10/32 table rt2; | ||
+ | ip rule add to 10.5.23.10/32 table rt2 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | |||
+ | ===== Two default gateways on Debian ===== | ||
+ | |||
+ | ==== Initial Position ==== | ||
+ | |||
+ | |||
+ | We will assume that we have two interfaces: eth0 and eth1. The two networks that should be used are 192.168.0.0/24 and 10.10.0.0/24, whereby the first IP address in each respective network should be the gateway. Under Debian, the initial configuration would appear as follows. /etc/network/interfaces | ||
+ | |||
+ | # 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 | ||
+ | |||
+ | <code bash> | ||
+ | allow-hotplug eth0 | ||
+ | iface eth0 inet static | ||
+ | address 192.168.0.10 | ||
+ | netmask 255.255.255.0 | ||
+ | gateway 192.168.0.1 | ||
+ | </code> | ||
+ | |||
+ | # The secondary network interface | ||
+ | <code bash> | ||
+ | allow-hotplug eth1 | ||
+ | iface eth1 inet static | ||
+ | address 10.10.0.10 | ||
+ | netmask 255.255.255.0 | ||
+ | </code> | ||
+ | |||
+ | Adding a Second Routing Table | ||
+ | |||
+ | To add a new routing table, the file, /etc/iproute2/rt_tables must be edited. We will call the routing table “rt2” and set its preference to 1. The named file should then appear as follows. | ||
+ | |||
+ | <code bash> | ||
+ | # | ||
+ | # reserved values | ||
+ | # | ||
+ | 255 local | ||
+ | 254 main | ||
+ | 253 default | ||
+ | 0 unspec | ||
+ | # | ||
+ | # local | ||
+ | # | ||
+ | #1 inr.ruhep | ||
+ | 1 rt2 | ||
+ | |||
+ | </code> | ||
+ | |||
+ | ==== Configuring the New Routing Table ==== | ||
+ | |||
+ | From this point, four commands are needed to achieve our goal. First, the new routing table needs to be populated, which is done using the following command. | ||
+ | <code bash> | ||
+ | ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2 | ||
+ | ip route add default via 10.10.0.1 dev eth1 table rt2 | ||
+ | </code> | ||
+ | |||
+ | The first command says that the network, 10.10.0.0/24, can be reached through the eth1 interface. The second command sets the default gateway. | ||
+ | Routing Rules | ||
+ | |||
+ | So that the system knows when to use our new routing table, two rules must be configured. | ||
+ | |||
+ | <code bash> | ||
+ | ip rule add from 10.10.0.10/32 table rt2 | ||
+ | ip rule add to 10.10.0.10/32 table rt2 | ||
+ | </code> | ||
+ | |||
+ | These rules say that both traffic from the IP address, 10.10.0.10, as well as traffic directed to or through this IP address, should use the rt2 routing table. | ||
+ | Making the Configuration permanent | ||
+ | |||
+ | The ip rule and ip route commands will become invalid after a re-boot, for which reason they should become part of a script (for example, /etc/rc.local) that will be executed once the network has been started after booting. For Debian, these command can also be written directly into the /etc/network/interfaces file, which would then appear as follows. | ||
+ | |||
+ | <code bash> | ||
+ | iface eth1 inet static | ||
+ | address 10.10.0.10 | ||
+ | netmask 255.255.255.0 | ||
+ | post-up ip route add 10.10.0.0/24 dev eth1 src 10.10.0.10 table rt2 | ||
+ | post-up ip route add default via 10.10.0.1 dev eth1 table rt2 | ||
+ | post-up ip rule add from 10.10.0.10/32 table rt2 | ||
+ | post-up ip rule add to 10.10.0.10/32 table rt2 | ||
+ | </code> | ||
+ | |||
+ | ==== More than Two Network Cards or Gateways ==== | ||
+ | |||
+ | If there are more than two networks, a routing table can be created for each additional network analogous to the example presented above. | ||
+ | Testing the Configuration | ||
+ | |||
+ | The following commands can be used to ensure that the rules as well as the routing entries are working as expected. | ||
+ | |||
+ | <code bash> | ||
+ | ip route list table rt2 | ||
+ | ip rule show | ||
+ | </code> | ||
+ | |||
+ | |||