﻿{"id":2008,"date":"2019-10-30T19:09:44","date_gmt":"2019-10-30T11:09:44","guid":{"rendered":"http:\/\/nick.txtcc.com\/?p=2008"},"modified":"2020-04-15T12:00:50","modified_gmt":"2020-04-15T04:00:50","slug":"ecs%e6%97%a0%e5%a4%96%e7%bd%91ip%e8%ae%bf%e9%97%ae%e5%a4%96%e7%bd%91%ef%bc%88nat%e7%bd%91%e5%85%b3%ef%bc%89","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/linux\/2008","title":{"rendered":"ECS\u65e0\u5916\u7f51IP\u8bbf\u95ee\u5916\u7f51\uff08NAT\u7f51\u5173\uff09"},"content":{"rendered":"<p>\u8bf4\u660e\n\u963f\u91cc\u4e91CentOS 7\u65e0\u5916\u7f51IP\u7684ECS\u8bbf\u95ee\u5916\u7f51\uff0c\u6709\u4e24\u53f0\u4ee5\u4e0aECS\u5b9e\u4f8b\uff0c\u5176\u4e2d\u53ea\u6709\u4e00\u53f0\u5206\u914d\u4e86\u516c\u7f51IP\uff0c\u65e0\u5916\u7f51IP\u7684ECS\u9700\u8981\u8bbf\u95ee\u5916\u7f51\n1\n\u81ea\u5efaNAT\u7f51\u5173\u914d\u7f6eSNAT\u548cDNAT\u8f6c\u53d1\u89c4\u5219\n\u963f\u91cc\u4e91-NAT\u7f51\u5173\uff08\u63a8\u8350\uff1a\u9700\u8981\u5916\u7f51\u8bbf\u95ee\u5185\u7f51\u65f6\u4f7f\u7528\uff0c\u7ed1\u5b9a\u5f39\u6027\u516c\u7f51IP\uff0c\u51cf\u5c11\u7aef\u53e3\u51b2\u7a81\uff09\uff08\u8981\u94b1\uff09<\/p>\n<p>\u642d\u5efaNAT\u7f51\u5173\u5c31\u662f\u4e3a\u4e86\u5b9e\u73b0\u5728\u76f8\u540cVPC\u5185\uff0c\u6ca1\u6709\u516c\u7f51IP\u7684ECS\u501f\u52a9\u6709\u516c\u7f51\u7684ECS\u8bbf\u95ee\u5916\u7f51\uff0c\u6216\u8005\u662f\u5916\u7f51\u901a\u8fc7\u7aef\u53e3\u6620\u5c04\u8bbf\u95ee\u5230\u5185\u7f51\u670d\u52a1\u5668\u3002\nSNAT\uff1a\u5b9e\u73b0\u6ca1\u6709\u516c\u7f51IP\u7684ECS\u5b9e\u4f8b\u501f\u52a9\u6709\u516c\u7f51\u7684ECS\u8bbf\u95ee\u5916\u7f51\uff0c\u4f46\u662f\u5916\u7f51\u65e0\u6cd5\u8bbf\u95ee\u5230\u5185\u7f51IP\uff1b\nDNAT\uff1a\u5b9e\u73b0\u5916\u7f51\u901a\u8fc7\u7aef\u53e3\u6620\u5c04\u8bbf\u95ee\u5230\u5185\u7f51\u670d\u52a1\u5668\uff0c\u4f46\u662f\u4e0d\u80fd\u5b9e\u73b0\u5185\u7f51ECS\u8bbf\u95ee\u5230\u5916\u7f51\u3002<\/p>\n<p>\u81ea\u5efaNAT\u7f51\u5173\u914d\u7f6eSNAT\u548cDNAT\u8f6c\u53d1\u89c4\u5219\ncentos7 \u4e3a\u4f8b\uff0c\u5728\u6709\u516c\u7f51\u7684ECS\uff08192.168.0.81\uff09\u4e0a\u64cd\u4f5c\uff1a<\/p>\n<p>\u8bbe\u7f6eSNAT\u89c4\u5219<\/p>\n<h1>\u5f00\u542f\u8f6c\u53d1<\/h1>\n<p>echo &#8220;net.ipv4.ip_forward = 1&#8221; >> \/etc\/sysctl.conf<\/p>\n<h1>\u751f\u6548<\/h1>\n<p>sysctl -p<\/p>\n<h1>\u914d\u7f6eiptables\u505aSNAT\uff1aiptables -t nat -I POSTROUTING -s VPC\u7684IP\u6bb5 -j SNAT &#8211;to-source \u6709\u516c\u7f51IP\u7684ECS\u5185\u7f51IP<\/h1>\n<p>iptables -t nat -I POSTROUTING -s 192.168.0.0\/24 -j SNAT &#8211;to-source 192.168.0.81<\/p>\n<h1>iptables\u89c4\u5219\u91cd\u542f\u4f1a\u6e05\u7a7a\uff0c\u6c38\u4e45\u751f\u6548\uff0c\u8fd8\u9700\u8981\u4fdd\u5b58\u5728iptables\u914d\u7f6e\u6587\u4ef6\u4e2d\uff1a<\/h1>\n<p>service iptables save<\/p>\n<h1>\u5982\u679c\u6ca1\u6709\u5b89\u88c5iptables<\/h1>\n<p>service iptables status #\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u4e86iptables\nyum install -y iptables #\u5b89\u88c5iptables\nyum update iptables #\u5347\u7ea7iptables\nyum install iptables-services #\u5b89\u88c5iptables-services\nsystemctl enable iptables.service # \u8bbe\u7f6e\u9632\u706b\u5899\u5f00\u673a\u542f\u52a8<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p>\u8bbe\u7f6eVPC\u8def\u7531\u6761\u76ee<\/p>\n<p>\u963f\u91cc\u4e91 -> \u4e13\u6709\u7f51\u7edc -> \u7ba1\u7406 -> \u8def\u7531\u8868 -> \u7ba1\u7406 -> \u6dfb\u52a0\u8def\u7531\u6761\u76ee<\/p>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2019\/10\/110944BbS.jpg\" alt=\"\" width=\"619\" height=\"401\" \/>\n<p>\u8bbe\u7f6eDNAT\u89c4\u5219<\/p>\n<h1>\u5c06192.168.0.81\u7684\u516c\u7f51 80 \u7aef\u53e3\u6620\u5c04\u7ed9192.168.0.82<\/h1>\n<h1>iptables -t nat -I PREROUTING -p tcp &#8211;dport \u6709\u516c\u7f51IP\u7684ECS\u7aef\u53e3\u53f7 -j DNAT &#8211;to \u76ee\u6807\u5185\u7f51ECS\u7684\u5185\u7f51IP<\/h1>\n<h1>iptables -t nat -I POSTROUTING -p tcp &#8211;dport \u6709\u516c\u7f51IP\u7684ECS\u7aef\u53e3\u53f7 -j MASQUERADE<\/h1>\n<p>iptables -t nat -I PREROUTING -p tcp &#8211;dport 80 -j DNAT &#8211;to 192.168.0.82\niptables -t nat -I POSTROUTING -p tcp &#8211;dport 80 -j MASQUERADE\nservice iptables save<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n\u524d\u540e\u7aef\u53e3\u6620\u5c04\u4e0d\u4e00\u81f4\uff0c\u9700\u5728\/etc\/sysconfig\/iptables\u6587\u4ef6nat\u8868\u7684dnat\u8f6c\u53d1\u89c4\u5219\uff08\u76ee\u6807IP\uff09\u540e\u9762\uff0c\u76f4\u63a5\u52a0\u7279\u5b9a\u7684\u7aef\u53e3\u53f7\u5c31\u884c\uff1a<\/p>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2019\/10\/110951bbL.jpg\" alt=\"\" width=\"717\" height=\"344\" \/>\n<p>\u91cd\u542fiptables\u4f7f\u89c4\u5219\u751f\u6548\uff1a\nsystemctl restart iptables.service<\/p>\n<p>\u53c2\u8003\uff1a\u4e09\u6b65\u642d\u5efaVPC\u4e13\u6709\u7f51\u7edcNAT\u7f51\u5173\uff0c\u914d\u7f6eSNAT\u548cDNAT\u89c4\u5219\nCentOS7\u5b89\u88c5iptables\u9632\u706b\u5899<\/p>\n<p>\u963f\u91cc\u4e91-NAT\u7f51\u5173\n\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<p>iptables\u89c4\u5219\u51b2\u7a81\u3002\u5982\u679cECS\u5185\u5b89\u88c5\u4e86docker\uff0cdocker\u81ea\u5e26\u7684nat\u8f6c\u53d1\u89c4\u5219\u4f1a\u6709\u5f71\u54cd\uff0c\u9700\u8981\u5408\u7406\u7684\u7f51\u6bb5\u89c4\u5212\u548c\u8def\u7531\u89c4\u5212\u3002\n\u5916\u7f51\u7aef\u53e3\u5360\u7528\u8fc7\u5927\uff0c\u52a0\u4e0a\u9700\u8981\u90e8\u7f72\u5982\uff1aRancher\u8fd9\u79cd\u5bb9\u5668\u96c6\u7fa4\u5e73\u53f0\uff0c\u8fd8\u662f\u63a8\u8350\u642d\u5efa\u4f7f\u7528\u963f\u91cc\u4e91-NAT\u7f51\u5173\uff0c\u4ee5\u7ed1\u5b9a\u5f39\u6027\u516c\u7f51IP\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u5360\u7528ECS\u5b9e\u4f8b\u4e0a\u7684\u516c\u7f51\u7aef\u53e3\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u660e \u963f\u91cc\u4e91CentOS 7\u65e0\u5916\u7f51IP\u7684ECS\u8bbf\u95ee\u5916\u7f51\uff0c\u6709\u4e24\u53f0\u4ee5\u4e0aECS\u5b9e\u4f8b\uff0c\u5176\u4e2d\u53ea\u6709\u4e00\u53f0\u5206\u914d\u4e86\u516c\u7f51IP\uff0c\u65e0\u5916\u7f51IP\u7684ECS\u9700\u8981\u8bbf\u95ee\u5916\u7f51 1 \u81ea\u5efaNAT\u7f51\u5173\u914d\u7f6eSNAT\u548cDNAT\u8f6c\u53d1\u89c4\u5219 \u963f\u91cc\u4e91-NA&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2008","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/2008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=2008"}],"version-history":[{"count":2,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/2008\/revisions"}],"predecessor-version":[{"id":2083,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/2008\/revisions\/2083"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=2008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=2008"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=2008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}