Two or more network interfaces: Difference between revisions

From Notes to self
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
''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 and we want to add an ip address <code>a.a.a.a</code> with gateway <code>g.g.g.g</code>.


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 network <code>n.n.n.n/22</code> and gateway <code>g.g.g.g</code>.
* Add the address to <code>/etc/rc.d/rc.inet1.conf</code>:
 
<pre>
* Add <code>eth1</code> data to <code>/etc/rc.d/rc.inet1.conf</code>. Do not change the gateway.
IPADDRS[X]="... a.a.a.a/aa"
</pre>


* 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 19:
#
#
#1      inr.ruhep
#1      inr.ruhep
1 rt1</pre>
2 rt2</pre>


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


* # /etc/rc.d/rc.firewall restart
* # /etc/rc.d/rc.inet1 restart
 
* $ /sbin/ip rule show


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

Revision as of 12:56, 1 April 2023

Suppose that we have a system with already configured network interface and we want to add an ip address a.a.a.a with gateway g.g.g.g.

  • Add the address to /etc/rc.d/rc.inet1.conf:
IPADDRS[X]="... a.a.a.a/aa"
  • Add a routing table to /etc/iproute2/rt_tables:
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
2 rt2
  • Add
if ! /sbin/ip -4 route show table rt2 | grep -wq default; then
  debug_log "/sbin/ip -4 route add default via g.g.g.g table rt2"
  /sbin/ip -4 route add default via g.g.g.g table rt2
fi
if ! /sbin/ip -4 rule show | grep "lookup rt2" 1> /dev/null ; then
  debug_log "/sbin/ip -4 rule add from a.a.a.a table rt2"
  /sbin/ip -4 rule add from a.a.a.a 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 rt2 | grep -wq default; then
  debug_log "/sbin/ip -4 route del default table rt2"
  /sbin/ip -4 route del default table rt2
fi
if /sbin/ip -4 rule show | grep "lookup rt2" 1> /dev/null ; then
  debug_log "/sbin/ip -4 rule del lookup rt2"
  /sbin/ip -4 rule del lookup rt2
fi

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

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