{"id":4739,"date":"2017-10-30T23:02:28","date_gmt":"2017-10-30T15:02:28","guid":{"rendered":"https:\/\/www.icocean.com\/blog\/?p=4739"},"modified":"2017-10-30T23:02:28","modified_gmt":"2017-10-30T15:02:28","slug":"iptables%e5%ae%9e%e7%8e%b0%e7%ab%af%e5%8f%a3%e6%98%a0%e5%b0%84","status":"publish","type":"post","link":"https:\/\/www.icocean.com\/blog\/?p=4739","title":{"rendered":"iptables\u5b9e\u73b0\u7aef\u53e3\u6620\u5c04"},"content":{"rendered":"<p>\u9700\u8981\u5c06\u5916\u7f51\u8bbf\u95ee\u672c\u5730IP(192.168.75.5)\u768480\u7aef\u53e3\u8f6c\u6362\u4e3a\u8bbf\u95ee192.168.75.3\u76848000\u7aef\u53e3\uff0c\u8fd9\u5c31\u9700\u8981\u7528\u5230iptables\u7684\u7aef\u53e3\u6620\u5c04\u3002<\/p>\n<p>\u5b9e\u73b0\uff1a<br \/>\n1. \u9700\u8981\u5148\u5f00\u542flinux\u7684\u6570\u636e\u8f6c\u53d1\u529f\u80fd<\/p>\n<p># vi \/etc\/sysctl.conf\uff0c\u5c06net.ipv4.ip_forward=0\u66f4\u6539\u4e3anet.ipv4.ip_forward=1<br \/>\n# sysctl -p  \/\/\u4f7f\u6570\u636e\u8f6c\u53d1\u529f\u80fd\u751f\u6548<\/p>\n<p>2. \u66f4\u6539iptables\uff0c\u4f7f\u4e4b\u5b9e\u73b0nat\u6620\u5c04\u529f\u80fd<\/p>\n<p>\u5c06\u5916\u7f51\u8bbf\u95ee192.168.75.5\u768480\u7aef\u53e3\u8f6c\u53d1\u5230192.168.75.3:8000\u7aef\u53e3\u3002<br \/>\n# iptables -t nat -A PREROUTING -d 192.168.75.5 -p tcp &#8211;dport 80 -j DNAT &#8211;to-destination 192.168.75.3:8000<\/p>\n<p>\u5c06192.168.75.3 8000\u7aef\u53e3\u5c06\u6570\u636e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u65f6\uff0c\u5c06\u6e90ip\u6539\u4e3a192.168.75.5<br \/>\n# iptables -t nat -A POSTROUTING -d 192.168.75.3 -p tcp &#8211;dport 8000 -j SNAT 192.168.75.5<\/p>\n<p>3. \u67e5\u770bnat\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\uff1aiptables -t nat \u2013list\u68c0\u67e5nat\u5217\u8868\u4fe1\u606f\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a<br \/>\niptables<\/p>\n<p>\u5907\u6ce8\uff1a\u4ee5\u4e0a\u662f\u9488\u5bf9\u4ece\u4e00\u53f0\u673a\u5230\u53e6\u4e00\u53f0\u673a\u7684\u7aef\u53e3\u8f6c\u53d1\uff0c\u5982\u679c\u8981\u9488\u5bf9\u672c\u673a\u8fdb\u884c\u7aef\u53e3\u8f6c\u53d1\uff0c\u5c31\u9700\u8981\u6309\u5982\u4e0b\u64cd\u4f5c<\/p>\n<p>\u5c06\u5916\u7f51\u8bbf\u95ee80\u7aef\u53e3\u7684\u6570\u636e\u8f6c\u53d1\u52308080\u7aef\u53e3<br \/>\n# iptables -t nat -A PREROUTING -p tcp &#8211;dport 80 -j REDIRECT &#8211;to-ports 8080<\/p>\n<p>\u5c06\u672c\u673a\u8bbf\u95ee80\u7aef\u53e3\u7684\u8f6c\u53d1\u5230\u672c\u673a8080<br \/>\n# iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 &#8211;dport 80 -j DNAT &#8211;to 127.0.0.1:8080<br \/>\n# iptables -t nat -A OUTPUT -p tcp -d 192.168.4.177 &#8211;dport 80 -j DNAT &#8211;to 127.0.0.1:8080<br \/>\n\u672c\u5730\u8fde\u63a5\u6307\u7684\u662f\u5728\u672c\u673a\u4e0a\uff0c\u7528 127.0.0.1 \u6216\u8005\u672c\u673a IP \u6765\u8bbf\u95ee\u672c\u673a\u7684\u7aef\u53e3\u3002\u672c\u5730\u8fde\u63a5\u7684\u6570\u636e\u5305\u4e0d\u4f1a\u901a\u8fc7\u7f51\u5361\uff0c\u800c\u662f\u7531\u5185\u6838\u5904\u7406\u540e\u76f4\u63a5\u53d1\u7ed9\u672c\u5730\u8fdb\u7a0b\u3002\u8fd9\u79cd\u6570\u636e\u5305\u5728 iptables \u4e2d\u53ea\u7ecf\u8fc7 OUTPUT \u94fe\uff0c\u800c\u4e0d\u4f1a\u7ecf\u8fc7 PREROUTING \u94fe\u3002\u6240\u4ee5\u9700\u8981\u5728 OUTPUT \u94fe\u4e2d\u8fdb\u884c DNAT\u3002\u9664\u4e86\u5bf9 127.0.0.1 \u4e4b\u5916\uff0c\u5bf9\u672c\u673a IP (\u5373 192.168.4.177) \u7684\u8bbf\u95ee\u4e5f\u5c5e\u4e8e\u672c\u5730\u8fde\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981\u5c06\u5916\u7f51\u8bbf\u95ee\u672c\u5730IP(192.168.75.5)\u768480\u7aef\u53e3\u8f6c\u6362\u4e3a\u8bbf\u95ee192.168.75.3\u76848000\u7aef\u53e3\uff0c <a href='https:\/\/www.icocean.com\/blog\/?p=4739' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[2574],"class_list":["post-4739","post","type-post","status-publish","format-standard","hentry","category-linuxunix","tag-iptables","category-16-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4739"}],"version-history":[{"count":1,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4739\/revisions"}],"predecessor-version":[{"id":4740,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4739\/revisions\/4740"}],"wp:attachment":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}