{"id":738,"date":"2021-11-26T09:04:00","date_gmt":"2021-11-26T09:04:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=738"},"modified":"2021-11-26T09:04:00","modified_gmt":"2021-11-26T09:04:00","slug":"738","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/11\/26\/738\/","title":{"rendered":"Proxmox VE \u4f7f\u7528iptables\u7aef\u53e3\u8f6c\u53d1\u4f7f\u7528"},"content":{"rendered":"<h2>Proxmox VE \u9ed8\u8ba4\u7aef\u53e3\u5728\u9632\u706b\u5899\u91cc\u5173\u95ed\uff0c\u4f7f\u7528\u7aef\u53e3\u8f6c\u53d1\u4e34\u65f6\u4f7f\u7528<\/h2>\n<pre><code>iptables -t nat -I PREROUTING -p tcp --dport 10086 -j REDIRECT --to-ports 8006\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u5b8c\u6210\u4ee5\u540e\uff0c\u628a\u7aef\u53e3\u8f6c\u53d1\u8bed\u53e5\u5173\u95ed<\/h3>\n<pre><code>iptables -t nat -D PREROUTING -p tcp --dport 10086 -j REDIRECT --to-ports 8006\n\n<\/code><\/pre>\n<h3>\u67d0\u6bb5\u7aef\u53e3\u7ed9\u7ed9\u5206\u914d \u67d0NAT\u5c0f\u9e21\u4f7f\u7528\u3002<\/h3>\n<pre><code>iptables -t nat -A PREROUTING -p tcp -m multiport --dport 11100:11199  -j DNAT --to-destination 10.10.10.111\n<\/code><\/pre>\n<h3>\u767b\u9646NAT\u5c0f\u9e21\uff0c\u53ea\u8981\u6267\u884c\u7aef\u53e3\u8f6c\u53d1\uff0c\u800c\u4e0d\u9700\u8981\u4fee\u6539\u7f51\u7edc\u670d\u52a1\u7684\u914d\u7f6e\u6587\u4ef6<\/h3>\n<pre><code>iptables -t nat -A PREROUTING -p tcp --dport 11188 -j REDIRECT --to-ports 80\n\n<\/code><\/pre>\n<h2>PVE NAT \u6279\u91cf\u7aef\u53e3\u8f6c\u53d1\u8bbe\u7f6e\u811a\u672c\uff0c\u4f7f\u7528\u547d\u4ee4 nat.sh $(lxc-ls) \u81ea\u52a8\u8c03\u7528 lxc\u5bb9\u5668id<\/h2>\n<pre><code># Usage: bash nat.sh $(lxc-ls)\n\n# \u5c0f\u9e21\u7684\u7aef\u53e3\u8f6c\u53d1\u89c4\u5219    ssh 22  http 80      10&lt;N&gt;00 : 10&lt;N&gt;99\n# ID\u5bf9\u5e94IP  101:         10122    10180        10100 : 10199\n#-------------------------------------------------------------#\ninput_id()\n{\n  id=101\n  echo -e \"\u6d4b\u8bd5\u9ed8\u8ba4ID: \u000033[41;37m ${id} \u000033[0m \u53ef\u4ee5\u4fee\u6539\u8bbe\u7f6e\u5176\u4ed6ID; \"\n  read -p \"\u8bf7\u8f93\u5165NAT\u5c0f\u9e21\u7684ID\u53f7(\u6309\u56de\u8f66\u4e0d\u4fee\u6539): \" -t 30 new\n  if [[ ! -z \"${new}\" ]]; then\n      id=\"${new}\"\n  fi\n  nat_port\n  iptables -t nat -nvL PREROUTING\n  echo -e \":: PVE NAT \u6279\u91cf\u7aef\u53e3\u8f6c\u53d1\u8bbe\u7f6e\u811a\u672c: \u000033[41;37m bash nat.sh $(lxc-ls) \u000033[0m\n\u4f7f\u7528\u53c2\u8003: https:\/\/lyvba.com\/index.php\/archives\/714\/\"\n}\n# \u4ee5id\u4e3aip\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\nnat_port()\n{\n    iptables -t nat -I PREROUTING -p tcp -m tcp --dport ${id}22  -j DNAT --to-destination 10.10.10.${id}:22\n    iptables -t nat -A PREROUTING -p tcp -m multiport --dport ${id}00:${id}99  -j DNAT --to-destination 10.10.10.${id}\n}\n\n# \u624b\u5de5\u8f93\u5165id\uff0cinput_id\u8c03\u7528nat\u7aef\u53e3\u8f6c\u53d1\nif [ $# -eq 0 ];\n  then\n  input_id\n  exit\nfi\n\n# \u904d\u5386\u53c2\u6570 \u6279\u91cf\u8bbe\u7f6e nat\u7aef\u53e3\u8f6c\u53d1\nfor arg in $*\ndo\n  id=$arg\n  nat_port\ndone\n\n# \u67e5\u770b nat PREROUTING \u7aef\u53e3\u6620\u5c04\u89c4\u5219\niptables -t nat -nvL PREROUTING\n\n# \u6e05\u7a7a nat PREROUTING \u7aef\u53e3\u6620\u5c04\u89c4\u5219\n# iptables -t nat -F PREROUTING\n# Usage: bash nat.sh $(lxc-ls)<\/code><\/pre>\n<h3>\u4f7f\u7528\u811a\u672c\u540e <code>multiport dports 11100:11199 to:10.10.10.111<\/code> \uff0c\u5206\u914d100\u4e2a\u7aef\u53e3\u7ed9ID 111\u53f7NAT\u5c0f\u9e21\u4f7f\u7528<\/h3>\n<ul>\n<li>\n<p>\u767b\u9646NAT\u5c0f\u9e21\uff0c\u4e0d\u9700\u8981\u4fee\u6539Nginx\u914d\u7f6e\uff0c\u53ea\u9700\u8981\u8fd0\u884c <code>iptables<\/code> \u628a11180\u8f6c\u53d1\u523080\u7aef\u53e3\u5c31\u884c<\/p>\n<p>iptables -t nat -A PREROUTING -p tcp --dport 11180 -j REDIRECT --to-ports 80<\/p>\n<\/li>\n<\/ul>\n<h3>\u7406\u89e3\u4ee5\u4e0a\u7b80\u5355\u7684\u7aef\u53e3\u8f6c\u53d1\uff0c\u518d\u6765\u52a0\u5f3a\u5b66\u4e60,\u6765\u5b66\u4e60\u9ad8\u7ea7\u70b9\u7684\u7528\u6cd5<\/h3>\n<h2>iptables\u7aef\u53e3\u8f6c\u53d1 <em> \u7531 <a href=\"https:\/\/www.taterli.com\/author\/taterli\/\" title=\"\nd8f9da8f0719db34aabf89c4cc9fb4b32\">TaterLi<\/a>  <\/em> 2019\u5e7410\u670821\u65e52019\u5e7410\u670826\u65e5<\/h2>\n<p>\u5b9e\u9645\u4e0a\u5f88\u591a\u4eba\u90fd\u77e5\u9053\u7aef\u53e3\u8f6c\u53d1,\u4f46\u662f\u5f88\u591a\u90fd\u662f\u4e00\u952e\u811a\u672c,\u5b9e\u9645\u4e0a\u4e5f\u6ca1\u6709\u90a3\u4e48\u9ebb\u70e6,\u6211\u4eec\u5f88\u7b80\u5355\u5c31\u80fd\u505a\u5230,\u91cd\u70b9\u662f\u5982\u4f55\u7406\u89e3\u7aef\u53e3\u8f6c\u53d1\u8fd9\u4e2a\u6982\u5ff5.<\/p>\n<p>\u8f6c\u53d1\u5305\u542b\u4e86TCP\u8f6c\u53d1,UDP\u8f6c\u53d1,\u6700\u5e38\u7528\u7684TCP\u8f6c\u53d1\u4e86\u5427,\u505a\u5821\u5792\u673a,\u76ee\u6807\u670d\u52a1\u5668\u9632\u706b\u5899\u767d\u540d\u5355\u673a\u5236\u4e4b\u7c7b\u7684.<\/p>\n<p>\u8f6c\u53d1\u64cd\u4f5c\u5168\u7a0b\u90fd\u5728\u81ea\u5df1\u7684\u673a\u5668\u4e0a\u505a\u5c31\u884c.<\/p>\n<p>\u8f6c\u53d1\u5c31\u5f97\u6253\u5f00\u8f6c\u53d1\u529f\u80fd,\u5982\u679c\u5305\u542bIPv6\u81ea\u5df1\u6539\u6539\u5c31\u884c.<\/p>\n<pre><code>net.ipv4.ip_forward = 1\n<\/code><\/pre>\n<p>\u5728\u8f6c\u53d1\u4e4b\u524d,\u6211\u4eec\u8981\u5148\u8bf4\u4e0b\u8def\u7531,\u4ed6\u4eec\u7684\u8def\u5f84\u5927\u81f4\u662f\u8fd9\u6837\u7684.<\/p>\n<p>\u6e90\u5730\u5740\u53d1\u9001\u6570\u636e-&gt; {PREROUTING-&gt;\u8def\u7531\u89c4\u5219-&gt;POSTROUTING} -&gt;\u76ee\u7684\u5730\u5740\u63a5\u6536\u5230\u6570\u636e<\/p>\n<p>\u53e6\u5916\u8f6c\u53d1\u662f\u9644\u5e26SNAT\u4f5c\u7528\u7684,\u6240\u4ee5\u5c31\u53ef\u4ee5\u8f6c\u6362\u6e90\u5730\u5740\u4e86\u554a.<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u4f8b\u5b50:<\/p>\n<pre><code>\niptables -t nat -A PREROUTING -p tcp --dport 49022 -j DNAT --to-destination 63.223.9.40:6022\niptables -t nat -A POSTROUTING -p tcp -d 63.223.9.40 --dport 6022 -j SNAT --to-source 47.56.22.202\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f60\u8bbf\u95ee47.56.22.202\u768449022\u7aef\u53e3,\u5c31\u76f8\u5f53\u4e8e\u900f\u660e\u7684\u8bbf\u95ee\u4e8663.223.9.40\u76846022\u7aef\u53e3,\u8fd9\u91cc\u53ea\u6253\u901a\u4e86TCP\u4f20\u8f93.<\/p>\n<p>\u4f46\u662f\u6709\u4e00\u70b9\u8981\u6ce8\u610f,\u5982\u679c\u4f60\u7684\u673a\u5668\u662f\u5185\u7f51\u673a\u5668,\u6bd4\u5982\u8bf4\u963f\u91cc\u4e91ECS\u4ec0\u4e48\u7684,47.56.22.202\u5c31\u5e94\u8be5\u66ff\u6362\u4e3a\u4f60\u7684\u5185\u7f51IP\u4e86,\u6bd4\u5982172.31.112.205\u8fd9\u6837\u7684,\u4e0d\u7136\u662f\u5230\u8fbe\u4e0d\u4e86\u7684,\u518d\u7531\u5404\u79cd\u8f6c\u53d1\u5668\u6765\u5b8c\u6210\u5404\u79cd\u9700\u6c42.<\/p>\n<p>\u53e6\u5916,\u53ea\u662f\u8f6c\u53d1\u5355\u4e2a\u7aef\u53e3,\u4e5f\u592a\u6d6a\u8d39\u4e86,\u8fd8\u53ef\u4ee5\u505a\u8d1f\u8f7d\u5747\u8861\u54e6.<\/p>\n<p>\u6bd4\u5982\u8fd9\u6837,\u5c31\u5747\u5206\u6570\u636e\u5305\u5230\u4e24\u53f0\u540e\u7aef\u670d\u52a1\u5668,\u66b4\u9732\u4e86Web\u7aef\u53e3.<\/p>\n<pre><code>iptables -t nat -A PREROUTING -p tcp --dport 80 -m statistic --mode nth --every 2 --packet 0 -j DNAT --to-destination 199.19.104.1:8080\n\niptables -t nat -A PREROUTING -p tcp --dport 80 -m statistic --mode nth --every 1 --packet 0 -j DNAT --to-destination 199.19.104.2:8080\n\n\niptables -t nat -A POSTROUTING -p tcp -d 199.19.104.1 --dport 8080 -j SNAT --to-source 172.31.112.205\n\niptables -t nat -A POSTROUTING -p tcp -d 199.19.104.2 --dport 8080 -j SNAT --to-source 172.31.112.205\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6bd4\u8f83\u7eff\u8272\u73af\u4fdd,\u4e0d\u7528\u989d\u5916\u88c5\u8f6f\u4ef6\u7684\u8f6c\u53d1,\u603b\u4f53\u6765\u8bf4\u8fd8\u662f\u4e0d\u9519\u7684.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Proxmox VE \u9ed8\u8ba4\u7aef\u53e3\u5728\u9632\u706b\u5899\u91cc\u5173\u95ed\uff0c\u4f7f\u7528\u7aef\u53e3\u8f6c\u53d1\u4e34\u65f6\u4f7f\u7528 iptables -t nat  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[60],"class_list":["post-738","post","type-post","status-publish","format-standard","hentry","category-vps","tag-pve"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=738"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/738\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}