{"id":410,"date":"2024-11-20T17:08:07","date_gmt":"2024-11-20T09:08:07","guid":{"rendered":"http:\/\/192.168.5.24\/?p=410"},"modified":"2025-04-27T18:25:19","modified_gmt":"2025-04-27T10:25:19","slug":"iptables%e6%9c%8d%e5%8a%a1%e7%9a%84%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%a7%84%e5%88%99%e5%88%b6%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/bigvip.dpdns.org\/?p=410","title":{"rendered":"iptables\u670d\u52a1\u7684\u914d\u7f6e\u53ca\u89c4\u5219\u5236\u5b9a"},"content":{"rendered":"\n<p>    iptables\u662f\u7ba1\u7406linux\u9632\u706b\u5899\u6a21\u5757netfilter\u7684\u7528\u6237\u7aef\u63a5\u53e3\u7a0b\u5e8f\uff0c\u901a\u8fc7iptables\u53ef\u4ee5\u65b9\u4fbf\u7684\u7ba1\u7406\u3001\u8bbe\u7f6e\u5185\u6838\u7684\u5305\u8fc7\u6ee4\u6a21\u5757\u3002<\/p>\n\n\n\n<p>      netfliter\u53ef\u4ee5\u4f7f\u7528iptables\u7ba1\u7406\u4e5f\u53ef\u4ee5\u7531firewalld\u7ba1\u7406\uff0cfirewalld\u5b83\u662f\u5728 iptables \u57fa\u7840\u4e4b\u4e0a\u8fdb\u884c\u4e86\u4e00\u5b9a\u7684\u5c01\u88c5\u548c\u62bd\u8c61\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u76f4\u89c2\u3001\u6613\u7528\u7684\u914d\u7f6e\u65b9\u5f0f\uff0c\u6bd4\u5982\u4f7f\u7528\u533a\u57df\uff08zone\uff09\u7684\u6982\u5ff5\uff0c\u4e0d\u540c\u7684\u533a\u57df\u9884\u5b9a\u4e49\u4e86\u4e0d\u540c\u7684\u7f51\u7edc\u8bbf\u95ee\u7b56\u7565\u3002<\/p>\n\n\n\n<p>       iptables \u7684\u914d\u7f6e\u662f\u76f4\u63a5\u4f5c\u7528\u4e8e\u5185\u6838\u89c4\u5219\u94fe\uff0c\u800c firewalld \u662f\u901a\u8fc7\u81ea\u8eab\u7684\u914d\u7f6e\u903b\u8f91\uff08\u5982\u533a\u57df\u914d\u7f6e\u3001\u670d\u52a1\u914d\u7f6e\u7b49\uff09\u518d\u53bb\u8f6c\u5316\u6210\u5185\u6838\u89c4\u5219\u3002\u5982\u679c\u540c\u65f6\u914d\u7f6e\u5e76\u542f\u7528\u4e8c\u8005\uff0cfirewalld \u540e\u7eed\u66f4\u65b0\u914d\u7f6e\u751f\u6210\u65b0\u7684\u5185\u6838\u89c4\u5219\u65f6\uff0c\u53ef\u80fd\u4f1a\u8986\u76d6\u6389 iptables \u624b\u52a8\u7cbe\u5fc3\u914d\u7f6e\u7684\u4e00\u4e9b\u89c4\u5219\uff0c\u4f7f\u5f97 iptables \u914d\u7f6e\u5931\u6548\uff1b\u53cd\u4e4b\u4ea6\u7136\uff0ciptables \u624b\u52a8\u6dfb\u52a0\u7684\u65b0\u89c4\u5219\u4e5f\u53ef\u80fd\u7834\u574f firewalld \u6309\u7167\u533a\u57df\u7b49\u6982\u5ff5\u8bbe\u5b9a\u597d\u7684\u7f51\u7edc\u8bbf\u95ee\u7b56\u7565\uff0c\u5bfc\u81f4 firewalld \u914d\u7f6e\u51fa\u73b0\u7d0a\u4e71\uff0c\u65e0\u6cd5\u6309\u7167\u9884\u671f\u7ba1\u63a7\u7f51\u7edc\u8bbf\u95ee\u3002<br>     \u57fa\u4e8e\u4e0a\u8ff0\u539f\u56e0\uff0c\u5728\u5b9e\u9645\u7684 Linux \u7cfb\u7edf\u5e94\u7528\u4e2d\uff0c<strong>\u901a\u5e38\u5efa\u8bae\u53ea\u9009\u62e9\u4f7f\u7528 iptables \u670d\u52a1\u6216\u8005 firewalld \u670d\u52a1\u4e2d\u7684\u4e00\u79cd\u6765\u8fdb\u884c\u7cfb\u7edf\u7684\u7f51\u7edc\u9632\u706b\u5899\u7ba1\u7406\uff0c\u907f\u514d\u4e8c\u8005\u540c\u65f6\u542f\u7528\u5e26\u6765\u7684\u5404\u79cd\u51b2\u7a81\u95ee\u9898\u3002<\/strong><\/p>\n\n\n\n<p>iptabes\u4f9d\u8d56\u7684\u8f6f\u4ef6\u6709\u4e24\u4e2a\uff1aiptables\u3001iptables-services\uff0c\u4e00\u822c\u7cfb\u7edf\u4f1a\u9ed8\u8ba4\u5b89\u88c5\u7b2c\u4e00\u4e2a\u5305\uff0c<strong>\u7b2c\u4e8c\u4e2a\u5305\u662f\u670d\u52a1\u5305\uff0c\u53ef\u4ee5\u5b9e\u73b0iptables\u89c4\u5219\u7684\u5f00\u673a\u81ea\u52a8\u52a0\u8f7d\uff0c\u5173\u673a\u81ea\u52a8\u4fdd\u5b58\u7b49\u529f\u80fd,\u7ed3\u5408\u4e00\u4e0b\u914d\u7f6e\u5185\u5bb9\u5b9e\u73b0\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables\u914d\u7f6e\u6587\u4ef6\uff1a\/etc\/sysconfig\/iptables-config\n\nIPTABLES_MODULES=\"nf_conntrack_ftp\"      \/\/\u8bbe\u7f6eiptables\u542f\u52a8\u65f6\u9ed8\u8ba4\u52a0\u8f7d\u7684\u6a21\u5757\n\nIPTABLES_SAVE_ON_STOP=\"yes\"              \/\/\u8bbe\u7f6e\u505c\u6b62\u65f6\u81ea\u52a8\u4fdd\u5b58iptables\u89c4\u5219\n\nIPTABLES_SAVE_ON_RESTART=\"yes\"           \/\/\u8bbe\u7f6e\u91cd\u542f\u65f6\u81ea\u52a8\u4fdd\u5b58iptables\u89c4\u5219\n\nIPTABLES_RESTORE_WAIT=0                  \/\/\u8bbe\u7f6eiptables\u5f00\u542f\u540e\u9a6c\u4e0a\u6062\u590d\u539f\u6709\u89c4\u5219<\/code><\/pre>\n\n\n\n<p>        \u914d\u7f6e\u5b8c\u6210\u540e\u91cd\u542fiptables\u670d\u52a1\uff1asystemctl enable iptables     &amp;&amp;  systemctl restart iptables \u5373\u53ef\u751f\u6548\u3002<br>       \u9ed8\u8ba4iptables\u7684\u89c4\u5219\u81ea\u52a8\u4fdd\u5b58\u5728\/etc\/sysconfig\/iptables\u4e2d;\u53ef\u4ee5\u4f7f\u7528 service iptables save \u5373\u523b\u4fdd\u5b58\u89c4\u5219\u3002 <br>        iptables-save >\/path\/to\/file  \u53ef\u4ee5\u628a\u9632\u706b\u5899\u89c4\u5219\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u3002\u6062\u590d\u6307\u5b9a\u7684\u9632\u706b\u5899\u89c4\u5219\u53ef\u4ee5\u4f7f\u7528\uff1aiptables-restore &lt;\/path\/to\/file<\/p>\n\n\n\n<p><strong>iptables\u7684\u89c4\u5219\u8bbe\u7f6e<\/strong><\/p>\n\n\n\n<p>      \u5728\u8bbe\u7f6eiptables\u89c4\u5219\u524d\u9700\u8981\u4e86\u89e3\u6570\u636e\u5305\u5728\u4f20\u8f93\u8fc7\u7a0b\uff08\u5982\u4e0b\u56fe\uff09<\/p>\n\n\n\n<p>1\u3001\u8fdb\u5165\u7cfb\u7edf\u5e94\u7528\u7a0b\u5e8f\u7684\u6570\u636e\u5305\uff1a<\/p>\n\n\n\n<p>2\u3001\u8f6c\u53d1\u7684\u6570\u636e\u5305\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"558\" src=\"http:\/\/192.168.5.24\/wp-content\/uploads\/2024\/11\/image-6.png\" alt=\"\" class=\"wp-image-415\" srcset=\"https:\/\/bigvip.dpdns.org\/wp-content\/uploads\/2024\/11\/image-6.png 960w, https:\/\/bigvip.dpdns.org\/wp-content\/uploads\/2024\/11\/image-6-300x174.png 300w, https:\/\/bigvip.dpdns.org\/wp-content\/uploads\/2024\/11\/image-6-768x446.png 768w, https:\/\/bigvip.dpdns.org\/wp-content\/uploads\/2024\/11\/image-6-816x474.png 816w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p><strong>iptables\u4e2d\u5171\u67094\u5f20\u8868<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n\n\n\n<li><strong>filter<\/strong>: \u8fc7\u6ee4\u529f\u80fd\uff0c\u786e\u5b9a\u662f\u5426\u653e\u884c\u8be5\u6570\u636e\u5305\uff0c\u5c5e\u4e8e\u771f\u6b63\u9632\u706b\u5899<\/li>\n\n\n\n<li><strong>nat<\/strong>: \u7f51\u7edc\u5730\u5740\u8f6c\u6362\u529f\u80fd\uff0c\u4fee\u6539\u6570\u636e\u5305\u4e2d\u7684\u6e90\u3001\u76ee\u6807IP\u5730\u5740\u6216\u7aef\u53e3<\/li>\n\n\n\n<li><strong>mangle<\/strong>: \u5bf9\u6570\u636e\u5305\u8fdb\u884c\u91cd\u65b0\u5c01\u88c5\u529f\u80fd\uff0c\u4e3a\u6570\u636e\u5305\u8bbe\u7f6e\u6807\u8bb0<\/li>\n\n\n\n<li><strong>raw<\/strong>: \u786e\u5b9a\u662f\u5426\u5bf9\u6570\u636e\u5305\u8fdb\u884c\u8ddf\u8e2a<\/li>\n<\/ul>\n\n\n\n<p><strong>iptables\u4e2d\u5171\u67095\u6761\u94fe<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>INPUT<\/strong>:\u5904\u7406\u5165\u7ad9\u6570\u636e\u5305<\/li>\n\n\n\n<li><strong>OUTPUT<\/strong>:\u5904\u7406\u51fa\u7ad9\u6570\u636e\u5305<\/li>\n\n\n\n<li><strong>FORWARD<\/strong>:\u5904\u7406\u8f6c\u53d1\u6570\u636e\u5305(\u4e3b\u8981\u662f\u5c06\u6570\u636e\u5305\u8f6c\u53d1\u81f3\u672c\u673a\u5176\u5b83\u7f51\u5361)\u5f53\u6570\u636e\u62a5\u6587\u7ecf\u8fc7\u672c\u673a\u65f6\uff0c\u7f51\u5361\u63a5\u6536\u6570\u636e\u62a5\u6587\u81f3\u7f13\u51b2\u533a\uff0c\u5185\u6838\u8bfb\u53d6\u62a5\u6587ip\u9996\u90e8\uff0c\u53d1\u73b0\u62a5\u6587\u4e0d\u662f\u9001\u5230\u672c\u673a\u65f6\uff08\u76ee\u7684ip\u4e0d\u662f\u672c\u673a\uff09\uff0c\u7531\u5185\u6838\u76f4\u63a5\u9001\u5230forward\u94fe\u505a\u5339\u914d\uff0c\u5339\u914d\u4e4b\u540e\u82e5\u7b26\u5408forward\u7684\u89c4\u5219\uff0c\u518d\u7ecf\u7531postrouting\u9001\u5f80\u4e0b\u4e00\u8df3\u6216\u76ee\u6807\u4e3b\u673a<\/li>\n\n\n\n<li><strong>PREROUTING<\/strong>:\u5728\u8fdb\u884c\u8def\u7531\u9009\u62e9\u524d\u5904\u7406\u6570\u636e\u5305\uff0c\u4fee\u6539\u5230\u8fbe\u9632\u706b\u5899\u6570\u636e\u5305\u7684\u76ee\u7684IP\u5730\u5740\uff0c\u7528\u4e8e\u5224\u65ad\u76ee\u6807\u4e3b\u673a<\/li>\n\n\n\n<li><strong>POSTROUTING<\/strong>:\u5728\u8fdb\u884c\u8def\u7531\u9009\u62e9\u540e\u5904\u7406\u6570\u636e\u5305\uff0c\u4fee\u6539\u8981\u79bb\u5f00\u9632\u706b\u5899\u6570\u636e\u5305\u7684\u6e90IP\u5730\u5740\uff0c\u5224\u65ad\u7ecf\u7531\u54ea\u4e00\u63a5\u53e3\u9001\u5f80\u4e0b\u4e00\u8df3<\/li>\n<\/ul>\n\n\n\n<p><br><strong>iptables\u4e2d\u8868\u94fe\u4e4b\u95f4\u7684\u5173\u7cfb<\/strong>\uff0c\u6bcf\u4e2a\u8868\u91cc\u9762\u5305\u542b\u6709\u591a\u4e2a\u94fe\uff0c\u4e0d\u540c\u7684\u8868\u5305\u542b\u7684\u94fe\u662f\u4e0d\u4e00\u6837\u7684<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>filter\u8868\u53ef\u4ee5\u4f7f\u7528\u7684\u94fe\uff1aINPUT,FORWARD,OUTPUT<\/li>\n\n\n\n<li>nat\u8868\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u94fe\uff1aPREROUTING,OUTPUT ,POSTROUTING,INPUT<\/li>\n\n\n\n<li>mangle \u8868\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u94fe\uff1aPREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING<\/li>\n\n\n\n<li>raw\u8868\u4e2d\u53ef\u4ee5\u7684\u94fe\uff1aPREROUTING,OUTPUT<\/li>\n<\/ul>\n\n\n\n<p><strong>iptables\u8bed\u6cd5\u7ed3\u6784 <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables &#91;-t \u8868\u540d] \u7ba1\u7406\u9009\u9879 &#91;\u94fe\u540d] &#91;\u6761\u4ef6\u5339\u914d] &#91;-j \u76ee\u6807\u52a8\u4f5c\u6216\u8df3\u8f6c]<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\" style=\"font-style:normal;font-weight:100\"><table><tbody><tr><td><\/td><td>   <strong>table<\/strong><\/td><td><strong>command<\/strong><\/td><td>     <strong>chain<\/strong><\/td><td><strong>Parameter &amp; Xmatch  <\/strong><\/td><td>   <strong> target<\/strong><\/td><\/tr><tr><td>iptables<\/td><td>-t filter<br>   nat<br>   mangle<br>   raw<\/td><td>-A<br>-I<br>-P<br>-D<br>-R<br>-F<br>-L<br>-Z<br>-n<br>-v<\/td><td>INPUT<br>FORWARD<br>OUPUT<br>PREROUTING<br>POSTROUTING<\/td><td>-p tcp\/udp\/icmp<br>       &#8211;sport<br>       &#8211;dport<br>-s<br>-d<br>-m multiport<br>     &#8211;dports<br>-m state<\/td><td>-j ACCEPT<br>   DROP<br>   REJECT<br>   DNAT<br>   SNAT<br>MASQUERADE<br>   LOG<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u914d\u7f6e\u6837\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#SNAT\u914d\u7f6e\n&#91;root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0\/24 -o ens34\n-j SNAT --to-source 1.1.1.1\n#\u6216\u8005\n&#91;root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0\/24 -o ens34\n-j MASQUERADE\n\n#DNAT\u914d\u7f6e\n&#91;root@localhost ~]# iptables -t nat -A PREROUTING -d 1.1.1.1 -p tcp --dport 80 -\nj DNAT --to-destination 192.168.100.10\n\n#\u57fa\u4e8e\u72b6\u6001\u8fc7\u6ee4\u6570\u636e\u5305\n&#91;root@localhost ~]# iptables -I INPUT -m state --state ESTABLISHED,RELATED -j\nACCEPT\n\n#\u5141\u8bb8ping\u56de\u5e94\uff08echo-reply\uff09\n&#91;root@localhost ~]# iptables -t filter -I INPUT -p icmp -m icmp --icmp-type\necho-reply -j ACCEPT \n\n#multiport\uff1a\u591a\u7aef\u53e3\n&#91;root@localhost ~]#iptables -I INPUT -d 192.168.2.10 -p tcp -m multiport --\ndports 22,80 -j ACCEPT\n\n#-m iprange\n#iprange\u591aip\u5730\u5740(-src-range\u539f\u5730\u5740\u8303\u56f4\uff0c--dst-range\u76ee\u7684\u5730\u5740\u8303\u56f4)\ni&#91;root@localhost ~]#ptables -A INPUT -d 192.168.2.10 -p tcp --dport 23 -m\niprange --src-range 192.168.2.11-192.168.2.21 -j ACCEPT\n\n#\u6bcf\u54681-5\u76848\uff1a00-18\uff1a00\n&#91;root@localhost ~]#iptables -A INPUT -d 192.168.2.10 -p tcp --dport 80 -m time -\n-weekdays Mon,Tus,Wed,Thu,Fri --timestart 08:00:00 --time-stop 18:00:00 -j\nACCEPT\n\n#2020-1-10\u768418\u70b9\u52302022-1-10\u768418\u70b9\n&#91;root@localhost ~]#iptables -I INPUT -s 172.17.1.132 -d 192.168.1.119 -m time\n--datestart 2020-1-10T18:00:0 --datestop 2022-1-10T18:00:00 -j ACCEPT\n\n#-m comment \u5bf9\u89c4\u5219\u8fdb\u884c\u5907\u6ce8\u8bf4\u660e\uff08\u6dfb\u52a0\u6ce8\u91ca\uff09\n&#91;root@localhost ~]#iptables -A INPUT -s 192.168.2.250 -m comment --comment \"\u6ce8\u91ca\n\u5185\u5bb9\" -j REJECT<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>iptables\u662f\u7ba1\u7406linux\u9632\u706b\u5899\u6a21\u5757netfilter\u7684\u7528\u6237\u7aef\u63a5\u53e3\u7a0b\u5e8f\uff0c\u901a\u8fc7iptables\u53ef\u4ee5\u65b9\u4fbf\u7684\u7ba1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[20,7],"tags":[70,71,50],"class_list":["post-410","post","type-post","status-publish","format-aside","hentry","category-linux","category-7","tag-iptables","tag-nat","tag-50","post_format-post-format-aside"],"_links":{"self":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=410"}],"version-history":[{"count":15,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":1083,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/410\/revisions\/1083"}],"wp:attachment":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}