-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2Networks.html
62 lines (62 loc) · 5.87 KB
/
2Networks.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<h1 id="setting-up-two-networks-in-one-server">Setting up Two Networks in One Server</h1>
<p>Hey guys, I am back with a report something interesting that I did today.</p>
<h2 id="the-targets">The Targets</h2>
<p>So yesterday, we restarted this work of setting up 2 networks in a single server using 2 network cards. Suraj and Alok installed CentOS in the server.</p>
<p>Today, Suraj and I configured both the network cards one by one and were able to ping either <IP1> or <IP2> from my local system. Also, we weren't able to ping to <IP3> from the server. ( <IP1> can be accessed from anywhere wheres <IP2> and <IP3> can only be accessed from within the local network)</p>
<p>We had 2 targets:</p>
<ol style="list-style-type: decimal">
<li>To be able to ping/ssh both <IP1> and <IP2> simultaneously.</li>
<li>To be able to ping <IP3> from the server.</li>
</ol>
<p>For that, we did quite the intensive research work. I even tried to learn the basics of networking since we had guessed that problem was between the gateway and the device because we were able to ping to both the gateways irrespective of systems being up or down.</p>
<p>And indeed, the research revealed that the problem was in the gateway since by default a system has only one gateway as multiple gateways may cause ambiguity. The general solution of having a common gateway for both the IP's didn't worked for us. We used the gateway of real IP for local IP and vice versa but in vain.</p>
<h2 id="the-solution">The Solution</h2>
<p>By this time, only me and Satinderpal sir were working on this problem.</p>
<p>I somehow found this nice, <a href="https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System">life-saving tutorial</a> on how to have two default gateways on a system.</p>
<p>According to that, the solution was to create another routing table (by default we have only one) using iproute2 and set up rules on when and how to use that routing table.</p>
<p>So I followed the tutorial, which can be briefly described as:</p>
<ol style="list-style-type: decimal">
<li><p>Add a new routing table called "rt2" (name can be anything) in the file /etc/iproute2/rt_tables with preference one. After which the rt_tables file should look like this:</p>
<p><code># # reserved values # 255 local 254 main 253 default 0 unspec # # local # #1 inr.ruhep 1 rt2</code></p></li>
<li><p>Add network details, IP Address and link it with the respective network card using command:</p></li>
</ol>
<p><code>ip route add <Network2>/24 dev <NetworkCard2> src <IP2> table rt2</code></p>
<ol style="list-style-type: decimal">
<li>Add gateway and link it to the card using:</li>
</ol>
<p><code>ip route add default via <Gateway2> dev <NetworkCard2> table rt2</code></p>
<ol style="list-style-type: decimal">
<li>Add rule to get traffic from IP address using the rt2 table:</li>
</ol>
<p><code>ip rule add from <IP2>/32 table rt2</code></p>
<ol style="list-style-type: decimal">
<li>Add rule to direct traffic to IP Address using the rt2 table:</li>
</ol>
<p><code>ip rule add to <IP2>/32 table rt2</code></p>
<p>After doing these temporary changes, I was able to ping both the IPs simultaneously from my local system, so first target was achieved (YAY!).</p>
<p>For the second target, Satinder sir helped me. We predicted from intuition that we only need to make some changes in the rules. And indeed that was the case. To ping to <IP3>, we only needed to add its from and to rules:</p>
<pre><code>````ip rule add from <IP3>/32 table rt2
ip rule add to <IP3>/32 table rt2````</code></pre>
<p>And Boom!!! We were able to ping to <IP3> from the server, so second target achieved.</p>
<h2 id="more-to-do">More to do</h2>
<p>But these were temporary changes, to make them permanent we had to add these command to some file like /etc/network/interfaces. But alas! CentOS 7 has no such interfaces file. Instead it has rc.local file (in fact 2 of them) where we need to add commands if we want run them at automatically after booting. The commands that should be added to the rc.local file are:</p>
<p><code>iface <NetworkCard2> inet static address <IP1> netmask 255.255.255.0 post-up ip route add <Network2>/24 dev <NetworkCard2> src <IP1> table rt2 post-up ip route add default via <Gateway2> dev <NetworkCard2> table rt2 post-up ip rule add from <IP1>/32 table rt2 post-up ip rule add to <IP1>/32 table rt2</code></p>
<p>The rc.local files requires permission to run after boot which is given by:</p>
<p><code>chmod +x /etc/rc.d/rc.local</code></p>
<p>So after all that, we thought that we were done. But no, after rebooting, the changes we had done did took affect but now the <IP1> was down. We had to shut the <IP2> and get it up again to get the <IP1> back. And then implement the temporary solution again to achieve both targets.</p>
<p>We then tried a few things and did a bit of debugging but nothing worked. So currently, the server is up with both IPs running and everything fine but after a network restart/ system reboot we will be back with only <IP1>.</p>
<p>We will tryo to find a permanent solution and then I will update this blog.</p>
<p>Till then, see ya.</p>
<p>Our guess of the problem is that we are setting the preference of rt2 table higher than the default routing table, thus it is making the network of rt2 table as the primary one. (we have tried changing the preference but to no effect.)</p>
</body>
</html>