{"id":15984,"date":"2024-11-18T10:25:08","date_gmt":"2024-11-18T02:25:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/15984\/"},"modified":"2024-11-18T10:25:08","modified_gmt":"2024-11-18T02:25:08","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8linux%e4%b8%8a%e8%ae%be%e7%bd%ae%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15984\/","title":{"rendered":"\u5982\u4f55\u5728Linux\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1"},"content":{"rendered":"<p>\u5982\u4f55\u5728linux\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1<\/p>\n<p>\u5728\u5404\u79cd\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u7aef\u53e3\u8f6c\u53d1\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u5916\u90e8\u6d41\u91cf\u8f6c\u53d1\u5230\u5185\u90e8\u7f51\u7edc\u4e2d\u7684\u7279\u5b9a\u7aef\u53e3\u4e0a\u3002\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u901a\u8fc7\u4f7f\u7528iptables\u548csysctl\u6765\u5b9e\u73b0\u7aef\u53e3\u8f6c\u53d1\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728linux\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u68c0\u67e5\u5185\u6838\u53c2\u6570<\/li>\n<\/ol>\n<p>\u5728\u5f00\u59cb\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u68c0\u67e5\u5185\u6838\u53c2\u6570\u662f\u5426\u5141\u8bb8\u8f6c\u53d1\u529f\u80fd\u3002\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684\u8f6c\u53d1\u8bbe\u7f6e\uff1a<\/p>\n<pre>sysctl net.ipv4.ip_forward<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u8f93\u51fa\u7ed3\u679c\u4e3anet.ipv4.ip_forward = 1\uff0c\u8bf4\u660e\u8f6c\u53d1\u529f\u80fd\u5df2\u7ecf\u5f00\u542f\u3002\u5982\u679c\u8f93\u51fa\u7ed3\u679c\u4e3anet.ipv4.ip_forward = 0\uff0c\u5219\u8868\u793a\u8f6c\u53d1\u529f\u80fd\u672a\u5f00\u542f\u3002\u5728\u8f6c\u53d1\u529f\u80fd\u672a\u5f00\u542f\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u4e34\u65f6\u5f00\u542f\u8f6c\u53d1\u529f\u80fd\uff1a<\/p>\n<pre>sysctl -w net.ipv4.ip_forward=1<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u9700\u8981\u6c38\u4e45\u5f00\u542f\u8f6c\u53d1\u529f\u80fd\uff0c\u53ef\u4ee5\u7f16\u8f91\/etc\/sysctl.conf\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u6216\u4fee\u6539\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n<pre>net.ipv4.ip_forward=1<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fee\u6539\u5b8c\u6bd5\u540e\uff0c\u4fdd\u5b58\u6587\u4ef6\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u5176\u751f\u6548\uff1a<\/p>\n<pre>sysctl -p<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1<\/li>\n<\/ol>\n<p>\u5b9e\u73b0\u7aef\u53e3\u8f6c\u53d1\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\uff1a\u4f7f\u7528iptables\u548c\u4f7f\u7528socat\u5de5\u5177\u3002<\/p>\n<p>A. \u4f7f\u7528iptables<\/p>\n<p>iptables\u662fLinux\u7cfb\u7edf\u4e0a\u5e38\u7528\u7684\u9632\u706b\u5899\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7iptables\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>iptables -t nat -A PREROUTING -p tcp --dport &lt;external_port&gt; -j DNAT --to-destination &lt;internal_ip&gt;:&lt;internal_port&gt;\niptables -t nat -A POSTROUTING -j MASQUERADE&lt;\/internal_port&gt;&lt;\/internal_ip&gt;&lt;\/external_port&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d  \u662f\u5916\u90e8\u7aef\u53e3\uff0c  \u662f\u5185\u90e8\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff0c  \u662f\u5185\u90e8\u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\u3002\u8fd9\u4e24\u6761\u547d\u4ee4\u5c06\u4f1a\u5c06\u5916\u90e8\u6d41\u91cf\u8f6c\u53d1\u5230\u5185\u90e8\u670d\u52a1\u5668\u4e0a\u3002\u5982\u679c\u5e0c\u671b\u5916\u90e8\u6d41\u91cf\u7ecf\u8fc7\u8f6c\u53d1\u540e\u4ecd\u7136\u80fd\u591f\u6b63\u786e\u8bc6\u522b\u6e90IP\u5730\u5740\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u547d\u4ee4\uff1a   <\/p>\n<pre>iptables -t nat -A POSTROUTING -o &lt;external_interface&gt; -j MASQUERADE&lt;\/external_interface&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d  \u662f\u5916\u90e8\u63a5\u53e3\u7684\u540d\u79f0\uff0c\u6bd4\u5982eth0\u3002 <\/p>\n<p>B. \u4f7f\u7528socat\u5de5\u5177<\/p>\n<p>socat\u662f\u4e00\u6b3e\u5f3a\u5927\u7684\u7f51\u7edc\u5de5\u5177\uff0c\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u7f51\u7edc\u8fde\u63a5\u548c\u8f6c\u53d1\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7socat\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>socat TCP-LISTEN:&lt;external_port&gt;,fork TCP:&lt;internal_ip&gt;:&lt;internal_port&gt;&lt;\/internal_port&gt;&lt;\/internal_ip&gt;&lt;\/external_port&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d  \u662f\u5916\u90e8\u7aef\u53e3\uff0c  \u662f\u5185\u90e8\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff0c  \u662f\u5185\u90e8\u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\u3002\u8fd9\u6761\u547d\u4ee4\u5c06\u4f1a\u5c06\u5916\u90e8\u6d41\u91cf\u8f6c\u53d1\u5230\u5185\u90e8\u670d\u52a1\u5668\u4e0a\u3002   <\/p>\n<ol>\n<li>\u5e94\u7528\u8f6c\u53d1\u89c4\u5219<\/li>\n<\/ol>\n<p>\u65e0\u8bba\u662f\u4f7f\u7528iptables\u8fd8\u662fsocat\u5de5\u5177\uff0c\u8bbe\u7f6e\u7684\u8f6c\u53d1\u89c4\u5219\u90fd\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u6709\u6548\u3002\u5982\u679c\u5e0c\u671b\u5728\u7cfb\u7edf\u91cd\u542f\u540e\u4f9d\u7136\u6709\u6548\uff0c\u9700\u8981\u5c06\u8fd9\u4e9b\u89c4\u5219\u5e94\u7528\u5230\u7cfb\u7edf\u4e2d\u3002<\/p>\n<p>A. \u4f7f\u7528iptables<\/p>\n<p>\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u5c06iptables\u89c4\u5219\u5e94\u7528\u5230\u7cfb\u7edf\u4e2d\uff1a<\/p>\n<pre>iptables-save &amp;gt; \/etc\/sysconfig\/iptables<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>B. \u4f7f\u7528socat\u5de5\u5177<\/p>\n<p>socat\u5de5\u5177\u9ed8\u8ba4\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u5982\u679c\u5e0c\u671bsocat\u89c4\u5219\u5728\u7cfb\u7edf\u91cd\u542f\u540e\u4f9d\u7136\u6709\u6548\uff0c\u53ef\u4ee5\u5c06socat\u914d\u7f6e\u52a0\u5165\u5230\u7cfb\u7edf\u542f\u52a8\u811a\u672c\u4e2d\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u7cfb\u7edf\u4e0a\u53ef\u4ee5\u7f16\u8f91\/etc\/rc.local\u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>\/path\/to\/socat TCP-LISTEN:&lt;external_port&gt;,fork TCP:&lt;internal_ip&gt;:&lt;internal_port&gt; &amp;amp;&lt;\/internal_port&gt;&lt;\/internal_ip&gt;&lt;\/external_port&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6ce8\u610f\uff0c\/path\/to\/socat\u9700\u8981\u66ff\u6362\u4e3a\u5b9e\u9645\u7684socat\u5de5\u5177\u8def\u5f84\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\uff0c\u63d0\u4f9b\u4e86\u4f7f\u7528iptables\u548csocat\u5de5\u5177\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u5916\u90e8\u6d41\u91cf\u8f6c\u53d1\u5230\u5185\u90e8\u670d\u52a1\u5668\u4e0a\uff0c\u5b9e\u73b0\u7f51\u7edc\u5e94\u7528\u7684\u8d1f\u8f7d\u5747\u8861\u3001\u7aef\u53e3\u6620\u5c04\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Linux\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728linux\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1 \u5728\u5404\u79cd\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u7aef\u53e3\u8f6c\u53d1\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u5916\u90e8\u6d41\u91cf\u8f6c\u53d1\u5230\u5185\u90e8\u7f51\u7edc\u4e2d\u7684\u7279\u5b9a\u7aef\u53e3\u4e0a\u3002\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u901a\u8fc7\u4f7f\u7528iptables\u548csysctl\u6765\u5b9e\u73b0\u7aef\u53e3\u8f6c\u53d1\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728linux\u4e0a\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u68c0\u67e5\u5185\u6838\u53c2\u6570 \u5728\u5f00\u59cb\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u68c0\u67e5\u5185\u6838\u53c2\u6570\u662f\u5426\u5141\u8bb8\u8f6c\u53d1\u529f\u80fd\u3002\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u7684\u8f6c\u53d1\u8bbe\u7f6e\uff1a sysctl net.ipv4.ip_forward \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u8f93\u51fa\u7ed3\u679c\u4e3anet.ipv4.ip_forward = 1\uff0c\u8bf4\u660e\u8f6c\u53d1\u529f\u80fd\u5df2\u7ecf\u5f00\u542f\u3002\u5982\u679c\u8f93\u51fa\u7ed3\u679c\u4e3anet.ipv4.ip_forward = 0\uff0c\u5219\u8868\u793a\u8f6c\u53d1\u529f\u80fd\u672a\u5f00\u542f\u3002\u5728\u8f6c\u53d1\u529f\u80fd\u672a\u5f00\u542f\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u4e34\u65f6\u5f00\u542f\u8f6c\u53d1\u529f\u80fd\uff1a sysctl -w net.ipv4.ip_forward=1 \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u9700\u8981\u6c38\u4e45\u5f00\u542f\u8f6c\u53d1\u529f\u80fd\uff0c\u53ef\u4ee5\u7f16\u8f91\/etc\/sysctl.conf\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u6216\u4fee\u6539\u4ee5\u4e0b\u53c2\u6570\uff1a net.ipv4.ip_forward=1 \u767b\u5f55\u540e\u590d\u5236 \u4fee\u6539\u5b8c\u6bd5\u540e\uff0c\u4fdd\u5b58\u6587\u4ef6\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u5176\u751f\u6548\uff1a sysctl -p \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1 \u5b9e\u73b0\u7aef\u53e3\u8f6c\u53d1\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\uff1a\u4f7f\u7528iptables\u548c\u4f7f\u7528socat\u5de5\u5177\u3002 A. \u4f7f\u7528iptables iptables\u662fLinux\u7cfb\u7edf\u4e0a\u5e38\u7528\u7684\u9632\u706b\u5899\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7iptables\u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a iptables -t nat -A PREROUTING -p tcp &#8211;dport &lt;external_port&gt; -j DNAT &#8211;to-destination &lt;internal_ip&gt;:&lt;internal_port&gt; iptables -t nat -A POSTROUTING -j MASQUERADE&lt;\/internal_port&gt;&lt;\/internal_ip&gt;&lt;\/external_port&gt; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d \u662f\u5916\u90e8\u7aef\u53e3\uff0c \u662f\u5185\u90e8\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff0c \u662f\u5185\u90e8\u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\u3002\u8fd9\u4e24\u6761\u547d\u4ee4\u5c06\u4f1a\u5c06\u5916\u90e8\u6d41\u91cf\u8f6c\u53d1\u5230\u5185\u90e8\u670d\u52a1\u5668\u4e0a\u3002\u5982\u679c\u5e0c\u671b\u5916\u90e8\u6d41\u91cf\u7ecf\u8fc7\u8f6c\u53d1\u540e\u4ecd\u7136\u80fd\u591f\u6b63\u786e\u8bc6\u522b\u6e90IP\u5730\u5740\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u547d\u4ee4\uff1a iptables -t [&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-15984","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15984","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=15984"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15984\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}