{"id":2356,"date":"2025-09-25T14:43:40","date_gmt":"2025-09-25T06:43:40","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2356"},"modified":"2025-09-25T14:43:40","modified_gmt":"2025-09-25T06:43:40","slug":"ip-rule-%e7%ad%96%e7%95%a5%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2356","title":{"rendered":"ip rule \u7b56\u7565\u8def\u7531"},"content":{"rendered":"\n<p><code>ip rule<\/code> \u662f Linux <strong>\u7b56\u7565\u8def\u7531\uff08Policy-based Routing, PBR\uff09<\/strong> \u7684\u6838\u5fc3\u547d\u4ee4\uff0c\u7528\u4e8e\u63a7\u5236\u5185\u6838\u5728\u67e5\u627e\u8def\u7531\u65f6<strong>\u4f7f\u7528\u54ea\u5f20\u8def\u7531\u8868<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e00\u3001<code>ip rule<\/code> \u57fa\u672c\u8bed\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ip rule [list|add|del] [selectors] [action]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>selectors<\/strong>\uff1a\u5339\u914d\u6761\u4ef6\uff08\u5982\u6e90 IP\u3001\u5165\u63a5\u53e3\u7b49\uff09<\/li>\n\n\n\n<li><strong>action<\/strong>\uff1a\u5339\u914d\u540e\u6267\u884c\u7684\u52a8\u4f5c\uff08\u6700\u5e38\u89c1\u7684\u662f <code>table N<\/code>\uff0c\u5373\u4f7f\u7528\u8def\u7531\u8868 N\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e8c\u3001<code>ip rule add from &lt;IP&gt; table &lt;N&gt;<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u542b\u4e49\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5339\u914d<strong>\u6e90 IP \u5730\u5740<\/strong>\u4e3a <code>&lt;IP><\/code> \u7684<strong>\u51fa\u7ad9\u6d41\u91cf<\/strong>\uff08\u5373\u672c\u673a\u4e3b\u52a8\u53d1\u51fa\u7684\u6570\u636e\u5305\uff09\u3002<\/li>\n\n\n\n<li>\u5f53\u672c\u673a\u53d1\u9001\u4e00\u4e2a\u5305\uff0c\u5176\u6e90 IP \u662f <code>192.168.1.XX<\/code>\uff0c\u5219\u4f7f\u7528\u8def\u7531\u8868 <code>&lt;N><\/code> \u67e5\u627e\u4e0b\u4e00\u8df3\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d \u4e3e\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ip rule add from 192.168.1.XX table 100<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4f60\u4ece\u672c\u673a\u6267\u884c <code>ping 8.8.8.8<\/code>\uff0c\u4e14\u7cfb\u7edf\u9009\u62e9 <code>192.168.1.XX<\/code> \u4f5c\u4e3a\u6e90 IP \u65f6\uff0c<\/li>\n\n\n\n<li>\u5185\u6838\u4f1a\u53bb\u67e5 <strong>\u8def\u7531\u8868 100<\/strong>\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684 <code>main<\/code> \u8868\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f \u6ce8\u610f\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>from<\/code> <strong>\u53ea\u5bf9\u672c\u673a\u53d1\u51fa\u7684\u6d41\u91cf\u751f\u6548<\/strong>\uff08OUTPUT \u548c\u8f6c\u53d1\u6d41\u91cf\u4e2d\u7684\u6e90 IP \u5339\u914d\uff09\u3002<\/li>\n\n\n\n<li>\u5bf9<strong>\u8fdb\u5165\u672c\u673a\u7684\u6d41\u91cf<\/strong>\uff08\u5982\u522b\u4eba ping \u4f60\uff09<strong>\u4e0d\u751f\u6548<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e09\u3001<code>ip rule add iif &lt;interface&gt; table &lt;N&gt;<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u542b\u4e49\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5339\u914d<strong>\u4ece\u6307\u5b9a\u63a5\u53e3\u8fdb\u5165<\/strong>\uff08ingress interface\uff09\u7684\u6d41\u91cf\u3002<\/li>\n\n\n\n<li>\u901a\u5e38\u7528\u4e8e<strong>\u8f6c\u53d1\u6d41\u91cf<\/strong>\u6216<strong>\u54cd\u5e94\u5165\u7ad9\u8bf7\u6c42\u7684\u56de\u5305\u8def\u5f84\u63a7\u5236<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d \u4e3e\u4f8b\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ip rule add iif eth1 table 200<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709\u4ece <code>eth1<\/code> \u63a5\u53e3\u8fdb\u6765\u7684\u5305\uff08\u6bd4\u5982\u522b\u4eba\u8bbf\u95ee\u4f60 eth1 \u4e0a\u7684 IP\uff09\uff0c<\/li>\n\n\n\n<li>\u5728\u51b3\u5b9a<strong>\u5982\u4f55\u56de\u5305<\/strong>\u65f6\uff0c\u4f1a\u4f7f\u7528\u8def\u7531\u8868 200 \u6765\u67e5\u627e\u51fa\u53e3\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f \u6ce8\u610f\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>iif lo<\/code> \u662f\u4e00\u4e2a\u7279\u4f8b\uff1a\u672c\u5730\u8fdb\u7a0b\u901a\u4fe1\uff08\u5982 <code>curl http:\/\/127.0.0.1<\/code>\uff09\u4e5f\u4f1a\u89e6\u53d1 <code>iif lo<\/code> \u89c4\u5219\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e<strong>\u672c\u673a\u4e3b\u52a8\u53d1\u8d77\u7684\u8fde\u63a5<\/strong>\uff0c<code>iif<\/code> \u901a\u5e38\u662f <code>lo<\/code>\uff0c\u800c\u4e0d\u662f\u7269\u7406\u7f51\u5361\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u56db\u3001\u5176\u4ed6\u5e38\u7528 <code>ip rule<\/code> \u5339\u914d\u6761\u4ef6\uff08selectors\uff09<\/h2>\n\n\n\n<p>\u9664\u4e86 <code>from<\/code> \u548c <code>iif<\/code>\uff0c\u8fd8\u6709\u4ee5\u4e0b\u5e38\u7528\u9009\u9879\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9009\u9879<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>to &lt;IP&gt;<\/code><\/td><td>\u5339\u914d<strong>\u76ee\u7684 IP<\/strong>\uff08\u51fa\u7ad9\u6216\u8f6c\u53d1\uff09<\/td><td><code>ip rule add to 10.0.0.5 table 10<\/code><\/td><\/tr><tr><td><code>oif &lt;iface&gt;<\/code><\/td><td>\u5339\u914d<strong>\u51fa\u7ad9\u63a5\u53e3<\/strong>\uff08\u8f83\u5c11\u7528\uff0c\u56e0\u8def\u7531\u51b3\u5b9a\u51fa\u53e3\uff09<\/td><td><code>ip rule add oif eth0 table 20<\/code><\/td><\/tr><tr><td><code>fwmark &lt;mark&gt;<\/code><\/td><td>\u5339\u914d\u7531 iptables\/netfilter \u6253\u7684\u6807\u8bb0<\/td><td><code>ip rule add fwmark 0x10 table 30<\/code><\/td><\/tr><tr><td><code>priority &lt;N&gt;<\/code><\/td><td>\u8bbe\u7f6e\u89c4\u5219\u4f18\u5148\u7ea7\uff08\u6570\u503c\u8d8a\u5c0f\u8d8a\u5148\u5339\u914d\uff09<\/td><td><code>ip rule add from 192.168.1.56 table 100 priority 1000<\/code><\/td><\/tr><tr><td><code>tos &lt;TOS&gt;<\/code><\/td><td>\u5339\u914d IP \u5934\u4e2d\u7684 TOS \u5b57\u6bb5\uff08\u5df2\u9010\u6e10\u88ab DSCP \u53d6\u4ee3\uff09<\/td><td><code>ip rule add tos 0x10 table 40<\/code><\/td><\/tr><tr><td><code>protocol &lt;proto&gt;<\/code><\/td><td>\u5339\u914d\u8def\u7531\u534f\u8bae\uff08\u5982 kernel, boot, static\uff09<\/td><td>\u8f83\u5c11\u7528\u4e8e\u7b56\u7565\u8def\u7531<\/td><\/tr><tr><td><code>suppress_prefixlength &lt;len&gt;<\/code><\/td><td>\u9690\u85cf\u5c0f\u4e8e\u67d0\u524d\u7f00\u957f\u5ea6\u7684\u8def\u7531\uff08\u9ad8\u7ea7\u7528\u6cd5\uff09<\/td><td>\u2014<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udca1 \u6700\u5e38\u7528\u7684\u662f\uff1a<code>from<\/code>\u3001<code>to<\/code>\u3001<code>iif<\/code>\u3001<code>fwmark<\/code>\u3001<code>priority<\/code><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e94\u3001\u89c4\u5219\u5339\u914d\u987a\u5e8f\uff1apriority\uff08\u4f18\u5148\u7ea7\uff09<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709\u89c4\u5219\u6309 <code>priority<\/code> \u4ece\u5c0f\u5230\u5927\u5339\u914d\u3002<\/li>\n\n\n\n<li>\u9ed8\u8ba4\u89c4\u5219\uff08\u5982 <code>0: from all lookup local<\/code>\uff09\u4f18\u5148\u7ea7\u4e3a <code>0<\/code>\u3001<code>32766<\/code>\u3001<code>32767<\/code>\u3002<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u89c4\u5219\u9ed8\u8ba4\u4f18\u5148\u7ea7\u662f <strong>\u81ea\u52a8\u5206\u914d<\/strong>\uff08\u901a\u5e38\u4ece 32765 \u5f80\u4e0b\uff09\uff0c\u4f46<strong>\u5f3a\u70c8\u5efa\u8bae\u663e\u5f0f\u6307\u5b9a<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u67e5\u770b\u5f53\u524d\u89c4\u5219\u53ca\u4f18\u5148\u7ea7\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ip rule show<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>0:    from all lookup local<br>1000: from 192.168.1.XX lookup 100<br>2000: iif eth1 lookup 200<br>32766: from all lookup main<br>32767: from all lookup default<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 <strong>\u5efa\u8bae<\/strong>\uff1a\u81ea\u5b9a\u4e49\u89c4\u5219\u7528 <code>priority 1000~10000<\/code> \u4e4b\u95f4\uff0c\u907f\u514d\u4e0e\u7cfb\u7edf\u89c4\u5219\u51b2\u7a81\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u516d\u3001\u5178\u578b\u5e94\u7528\u573a\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f 1\uff1a\u591a\u51fa\u53e3\uff08\u53cc WAN\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ip rule add from 192.168.1.XX table wan1<br>ip rule add from 192.168.1.YY table wan2<\/code><\/pre>\n\n\n\n<p>\u2192 \u4e0d\u540c IP \u8d70\u4e0d\u540c\u8fd0\u8425\u5546\u7ebf\u8def\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f 2\uff1a\u5bf9\u79f0\u8def\u7531<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ip rule add iif eth1 table 100   # \u4ece eth1 \u8fdb\u6765\u7684\uff0c\u56de\u5305\u67e5\u8868 100<br>ip route add default via 192.168.1.1 dev eth1 table 100<\/code><\/pre>\n\n\n\n<p>\u2192 \u786e\u4fdd\u56de\u5305\u4ece\u539f\u5165\u53e3\u8fd4\u56de\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u573a\u666f 3\uff1a\u57fa\u4e8e\u5e94\u7528\u6807\u8bb0\u8def\u7531\uff08\u914d\u5408 iptables\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>iptables -t mangle -A OUTPUT -p tcp --dport 443 -j MARK --set-mark 1<br>ip rule add fwmark 1 table https_route<\/code><\/pre>\n\n\n\n<p>\u2192 \u6240\u6709 HTTPS \u6d41\u91cf\u8d70\u7279\u5b9a\u8def\u7531\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc \u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8def\u7531\u8868\u5fc5\u987b\u5b58\u5728<\/strong>\uff1a<code>table N<\/code> \u4e2d\u7684 N \u5fc5\u987b\u5728 <code>\/etc\/iproute2\/rt_tables<\/code> \u4e2d\u5b9a\u4e49\uff08\u6216\u76f4\u63a5\u7528\u6570\u5b57\uff09\u3002<\/li>\n\n\n\n<li><strong>\u89c4\u5219\u4e0d\u7b49\u4e8e\u8def\u7531<\/strong>\uff1a<code>ip rule<\/code> \u53ea\u51b3\u5b9a\u201c\u67e5\u54ea\u5f20\u8868\u201d\uff0c\u8868\u91cc\u5fc5\u987b\u6709\u5b9e\u9645\u8def\u7531\uff08<code>ip route add ... table N<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u672c\u5730\u5730\u5740\u4f18\u5148<\/strong>\uff1a<code>local<\/code> \u8868\uff08\u4f18\u5148\u7ea7 0\uff09\u4f1a\u4f18\u5148\u5904\u7406\u672c\u673a IP\uff0c\u6240\u4ee5 <code>from<\/code> \u89c4\u5219\u901a\u5e38\u5728 <code>local<\/code> \u4e4b\u540e\u751f\u6548\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u8bd5\u547d\u4ee4<\/strong>\uff1a <code>ip route get 8.8.8.8 from 192.168.1.56 # \u67e5\u770b\u5b9e\u9645\u4f7f\u7528\u54ea\u5f20\u8868 ip rule show ip route show table 100<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9009\u9879<\/th><th>\u4f5c\u7528<\/th><th>\u9002\u7528\u6d41\u91cf\u65b9\u5411<\/th><\/tr><\/thead><tbody><tr><td><code>from &lt;IP&gt;<\/code><\/td><td>\u6309<strong>\u6e90 IP<\/strong> \u9009\u8def\u7531\u8868<\/td><td>\u672c\u673a\u53d1\u51fa\uff08OUTPUT\uff09<\/td><\/tr><tr><td><code>iif &lt;iface&gt;<\/code><\/td><td>\u6309<strong>\u5165\u63a5\u53e3<\/strong> \u9009\u8def\u7531\u8868<\/td><td>\u5165\u7ad9\uff08INPUT\/FORWARD\uff09<\/td><\/tr><tr><td><code>to &lt;IP&gt;<\/code><\/td><td>\u6309<strong>\u76ee\u7684 IP<\/strong> \u9009\u8def\u7531\u8868<\/td><td>\u6240\u6709\u65b9\u5411<\/td><\/tr><tr><td><code>fwmark<\/code><\/td><td>\u6309\u9632\u706b\u5899\u6807\u8bb0\u9009\u8868<\/td><td>\u9ad8\u7ea7\u6d41\u91cf\u5206\u7c7b<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>ip rule \u662f Linux \u7b56\u7565\u8def\u7531\uff08Policy-based Routing, PBR\uff09 \u7684\u6838\u5fc3\u547d\u4ee4\uff0c\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":450,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-my","format":"standard","meta":{"footnotes":""},"categories":[135,131],"tags":[237,19,698,696,697],"class_list":["post-2356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-131","tag-linux","tag-19","tag-698","tag-696","tag-697"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2356"}],"version-history":[{"count":1,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2356\/revisions"}],"predecessor-version":[{"id":2357,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2356\/revisions\/2357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/450"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}