{"id":2972,"date":"2012-04-19T16:49:29","date_gmt":"2012-04-19T08:49:29","guid":{"rendered":"http:\/\/jpuyy.com\/?p=2972"},"modified":"2021-07-26T00:23:24","modified_gmt":"2021-07-25T16:23:24","slug":"iptables-snat-masquerading","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=2972","title":{"rendered":"iptables\u7684Source NAT\u548cMasquerading"},"content":{"rendered":"<p>\u6765\u81ea\u4e8e<\/p>\n<p>Linux iptables Pocket Reference<\/p>\n<p>Source NAT(SNAT) \u662f\u7528\u6765\u5728\u591a\u53f0\u7535\u8111\u4e4b\u95f4\u5206\u4eab\u4e0a\u7f51\u7684\uff0c\u5145\u5f53gateway\u7684\u7535\u8111\u7528SNAT\uff08\u540c\u65f6\u5f00\u542fconnection tracking\uff09\u6765\u91cd\u5199\u5185\u5916\u7f51\u4e4b\u95f4\u7684\u5305\u3002\u5411\u5916\u53bb\u7684\u5305\u88ab\u6253\u4e0agateway\u7684IP\u5730\u5740\u3002\u5f53\u5916\u90e8\u54cd\u5e94\u540e\uff0c\u4f1a\u5efa\u7acb\u5230\u7f51\u5173IP\u7684\u8fde\u63a5\uff0c\u7f51\u5173\u63a5\u6536\u5230\u8fd9\u4e9b\u5305\u540e\uff0c\u7f51\u5173\u622a\u83b7\u5230\u8fd9\u4e9b\u5305\u540e\uff0c\u628a\u76ee\u6807\u5730\u5740\u8f6c\u5230\u6210\u6b63\u786e\u7684\u5185\u90e8\u7535\u8111\u4e0a\u3002<\/p>\n<p>\u56e0\u4e3aSNAT \u662f\u5728\u5305\u79bb\u5f00\u5185\u6838\u4e4b\u524d\u63a5\u53d7\u4fee\u6539\u4e86\u5b83\u7684\u5730\u5740\u548c\/\u6216\u7aef\u53e3\uff0c\u5728NAT\u8868\u4e2d\u8868\u73b0\u4e3aPOSTROUTING.<\/p>\n<p>\u5728iptables\u4e2d\u6709\u4e24\u79cd\u5b9e\u73b0SNAT\u7684\u65b9\u5f0f\uff1a<\/p>\n<p>\u76ee\u6807\u64cd\u4f5c\u4e3aSNAT\uff0c\u5728\u7f51\u5173ip\u662f\u56fa\u5b9a\u7684\u65f6\u5019<\/p>\n<pre>iptables -t nat -A POSTROUTING -o eth1 -j SNAT<\/pre>\n<p>\u800c\u4f7f\u7528MASQUERADE\u662f\u9632\u6b62\u6389\u7ebf\u540e\u518d\u8fde\u63a5ip\u5730\u5740\u53d1\u751f\u53d8\u5316\u7684\u51fa\u73b0\u3002<\/p>\n<pre>iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE<\/pre>\n<p>\u5982\u679c\u7528ip\u7684\u65b9\u5f0f\u5168\u90e8\u8f6c\u53d1\u7ed9192.168.1.16\u53ef\u7528<\/p>\n<pre>\/sbin\/iptables -t nat -A POSTROUTING -j SNAT --to-source 192.168.1.16<\/pre>\n<p>forwarder \u673a\u5668<\/p>\n<pre>\r\n-A PREROUTING -p tcp -m tcp --dport 30101 -j DNAT --to-destination 74.12.20.82:443\r\n-A POSTROUTING -d 74.12.20.82\/32 -p tcp -m tcp --dport 443 -j MASQUERADE\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u81ea\u4e8e Linux iptables Pocket Reference Source NAT(SNAT) \u662f\u7528\u6765\u5728\u591a\u53f0\u7535\u8111\u4e4b\u95f4\u5206\u4eab\u4e0a\u7f51\u7684\uff0c\u5145\u5f53gateway\u7684\u7535\u8111\u7528SNAT\uff08\u540c\u65f6\u5f00\u542fconnection tracking\uff09\u6765\u91cd\u5199\u5185\u5916\u7f51\u4e4b\u95f4\u7684\u5305\u3002\u5411\u5916\u53bb\u7684\u5305\u88ab\u6253\u4e0agateway\u7684IP\u5730\u5740\u3002\u5f53\u5916\u90e8\u54cd\u5e94\u540e\uff0c\u4f1a\u5efa\u7acb\u5230\u7f51\u5173IP\u7684\u8fde\u63a5\uff0c\u7f51\u5173\u63a5\u6536\u5230\u8fd9\u4e9b\u5305\u540e\uff0c\u7f51\u5173\u622a\u83b7\u5230\u8fd9\u4e9b\u5305\u540e\uff0c\u628a\u76ee\u6807\u5730\u5740\u8f6c\u5230\u6210\u6b63\u786e\u7684\u5185\u90e8\u7535\u8111\u4e0a\u3002 \u56e0\u4e3aSNAT \u662f\u5728\u5305\u79bb\u5f00\u5185\u6838\u4e4b\u524d\u63a5\u53d7\u4fee\u6539\u4e86\u5b83\u7684\u5730\u5740\u548c\/\u6216\u7aef\u53e3\uff0c\u5728NAT\u8868\u4e2d\u8868\u73b0\u4e3aPOSTROUTING. \u5728iptables\u4e2d\u6709\u4e24\u79cd\u5b9e\u73b0SNAT\u7684\u65b9\u5f0f\uff1a \u76ee\u6807\u64cd\u4f5c\u4e3aSNAT\uff0c\u5728\u7f51\u5173ip\u662f\u56fa\u5b9a\u7684\u65f6\u5019 iptables -t nat -A POSTROUTING -o eth1 -j SNAT \u800c\u4f7f\u7528MASQUERADE\u662f\u9632\u6b62\u6389\u7ebf\u540e\u518d\u8fde\u63a5ip\u5730\u5740\u53d1\u751f\u53d8\u5316\u7684\u51fa\u73b0\u3002 iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE \u5982\u679c\u7528ip\u7684\u65b9\u5f0f\u5168\u90e8\u8f6c\u53d1\u7ed9192.168.1.16\u53ef\u7528 \/sbin\/iptables -t nat -A POSTROUTING -j SNAT &#8211;to-source 192.168.1.16 forwarder \u673a\u5668 -A PREROUTING -p tcp -m tcp &#8211;dport 30101 -j DNAT &#8211;to-destination [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-2972","post","type-post","status-publish","format-standard","hentry","category-iptables"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/2972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2972"}],"version-history":[{"count":5,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/2972\/revisions"}],"predecessor-version":[{"id":9351,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/2972\/revisions\/9351"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}