{"id":139001,"date":"2012-01-11T09:51:00","date_gmt":"2012-01-11T01:51:00","guid":{"rendered":"\/2012\/01\/11\/dual_nic.html"},"modified":"2012-01-11T09:51:00","modified_gmt":"2012-01-11T01:51:00","slug":"dual_nic","status":"publish","type":"post","link":"https:\/\/blog.zhenglei.net\/?p=139001","title":{"rendered":"Dual NIC configuration on Ubuntu"},"content":{"rendered":"<p>Suppose there are two NIC on one Ubuntu box:<\/p>\n<p>nic0:&nbsp;&nbsp; eth0&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ip=192.168.127.88<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gateway=192.168.127.2<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<p>nic1:&nbsp;&nbsp; eth1<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ip=172.24.220.72<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gateway=172.24.220.1<\/p>\n<p>&nbsp;<\/p>\n<p>And the original networking configuration is:<\/p>\n<p>&gt; netstat -arp<\/p>\n<p>172.24.0.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; 255.255.0.0&nbsp;&nbsp;&nbsp;&nbsp; U&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 0 0 &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 eth1<br \/>link-local&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; 255.255.0.0&nbsp;&nbsp;&nbsp;&nbsp; U&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 0&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 eth0<br \/>192.168.0.0&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 255.255.0.0&nbsp;&nbsp;&nbsp;&nbsp; U&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 0&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 0 eth0<br \/>default&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 192.168.127.2&nbsp;&nbsp; 0.0.0.0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 0 0&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 0 eth0<\/p>\n<p>&nbsp;<\/p>\n<p>In the above network configuration,&nbsp;&nbsp; eth0 will be used as default gateway of server, all output traffic will go through eth0, except the target address is 172.24.0.0\/16.<\/p>\n<p>&nbsp;<\/p>\n<p>issue:<\/p>\n<p>if one 172.24.0.0\/16&nbsp; client access service via eth0, the return IP trafic will go through eth1, instead of erh0, according to the route table.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution:&nbsp; Using policy route mechanism of linux:<\/p>\n<p>&nbsp;<\/p>\n<p>Step 1 ******&nbsp; &nbsp; Add extra route table for eth1 &amp; eth0:<\/p>\n<p>&nbsp;&nbsp;&nbsp; <span style=\"color: #0000ff;\"><em># echo &#8220;251&nbsp;&nbsp; eth1&#8221; &gt;&gt;\/etc\/iproute2\/rt_tables<\/em><\/span><\/p>\n<p><span style=\"color: #0000ff;\"><em>&nbsp;&nbsp;&nbsp; # echo &#8220;250 &nbsp; eth1&#8221; &gt;&gt;\/etc\/iproute2\/rt_tables<\/em><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Step 2 ******&nbsp;&nbsp; Setup eth1 route table:<\/p>\n<p>&nbsp; &nbsp; <span style=\"color: #0000ff;\"><em>#&nbsp; ip route add 172.24.220.0\/24 dev eth1 src 172.24.220.72 table eth1<br \/>&nbsp; &nbsp; #&nbsp; ip route add default via 172.24.220.1 dev eth1 table eth1<\/em><\/span><\/p>\n<p>&nbsp;&nbsp;&nbsp; <span style=\"color: #0000ff;\"><em># ip route add 192.168.127.0\/24 dev eth0 src 192.168.127.88 table eth0<br \/>&nbsp;&nbsp;&nbsp; # ip route add default via 192.168.127.2 dev eth0 table eth0<\/em><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Step 3 ******&nbsp;&nbsp; Setup rule of eth0 &amp; eth1 table:<\/p>\n<p>&nbsp; <span style=\"color: #0000ff;\"><em># ip rule del prio 1000<br \/>&nbsp; # ip rule del prio 2000<\/p>\n<p>&nbsp; # ip rule add from 172.24.220.72\/16&nbsp; table eth1 prio 1000<br \/>&nbsp; # ip rule add from 192.168.127.88\/16 table eth0 prio 2000<\/em><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Step 4 ******&nbsp;&nbsp; Remove eth1 entry in the default&nbsp; table:<\/p>\n<p>&nbsp; <span style=\"color: #0000ff;\"><em># sudo route del -net 172.24.0.0\/16<\/em><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Step 5 ******&nbsp;&nbsp; Clean route table cache<\/p>\n<p>&nbsp;<span style=\"color: #0000ff;\"><em># sudo ip -s -s route flush cache<\/em><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Example:<\/p>\n<p><strong># cat&nbsp; \/etc\/network\/interfaces<\/strong><\/p>\n<p><em>auto lo<br \/>iface lo inet loopback<\/p>\n<p>auto&nbsp; eth0<br \/>iface eth0 inet dhcp<\/p>\n<p>auto eth1<br \/>iface eth1 inet dhcp<br \/>&nbsp;&nbsp;&nbsp; post-up \/etc\/init.d\/seteth1mask.sh<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>#cat&nbsp; \/etc\/init.d\/seteth1mask.sh<\/strong><\/p>\n<p><em>#! \/bin\/sh<br \/>\/sbin\/ifconfig eth1 netmask 255.255.0.0<br \/>\/sbin\/route add -host 172.24.61.252 gw 192.168.127.2 dev eth0<\/p>\n<p>ip route add 172.24.220.0\/24 dev eth1 src 172.24.220.72 table eth1<br \/>ip route add default via 172.24.220.1 dev eth1 table eth1<\/p>\n<p>ip route add 192.168.127.0\/24 dev eth0 src 192.168.127.88 table eth0<br \/>ip route add default via 192.168.127.2 dev eth0 table eth0<\/p>\n<p>ip rule del prio 1000<br \/>ip rule del prio 2000<\/p>\n<p>ip rule add from 172.24.220.72\/16 table eth1 prio 1000<br \/>ip rule add from 192.168.127.88\/16 table eth0 prio 2000<\/p>\n<p>ip route flush cache<br \/>\/sbin\/route del -net 172.24.0.0\/16<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>****** Note ******<\/p>\n<p>&nbsp; Before network link switch on windows xp\/samba client,&nbsp; need to clean the network cache on windows system by logout action<\/p>\n<p><em><br \/><\/em><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>multi-homed Linux network configuration <a href=\"https:\/\/blog.zhenglei.net\/?p=139001\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,2],"tags":[82],"class_list":["post-139001","post","type-post","status-publish","format-standard","hentry","category-internet","category-linux","tag-route-linux"],"_links":{"self":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts\/139001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=139001"}],"version-history":[{"count":0,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=\/wp\/v2\/posts\/139001\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=139001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=139001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhenglei.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=139001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}