Two or more network interfaces: Difference between revisions

From Notes to self
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
''Based on [https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System the Thomas-Krenn-Wiki]''
''Based on [https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System the Thomas-Krenn-Wiki]''


Suppose that we have a system with already configured network interface <code>eth0</code> and we want to add another interface <code>eth1</code> with address <code>a.a.a.a</code> from the network <code>n.n.n.n/22</code> and the gateway <code>g.g.g.g</code>.
Suppose that we have an additional network interface <code>ethX</code> with a network <code>n.n.n.n/n</code>, an ip address <code>a.a.a.a</code>, and a gateway <code>g.g.g.g</code>.
 
* Add <code>eth1</code> data to <code>/etc/rc.d/rc.inet1.conf</code>. Do not change the gateway.


* Add a routing table to <code>/etc/iproute2/rt_tables</code>:
* Add a routing table to <code>/etc/iproute2/rt_tables</code>:
Line 18: Line 16:
#
#
#1      inr.ruhep
#1      inr.ruhep
1 rt2</pre>
1 rt1</pre>


* Add
* Add
<pre>
<pre>
if ! /sbin/ip route list table rt2 | grep "^default via g.g.g.g dev eth1" 1> /dev/null ; then
if /sbin/ip -4 route show | grep -wq "n.n.n.n/n dev ethX"; then
   /sbin/ip route add n.n.n.n/22 dev eth1 proto kernel src a.a.a.a table rt2
  /sbin/ip -4 route del n.n.n.n/n dev ethX
   /sbin/ip route add default via g.g.g.g dev eth1 metric 1 table rt2
fi
fi</pre>
if ! /sbin/ip -4 route show table rt1 | grep -wq "n.n.n.n/n dev ethX"; then
to the end of the function <code>gateway_up</code> in <code>/etc/rc.d/rc.inet1</code>
   /sbin/ip -4 route add n.n.n.n/n dev ethX scope link src a.a.a.a table rt1
 
fi
* # /etc/rc.d/rc.inet1 restart
if ! /sbin/ip -4 route show table rt1 | grep -wq default; then
 
   /sbin/ip -4 route add default via g.g.g.g dev ethX table rt1
* $ /sbin/ip route list table rt2
fi
if ! /sbin/ip -4 rule show | grep -wq "lookup rt1"; then
  /sbin/ip -4 rule add from a.a.a.a/32 table rt2
  /sbin/ip -4 rule add to a.a.a.a/32 table rt2
fi
</pre>
to the end of IPv4 section of the function <code>gateway_up</code> in <code>/etc/rc.d/rc.inet1</code>


* Add
* Add
<pre>
<pre>
if ! /usr/sbin/ip rule show | grep "a.a.a.a lookup rt2" 1> /dev/null ; then
if /sbin/ip -4 route show table rt1 | grep -wq "n.n.n.n/n dev ethX"; then
   /usr/sbin/ip rule add from a.a.a.a/32 table rt2
   /sbin/ip -4 route del n.n.n.n/n dev ethX table rt1
  /usr/sbin/ip rule add to a.a.a.a/32 table rt2
fi
fi
</pre>to <code>/etc/rc.d/rc.firewall</code>
if /sbin/ip -4 route show table rt1 | grep -wq default; then
 
  /sbin/ip -4 route del default table rt1
* # /etc/rc.d/rc.fiorewall restart
fi
if /sbin/ip -4 rule show | grep -wq "lookup rt1"; then
  /sbin/ip -4 rule del from a.a.a.a/32 table rt1
  /sbin/ip -4 rule del to a.a.a.a/32 table rt1
fi
</pre>
to the end of IPv4 section of the function <code>gateway_down</code> in <code>/etc/rc.d/rc.inet1</code>


* $ /sbin/ip rule show
* <code># /etc/rc.d/rc.inet1 restart</code>


[[Category: Linux]]
[[Category: Linux]]

Latest revision as of 16:15, 13 December 2023

Based on the Thomas-Krenn-Wiki

Suppose that we have an additional network interface ethX with a network n.n.n.n/n, an ip address a.a.a.a, and a gateway g.g.g.g.

  • Add a routing table to /etc/iproute2/rt_tables:
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
1 rt1
  • Add
if /sbin/ip -4 route show | grep -wq "n.n.n.n/n dev ethX"; then
  /sbin/ip -4 route del n.n.n.n/n dev ethX
fi
if ! /sbin/ip -4 route show table rt1 | grep -wq "n.n.n.n/n dev ethX"; then
  /sbin/ip -4 route add n.n.n.n/n dev ethX scope link src a.a.a.a table rt1
fi
if ! /sbin/ip -4 route show table rt1 | grep -wq default; then
  /sbin/ip -4 route add default via g.g.g.g dev ethX table rt1
fi
if ! /sbin/ip -4 rule show | grep -wq "lookup rt1"; then
  /sbin/ip -4 rule add from a.a.a.a/32 table rt2
  /sbin/ip -4 rule add to a.a.a.a/32 table rt2
fi

to the end of IPv4 section of the function gateway_up in /etc/rc.d/rc.inet1

  • Add
if /sbin/ip -4 route show table rt1 | grep -wq "n.n.n.n/n dev ethX"; then
  /sbin/ip -4 route del n.n.n.n/n dev ethX table rt1
fi
if /sbin/ip -4 route show table rt1 | grep -wq default; then
  /sbin/ip -4 route del default table rt1
fi
if /sbin/ip -4 rule show | grep -wq "lookup rt1"; then
  /sbin/ip -4 rule del from a.a.a.a/32 table rt1
  /sbin/ip -4 rule del to a.a.a.a/32 table rt1
fi

to the end of IPv4 section of the function gateway_down in /etc/rc.d/rc.inet1

  • # /etc/rc.d/rc.inet1 restart