{"id":1085,"date":"2023-03-15T19:17:55","date_gmt":"2023-03-15T11:17:55","guid":{"rendered":"https:\/\/www.qiangzhenshuai.com\/?p=1085"},"modified":"2023-05-10T13:00:53","modified_gmt":"2023-05-10T05:00:53","slug":"iptables%e9%98%b2%e7%81%ab%e5%a2%99","status":"publish","type":"post","link":"https:\/\/www.qiangzhenshuai.com\/?p=1085","title":{"rendered":"iptables\u9632\u706b\u5899"},"content":{"rendered":"<h1>1.iptables\u6982\u8ff0<\/h1>\n<p>Linux \u7684\u5305\u8fc7\u6ee4\u529f\u80fd\uff0c\u5373 Linux \u9632\u706b\u5899\uff0c\u5b83\u7531\u00a0netfilter\u00a0\u548c iptables \u4e24\u4e2a\u7ec4\u4ef6\u7ec4\u6210\u3002netfilter \u4f4d\u4e8e\u5185\u6838\u7a7a\u95f4\uff0c\u7531\u4e00\u4e9b\u4fe1\u606f\u5305\u8fc7\u6ee4\u8868\u7ec4\u6210\uff0c\u8fd9\u4e9b\u8868\u5305\u542b\u5185\u6838\u7528\u6765\u63a7\u5236\u4fe1\u606f\u5305\u8fc7\u6ee4\u5904\u7406\u7684\u89c4\u5219\u96c6\u3002iptables \u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4f4d\u4e8e\u7528\u6237\u7a7a\u95f4\uff0c\u5b83\u4f7f\u5f97\u63d2\u5165\u3001\u4fee\u6539\u548c\u5220\u9664\u4fe1\u606f\u5305\u8fc7\u6ee4\u8868\u4e2d\u7684\u89c4\u5219\u53d8\u5f97\u5bb9\u6613\u3002<\/p>\n<p>\u6211\u4eec\u77e5\u9053\u00a0iptables\u00a0\u662f\u6309\u7167\u89c4\u5219\u6765\u529e\u4e8b\u7684\uff0c\u89c4\u5219\u5176\u5b9e\u5c31\u662f\u7f51\u7edc\u7ba1\u7406\u5458\u9884\u5b9a\u4e49\u7684\u6761\u4ef6\uff0c\u89c4\u5219\u4e00\u822c\u7684\u5b9a\u4e49\u4e3a&#8221;\u5982\u679c\u6570\u636e\u5305\u5934\u7b26\u5408\u8fd9\u6837\u7684\u6761\u4ef6\uff0c\u5c31\u8fd9\u6837\u5904\u7406\u8fd9\u4e2a\u6570\u636e\u5305&#8221;\u3002\u89c4\u5219\u5b58\u50a8\u5728\u5185\u6838\u7a7a\u95f4\u7684\u4fe1\u606f\u5305\u8fc7\u6ee4\u8868\u4e2d\uff0c\u8fd9\u4e9b\u89c4\u5219\u5206\u522b\u6307\u5b9a\u4e86\u6e90\u5730\u5740\u3001\u76ee\u7684\u5730\u5740\u3001\u4f20\u8f93\u534f\u8bae\uff08\u5982\uff1aTCP\u3001UDP\u3001ICMP\uff09\u548c\u670d\u52a1\u7c7b\u578b\uff08\u5982\uff1aHTTP\u3001FTP\u548c SMTP\uff09\u7b49\u3002\u5f53\u6570\u636e\u5305\u4e0e\u89c4\u5219\u5339\u914d\u65f6\uff0ciptables \u5c31\u6839\u636e\u89c4\u5219\u6240\u5b9a\u4e49\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u5305\uff0c\u5982\u653e\u884c\uff08ACCEPT\uff09\u3001\u62d2\u7edd\uff08REJECT\uff09\u548c\u4e22\u5f03\uff08DROP\uff09\u7b49\u3002\u914d\u7f6e\u9632\u706b\u5899\u7684\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u6dfb\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u8fd9\u4e9b\u89c4\u5219\u3002<\/p>\n<p>\u5f53\u5ba2\u6237\u7aef\u8bbf\u95ee\u670d\u52a1\u5668\u7684\u670d\u52a1\u65f6\uff0c\u5ba2\u6237\u7aef\u53d1\u9001\u62a5\u6587\u5230\u670d\u52a1\u5668\u7684\u7f51\u5361\uff0c\u800c TCP\/IP \u534f\u8bae\u6808\u662f\u5c5e\u4e8e\u5185\u6838\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5ba2\u6237\u7aef\u7684\u4fe1\u606f\u4f1a\u901a\u8fc7\u5185\u6838\u7684 TCP \u534f\u8bae\u4f20\u8f93\u5230\u7528\u6237\u7a7a\u95f4\u4e2d\u7684\u670d\u52a1\u4e2d\uff0c\u800c\u6b64\u65f6\u5ba2\u6237\u7aef\u62a5\u6587\u7684\u76ee\u6807\u7ec8\u70b9\uff08destination\uff09\u4e3a\u670d\u52a1\u6240\u76d1\u542c\u7684\u5957\u63a5\u5b57\uff08IP:PORT\uff09\uff0c\u5f53\u670d\u52a1\u9700\u8981\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\u65f6\uff0c\u670d\u52a1\u53d1\u51fa\u7684\u54cd\u5e94\u62a5\u6587\u7684\u76ee\u6807\u7ec8\u70b9\u5219\u4e3a\u5ba2\u6237\u7aef\uff0c\u8fd9\u4e2a\u65f6\u5019\u670d\u52a1\u6240\u76d1\u542c\u7684IP\u4e0e\u7aef\u53e3\u53cd\u800c\u53d8\u6210\u4e86\u539f\u70b9\uff08source\uff09\u3002netfilter \u624d\u662f\u771f\u6b63\u7684\u9632\u706b\u5899\uff0c\u5b83\u662f\u5185\u6838\u7684\u4e00\u90e8\u5206\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u9632\u706b\u5899\u80fd\u591f\u8fbe\u5230&#8221;\u9632\u706b&#8221;\u7684\u76ee\u7684\uff0c\u5219\u9700\u8981\u5728\u5185\u6838\u4e2d\u8bbe\u7f6e\u5173\u5361\uff0c\u6240\u6709\u8fdb\u51fa\u7684\u62a5\u6587\u90fd\u8981\u901a\u8fc7\u8fd9\u4e9b\u5173\u5361\uff0c\u7ecf\u8fc7\u68c0\u67e5\u540e\uff0c\u7b26\u5408\u653e\u884c\u6761\u4ef6\u7684\u624d\u80fd\u653e\u884c\uff0c\u7b26\u5408\u963b\u62e6\u6761\u4ef6\u7684\u5219\u9700\u8981\u88ab\u963b\u6b62\uff0c\u4e8e\u662f\u5c31\u51fa\u73b0\u4e86 INPUT \u5173\u5361\u548c OUTPUT \u5173\u5361\u3002\u7136\u800c\u8fd9\u4e9b\u5173\u5361\u5728 iptables \u4e2d\u5e76\u4e0d\u88ab\u79f0\u4e3a&#8221;\u5173\u5361&#8221;\uff0c\u800c\u662f\u88ab\u79f0\u4e3a&#8221;\u94fe&#8221;\u3002<\/p>\n<p><a href=\"http:\/\/netfilter.org\">\u5b98\u7f51<\/a><\/p>\n<h1>2.<span class=\"kdocs-bold\">iptables\u7ed3\u6784<\/span><\/h1>\n<p>iptables \u7531\u8868\uff08table\uff09\u3001\u94fe\uff08chain\uff09\u548c\u89c4\u5219\uff08rule\uff09\u7ec4\u6210\uff0c\u5176\u4e2d\u8868\u5305\u542b\u94fe\uff0c\u94fe\u5305\u542b\u89c4\u5219<\/p>\n<h2>\u8868<\/h2>\n<p>filter\uff1a\u8fd9\u91cc\u9762\u7684\u94fe\u6761\uff0c\u89c4\u5219\uff0c\u53ef\u4ee5\u51b3\u5b9a\u4e00\u4e2a\u6570\u636e\u5305\u662f\u5426\u53ef\u4ee5\u5230\u8fbe\u76ee\u6807\u8fdb\u7a0b\u7aef\u53e3<br \/>\n\u4e09\u4e2a\u94fe\uff1aINPUT\u3001FORWARD\u3001OUTPUT<br \/>\n\u5185\u6838\u6a21\u5757\uff1aiptables_filter<\/p>\n<p>mangle: \u8fd9\u91cc\u9762\u7684\u94fe\u6761\uff0c\u89c4\u5219\uff0c\u53ef\u4ee5\u4fee\u6539\u6570\u636e\u5305\u7684\u5185\u5bb9\uff0c\u6bd4\u5982ttl<br \/>\n\u4e94\u4e2a\u94fe\uff1aPREROUTING\u3001POSTROUTING\u3001INPUT\u3001OUTPUT\u3001FORWARD<br \/>\n\u5185\u6838\u6a21\u5757\uff1aiptable_mangle<\/p>\n<p>nat\uff1a\u8fd9\u91cc\u9762\u7684\u94fe\u6761\uff0c\u89c4\u5219\uff0c\u53ef\u4ee5\u4fee\u6539\u6e90\u548c\u76ee\u6807\u7684ip\u5730\u5740\uff0c\u7f51\u7edc\u5730\u5740\u8f6c\u6362\uff0c\u4f8b\u5982\u7aef\u53e3\u8f6c\u53d1<br \/>\n\u4e09\u4e2a\u94fe\uff1aPREROUTING\u3001POSTROUTING\u3001OUTPUT<br \/>\n\u5185\u6838\u6a21\u5757\uff1aiptable_nat<\/p>\n<p>raw\uff1a\u8fd9\u91cc\u9762\u7684\u94fe\u6761\uff0c\u89c4\u5219\uff0c\u80fd\u57fa\u4e8e\u6570\u636e\u5305\u7684\u72b6\u6001\u8fdb\u884c\u89c4\u5219\u8bbe\u5b9a<br \/>\n\u4e24\u4e2a\u94fe\uff1aOUTPUT\u3001PREROUTING<br \/>\n\u5185\u6838\u6a21\u5757\uff1aiptable_raw<\/p>\n<h2>\u94fe<\/h2>\n<p>INPUT\u94fe\uff1a\u5f53\u6536\u5230\u8bbf\u95ee\u9632\u706b\u5899\u672c\u673a\u5730\u5740\u7684\u6570\u636e\u5305(\u5165\u7ad9)\u65f6\uff0c\u5e94\u7528\u6b64\u94fe\u4e2d\u7684\u89c4\u5219\u3002<\/p>\n<p>OUTPUT\u94fe\uff1a\u5f53\u9632\u706b\u5899\u672c\u673a\u5411\u5916\u53d1\u9001\u6570\u636e\u5305(\u51fa\u7ad9)\u65f6\uff0c\u5e94\u7528\u6b64\u94fe\u4e2d\u7684\u89c4\u5219\u3002<\/p>\n<p>FORWARD\u94fe:\u5f53\u63a5\u6536\u5230\u9700\u8981\u901a\u8fc7\u9632\u706b\u5899\u4e2d\u8f6c\u53d1\u9001\u7ed9\u5176\u4ed6\u5730\u5740\u7684\u6570\u636e\u5305(\u8f6c\u53d1)\u662f\uff0c\u5e94\u7528\u6d4b\u94fe\u4e2d\u7684\u89c4\u5219\u3002<\/p>\n<p>PREROUTING\u94fe\uff1a\u5728\u5bf9\u6570\u636e\u5305\u505a\u8def\u7531\u9009\u62e9\u4e4b\u524d\uff0c\u5e94\u7528\u6d4b\u94fe\u4e2d\u7684\u89c4\u5219\uff08\u76ee\u6807\u5730\u5740\u8f6c\u6362\uff09\u3002<\/p>\n<p>POSTROUTING\u94fe\uff1a\u5728\u5bf9\u6570\u636e\u5305\u505a\u8def\u7531\u9009\u62e9\u4e4b\u540e\uff0c\u5e94\u7528\u6b64\u94fe\u4e2d\u7684\u89c4\u5219\uff08\u6e90\u5730\u5740\u8f6c\u6362\uff09\u3002<\/p>\n<h2><strong>\u89c4\u5219\u8868\u4e4b\u95f4\u7684\u4f18\u5148\u987a\u5e8f<\/strong><\/h2>\n<p>Raw\u2014\u2014mangle\u2014\u2014nat\u2014\u2014filter<\/p>\n<h4>\u5165\u7ad9\u6570\u636e\u6d41\u5411<\/h4>\n<p>\u4ece\u5916\u754c\u5230\u8fbe\u9632\u706b\u5899\u7684\u6570\u636e\u5305\uff0c\u5148\u88abPREROUTING\u89c4\u5219\u94fe\u5904\u7406\uff08\u662f\u5426\u4fee\u6539\u6570\u636e\u5305\u5730\u5740\u7b49\uff09\uff0c\u4e4b\u540e\u4f1a\u8fdb\u884c\u8def\u7531\u9009\u62e9\uff08\u5224\u65ad\u8be5\u6570\u636e\u5305\u5e94\u8be5\u53d1\u5f80\u4f55\u5904\uff09\uff0c\u5982\u679c\u6570\u636e\u5305 \u7684\u76ee\u6807\u4e3b\u673a\u662f\u9632\u706b\u5899\u672c\u673a\uff08\u6bd4\u5982\u8bf4Internet\u7528\u6237\u8bbf\u95ee\u9632\u706b\u5899\u4e3b\u673a\u4e2d\u7684web\u670d\u52a1\u5668\u7684\u6570\u636e\u5305\uff09\uff0c\u90a3\u4e48\u5185\u6838\u5c06\u5176\u4f20\u7ed9INPUT\u94fe\u8fdb\u884c\u5904\u7406\uff08\u51b3\u5b9a\u662f\u5426\u5141\u8bb8\u901a \u8fc7\u7b49\uff09\uff0c\u901a\u8fc7\u4ee5\u540e\u518d\u4ea4\u7ed9\u7cfb\u7edf\u4e0a\u5c42\u7684\u5e94\u7528\u7a0b\u5e8f\uff08\u6bd4\u5982Apache\u670d\u52a1\u5668\uff09\u8fdb\u884c\u54cd\u5e94<\/p>\n<h4>\u8f6c\u53d1\u6570\u636e\u6d41\u5411<\/h4>\n<p>\u6765\u81ea\u5916\u754c\u7684\u6570\u636e\u5305\u5230\u8fbe\u9632\u706b\u5899\u540e\uff0c\u9996\u5148\u88abPREROUTING\u89c4\u5219\u94fe\u5904\u7406\uff0c\u4e4b\u540e\u4f1a\u8fdb\u884c\u8def\u7531\u9009\u62e9\uff0c\u5982\u679c\u6570\u636e\u5305\u7684\u76ee\u6807\u5730\u5740\u662f\u5176\u5b83\u5916\u90e8\u5730\u5740\uff08\u6bd4\u5982\u5c40\u57df\u7f51\u7528\u6237\u901a\u8fc7\u7f51 \u5173\u8bbf\u95eeQQ\u7ad9\u70b9\u7684\u6570\u636e\u5305\uff09\uff0c\u5219\u5185\u6838\u5c06\u5176\u4f20\u9012\u7ed9FORWARD\u94fe\u8fdb\u884c\u5904\u7406\uff08\u662f\u5426\u8f6c\u53d1\u6216\u62e6\u622a\uff09\uff0c\u7136\u540e\u518d\u4ea4\u7ed9POSTROUTING\u89c4\u5219\u94fe\uff08\u662f\u5426\u4fee\u6539\u6570\u636e\u5305\u7684\u5730 \u5740\u7b49\uff09\u8fdb\u884c\u5904\u7406<\/p>\n<h4>\u51fa\u7ad9\u6570\u636e\u6d41\u5411<\/h4>\n<p>\u9632\u706b\u5899\u672c\u673a\u5411\u5916\u90e8\u5730\u5740\u53d1\u9001\u7684\u6570\u636e\u5305\uff08\u6bd4\u5982\u5728\u9632\u706b\u5899\u4e3b\u673a\u4e2d\u6d4b\u8bd5\u516c\u7f51DNS\u670d\u52a1\u5668\u65f6\uff09\uff0c\u9996\u5148\u88abOUTPUT\u89c4\u5219\u94fe\u5904\u7406\uff0c\u4e4b\u540e\u8fdb\u884c\u8def\u7531\u9009\u62e9\uff0c\u7136\u540e\u4f20\u9012\u7ed9POSTROUTING\u89c4\u5219\u94fe\uff08\u662f\u5426\u4fee\u6539\u6570\u636e\u5305\u7684\u5730\u5740\u7b49\uff09\u8fdb\u884c\u5904\u7406<\/p>\n<h1>3.iptables\u6267\u884c\u8fc7\u7a0b<\/h1>\n<p>\u9632\u706b\u5899\u662f\u5c42\u5c42\u8fc7\u6ee4\u7684\uff0c\u5b9e\u9645\u662f\u6309\u7167\u914d\u7f6e\u89c4\u5219\u7684\u987a\u5e8f\u4ece\u4e0a\u5230\u4e0b\uff0c\u4ece\u524d\u5230\u540e\u8fdb\u884c\u8fc7\u6ee4\u7684\u3002<\/p>\n<p>\u5982\u679c\u5339\u914d\u6210\u529f\u89c4\u5219\uff0c\u5373\u660e\u786e\u8868\u793a\u662f\u62d2\u7edd(DROP)\u8fd8\u662f\u63a5\u6536(ACCEPT)\uff0c\u6570 \u636e\u5305\u5c31\u4e0d\u518d\u5411\u4e0b\u5339\u914d\u65b0\u7684\u89c4\u5219\u3002<\/p>\n<p>\u5982\u679c\u89c4\u5219\u4e2d\u6ca1\u6709\u660e\u786e\u8868\u660e\u662f\u963b\u6b62\u8fd8\u662f\u901a\u8fc7\u7684\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709\u5339\u914d\u89c4\u5219\uff0c\u5411\u4e0b\u8fdb\u884c\u5339\u914d\uff0c\u76f4\u5230\u5339\u914d\u9ed8\u8ba4\u89c4\u5219\u5f97\u5230\u660e\u786e\u7684\u963b\u6b62\u8fd8\u662f\u901a\u8fc7\u3002<\/p>\n<p>\u9632\u706b\u5899\u7684\u9ed8\u8ba4\u89c4\u5219\u662f\u6240\u6709\u89c4\u5219\u90fd\u5339\u914d\u5b8c\u624d\u4f1a\u5339\u914d\u7684\u3002<\/p>\n<p><a href=\"https:\/\/www.qiangzhenshuai.com\/?attachment_id=1089\" rel=\"attachment wp-att-1089\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1089\" src=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u6267\u884c\u8fc7\u7a0b.png?resize=640%2C403&#038;ssl=1\" alt=\"\" width=\"640\" height=\"403\" srcset=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u6267\u884c\u8fc7\u7a0b.png?w=1453&amp;ssl=1 1453w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u6267\u884c\u8fc7\u7a0b.png?resize=300%2C189&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u6267\u884c\u8fc7\u7a0b.png?resize=1024%2C646&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u6267\u884c\u8fc7\u7a0b.png?resize=768%2C484&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u6267\u884c\u8fc7\u7a0b.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<h1>4.iptables\u547d\u4ee4\u7684\u7ba1\u7406\u63a7\u5236\u9009\u9879<\/h1>\n<p>-A\uff1a\u5728\u6307\u5b9a\u94fe\u7684\u672b\u5c3e\u6dfb\u52a0\uff08append\uff09\u4e00\u6761\u65b0\u7684\u89c4\u5219<br \/>\n-D\uff1a\u5220\u9664\uff08delete\uff09\u6307\u5b9a\u94fe\u4e2d\u7684\u67d0\u4e00\u6761\u89c4\u5219\uff0c\u53ef\u4ee5\u6309\u89c4\u5219\u5e8f\u53f7\u548c\u5185\u5bb9\u5220\u9664<br \/>\n-I\uff1a\u5728\u6307\u5b9a\u94fe\u4e2d\u63d2\u5165\uff08insert\uff09\u4e00\u6761\u65b0\u7684\u89c4\u5219\uff0c\u9ed8\u8ba4\u5728\u7b2c\u4e00\u884c\u6dfb\u52a0<br \/>\n-R\uff1a\u4fee\u6539\u3001\u66ff\u6362\uff08replace\uff09\u6307\u5b9a\u94fe\u4e2d\u7684\u67d0\u4e00\u6761\u89c4\u5219\uff0c\u53ef\u4ee5\u6309\u89c4\u5219\u5e8f\u53f7\u548c\u5185\u5bb9\u66ff\u6362<br \/>\n-L\uff1a\u5217\u51fa\uff08list\uff09\u6307\u5b9a\u94fe\u4e2d\u6240\u6709\u7684\u89c4\u5219\u8fdb\u884c\u67e5\u770b<br \/>\n-E\uff1a\u91cd\u547d\u540d\u7528\u6237\u5b9a\u4e49\u7684\u94fe\uff0c\u4e0d\u6539\u53d8\u94fe\u672c\u8eab<br \/>\n-F\uff1a\u6e05\u7a7a\uff08flush\uff09\u6307\u5b9a\u94fe\u4e2d\u7684\u6240\u6709\u89c4\u5219\uff0c\u9ed8\u8ba4\u6e05\u7a7a\u8868\u4e2d\u6240\u6709\u94fe\u7684\u5185\u5bb9<br \/>\n-N\uff1a\u65b0\u5efa\uff08new-chain\uff09\u4e00\u6761\u7528\u6237\u81ea\u5df1\u5b9a\u4e49\u7684\u89c4\u5219\u94fe<br \/>\n-X\uff1a\u5220\u9664\u6307\u5b9a\u8868\u4e2d\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u89c4\u5219\u94fe\uff08delete-chain\uff09<br \/>\n-P\uff1a\u8bbe\u7f6e\u6307\u5b9a\u94fe\u7684\u9ed8\u8ba4\u7b56\u7565\uff08policy\uff09<br \/>\n-Z\uff1a\u5c06\u6240\u6709\u8868\u7684\u6240\u6709\u94fe\u7684\u5b57\u8282\u548c\u6570\u636e\u5305\u8ba1\u6570\u5668\u6e05\u96f6<br \/>\n-n\uff1a\u4f7f\u7528\u6570\u5b57\u5f62\u5f0f\uff08numeric\uff09\u663e\u793a\u8f93\u51fa\u7ed3\u679c<br \/>\n-v\uff1a\u67e5\u770b\u89c4\u5219\u8868\u8be6\u7ec6\u4fe1\u606f\uff08verbose\uff09\u7684\u4fe1\u606f<br \/>\n-V\uff1a\u67e5\u770b\u7248\u672c(version)<br \/>\n-h\uff1a\u83b7\u53d6\u5e2e\u52a9\uff08help\uff09<br \/>\n-t\uff1a\u6307\u5b9a\u8868\uff0c\u4e0d\u6307\u5b9a\u9ed8\u8ba4\u7684filter\u8868<br \/>\n-j\uff1a\u6ee1\u8db3\u6761\u4ef6\u540e\u7684\u52a8\u4f5c:DROP\/ACCEPT\/REJECT DROP REJECT\u62d2\u7edd DROP \u628a\u6570\u636e\u4e22\u6389 \u4e0d\u4f1a\u8fd4\u56de\u4fe1\u606f\u7ed9\u7528\u6237 REJECT \u62d2\u7edd \u8fd4\u56de\u62d2\u7edd\u4fe1\u606f<\/p>\n<h2>iptables\u6761\u4ef6\u5339\u914d<\/h2>\n<p>-p,&#8211;protocol\uff1a\u5339\u914d\u6307\u5b9a\u7684\u534f\u8bae\uff08\u53ef\u4ee5\u662ftcp\u3001udp\u3001icmp\uff09<br \/>\n-s,&#8211;src,&#8211;source\uff1a\u5339\u914d\u6570\u636e\u5305\u4e2d\u7684\u6e90IP\u5730\u5740<br \/>\n-d,&#8211;dst,&#8211;dstination\uff1a\u5339\u914d\u6570\u636e\u5305\u4e2d\u7684\u76ee\u7684IP\u5730\u5740<br \/>\n-i,&#8211;in-interface\uff1a\u4ee5\u6570\u636e\u5305\u8fdb\u5165\u672c\u5730\u6240\u4f7f\u7528\u7684\u7f51\u7edc\u63a5\u53e3\u8fdb\u884c\u5339\u914d<br \/>\n-o,&#8211;out-interface\uff1a\u4ee5\u6570\u636e\u5305\u79bb\u5f00\u672c\u5730\u6240\u4f7f\u7528\u7684\u7f51\u7edc\u63a5\u53e3\u8fdb\u884c\u5339\u914d<br \/>\n-f,&#8211;fragment\uff1a \u5339\u914d\u4e00\u4e2a\u88ab\u5206\u7247\u7684\u5305\u7684\u7b2c\u4e8c\u7247\u6216\u53ca\u4ee5\u540e\u7684\u90e8\u5206<br \/>\n&#8211;sport,&#8211;source-port\uff1a\u5339\u914d\u57fa\u4e8eTCP\u534f\u8bae\u6570\u636e\u5305\u7684\u6e90\u7aef\u53e3<br \/>\n&#8211;dport,&#8211;destination-port\uff1a\u5339\u914d\u57fa\u4e8eTCP\u534f\u8bae\u6570\u636e\u5305\u7684\u76ee\u7684\u7aef\u53e3<br \/>\n&#8211;tcp-flags\uff1a\u5339\u914d\u6307\u5b9a\u7684TCP\u6807\u8bb0<br \/>\n&#8211;syn\uff1a\u5339\u914d\u90a3\u4e9bSYN\u6807\u8bb0\u88ab\u8bbe\u7f6e\u800cACK\u548cRST\u6807\u8bb0\u6ca1\u6709\u8bbe\u7f6e\u7684\u6570\u636e\u5305<br \/>\n&#8211;tcp-option\uff1a\u5339\u914dTCP\u534f\u8bae\u6570\u636e\u5305\u7684\u5934\u90e8\u4fe1\u606f<br \/>\n&#8211;icmp-type\uff1a\u6839\u636eICMP\u7c7b\u578b\u5339\u914d\u6570\u636e\u5305\uff0c \u9488\u5bf9-p icmp<br \/>\n&#8211;linit\uff1a\u4e3alinit match\u8bbe\u7f6e\u6700\u5927\u5e73\u5747\u5339\u914d\u901f\u7387\uff0c\u4e5f\u5c31\u662f\u5355\u4f4d\u65f6\u95f4\u5185linit match\u53ef\u4ee5\u5339\u914d\u51e0\u4e2a\u5305\uff0c\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m limit\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;limit-burst\uff1a\u5f85\u5339\u914d\u5305\u521d\u59cb\u4e2a\u6570\u7684\u6700\u5927\u503c:\u82e5\u524d\u9762\u6307\u5b9a\u7684\u6781\u9650\u8fd8\u6ca1\u8fbe\u5230\u8fd9\u4e2a\u6570\u503c,\u5219\u6982\u6570\u5b57\u52a01.\u9ed8\u8ba4\u503c\u4e3a5<br \/>\n&#8211;mac-source\uff1a\u57fa\u4e8e\u6570\u636e\u5305\u7684MAC\u6e90\u5730\u5740\u8fdb\u884c\u5339\u914d\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m mac\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;mark\uff1a\u4ee5\u6570\u636e\u5305\u88ab\u8bbe\u7f6e\u7684mark\u503c\u6765\u5339\u914d\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m mark\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;uid-owner\uff1a\u6309\u751f\u6210\u6570\u636e\u5305\u7684\u7528\u6237\u7684ID\uff08UID\uff09\u6765\u5339\u914d\u5916\u51fa\u7684\u5305\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m owner\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;gid-owner\uff1a\u6309\u751f\u6210\u6570\u636e\u5305\u7684\u7528\u6237\u6240\u5728\u7ec4\u7684ID\uff08GID\uff09\u6765\u5339\u914d\u5916\u51fa\u7684\u5305\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m owner\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;pid-owner\uff1a\u6309\u751f\u6210\u6570\u636e\u5305\u7684\u8fdb\u7a0b\u7684ID\uff08UID\uff09\u6765\u5339\u914d\u5916\u51fa\u7684\u5305\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m owner\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;sid-owner\uff1a\u6309\u751f\u6210\u6570\u636e\u5305\u7684\u4f1a\u8bdd\u7684ID\uff08SID\uff09\u6765\u5339\u914d\u5916\u51fa\u7684\u5305\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m owner\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;state\uff1a\u6307\u5b9a\u8981\u5339\u914d\u6570\u636e\u5305\u7684\u72b6\u6001\uff0c\u4e00\u5171\u67094\u79cd\u72b6\u6001\u53ef\u4ee5\u4f7f\u7528\uff1aINVALID\u3001ESTABLISHED\u3001NEW\u548cRELATED\u3002\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m state\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;tos\uff1a\u6839\u636eTOS\u5b57\u6bb5\u5339\u914d\u6570\u636e\u5305\uff0c\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m tos\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;ttl\uff1a\u6839\u636eTTL\u7684\u503c\u6765\u5339\u914d\u6570\u636e\u5305\uff0c\u8fd9\u4e2a\u5339\u914d\u64cd\u4f5c\u5fc5\u987b\u7531-m ttl\u660e\u786e\u6307\u5b9a\u624d\u80fd\u4f7f\u7528<br \/>\n&#8211;line-numbers\uff1a\u5f53\u5217\u8868\u663e\u793a\u89c4\u5219\u65f6\uff0c\u5728\u6bcf\u4e2a\u89c4\u5219\u7684\u524d\u9762\u52a0\u4e0a\u884c\u53f7\uff0c\u4e0e\u8be5\u89c4\u5219\u5728\u94fe\u4e2d\u7684\u4f4d\u7f6e\u76f8\u5bf9\u5e94<\/p>\n<h2>iptables\u52a8\u4f5c\/\u76ee\u6807<\/h2>\n<p>ACCEPT\uff1a\u5141\u8bb8\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u5305\u901a\u8fc7<br \/>\nDROP\uff1a\u62d2\u7edd\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u5305\u901a\u8fc7<br \/>\nLOG\uff1a\u7528\u6765\u8bb0\u5f55\u4e0e\u6570\u636e\u5305\u76f8\u5173\u7684\u4fe1\u606f<br \/>\nMARK\uff1a\u8bbe\u7f6emark\u503c\uff0c\u8fd9\u4e2a\u503c\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u7684\u6574\u6570<br \/>\nMASQUERADE\uff1a\u548cSNAT\u7684\u4f5c\u7528\u76f8\u540c\uff0c\u533a\u522b\u5728\u4e8e\u5b83\u4e0d\u9700\u8981\u6307\u5b9a&#8211;to-source<br \/>\nSNAT\uff1a\u6e90\u7f51\u7edc\u5730\u5740\u8f6c\u6362<br \/>\nDNAT\uff1a\u76ee\u7684\u7f51\u7edc\u5730\u5740\u8f6c\u6362<br \/>\nREDIRECT\uff1a\u8f6c\u53d1\u6570\u636e\u5305\u4e00\u53e6\u4e00\u4e2a\u7aef\u53e3<br \/>\nREJECT\uff1aREJECT\u548cDROP\u90fd\u4f1a\u5c06\u6570\u636e\u5305\u4e22\u5f03\uff0c\u533a\u522b\u5728\u4e8eREJECT\u9664\u4e86\u4e22\u5f03\u6570\u636e\u5305\u5916\uff0c\u8fd8\u5411\u53d1\u9001\u8005\u8fd4\u56de\u9519\u8bef\u4fe1\u606f<br \/>\nRETURN\uff1a\u4f7f\u6570\u636e\u5305\u8fd4\u56de\u4e0a\u4e00\u5c42<br \/>\nTOS\uff1a\u7528\u6765\u8bbe\u7f6eIP\u5934\u90e8\u4e2d\u7684Type Of Service\u5b57\u6bb5<br \/>\nTTL\uff1a\u7528\u4e8e\u4fee\u6539IP\u5934\u90e8\u4e2dTime To Live\u5b57\u6bb5\u7684\u503c<br \/>\nULOG\uff1aULOG\u53ef\u4ee5\u5728\u7528\u6237\u7a7a\u95f4\u8bb0\u5f55\u88ab\u5339\u914d\u7684\u5305\u7684\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4fe1\u606f\u548c\u6574\u4e2a\u5305\u90fd\u4f1a\u901a\u8fc7netlink socket\u88ab\u591a\u64ad<br \/>\nQUEUE\uff1a\u4e3a\u7528\u6237\u7a7a\u95f4\u7684\u7a0b\u5e8f\u6216\u5e94\u7528\u8f6f\u4ef6\u7ba1\u7406\u5305\u961f\u5217<br \/>\nMIRROR\uff1a\u98a0\u5012IP\u5934\u90e8\u4e2d\u7684\u6e90\u76ee\u5730\u5740\uff0c\u7136\u540e\u518d\u8f6c\u53d1\u5305<\/p>\n<h1>5.\u73af\u5883\u51c6\u5907<\/h1>\n<h4>\u51c6\u5907\u673a\u5668<\/h4>\n<p>[root@web01 ~]$ hostname -I<br \/>\n10.0.0.45 172.16.1.45<\/p>\n<p>[root@web02 ~]$ hostname -I<br \/>\n10.0.0.145 172.16.1.145<\/p>\n<p>[root@lb01 ~]$ hostname -I<br \/>\n10.0.0.163 10.0.0.3 172.16.1.163<\/p>\n<h4>\u5b89\u88c5<\/h4>\n<p>[root@lb01 ~]$ yum install -y iptables-services<\/p>\n<h4>\u76ee\u5f55\u8be6\u60c5<\/h4>\n<p>[root@lb01 ~]$ rpm -ql iptables-services<br \/>\n\/etc\/sysconfig\/iptables\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#\u9632\u706b\u5899\u7684\u914d\u7f6e\u4ef6<br \/>\n\/usr\/lib\/systemd\/system\/iptables.service\u00a0 \u00a0 \u00a0#\u9632\u706b\u5899\u670d\u52a1\u914d\u7f6e\u6587\u4ef6(\u547d\u4ee4)<\/p>\n<p>[root@lb01 ~]$ rpm -ql iptables<\/p>\n<p>\/usr\/sbin\/iptables\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #iptables \u547d\u4ee4 \u6dfb\u52a0\/\u5220\u9664\/\u67e5\u770b \u89c4\u5219(4\u8868\u4f0d\u94fe)<br \/>\n\/usr\/sbin\/iptables-save\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #iptables\u89c4\u5219 \u8f93\u51fa(\u4fdd\u5b58)<br \/>\n\/usr\/sbin\/iptables-restore\u00a0 \u00a0 \u00a0 #\u6062\u590d<\/p>\n<h4>\u9632\u706b\u5899\u76f8\u5173\u6a21\u5757 \u52a0\u8f7d\u5230\u5185\u6838\u4e2d<br \/>\n\u5199\u5165\u5230\u5f00\u673a\u81ea\u542f\u52a8<\/h4>\n<p>[root@lb01 ~]$ cat \/etc\/rc.local<\/p>\n<p>modprobe ip_tables<br \/>\nmodprobe iptable_filter<br \/>\nmodprobe iptable_nat<br \/>\nmodprobe iptable_raw<br \/>\nmodprobe iptable_mangle<br \/>\nmodprobe ip_conntrack<br \/>\nmodprobe ip_conntrack_ftp<br \/>\nmodprobe ip_nat_ftp<br \/>\nmodprobe ipt_state<\/p>\n<h4>\u542f\u52a8<\/h4>\n<p>[root@lb01 ~]$ systemctl start iptables.service<br \/>\n[root@lb01 ~]$ systemctl enable iptables.service<br \/>\nCreated symlink from \/etc\/systemd\/system\/basic.target.wants\/iptables.service to \/usr\/lib\/systemd\/system\/iptables.service.<\/p>\n<h4>\u68c0\u67e5\u6a21\u5757\u662f\u5426\u5f00\u542f<\/h4>\n<p>[root@lb01 ~]$ lsmod | egrep &#8216;nat|filter&#8217;<br \/>\nnf_nat_ftp 12809 0<br \/>\nnf_conntrack_ftp 18478 1 nf_nat_ftp<br \/>\niptable_nat 12875 0<br \/>\nnf_nat_ipv4 14115 1 iptable_nat<br \/>\nnf_nat 26583 2 nf_nat_ftp,nf_nat_ipv4<br \/>\nnf_conntrack 139264 8 nf_nat_ftp,ip_vs,nf_nat,xt_state,nf_nat_ipv4,xt_conntrack,nf_conntrack_ftp,nf_conntrack_ipv4<br \/>\niptable_filter 12810 1<br \/>\nip_tables 27126 4 iptable_filter,iptable_mangle,iptable_nat,iptable_raw<br \/>\nlibcrc32c 12644 4 xfs,ip_vs,nf_nat,nf_conntrack<\/p>\n<h4>\u67e5\u770b\u89c4\u5219\uff0c\u9ed8\u8ba4\u67e5\u770b\u7684\u662ffilter\u8868<\/h4>\n<p>[root@lb01 ~]$ iptables -nL<br \/>\nChain INPUT (policy ACCEPT)<br \/>\ntarget prot opt source destination<br \/>\nACCEPT all &#8212; 0.0.0.0\/0 0.0.0.0\/0 state RELATED,ESTABLISHED<br \/>\nACCEPT icmp &#8212; 0.0.0.0\/0 0.0.0.0\/0<br \/>\nACCEPT all &#8212; 0.0.0.0\/0 0.0.0.0\/0<br \/>\nACCEPT tcp &#8212; 0.0.0.0\/0 0.0.0.0\/0 state NEW tcp dpt:22<br \/>\nREJECT all &#8212; 0.0.0.0\/0 0.0.0.0\/0 reject-with icmp-host-prohibited<\/p>\n<p>Chain FORWARD (policy ACCEPT)<br \/>\ntarget prot opt source destination<br \/>\nREJECT all &#8212; 0.0.0.0\/0 0.0.0.0\/0 reject-with icmp-host-prohibited<\/p>\n<p>Chain OUTPUT (policy ACCEPT)<br \/>\ntarget prot opt source destination<\/p>\n<p>Chain L (0 references)<br \/>\ntarget prot opt source destination<\/p>\n<p><a href=\"https:\/\/www.qiangzhenshuai.com\/?attachment_id=1097\" rel=\"attachment wp-att-1097\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1097\" src=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u9ed8\u8ba4\u89c4\u5219.png?resize=640%2C174&#038;ssl=1\" alt=\"\" width=\"640\" height=\"174\" srcset=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u9ed8\u8ba4\u89c4\u5219.png?w=1397&amp;ssl=1 1397w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u9ed8\u8ba4\u89c4\u5219.png?resize=300%2C82&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u9ed8\u8ba4\u89c4\u5219.png?resize=1024%2C279&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u9ed8\u8ba4\u89c4\u5219.png?resize=768%2C209&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u9ed8\u8ba4\u89c4\u5219.png?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<h4>\u67e5\u770b\u6307\u5b9a\u8868\u4e2d\u7684\u89c4\u5219<\/h4>\n<p>[root@lb01 ~]$ iptables -nL -t nat<br \/>\nChain PREROUTING (policy ACCEPT)<br \/>\ntarget prot opt source destination<\/p>\n<p>Chain INPUT (policy ACCEPT)<br \/>\ntarget prot opt source destination<\/p>\n<p>Chain OUTPUT (policy ACCEPT)<br \/>\ntarget prot opt source destination<\/p>\n<p>Chain POSTROUTING (policy ACCEPT)<br \/>\ntarget prot opt source destination<\/p>\n<h1>6.\u6848\u4f8b<\/h1>\n<h2>\u6848\u4f8b1<\/h2>\n<h3>\u7981\u6b62\u8bbf\u95ee22\u7aef\u53e3<br \/>\n\uff08\u8fd9\u4e2a\u65f6\u5019\u50cfXshell\u5c31\u8fde\u63a5\u4e0d\u4e0a\uff09<\/h3>\n<p>[root@lb01 ~]$ iptables -t filter -A INPUT -p tcp &#8211;dport 22 -j DROP<\/p>\n<h4>\u67e5\u770b\u89c4\u5219\u5e76\u52a0\u4e0a\u5e8f\u53f7<\/h4>\n<p>iptables -t filter \u00a0-nL \u00a0 &#8211;line-number<\/p>\n<p><a href=\"https:\/\/www.qiangzhenshuai.com\/?attachment_id=1098\" rel=\"attachment wp-att-1098\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1098\" src=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u67e5\u770b\u89c4\u5219\u5e76\u52a0\u4e0a\u5e8f\u53f7.png?resize=640%2C147&#038;ssl=1\" alt=\"\" width=\"640\" height=\"147\" srcset=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u67e5\u770b\u89c4\u5219\u5e76\u52a0\u4e0a\u5e8f\u53f7.png?w=711&amp;ssl=1 711w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u67e5\u770b\u89c4\u5219\u5e76\u52a0\u4e0a\u5e8f\u53f7.png?resize=300%2C69&amp;ssl=1 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<h4>\u5220\u9664\u89c4\u5219<br \/>\n\uff08\u6839\u636e\u5e8f\u53f7\u5220\u9664\uff09<\/h4>\n<p>iptables -t filter \u00a0-D INPUT \u00a01<\/p>\n<p><a href=\"https:\/\/www.qiangzhenshuai.com\/?attachment_id=1099\" rel=\"attachment wp-att-1099\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1099\" src=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u5220\u9664\u89c4\u5219.png?resize=426%2C28&#038;ssl=1\" alt=\"\" width=\"426\" height=\"28\" srcset=\"https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u5220\u9664\u89c4\u5219.png?w=426&amp;ssl=1 426w, https:\/\/i0.wp.com\/www.qiangzhenshuai.com\/wp-content\/uploads\/2023\/03\/iptables\u5220\u9664\u89c4\u5219.png?resize=300%2C20&amp;ssl=1 300w\" sizes=\"(max-width: 426px) 100vw, 426px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<h2>\u6848\u4f8b2<\/h2>\n<h3>\u5c01ip\u5c4f\u853d\u67d0\u4e2aip<\/h3>\n<p>[root@lb01 ~]$ iptables -I INPUT -s 10.0.0.45 -j DROP<\/p>\n<h4>\u67e5\u770b<\/h4>\n<p>[root@lb01 ~]$ iptables -nL<br \/>\nChain INPUT (policy ACCEPT)<br \/>\ntarget prot opt source destination<br \/>\nDROP all &#8212; 10.0.0.45 0.0.0.0\/0<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>[root@web01 ~]$ ping 10.0.0.163<\/p>\n<h2>\u6848\u4f8b3<\/h2>\n<h3>\u7981\u6b62\u7f51\u6bb5\u8fde\u5165<\/h3>\n<p>[root@lb01 ~]$ iptables -I INPUT -s 10.0.0.0\/24 -j DROP<\/p>\n<h2>\u6848\u4f8b4<\/h2>\n<h3>\u5c4f\u853dip\u52a0\u7aef\u53e3<\/h3>\n<p>[root@lb01 ~]$ iptables -I INPUT -s 10.0.0.45 -p tcp &#8211;dport 22 -j DROP<\/p>\n<h2>\u6848\u4f8b5<\/h2>\n<h3>\u53ea\u5141\u8bb8\u6307\u5b9a\u7f51\u6bb5\u8fde\u5165<\/h3>\n<h4>\u65b9\u6cd51(! \u53d6\u53cd)<\/h4>\n<p>[root@lb01 ~]$ iptables -I INPUT ! -s 10.0.0.0\/24 -j DROP<\/p>\n<h4>\u65b9\u6cd51(\u4fee\u6539\u94fe\u9ed8\u8ba4\u89c4\u5219 \u4fee\u6539\u4e3a\u62d2\u7edd \u6dfb\u52a0\u51c6\u8bb8)<\/h4>\n<p>[root@lb01 ~]$ iptables -I INPUT -s 10.0.0.0\/24 -j ACCEPT<\/p>\n<h4>\u4fee\u6539\u9ed8\u8ba4\u89c4\u5219<\/h4>\n<p>[root@lb01 ~]$ iptables -P INPUT DROP<\/p>\n<h4>\u6d4b\u8bd5\u5b8c\u6210\u540e,\u4fee\u6539\u56de\u53bb<\/h4>\n<p>[root@lb01 ~]$ iptables \u00a0-P INPUT ACCEPT<\/p>\n<h2>\u6848\u4f8b6<\/h2>\n<h3>\u6307\u5b9a\u591a\u4e2a\u7aef\u53e3<\/h3>\n<h4>\u6dfb\u52a0\u6a21\u5757<\/h4>\n<h4>-m\uff1amultiport \u6307\u5b9a\u591a\u7aef\u53e3\u53f7<\/h4>\n<h4>\u6dfb\u52a0\u7aef\u53e3<\/h4>\n<p>[root@lb01 ~]$ iptables -A INPUT -m multiport -p tcp &#8211;dport 80,443 -j ACCEPT<br \/>\n[root@lb01 ~]$ iptables -nL<br \/>\nChain INPUT (policy ACCEPT)<br \/>\ntarget prot opt source destination<br \/>\nACCEPT tcp &#8212; 0.0.0.0\/0 0.0.0.0\/0 multiport dports 80,443<\/p>\n<h4>\u5982\u679c\u662f\u8fde\u7eed\u7684\u7aef\u53e3\u53ef\u4ee5\u4e0d\u52a0\u4e0a-m multiport<\/h4>\n<p>[root@lb01 ~]$ iptables -A INPUT -p tcp &#8211;dport 1:1024 -j ACCEPT<\/p>\n<h2>\u6848\u4f8b7<\/h2>\n<h3>\u5339\u914dICMP\u7c7b\u578b<\/h3>\n<p>ICMP\uff08Internet Control Message Protocol\uff09Internet\u63a7\u5236\u62a5\u6587\u534f\u8baeping<\/p>\n<h4>\u901a\u8fc7\u9632\u706b\u5899\u89c4\u5219 \u63a7\u5236\u662f\u5426\u53ef\u4ee5ping<\/h4>\n<p class=\"hljs nginx\"><span class=\"hljs-comment\">[root@lb01 ~]$ iptables -A INPUT -p icmp &#8211;icmp-type 8 -j DROP<\/span><\/p>\n<p>&#8220;Echo-Request&#8221;\uff08\u4ee3\u7801\u4e3a 8\uff09\u8868\u793a \u8bf7\u6c42<br \/>\n&#8220;Echo-Reply&#8221;\uff08\u4ee3\u7801\u4e3a 0\uff09\u8868\u793a \u56de\u663e<\/p>\n<h4>\u901a\u8fc7\u5185\u6838\u53c2\u6570\u63a7\u5236\u7981\u6b62\u88abping<\/h4>\n<p>[root@lb01 ~]$ vim \/etc\/sysctl.conf<\/p>\n<h4>sysctl\u7528\u6765\u8bbe\u7f6e\u7cfb\u7edf\u7684\u6838\u5fc3\u53c2\u6570 -p\u7acb\u5373\u751f\u6548<\/h4>\n<p>[root@lb01 ~]$ sysctl -p<br \/>\nnet.ipv4.icmp_echo_ignore_all = 1<\/p>\n<h2>\u6848\u4f8b8<\/h2>\n<h3>\u5339\u914d\u7f51\u7edc\u72b6\u6001\uff08TCP\/IP\u8fde\u63a5\u72b6\u6001\uff09<\/h3>\n<p>NEW\uff1a\u5df2\u7ecf\u6216\u5c06\u542f\u52a8\u65b0\u7684\u8fde\u63a5<br \/>\nESTABLISHED\uff1a\u5df2\u5efa\u7acb\u7684\u8fde\u63a5<br \/>\nRELATED\uff1a\u6b63\u5728\u542f\u52a8\u7684\u65b0\u8fde\u63a5<br \/>\nINVALID\uff1a\u975e\u6cd5\u6216\u65e0\u6cd5\u8bc6\u522b\u7684<\/p>\n<p>[root@lb01 ~]$ iptables -A INPUT -m state &#8211;state ESTABLISHED,RELATED -j ACCEPT<\/p>\n<h2>\u6848\u4f8b9<\/h2>\n<h3>\u9650\u5236\u5e76\u53d1\u53ca\u901f\u7387<\/h3>\n<p>[root@lb01 ~]$ iptables -I INPUT -p icmp -m limit &#8211;limit 10\/minute &#8211;limit-burst 5 -j ACCEPT<\/p>\n<p>&#8211;limit 10\/minute\uff1a\u6bcf\u5206\u949f\u53ea\u80fd\u670910\u4e2a\u6570\u636e\u5305 \u6bcf6\u79d2\u751f\u6210<\/p>\n<p>&#8211;limit-burst n\uff1a\u6307\u5b9a\u65f6\u95f4\u5185\u7684\u8bf7\u6c42\u901f\u7387\u201dn\u201d\u4e3a\u901f\u7387\uff0c\u540e\u9762\u4e3a\u65f6\u95f4\u5206\u522b\u4e3a\uff1a\u79d2 \u5206 \u65f6\uff0c<\/p>\n<p>\u4e0d\u6307\u5b9a\u9ed8\u8ba4\u4e3a5<\/p>\n<h1>7.\u9632\u706b\u5899\u89c4\u5219\u7684\u4fdd\u5b58\u4e0e\u6062\u590d<\/h1>\n<h4>\u9ed8\u8ba4\u8f93\u51fa\u5230\u5c4f\u5e55<\/h4>\n<p>[root@lb01 ~]$ iptables-save<\/p>\n<h4>\u8ffd\u52a0\u4e0a\u6587\u4ef6<\/h4>\n<p>[root@lb01 ~]$ iptables-save &gt; \/etc\/sysconfig\/iptables<br \/>\n[root@lb01 ~]$ cat \/etc\/sysconfig\/iptables<\/p>\n<h4>\u5220\u9664\u89c4\u5219<\/h4>\n<p>[root@lb01 ~]$ iptables -F<\/p>\n<h4>\u91cd\u542fiptables\uff0c\u67e5\u770b\u89c4\u5219\u662f\u5426\u6062\u590d<\/h4>\n<p>[root@lb01 ~]$ systemctl restart iptables<br \/>\n\u6216<br \/>\n[root@lb01 ~]$ iptables-restore &lt;\/etc\/sysconfig\/iptables<\/p>\n<p>[root@lb01 ~]$ iptables -nL<\/p>\n<h1>8.nat\u8868\u89c4\u5219<\/h1>\n<h2>\u5171\u4eab\u4e0a\u7f51<\/h2>\n<p>\u662f\u4e00\u79cd\u7f51\u7edc\u5730\u5740\u8f6c\u6362\u6280\u672f\uff0c\u53ef\u4ee5\u5c06\u79c1\u6709IP\u5730\u5740\u8f6c\u6362\u4e3a\u516c\u7f51IP\u5730\u5740\uff0c\u8ba9\u591a\u53f0\u8bbe\u5907\u5171\u4eab\u4e00\u4e2a\u516c\u7f51IP\u5730\u5740\u4e0a\u7f51\u3002<\/p>\n<p>\u5728\u5b9e\u73b0NAT\u5171\u4eab\u4e0a\u7f51\u65f6\uff0c\u9700\u8981\u4e00\u4e2aNAT\u8def\u7531\u5668\uff0c\u5c06\u5176\u4f5c\u4e3a\u7f51\u7edc\u7684\u51fa\u53e3\u8bbe\u5907\u3002NAT\u8def\u7531\u5668\u4f1a\u4e3a\u5185\u90e8\u8bbe\u5907\u5206\u914d\u79c1\u6709IP\u5730\u5740\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u516c\u7f51IP\u5730\u5740\uff0c\u4ece\u800c\u5b9e\u73b0\u591a\u53f0\u8bbe\u5907\u5171\u4eab\u4e00\u4e2a\u516c\u7f51IP\u5730\u5740\u4e0a\u7f51\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>NAT\u8def\u7531\u5668\u5c06\u5185\u90e8\u8bbe\u5907\u7684IP\u5730\u5740\u8f6c\u6362\u4e3a\u516c\u7f51IP\u5730\u5740\u3002<br \/>\n\u5185\u90e8\u8bbe\u5907\u5c06\u6570\u636e\u53d1\u9001\u5230NAT\u8def\u7531\u5668\uff0cNAT\u8def\u7531\u5668\u5c06\u5176\u8f6c\u53d1\u5230\u516c\u7f51\u4e0a\u3002<br \/>\n\u516c\u7f51\u670d\u52a1\u5668\u63a5\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u5c06\u54cd\u5e94\u6570\u636e\u53d1\u9001\u5230NAT\u8def\u7531\u5668\u3002<br \/>\nNAT\u8def\u7531\u5668\u5c06\u54cd\u5e94\u6570\u636e\u8f6c\u53d1\u7ed9\u5185\u90e8\u8bbe\u5907\u3002<\/p>\n<p>\u901a\u8fc7NAT\u5171\u4eab\u4e0a\u7f51\uff0c\u53ef\u4ee5\u6709\u6548\u5229\u7528\u516c\u7f51IP\u5730\u5740\u8d44\u6e90\uff0c\u964d\u4f4e\u7f51\u7edc\u6210\u672c\uff0c\u63d0\u9ad8\u7f51\u7edc\u5b89\u5168\u6027\u3002\u4f46\u662f\uff0cNAT\u5171\u4eab\u4e0a\u7f51\u4e5f\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u6bd4\u5982\u53ef\u80fd\u4f1a\u5f71\u54cd\u7f51\u7edc\u6027\u80fd\uff0c\u9650\u5236\u4e86\u5bf9\u5185\u90e8\u8bbe\u5907\u7684\u8bbf\u95ee\u7b49\u3002<\/p>\n<h3>\u73af\u5883\u51c6\u5907<\/h3>\n<h4>web01<\/h4>\n<p>web01 172.16.1.45 (\u53ea\u4fdd\u7559\u8fd9\u4e2a\u7f51\u5361), eth0\u7f51\u5361\u5173\u95ed(ONBOOT=no)<\/p>\n<p>[root@web01 ~]$ vim \/etc\/sysconfig\/network-scripts\/ifcfg-eth0<\/p>\n<p>[root@web01 ~]$ systemctl restart network<br \/>\n[root@web01 ~]$ ip a<\/p>\n<h4>lb01<\/h4>\n<p>\u6765\u81ea\u4e8e172.16.1.0\/24\u7f51\u6bb5\u7684ip,\u901a\u8fc7\u9632\u706b\u5899\u7684nat\u8868\u7684POSTROUTING\u94fe<br \/>\n\u6e90ip\u88ab\u4fee\u6539\u4e3a 10.0.0.163 \u901a\u8fc7SNAT\u65b9\u5f0f<\/p>\n<p>[root@lb01 ~]$ iptables -t nat -I POSTROUTING -s 172.16.1.0\/24 -j SNAT &#8211;to-source 10.0.0.163<\/p>\n<h4>web01<\/h4>\n<p>[root@web01 ~]$ tail -1 \/etc\/sysconfig\/network-scripts\/ifcfg-eth1<br \/>\nGATEWAY=172.16.1.163<\/p>\n<h4>lb01\u6dfb\u52a0\u5185\u6838<\/h4>\n<p>[root@lb01 ~]$ tail -1 \/etc\/sysctl.conf<br \/>\nnet.ipv4.ip_forward =1<\/p>\n<p>[root@lb01 ~]$ sysctl -p<br \/>\nnet.ipv4.ip_forward = 1<\/p>\n<h4>\u67e5\u770bnat\u8868<\/h4>\n<p>[root@lb01 ~]$ iptables -nL -t nat<\/p>\n<h4>\u5220\u9664nat\u8868<\/h4>\n<p>[root@lb01 ~]$ iptables -t nat -D PREROUTING 1<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>[root@web01 ~]$ ping 172.16.1.163<\/p>\n<p>[root@web01 ~]$ ping baidu.com<\/p>\n<h2>\u5b9e\u73b0\u7aef\u53e3\u8f6c\u53d1<\/h2>\n<h4>lb01<\/h4>\n<p>[root@lb01 ~]$ iptables -t nat -I PREROUTING -p tcp &#8211;dport 9000 -j DNAT &#8211;to-destination 172.16.1.45:22<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>[c:\\~]$ ssh 10.0.0.163 9000<\/p>\n<h2>\u5b9e\u73b0IP\u8f6c\u53d1<\/h2>\n<h4>lb01<\/h4>\n<p>[root@lb01 ~]$ ip addr add 10.0.0.9\/24 dev eth0 label eth0:0<\/p>\n<p>[root@lb01 ~]$ iptables -t nat -I PREROUTING -d 10.0.0.9 -j DNAT &#8211;to-destination 172.16.1.45<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>[c:\\~]$ ssh 10.0.0.9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.iptables\u6982\u8ff0 Linux \u7684\u5305\u8fc7\u6ee4\u529f\u80fd\uff0c\u5373 Linux \u9632\u706b\u5899\uff0c\u5b83\u7531\u00a0netfilter\u00a0\u548c ip [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[12],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1085"}],"collection":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1085"}],"version-history":[{"count":13,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions"}],"predecessor-version":[{"id":1108,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions\/1108"}],"wp:attachment":[{"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qiangzhenshuai.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}