{"id":61420,"date":"2025-04-29T10:28:34","date_gmt":"2025-04-29T02:28:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/61420\/"},"modified":"2025-04-29T10:28:34","modified_gmt":"2025-04-29T02:28:34","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8linux%e4%b8%8a%e9%85%8d%e7%bd%ae%e9%ab%98%e5%8f%af%e7%94%a8%e7%9a%84nat%e7%bd%91%e5%85%b3-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/61420\/","title":{"rendered":"\u5982\u4f55\u5728Linux\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173"},"content":{"rendered":"<p>\u5982\u4f55\u5728\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684nat\u7f51\u5173<\/p>\n<p>\u6458\u8981\uff1a<br \/>\u7f51\u7edc\u5730\u5740\u8f6c\u6362\uff08NAT\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f51\u7edc\u6280\u672f\uff0c\u7528\u4e8e\u5c06\u79c1\u6709\u7f51\u7edc\u7684IP\u5730\u5740\u8f6c\u6362\u4e3a\u516c\u5171\u7f51\u7edc\u7684IP\u5730\u5740\u3002\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u53ef\u4ee5\u63d0\u9ad8\u7f51\u7edc\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Keepalived\u548ciptables\u5de5\u5177\uff0c\u5728Linux\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u3002<\/p>\n<p>\u5173\u952e\u8bcd\uff1aNAT\u3001\u9ad8\u53ef\u7528\u3001Keepalived\u3001iptables<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u73b0\u4ee3\u7684\u7f51\u7edc\u73af\u5883\u4e2d\uff0c\u7f51\u7edc\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e3a\u4e86\u5b9e\u73b0\u6b64\u76ee\u6807\uff0c\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u975e\u5e38\u5173\u952e\u3002\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u53ef\u4ee5\u5728\u4e3b\u5907\u6a21\u5f0f\u4e0b\u5de5\u4f5c\uff0c\u4ee5\u786e\u4fdd\u5f53\u4e3b\u673a\u53d1\u751f\u6545\u969c\u65f6\uff0c\u5907\u7528\u7f51\u5173\u80fd\u591f\u81ea\u52a8\u63a5\u7ba1\u5de5\u4f5c\uff0c\u5e76\u4fdd\u6301\u7f51\u7edc\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<p>\u6b65\u9aa41\uff1a\u5b89\u88c5\u548c\u914d\u7f6eKeepalived<br \/>1.1 \u5b89\u88c5Keepalived\u8f6f\u4ef6\u5305\uff1a<br \/>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Keepalived\u8f6f\u4ef6\u5305\uff1a<\/p>\n<p>sudo apt-get install keepalived<\/p>\n<p>1.2 \u914d\u7f6eKeepalived\uff1a<br \/>\u8fdb\u5165Keepalived\u7684\u914d\u7f6e\u6587\u4ef6\u76ee\u5f55\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u540d\u4e3akeepalived.conf\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u914d\u7f6e\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4fee\u6539\uff1a<\/p>\n<p>global_defs {<br \/> router_id NatRouter<br \/>}<\/p>\n<p>vrrp_script check_nat_gateway {<br \/> script &#8220;pidof keepalived&#8221;<br \/> interval 2<br \/> weight -1<br \/>}<\/p>\n<p>vrrp_instance NatGateway {<br \/> state MASTER<br \/> interface eth0<br \/> virtual_router_id 1<br \/> priority 100<br \/> advert_int 1<br \/> authentication {<\/p>\n<pre>  auth_type PASS\n  auth_pass YourPasswordHere<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<br \/> virtual_ipaddress {<\/p>\n<pre>  192.168.1.1\/24<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<br \/> track_script {<\/p>\n<pre>  check_nat_gateway<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<br \/>}<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p>\u6b65\u9aa42\uff1a\u914d\u7f6eiptables<br \/>\u4e3a\u4e86\u5b9e\u73b0\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u914d\u7f6eiptables\uff0c\u4f7f\u5176\u53ef\u4ee5\u6b63\u786e\u8f6c\u53d1\u548c\u5904\u7406\u7f51\u7edc\u6d41\u91cf\u3002<\/p>\n<p>2.1 \u542f\u7528IP\u8f6c\u53d1\uff1a<br \/>\u6253\u5f00\/etc\/sysctl.conf\u6587\u4ef6\uff0c\u5e76\u627e\u5230\u4ee5\u4e0b\u884c\uff1a<\/p>\n<h1>net.ipv4.ip_forward=1<\/h1>\n<p>\u53d6\u6d88\u6ce8\u91ca\u8be5\u884c\uff0c\u4f7f\u5176\u53d8\u4e3a\uff1a<\/p>\n<p>net.ipv4.ip_forward=1<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002\u7136\u540e\uff0c\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u4fee\u6539\u751f\u6548\uff1a<\/p>\n<p>sudo sysctl -p<\/p>\n<p>2.2 \u914d\u7f6eiptables\uff1a<br \/>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u914d\u7f6eiptables\uff1a<\/p>\n<p>sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE<\/p>\n<p>\u4fdd\u5b58iptables\u89c4\u5219\uff0c\u5e76\u5c06\u5176\u5199\u5165\u6301\u4e45\u6027\u89c4\u5219\u6587\u4ef6\uff0c\u4f7f\u5176\u5728\u7cfb\u7edf\u91cd\u65b0\u542f\u52a8\u65f6\u751f\u6548\uff1a<\/p>\n<p>sudo iptables-save | sudo tee \/etc\/iptables\/rules.v4<\/p>\n<p>\u6b65\u9aa43\uff1a\u542f\u52a8\u548c\u6d4b\u8bd5\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173<br \/>3.1 \u542f\u52a8Keepalived\u670d\u52a1\uff1a<br \/>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Keepalived\u670d\u52a1\uff1a<\/p>\n<p>sudo service keepalived start<\/p>\n<p>3.2 \u6d4b\u8bd5\u9ad8\u53ef\u7528\u6027\uff1a<br \/>\u901a\u8fc7ping\u547d\u4ee4\u6d4b\u8bd5\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u3002\u4ece\u53e6\u4e00\u53f0\u8ba1\u7b97\u673aping\u865a\u62dfIP\u5730\u5740\uff08192.168.1.1\uff09\uff0c\u5982\u679c\u7f51\u7edc\u6b63\u5e38\uff0c\u5219\u5907\u4efd\u7f51\u5173\u5df2\u6b63\u786e\u63a5\u7ba1\u3002<\/p>\n<p>\u7ed3\u8bba\uff1a<br \/>\u901a\u8fc7\u4f7f\u7528Keepalived\u548ciptables\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Linux\u7cfb\u7edf\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u3002\u8fd9\u79cd\u914d\u7f6e\u65b9\u5f0f\u786e\u4fdd\u4e86\u5f53\u4e3b\u7f51\u5173\u53d1\u751f\u6545\u969c\u65f6\uff0c\u5907\u4efd\u7f51\u5173\u80fd\u591f\u81ea\u52a8\u63a5\u7ba1\u5de5\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u7f51\u7edc\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u53c2\u8003\u6587\u732e\uff1a<\/p>\n<ul>\n<li>&#8220;How To Set Up a High Availability Rolling Update NAT Gateway with Keepalived and HAproxy on Ubuntu 14.04 | DigitalOcean&#8221; (https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-a-high-availability-rolling-update-nat-gateway-with-keepalived-and-haproxy-on-ubuntu-14-04)<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Linux\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684nat\u7f51\u5173 \u6458\u8981\uff1a\u7f51\u7edc\u5730\u5740\u8f6c\u6362\uff08NAT\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f51\u7edc\u6280\u672f\uff0c\u7528\u4e8e\u5c06\u79c1\u6709\u7f51\u7edc\u7684IP\u5730\u5740\u8f6c\u6362\u4e3a\u516c\u5171\u7f51\u7edc\u7684IP\u5730\u5740\u3002\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u53ef\u4ee5\u63d0\u9ad8\u7f51\u7edc\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Keepalived\u548ciptables\u5de5\u5177\uff0c\u5728Linux\u4e0a\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u3002 \u5173\u952e\u8bcd\uff1aNAT\u3001\u9ad8\u53ef\u7528\u3001Keepalived\u3001iptables \u5f15\u8a00\uff1a\u5728\u73b0\u4ee3\u7684\u7f51\u7edc\u73af\u5883\u4e2d\uff0c\u7f51\u7edc\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e3a\u4e86\u5b9e\u73b0\u6b64\u76ee\u6807\uff0c\u914d\u7f6e\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u975e\u5e38\u5173\u952e\u3002\u9ad8\u53ef\u7528\u7684NAT\u7f51\u5173\u53ef\u4ee5\u5728\u4e3b\u5907\u6a21\u5f0f\u4e0b\u5de5\u4f5c\uff0c\u4ee5\u786e\u4fdd\u5f53\u4e3b\u673a\u53d1\u751f\u6545\u969c\u65f6\uff0c\u5907\u7528\u7f51\u5173\u80fd\u591f\u81ea\u52a8\u63a5\u7ba1\u5de5\u4f5c\uff0c\u5e76\u4fdd\u6301\u7f51\u7edc\u7ee7\u7eed\u8fd0\u884c\u3002 \u6b65\u9aa41\uff1a\u5b89\u88c5\u548c\u914d\u7f6eKeepalived1.1 \u5b89\u88c5Keepalived\u8f6f\u4ef6\u5305\uff1a\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Keepalived\u8f6f\u4ef6\u5305\uff1a sudo apt-get install keepalived 1.2 \u914d\u7f6eKeepalived\uff1a\u8fdb\u5165Keepalived\u7684\u914d\u7f6e\u6587\u4ef6\u76ee\u5f55\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u540d\u4e3akeepalived.conf\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u914d\u7f6e\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4fee\u6539\uff1a global_defs { router_id NatRouter} vrrp_script check_nat_gateway { script &#8220;pidof keepalived&#8221; interval 2 weight -1} vrrp_instance NatGateway { state MASTER interface eth0 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass YourPasswordHere \u767b\u5f55\u540e\u590d\u5236 } virtual_ipaddress { 192.168.1.1\/24 \u767b\u5f55\u540e\u590d\u5236 } track_script { check_nat_gateway [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-61420","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/61420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=61420"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/61420\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=61420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=61420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=61420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}