





{"id":6132,"date":"2024-07-23T16:58:15","date_gmt":"2024-07-23T08:58:15","guid":{"rendered":"http:\/\/www.killtest.hk\/?p=6132"},"modified":"2024-07-23T16:58:15","modified_gmt":"2024-07-23T08:58:15","slug":"linux%e4%b8%ad%e7%ad%96%e7%95%a5%e8%b7%af%e7%94%b1%e5%af%a6%e4%bd%9c%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.killtest.hk\/index.php\/killtest\/linux%e4%b8%ad%e7%ad%96%e7%95%a5%e8%b7%af%e7%94%b1%e5%af%a6%e4%bd%9c%e5%8e%9f%e7%90%86\/","title":{"rendered":"Linux\u4e2d\u7b56\u7565\u8def\u7531\u5be6\u4f5c\u539f\u7406"},"content":{"rendered":"<p>\u8f49\u81eaToToSun \u534f\u8bae\u68ee\u6797<br \/>\n\u6982\u5ff5<\/p>\n<p><a href=\"https:\/\/www.killtest.net\/LPIC-2\/117-201.asp\">Linux<\/a>\u4e2d\u7b56\u7565\u8def\u7531\u5be6\u4f5c\u539f\u7406<br \/>\n\u50b3\u7d71\u8def\u7531\u53ea\u4f7f\u7528\u4e00\u5f35\u8def\u7531\u8868\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u4f7f\u7528\u591a\u8def\u7531\u8868\uff08multiple Routing Tables)\u3002\u7b56\u7565\u8def\u7531\u6307\u5c0dIP\u5305\u6839\u64da\u7279\u5b9a\u7b56\u7565\u9032\u884c\u8def\u7531\uff0c\u4f8b\u5982\u201c\u6240\u6709\u4f86\u81ea\u7db2\u8defA\u7684\u5305\uff0c\u9078\u64c7X\u8def\u5f91\uff1b\u5176\u4ed6\u9078\u64c7Y\u8def\u5f91\u201d\uff0c\u6216\u8005\u662f\u201c\u6240\u6709TOS\u70baA\u7684\u5305\u9078\u64c7\u8def\u5f91X\uff1b\u5176\u4ed6\u9078\u865f\u8def\u5f91Y\u201d \u3002<br \/>\n\u898f\u5247\u662f\u7b56\u7565\u8def\u7531\u95dc\u9375\u6982\u5ff5\uff0c\u4ec0\u9ebc\u6a23\u7684\u5305\uff0c\u5c07\u61c9\u7528\u672c\u898f\u5247\uff08RULE\uff09\uff0c\u7b26\u5408\u672c\u898f\u5247\u7684\u5305\u5c07\u5c0d\u5176\u63a1\u53d6\u4ec0\u9ebc\u52d5\u4f5c\uff08ACTION\uff09\uff1b\u672c\u898f\u5247\u512a\u5148\u7d1a\u5225\u8d8a\u9ad8\uff0c\u898f\u5247\u8d8a\u5148\u5339\u914d\uff0c\u6578\u503c\u8d8a\u5c0f\u512a\u5148\u7b49\u7d1a\u8d8a\u9ad8\u3002<br \/>\niproute2\u5de5\u5177<\/p>\n<p>\u5be6\u4f5c\u7b56\u7565\u8def\u7531\u914d\u7f6e\u5de5\u5177\u4e00\u822c\u7528iproute2\u5de5\u5177\u5305\u3002 Linux\u6700\u591a\u53ef\u4ee5\u652f\u63f4255\u5f35\u8def\u7531\u8868\uff0c\u5176\u4e2d\u67093\u5f35\u8868\u662f\u5167\u5efa\u7684\uff1a<\/p>\n<p>\u8868255\uff1a\u672c\u5730\u8def\u7531\u8868\uff08Local table\uff09\u672c\u5730\u4ecb\u9762\u4f4d\u5740\uff0c\u5ee3\u64ad\u4f4d\u5740\u4ee5\u53caNAT\u4f4d\u5740\u90fd\u5b58\u653e\u5728\u9019\u5f35\u8868\u3002\u6b64\u8def\u7531\u8868\u7531\u7cfb\u7d71\u81ea\u52d5\u7dad\u8b77\uff0c\u7ba1\u7406\u54e1\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u3002<\/p>\n<p>\u8868254\uff1a\u4e3b\u8def\u7531\u8868\uff08Main table\uff09\u5982\u679c\u6c92\u6709\u6307\u660e\u8def\u7531\u6240\u5c6c\u8868\uff0c\u6240\u6709\u7684\u8def\u7531\u90fd\u9810\u8a2d\u653e\u5728\u9019\u500b\u8868\u88e1\uff0c\u4e00\u822c\u4f86\u8aaa\uff0c\u820a\u8def\u7531\u5de5\u5177\uff08\u5982route\uff09\u6240\u65b0\u589e\u7684\u8def\u7531\u90fd\u6703\u52a0\u5230\u9019\u500b\u8868\u3002\u4e00\u822c\u662f\u666e\u901a\u8def\u7531\u3002<\/p>\n<p>\u8868253\uff1a\u9810\u8a2d\u8def\u7531\u8868\uff08Default table\uff09\u4e00\u822c\u4f86\u8aaa\u9810\u8a2d\u8def\u7531\u90fd\u653e\u5728\u9019\u5f35\u8868\u3002<\/p>\n<p>root:\/# ip rule show<br \/>\n0:      from all lookup local<br \/>\n32766:  from all lookup main<br \/>\n32767:  from all lookup default<br \/>\nroot:\/#<br \/>\nroot:\/# ip route show table local<br \/>\nbroadcast 127.0.0.0 dev lo proto kernel scope link src 127.0.0.1<br \/>\nlocal 127.0.0.0\/8 dev lo proto kernel scope host src 127.0.0.1<br \/>\nlocal 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1<br \/>\nbroadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1<br \/>\nbroadcast 192.168.1.0 dev enp9s0f0 proto kernel scope link src 192.168.1.182<br \/>\nlocal 192.168.1.182 dev enp9s0f0 proto kernel scope host src 192.168.1.182<br \/>\nbroadcast 192.168.1.255 dev enp9s0f0 proto kernel scope link src 192.168.1.182<br \/>\nbroadcast 192.168.2.0 dev enp9s0f1 proto kernel scope link src 192.168.2.182 linkdown<br \/>\nlocal 192.168.2.182 dev enp9s0f1 proto kernel scope host src 192.168.2.182<br \/>\nbroadcast 192.168.2.255 dev enp9s0f1 proto kernel scope link src 192.168.2.182 linkdown<br \/>\nbroadcast 192.168.3.0 dev enp9s0f2 proto kernel scope link src 192.168.3.182 linkdown<br \/>\nlocal 192.168.3.182 dev enp9s0f2 proto kernel scope host src 192.168.3.182<br \/>\nbroadcast 192.168.3.255 dev enp9s0f2 proto kernel scope link src 192.168.3.182 linkdown<br \/>\nbroadcast 192.168.6.0 dev enp52s0f1 proto kernel scope link src 192.168.6.183 linkdown<br \/>\nlocal 192.168.6.183 dev enp52s0f1 proto kernel scope host src 192.168.6.183<br \/>\nbroadcast 192.168.6.255 dev enp52s0f1 proto kernel scope link src 192.168.6.183 linkdown<br \/>\nroot:\/<br \/>\nroot:\/<br \/>\nroot:\/# ip route show table main<br \/>\n192.168.1.0\/24 dev enp9s0f0 proto kernel scope link src 192.168.1.182<br \/>\n192.168.1.0\/24 dev enp9s0f0 proto kernel scope link src 192.168.1.182 metric 101<br \/>\n192.168.2.0\/24 dev enp9s0f1 proto kernel scope link src 192.168.2.182 linkdown<br \/>\n192.168.3.0\/24 dev enp9s0f2 proto kernel scope link src 192.168.3.182 linkdown<br \/>\n192.168.6.0\/24 dev enp52s0f1 proto kernel scope link src 192.168.6.183 linkdown<br \/>\n \u89c4\u5219\u914d\u7f6e<\/p>\n<p>\u5728\u6dfb\u52a0\u89c4\u5219\u65f6\uff0c\u5fc5\u987b\u5148\u786e\u5b9a\u597d\u201c\u6761\u4ef6\u201d\u3001\u201c\u4f18\u5148\u7ea7\u522b\u201d\u53ca\u201c\u8def\u7531\u8868\u201d\uff0c\u6b64\u540e\u624d\u53ef\u6267\u884c\u6dfb\u52a0\u89c4\u5219\u64cd\u4f5c\u3002\u6761\u4ef6\u662f\u7528\u6765\u51b3\u5b9a\u54ea\u7c7b\u6570\u636e\u5305\u53ef\u4ee5\u7b26\u5408\u8fd9\u9879\u89c4\u5219\uff0c\u800c\u53ef\u7528\u6765\u5339\u914d\u7684\u5b57\u6bb5\u4e3aSource IP\u3001Destination IP\u3001Type of Service\u3001fwmark\u53cadev\u7b49\uff0c\u8fd9\u4e9b\u5b57\u6bb5\u7684\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<br \/>\n #\u6839\u636e\u6e90IP<br \/>\nip rule add from 192.168.1.10 table 100<br \/>\nip rule add from 192.168.2.0\/24 table 200<\/p>\n<p>#\u6839\u636e\u76ee\u7684IP<br \/>\nip rule add to 10.0.2.1 table 100<br \/>\nip rule add to 10.0.2.0\/24 table 200<br \/>\n\u5c06fwmark\u4f5c\u4e3a\u5339\u914d\u6761\u4ef6\u65f6\uff0c\u5fc5\u987b\u642d\u914dNetfilter\u4e00\u8d77\u4f7f\u7528\u3002\u67d0\u516c\u53f8\u5bf9\u5916\u6709\u4e09\u79cd\u8def\u7531\uff0c\u5e0c\u671b\u6240\u6709HTTP\u534f\u8bae\u7ecf\u7531\u7b2c\u4e00\u6761ADSL \uff0cSMTP\u53caPOP3\u7ecf\u7531\u7b2c\u4e8c\u6761VDSL\uff0c\u5176\u4f59\u6d41\u91cf\u5219\u7ecf\u7531\u7b2c\u4e09\u6761ETH\u3002\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5b9e\u73b0\u7b56\u7565\u8def\u7531\uff1a<\/p>\n<p>iptables -t mangle -A FORWARD -i adsl -p tcp &#8211;dport 80 -j MARK &#8211;set-mark 1<br \/>\niptables -t mangle -A FORWARD -i vdsl -p tcp &#8211;dport 25 -j MARK &#8211;set-mark 2<br \/>\niptables -t mangle -A FORWARD -i vdsl -p tcp &#8211;dport 110 -j MARK &#8211;set-mark 2<br \/>\niptables -t mangle -A FORWARD -i eth3 -j MARK &#8211;set-mark 3<br \/>\nip rule add fwmark 1 table 1<br \/>\nip rule add fwmark 2 table 2<br \/>\nip rule add fwmark 3 table 3<\/p>\n<p>\u7531eth2\u63a5\u53e3\u9001\u5165\u7684\u6570\u636e\u5305\u90fd\u7531eth0\u63a5\u53e3\u8f6c\u53d1\u51fa\u53bb\uff0c\u7531eth3\u63a5\u53e3\u9001\u5165\u7684\u6570\u636e\u5305\u90fd\u7531eth1\u63a5\u53e3\u8f6c\u53d1\u51fa\u53bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5b9e\u73b0\u7b56\u7565\u8def\u7531\uff1a<\/p>\n<p>ip rule add dev eth2 table 1<br \/>\nip rule add dev eth3 table 3<br \/>\nip rule show\u547d\u4ee4\u6240\u663e\u793a\u5185\u5bb9\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u5c31\u662f\u4f18\u5148\u7ea7\u522b\uff0c\u6570\u5b57\u8d8a\u5c0f\uff0c\u4ee3\u8868\u4f18\u5148\u7ea7\u522b\u8d8a\u9ad8\uff0c\u4e5f\u4ee3\u8868\u8fd9\u6761\u89c4\u5219\u53ef\u4ee5\u6392\u5f97\u8d8a\u9760\u524d\uff0c\u5982\u6b64\u6570\u636e\u5305\u5728\u8fdb\u884c\u6761\u4ef6\u5339\u914d\u65f6\uff0c\u5c31\u4f1a\u8d8a\u65e9\u5339\u914d\u5230\u8fd9\u6761\u89c4\u5219\u3002<\/p>\n<p>[root@localhost ~]# ip rule show<br \/>\n0: from all lookup local<br \/>\n32766: from all lookup main<br \/>\n32767: from all lookup default<br \/>\n[root@localhost ~]#<br \/>\n[root@localhost ~]# ip rule add from 192.168.1.0\/24 table 1 prio 10<br \/>\n[root@localhost ~]# ip rule add from 192.168.2.0\/24 table 2 prio 20<br \/>\n[root@localhost ~]#<br \/>\n[root@localhost ~]# ip rule show<br \/>\n0: from all lookup local<br \/>\n10: from 192.168.1.0\/24 lookup 1<br \/>\n20: from 192.168.2.0\/24 lookup 2<br \/>\n32766: from all lookup main<br \/>\n32767: from all lookup default<br \/>\nip\u547d\u4ee4\u63d0\u4f9b\u7684\u5220\u9664\u89c4\u5219\u7684\u65b9\u5f0f\u5341\u5206\u7075\u6d3b\uff0c\u4f8b\u5982\uff0c\u8981\u5220\u9664\u4e0b\u5217\u7b2c2\u6761\u89c4\u5219\uff0c\u53ef\u4ee5\u5206\u522b\u4f7f\u7528\u201c\u4f18\u5148\u7ea7\u522b\u201d\u3001\u201c\u6761\u4ef6\u201d\u53ca\u201c\u8def\u7531\u8868\u201d\u5f53\u4e2d\u4efb\u4f55\u4e00\u4e2a\u552f\u4e00\u7684\u503c\u6765\u8bbe\u7f6e\u6240\u9700\u5220\u9664\u7684\u89c4\u5219\u3002<\/p>\n<p>ip rule del prio 10<br \/>\nip rule del from 192.168.1.0\/24<br \/>\nip rule del table 1<br \/>\nip rule del from 192.168.1.0\/24 table 1 prio 10<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f49\u81eaToToSun \u534f\u8bae\u68ee\u6797 \u6982\u5ff5 Linux\u4e2d\u7b56\u7565\u8def\u7531\u5be6\u4f5c\u539f\u7406 \u50b3\u7d71\u8def\u7531\u53ea\u4f7f\u7528\u4e00\u5f35\u8def\u7531\u8868\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u9700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[336],"class_list":["post-6132","post","type-post","status-publish","format-standard","hentry","category-killtest","tag-117-201"],"_links":{"self":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/6132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/comments?post=6132"}],"version-history":[{"count":1,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/6132\/revisions"}],"predecessor-version":[{"id":6133,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/posts\/6132\/revisions\/6133"}],"wp:attachment":[{"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/media?parent=6132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/categories?post=6132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.killtest.hk\/index.php\/wp-json\/wp\/v2\/tags?post=6132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}