{"id":43,"date":"2021-07-03T12:48:41","date_gmt":"2021-07-03T04:48:41","guid":{"rendered":"http:\/\/szx622.top\/?p=43"},"modified":"2021-07-05T11:51:48","modified_gmt":"2021-07-05T03:51:48","slug":"iptables%e4%b8%adsnat%e3%80%81dnat%e4%b8%8emasqurade%e7%9a%84%e5%8c%ba%e5%88%ab%e4%bb%a5%e5%8f%8a%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91redirct","status":"publish","type":"post","link":"https:\/\/szx622.top\/?p=43","title":{"rendered":"iptables\u4e2dSNAT\u3001DNAT\u4e0eMASQURADE\u7684\u533a\u522b\u4ee5\u53ca\u7aef\u53e3\u8f6c\u53d1REDIRCT"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">iptables\u4e2d\u7684\u8868nat <\/h2>\n\n\n\n<p>\u8be5\u8868\u4e3b\u8981\u7528\u4e8e\u5404\u79cd\u7075\u6d3b\u7684\u7f51\u7edc\u5730\u5740\u548c\u7aef\u53e3\u8f6c\u6362\u3002\u5730\u5740\u8f6c\u6362\u5206\u4e3aSANT\uff08source network address translation\uff09\u6e90\u5730\u5740\u8f6c\u6362\u548cDNAT\uff08destination network address translation\uff09\u76ee\u6807\u5730\u5740\u8f6c\u6362\uff1b\u7aef\u53e3\u8f6c\u6362\u5219\u4e3aREDIRCT\u3002<br>\u5728\u4efb\u4f55\u4e00\u4e2aIP\u6570\u636e\u5305\u7684\u62a5\u5934\u4e2d\uff0c\u90fd\u4f1a\u6709Source IP Address\u4e0eDestination IP Address\u8fd9\u4e24\u4e2a\u5b57\u6bb5\uff0c\u6570\u636e\u5305\u6240\u7ecf\u8fc7\u7684\u8def\u7531\u5668\u4e5f\u662f\u6839\u636e\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u662f\u5224\u5b9a\u6570\u636e\u5305\u662f\u7531\u4ec0\u4e48\u5730\u65b9\u53d1\u8fc7\u6765\u7684\uff0c\u5b83\u8981\u5c06\u6570\u636e\u5305\u53d1\u5230\u4ec0\u4e48\u5730\u65b9\u53bb\u3002iptables\u7684\u539f\u7406\u5c31\u662f\u901a\u8fc7\u66f4\u6539\u5176\u4e2d\u7684\u6e90\u5730\u5740Source IP\u6216\u8005\u76ee\u6807\u5730\u5740Destination IP\u6765\u8fbe\u5230\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u3002<br>\u63a5\u7740\uff0c\u6211\u4eec\u518d\u6765\u770b\u770b\u6570\u636e\u5305\u662f\u5728iptables\u8868\u4e2d\u8981\u7ecf\u8fc7\u7684\u94fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image is-style-nm-none\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/szx622.top\/wp-content\/uploads\/2021\/07\/20200906223545710.jpg\" alt=\"\" class=\"wp-image-44\" width=\"540\" height=\"287\"\/><\/figure><\/div>\n\n\n\n<p><br>\u5176\u4e2d\uff0c\u83f1\u5f62\u94fe\u4e3a\u5224\u65ad\u94fe\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u5176\u4e2d\u7684\u62a5\u6587\u5934\u90e8\u4e2d\u7684\u76ee\u6807\u5730\u5740\u5373destination\u4e3a\u672c\u673a\u5730\u5740\uff0c\u5219\u4f1a\u76f4\u63a5\u5c06\u6570\u636e\u53d1\u9001\u7ed9input\u7136\u540e\u4f20\u9001\u7ed9\u672c\u673a\uff1b\u5982\u679c\u4e0d\u662f\u5219\u7ee7\u7eed\u4f20\u7ed9forward\u94fe\u68c0\u6d4b\u3002\u6240\u4ee5\uff0c\u60f3\u8ba9DNAT\u76ee\u6807\u5730\u5740\u8f6c\u6362\u4e0eREDIRECT\u7aef\u53e3\u8f6c\u6362\u751f\u6548\uff0c\u5219\u5fc5\u987b\u5728\u5176\u7ecf\u8fc7routing decision\u5224\u65ad\u524d\u4fee\u6539\u3002\u5373PREROUTING\u94fe\u4e0a\u8fdb\u884c\u76ee\u6807\u5730\u5740destination IP\u7684\u8f6c\u6362\uff1b\u800c\u6e90\u5730\u5740Source IP\u7684\u8f6c\u6362\uff0c\u5219\u9700\u8981\u5728POSTROUTING\u94fe\u4e2d\u8fdb\u884c<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DNAT<\/h2>\n\n\n\n<p>\u529f\u80fd\uff1a\u5c06\u8bbf\u95ee\u672c\u673a\u6216\u8bbf\u95ee\u4ee5\u672c\u673a\u4f5c\u4e3a\u7f51\u5173\u673a\u7684\u4e3b\u673a\u7684\u6570\u636e\u5305\u5934\u4e2d\u7684\u76ee\u6807\u5730\u5740\uff08Destination IP\uff09\u7531\u539f\u6765\u7684\u76ee\u6807\u5730\u5740\u66ff\u6362\u6210\u6307\u5b9a\u7684\u76ee\u6807\u5730\u5740\u3002<br>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A PREROUTING -d 192.168.127.130 -j DNAT --to-destination 192.168.0.110<\/code><\/pre>\n\n\n\n<p>\u91ca\u4e49\uff1a-t \u5728nat\u8868\u4e2d -A \u6dfb\u52a0\u5230 PREROUTING\u94fe\u4e2d -d \u539f\u76ee\u6807\u5730\u5740\uff08Destination IP\uff09 -j \u6267\u884c\u52a8\u4f5c\u4e3aDNAT\uff08\u76ee\u6807\u5730\u5740\u8f6c\u6362\uff09 &#8211;to-destination \u8f6c\u6362\u5230192.168.0.110<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">REDIRECT<\/h2>\n\n\n\n<p>\u529f\u80fd\uff1a\u5c06\u8bbf\u95ee\u672c\u673a\u6216\u8bbf\u95ee\u4ee5\u672c\u673a\u4f5c\u4e3a\u7f51\u5173\u673a\u7684\u4e3b\u673a\u7684\u6570\u636e\u5305\u5934\u4e2d\u7684\u7aef\u53e3\u7531\u539f\u6765\u7684\u76ee\u6807\u7aef\u53e3\u66ff\u6362\u6210\u6307\u5b9a\u7684\u76ee\u6807\u7aef\u53e3\u3002<br>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A PREROUTING --dport 80 -p tcp -j REDIRCT --to -port 8080<\/code><\/pre>\n\n\n\n<p>\u91ca\u4e49\uff1a\u5c06\u53d1\u9001\u5230\u539f\u76ee\u6807\u7aef\u53e380\u7684\u6570\u636e\u53d1\u9001\u52308080\u7aef\u53e3\u3002\u5728\u7aef\u53e3\u8f6c\u53d1\u65f6\u8fd8\u9700\u6307\u5b9a\u4f20\u8f93\u5c42\u534f\u8bae\u5373tcp\u534f\u8bae\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SNAT<\/h2>\n\n\n\n<p>\u529f\u80fd\uff1a\u5c06\u7ecf\u8fc7\u672c\u673a\u6216\u672c\u7f51\u5173\u673a\u53d1\u51fa\u7684\u6570\u636e\u62a5\u6587\u7684\u5934\u90e8\u4e2d\u7684\u6e90\u5730\u5740\uff08Source IP\uff09\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u6e90\u5730\u5740\u3002<br>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A POSTROUTING -s 192.168.127.0\/24 -j SNAT --to-source 192.168.0.110<\/code><\/pre>\n\n\n\n<p>\u91ca\u4e49\uff1a\u5c06\u6e90\u5730\u5740\u4e3a192.168.127.0\/24\u7f51\u6bb5\u7684\u6570\u636e\u5305\u6539\u4e3a\u4ee5192.168.0.110\u4e3a\u6e90\u5730\u5740\u518d\u53d1\u9001\u3002<br>\u5e94\u7528\u573a\u666f\uff1a\u6570\u636e\u5305\u4ee5\u8be5\u7f51\u6bb5\u7684\u5176\u4ed6\u65e0\u6cd5\u8054\u7f51\u7684\u7f51\u5361\u53d1\u51fa\u65f6\uff0c\u5c06\u5176\u6807\u8bb0\u4e3a\u80fd\u591f\u8054\u7f51\u7684\u7f51\u5361\u518d\u53d1\u9001\uff0c\u7136\u540e\u5728\u670d\u52a1\u5668\u8fd4\u56de\u6570\u636e\u7684\u65f6\u5019\u624d\u80fd\u627e\u5230\u6e90\u5730\u5740\uff0c\u518d\u7531\u53ef\u8054\u7f51\u7684\u7f51\u5361\u8f6c\u53d1\u56de\u771f\u5b9e\u7684\u6e90\u5730\u5740\u3002\u5426\u5219\uff0c\u7f51\u5173\u4f1a\u627e\u4e0d\u5230\u771f\u5b9e\u7684\u6e90\u5730\u5740\uff0c\u5c31\u65e0\u6cd5\u6536\u5230reply\u3002\u5728\u4e3a\u4f2a\u88c5\u6e90\u5730\u5740\u65f6\u4e5f\u4f1a\u4f7f\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MASQUERADE<\/h2>\n\n\n\n<p>\u529f\u80fd\uff1a\u4e0eSNAT\u76f8\u540c\uff0c\u4f46\u662fMASQ\u66f4\u52a0\u667a\u80fd\u3002\u5728MASQ\u8bbe\u5b9a\u597d\u4e4b\u540e\uff0c\u5373\u4f7fIP\u5730\u5740\u53d1\u751f\u53d8\u5316\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd\uff0cMASQ\u4e3b\u8981\u7528\u4e8e\uff0c\u62e8\u53f7\u4e0a\u7f51\u6216DHCP\u6a21\u5f0f\u7b49IP\u5730\u5740\u5bb9\u6613\u53d1\u751f\u5927\u53d8\u5316\u7684\u65f6\u5019\uff0c\u5c24\u5176\u662f\u7f51\u6bb5\u90fd\u4f1a\u53d8\u5316\u7684\u65f6\u5019\u3002<br>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE<\/code><\/pre>\n\n\n\n<p>\u91ca\u4e49\uff1a\u5c06\u7ecf\u8fc7\u672c\u673a\u53d1\u51fa\u7684\u6240\u6709\u6570\u636e\u62a5\u6587\u7684\u5934\u90e8\u4e2d\u7684\u6e90\u5730\u5740\u66ff\u6362\u4e3a\uff0cens33\u7684IP\u5730\u5740-o\u4e3a\u6307\u5b9a\u6570\u636e\u6d41\u51fa\u8bbe\u5907\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>DNAT\u4e0eREDIRCT\u4e3b\u8981\u7528\u4e8e\u4f2a\u88c5\u670d\u52a1\u5668\u771f\u5b9eIP\u6216\u7aef\u53e3\u65f6\u4f7f\u7528\u3002<br>SNAT\u4e0eMASQUERADE\u4e3b\u8981\u7528\u4e8e\u5185\u7f51\u5176\u4ed6\u673a\u5668\u65e0\u6cd5\u8bbf\u95ee\u7f51\u7edc\u65f6\u7528\uff0c\u5c06\u5176\u4ed6\u673a\u5668\u7684\u5185\u7f51\u5730\u5740\u8f6c\u4e3a\u516c\u7f51\u5730\u5740\u3002<br><strong>DNAT<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A PREROUTING -d \u539f\u76ee\u6807\u5730\u5740 -j DNAT --to-destination \u6307\u5b9a\u76ee\u6807\u5730\u5740<\/code><\/pre>\n\n\n\n<p><strong>REDIRECT<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A PREROUTNG --dport \u539f\u76ee\u6807\u7aef\u53e3 -j REDIRECT --to-port \u6307\u5b9a\u76ee\u6807\u7aef\u53e3<\/code><\/pre>\n\n\n\n<p><strong>SNAT<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A POSTROUTING -s \u539f\u6e90\u5730\u5740 -j SNAT --to-source \u6307\u5b9a\u6e90\u5730\u5740<\/code><\/pre>\n\n\n\n<p><strong>MASQUERADE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -t nat -A POSTROUTING -o \u6307\u5b9a\u6d41\u51fa\u7f51\u5361 -j MASQUERADE <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>iptables\u4e2d\u7684\u8868nat \u8be5\u8868\u4e3b\u8981\u7528\u4e8e\u5404\u79cd\u7075\u6d3b\u7684\u7f51\u7edc\u5730\u5740\u548c\u7aef\u53e3\u8f6c\u6362\u3002\u5730\u5740\u8f6c\u6362\u5206\u4e3aSANT\uff08source n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12],"tags":[13,15,14],"class_list":{"0":"post-43","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-linux","8":"tag-iptables","9":"tag-ip","10":"tag-14"},"_links":{"self":[{"href":"https:\/\/szx622.top\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/szx622.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/szx622.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/szx622.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/szx622.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":3,"href":"https:\/\/szx622.top\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/szx622.top\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/szx622.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/szx622.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/szx622.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}