{"id":16696,"date":"2024-11-18T13:50:07","date_gmt":"2024-11-18T05:50:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16696\/"},"modified":"2024-11-18T13:50:07","modified_gmt":"2024-11-18T05:50:07","slug":"%e9%ab%98%e5%b9%b6%e5%8f%91%e6%83%85%e5%86%b5%e4%b8%8b-linux-%e7%b3%bb%e7%bb%9f%e5%8f%8a-kernel-%e5%8f%82%e6%95%b0%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16696\/","title":{"rendered":"\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b Linux \u7cfb\u7edf\u53ca kernel \u53c2\u6570\u4f18\u5316"},"content":{"rendered":"<p> <br \/>\n<span style=\"font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\u4f17\u6240\u5468\u77e5\u5728\u9ed8\u8ba4\u53c2\u6570\u60c5\u51b5\u4e0bLinux\u5bf9\u9ad8\u5e76\u53d1\u652f\u6301\u5e76\u4e0d\u597d\uff0c\u4e3b\u8981\u53d7\u9650\u4e8e\u5355\u8fdb\u7a0b\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u3001\u5185\u6838TCP\u53c2\u6570\u65b9\u9762\u548cIO\u4e8b\u4ef6\u5206\u914d\u673a\u5236\u7b49\u3002<\/span><br \/>\n<span style=\"font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light,\">\u4e0b\u9762\u5c31\u4ece\u51e0\u65b9\u9762\u6765\u8c03\u6574\u4f7fLinux\u7cfb\u7edf\u80fd\u591f\u652f\u6301\u9ad8\u5e76\u53d1\u73af\u5883\u3002<\/span><br \/>\n<span style=\"font-size: 15px;letter-spacing: 1px\"><\/span>   <\/p>\n<h3><span style=\"padding-left: 20px;font-size: 1em\">Iptables\u76f8\u5173<\/span><\/h3>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5982\u975e\u5fc5\u987b\uff0c\u5173\u6389\u6216\u5378\u8f7diptables\u9632\u706b\u5899\uff0c\u5e76\u963b\u6b62kernel\u52a0\u8f7diptables\u6a21\u5757\u3002\u8fd9\u4e9b\u6a21\u5757\u4f1a\u5f71\u54cd\u5e76\u53d1\u6027\u80fd\u3002<\/p>\n<h3><span style=\"padding-left: 20px;font-size: 1em\">\u5355\u8fdb\u7a0b\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236<\/span><\/h3>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u4e00\u822c\u7684\u53d1\u884c\u7248\uff0c\u9650\u5236\u5355\u8fdb\u7a0b\u6700\u5927\u53ef\u4ee5\u6253\u5f001024\u4e2a\u6587\u4ef6\uff0c\u8fd9\u662f\u8fdc\u8fdc\u4e0d\u80fd\u6ee1\u8db3\u9ad8\u5e76\u53d1\u9700\u6c42\u7684\uff0c\u8c03\u6574\u8fc7\u7a0b\u5982\u4e0b\uff1a\u5728#\u53f7\u63d0\u793a\u7b26\u4e0b\u6572\u5165\uff1a<\/p>\n<pre>#&nbsp;ulimit\u2013n&nbsp;65535<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5c06root\u542f\u52a8\u7684\u5355\u4e00\u8fdb\u7a0b\u7684\u6700\u5927\u53ef\u4ee5\u6253\u5f00\u7684\u6587\u4ef6\u6570\u8bbe\u7f6e\u4e3a65535\u4e2a\u3002\u5982\u679c\u7cfb\u7edf\u56de\u663e\u7c7b\u4f3c\u4e8e\u201cOperation not permitted\u201d\u4e4b\u7c7b\u7684\u8bdd\uff0c\u8bf4\u660e\u4e0a\u8ff0\u9650\u5236\u4fee\u6539\u5931\u8d25\uff0c\u5b9e\u9645\u4e0a\u662f\u56e0\u4e3a\u6307\u5b9a\u7684\u6570\u503c\u8d85\u8fc7\u4e86Linux\u7cfb\u7edf\u5bf9\u8be5\u7528\u6237\u6253\u5f00\u6587\u4ef6\u6570\u7684\u8f6f\u9650\u5236\u6216\u786c\u9650\u5236\u3002\u56e0\u6b64\uff0c\u5c31\u9700\u8981\u4fee\u6539Linux\u7cfb\u7edf\u5bf9\u7528\u6237\u7684\u5173\u4e8e\u6253\u5f00\u6587\u4ef6\u6570\u7684\u8f6f\u9650\u5236\u548c\u786c\u9650\u5236\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7b2c\u4e00\u6b65\uff0c\u4fee\u6539limits.conf\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\uff1a<\/p>\n<pre>#&nbsp;vim&nbsp;\/etc\/security\/limits.conf\n*&nbsp;softnofile&nbsp;65536\n*&nbsp;hard&nbsp;nofile65536<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5176\u4e2d&#8217;*&#8217;\u53f7\u8868\u793a\u4fee\u6539\u6240\u6709\u7528\u6237\u7684\u9650\u5236\uff1bsoft\u6216hard\u6307\u5b9a\u8981\u4fee\u6539\u8f6f\u9650\u5236\u8fd8\u662f\u786c\u9650\u5236\uff1b65536\u5219\u6307\u5b9a\u4e86\u60f3\u8981\u4fee\u6539\u7684\u65b0\u7684\u9650\u5236\u503c\uff0c\u5373\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570(\u8bf7\u6ce8\u610f\u8f6f\u9650\u5236\u503c\u8981\u5c0f\u4e8e\u6216\u7b49\u4e8e\u786c\u9650\u5236)\u3002\u4fee\u6539\u5b8c\u540e\u4fdd\u5b58\u6587\u4ef6\u3002\u7b2c\u4e8c\u6b65\uff0c\u4fee\u6539\/etc\/pam.d\/login\u6587\u4ef6\uff0c\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u884c\uff1a<\/p>\n<pre>#&nbsp;vim&nbsp;\/etc\/pam.d\/login\nsessionrequired&nbsp;\/lib\/security\/pam_limits.so<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u8fd9\u662f\u544a\u8bc9Linux\u5728\u7528\u6237\u5b8c\u6210\u7cfb\u7edf\u767b\u5f55\u540e\uff0c\u5e94\u8be5\u8c03\u7528pam_limits.so\u6a21\u5757\u6765\u8bbe\u7f6e\u7cfb\u7edf\u5bf9\u8be5\u7528\u6237\u53ef\u4f7f\u7528\u7684\u5404\u79cd\u8d44\u6e90\u6570\u91cf\u7684\u6700\u5927\u9650\u5236(\u5305\u62ec\u7528\u6237\u53ef\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u9650\u5236)\uff0c\u800cpam_limits.so\u6a21\u5757\u5c31\u4f1a\u4ece\/etc\/security\/limits.conf\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6765\u8bbe\u7f6e\u8fd9\u4e9b\u9650\u5236\u503c\u3002\u4fee\u6539\u5b8c\u540e\u4fdd\u5b58\u6b64\u6587\u4ef6\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7b2c\u4e09\u6b65\uff0c\u67e5\u770bLinux\u7cfb\u7edf\u7ea7\u7684\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\uff0c\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>#&nbsp;cat\/proc\/sys\/fs\/file-max\n32568<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u8fd9\u8868\u660e\u8fd9\u53f0Linux\u7cfb\u7edf\u6700\u591a\u5141\u8bb8\u540c\u65f6\u6253\u5f00(\u5373\u5305\u542b\u6240\u6709\u7528\u6237\u6253\u5f00\u6587\u4ef6\u6570\u603b\u548c)32568\u4e2a\u6587\u4ef6\uff0c\u662fLinux\u7cfb\u7edf\u7ea7\u786c\u9650\u5236\uff0c\u6240\u6709\u7528\u6237\u7ea7\u7684\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u90fd\u4e0d\u5e94\u8d85\u8fc7\u8fd9\u4e2a\u6570\u503c\u3002\u901a\u5e38\u8fd9\u4e2a\u7cfb\u7edf\u7ea7\u786c\u9650\u5236\u662fLinux\u7cfb\u7edf\u5728\u542f\u52a8\u65f6\u6839\u636e\u7cfb\u7edf\u786c\u4ef6\u8d44\u6e90\u72b6\u51b5\u8ba1\u7b97\u51fa\u6765\u7684\u6700\u4f73\u7684\u6700\u5927\u540c\u65f6\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\uff0c\u5982\u679c\u6ca1\u6709\u7279\u6b8a\u9700\u8981\uff0c\u4e0d\u5e94\u8be5\u4fee\u6539\u6b64\u9650\u5236\uff0c\u9664\u975e\u60f3\u4e3a\u7528\u6237\u7ea7\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u8bbe\u7f6e\u8d85\u8fc7\u6b64\u9650\u5236\u7684\u503c\u3002\u4fee\u6539\u6b64\u786c\u9650\u5236\u7684\u65b9\u6cd5\u662f\u4fee\u6539\/etc\/sysctl.conf\u6587\u4ef6\u5185fs.file-max= 131072<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u8fd9\u662f\u8ba9Linux\u5728\u542f\u52a8\u5b8c\u6210\u540e\u5f3a\u884c\u5c06\u7cfb\u7edf\u7ea7\u6253\u5f00\u6587\u4ef6\u6570\u786c\u9650\u5236\u8bbe\u7f6e\u4e3a131072\u3002\u4fee\u6539\u5b8c\u540e\u4fdd\u5b58\u6b64\u6587\u4ef6\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\u91cd\u542f\u7cfb\u7edf\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u5c31\u53ef\u4ee5\u5c06Linux\u7cfb\u7edf\u5bf9\u6307\u5b9a\u7528\u6237\u7684\u5355\u4e00\u8fdb\u7a0b\u5141\u8bb8\u540c\u65f6\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u9650\u5236\u8bbe\u4e3a\u6307\u5b9a\u7684\u6570\u503c\u3002\u5982\u679c\u91cd\u542f\u540e\u7528ulimit-n\u547d\u4ee4\u67e5\u770b\u7528\u6237\u53ef\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u4ecd\u7136\u4f4e\u4e8e\u4e0a\u8ff0\u6b65\u9aa4\u4e2d\u8bbe\u7f6e\u7684\u6700\u5927\u503c\uff0c\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u5728\u7528\u6237\u767b\u5f55\u811a\u672c\/etc\/profile\u4e2d\u4f7f\u7528ulimit-n\u547d\u4ee4\u5df2\u7ecf\u5c06\u7528\u6237\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6587\u4ef6\u6570\u505a\u4e86\u9650\u5236\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7531\u4e8e\u901a\u8fc7ulimit-n\u4fee\u6539\u7cfb\u7edf\u5bf9\u7528\u6237\u53ef\u540c\u65f6\u6253\u5f00\u6587\u4ef6\u7684\u6700\u5927\u6570\u9650\u5236\u65f6\uff0c\u65b0\u4fee\u6539\u7684\u503c\u53ea\u80fd\u5c0f\u4e8e\u6216\u7b49\u4e8e\u4e0a\u6b21ulimit-n\u8bbe\u7f6e\u7684\u503c\uff0c\u56e0\u6b64\u60f3\u7528\u6b64\u547d\u4ee4\u589e\u5927\u8fd9\u4e2a\u9650\u5236\u503c\u662f\u4e0d\u53ef\u80fd\u7684\u3002\u6240\u4ee5\uff0c\u5982\u679c\u6709\u4e0a\u8ff0\u95ee\u9898\u5b58\u5728\uff0c\u5c31\u53ea\u80fd\u53bb\u6253\u5f00\/etc\/profile\u811a\u672c\u6587\u4ef6\uff0c\u5728\u6587\u4ef6\u4e2d\u67e5\u627e\u662f\u5426\u4f7f\u7528\u4e86ulimit-n\u9650\u5236\u4e86\u7528\u6237\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u91cf\uff0c\u5982\u679c\u627e\u5230\uff0c\u5219\u5220\u9664\u8fd9\u884c\u547d\u4ee4\uff0c\u6216\u8005\u5c06\u5176\u8bbe\u7f6e\u7684\u503c\u6539\u4e3a\u5408\u9002\u7684\u503c\uff0c\u7136\u540e\u4fdd\u5b58\u6587\u4ef6\uff0c\u7528\u6237\u9000\u51fa\u5e76\u91cd\u65b0\u767b\u5f55\u7cfb\u7edf\u5373\u53ef\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u5c31\u4e3a\u652f\u6301\u9ad8\u5e76\u53d1TCP\u8fde\u63a5\u5904\u7406\u7684\u901a\u8baf\u5904\u7406\u7a0b\u5e8f\u89e3\u9664\u5173\u4e8e\u6253\u5f00\u6587\u4ef6\u6570\u91cf\u65b9\u9762\u7684\u7cfb\u7edf\u9650\u5236\u3002<\/p>\n<h3><span style=\"padding-left: 20px;font-size: 1em\">\u5185\u6838TCP\u53c2\u6570\u65b9\u9762<\/span><\/h3>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">Linux\u7cfb\u7edf\u4e0b\uff0cTCP\u8fde\u63a5\u65ad\u5f00\u540e\uff0c\u4f1a\u4ee5TIME_WAIT\u72b6\u6001\u4fdd\u7559\u4e00\u5b9a\u7684\u65f6\u95f4\uff0c\u7136\u540e\u624d\u4f1a\u91ca\u653e\u7aef\u53e3\u3002\u5f53\u5e76\u53d1\u8bf7\u6c42\u8fc7\u591a\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u4ea7\u751f\u5927\u91cf\u7684TIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\uff0c\u65e0\u6cd5\u53ca\u65f6\u65ad\u5f00\u7684\u8bdd\uff0c\u4f1a\u5360\u7528\u5927\u91cf\u7684\u7aef\u53e3\u8d44\u6e90\u548c\u670d\u52a1\u5668\u8d44\u6e90\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u4f18\u5316TCP\u7684\u5185\u6838\u53c2\u6570\uff0c\u6765\u53ca\u65f6\u5c06TIME_WAIT\u72b6\u6001\u7684\u7aef\u53e3\u6e05\u7406\u6389\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u4e0b\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u53ea\u5bf9\u62e5\u6709\u5927\u91cfTIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u6d88\u8017\u6709\u6548\uff0c\u5982\u679c\u4e0d\u662f\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6548\u679c\u53ef\u80fd\u4e0d\u660e\u663e\u3002\u53ef\u4ee5\u4f7f\u7528netstat\u547d\u4ee4\u53bb\u67e5TIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\u72b6\u6001\uff0c\u8f93\u5165\u4e0b\u9762\u7684\u7ec4\u5408\u547d\u4ee4\uff0c\u67e5\u770b\u5f53\u524dTCP\u8fde\u63a5\u7684\u72b6\u6001\u548c\u5bf9\u5e94\u7684\u8fde\u63a5\u6570\u91cf\uff1a<\/p>\n<pre>#&nbsp;netstat-n&nbsp;|&nbsp;awk&nbsp;\u2018\/^tcp\/&nbsp;{++S[$NF]}&nbsp;END&nbsp;{for(a&nbsp;in&nbsp;S)&nbsp;print&nbsp;a,&nbsp;S[a]}\u2019<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4e0b\u9762\u7684\u7ed3\u679c\uff1a<\/p>\n<pre>LAST_ACK16\nSYN_RECV348\nESTABLISHED70\nFIN_WAIT1229\nFIN_WAIT230\nCLOSING33\nTIME_WAIT18098<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u6211\u4eec\u53ea\u7528\u5173\u5fc3TIME_WAIT\u7684\u4e2a\u6570\uff0c\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0c\u670918000\u591a\u4e2aTIME_WAIT\uff0c\u8fd9\u6837\u5c31\u5360\u7528\u4e8618000\u591a\u4e2a\u7aef\u53e3\u3002\u8981\u77e5\u9053\u7aef\u53e3\u7684\u6570\u91cf\u53ea\u670965535\u4e2a\uff0c\u5360\u7528\u4e00\u4e2a\u5c11\u4e00\u4e2a\uff0c\u4f1a\u4e25\u91cd\u7684\u5f71\u54cd\u5230\u540e\u7ee7\u7684\u65b0\u8fde\u63a5\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c31\u6709\u5fc5\u8981\u8c03\u6574\u4e0bLinux\u7684TCP\u5185\u6838\u53c2\u6570\uff0c\u8ba9\u7cfb\u7edf\u66f4\u5feb\u7684\u91ca\u653eTIME_WAIT\u8fde\u63a5\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6:\/etc\/sysctl.conf\uff0c\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u52a0\u5165\u4e0b\u9762\u7684\u51e0\u884c\u5185\u5bb9\uff1a<\/p>\n<pre>#&nbsp;vim&nbsp;\/etc\/sysctl.conf\nnet.ipv4.tcp_syncookies=&nbsp;1\nnet.ipv4.tcp_tw_reuse=&nbsp;1\nnet.ipv4.tcp_tw_recycle=&nbsp;1\nnet.ipv4.tcp_fin_timeout=&nbsp;30<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u8f93\u5165\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u8ba9\u5185\u6838\u53c2\u6570\u751f\u6548\uff1a<\/p>\n<pre>#&nbsp;sysctl-p<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7b80\u5355\u7684\u8bf4\u660e\u4e0a\u9762\u7684\u53c2\u6570\u7684\u542b\u4e49\uff1a<\/p>\n<ul>\n<li>  net.ipv4.tcp_syncookies= 1 \u8868\u793a\u5f00\u542fSYNCookies\u3002\u5f53\u51fa\u73b0SYN\u7b49\u5f85\u961f\u5217\u6ea2\u51fa\u65f6\uff0c\u542f\u7528cookies\u6765\u5904\u7406\uff0c\u53ef\u9632\u8303\u5c11\u91cfSYN\u653b\u51fb\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u5173\u95ed\uff1b <\/li>\n<li>  net.ipv4.tcp_tw_reuse= 1 \u8868\u793a\u5f00\u542f\u91cd\u7528\u3002\u5141\u8bb8\u5c06TIME-WAITsockets\u91cd\u65b0\u7528\u4e8e\u65b0\u7684TCP\u8fde\u63a5\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u5173\u95ed\uff1b <\/li>\n<li>  net.ipv4.tcp_tw_recycle= 1 \u8868\u793a\u5f00\u542fTCP\u8fde\u63a5\u4e2dTIME-WAITsockets\u7684\u5feb\u901f\u56de\u6536\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u5173\u95ed\uff1b <\/li>\n<li>  net.ipv4.tcp_fin_timeout \u4fee\u6539\u7cfb\u7d71\u9ed8\u8ba4\u7684TIMEOUT \u65f6\u95f4\u3002 <\/li>\n<\/ul>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5728\u7ecf\u8fc7\u8fd9\u6837\u7684\u8c03\u6574\u4e4b\u540e\uff0c\u9664\u4e86\u4f1a\u8fdb\u4e00\u6b65\u63d0\u5347\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u80fd\u529b\u4e4b\u5916\uff0c\u8fd8\u80fd\u591f\u9632\u5fa1\u5c0f\u6d41\u91cf\u7a0b\u5ea6\u7684DoS\u3001CC\u548cSYN\u653b\u51fb\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u6b64\u5916\uff0c\u5982\u679c\u4f60\u7684\u8fde\u63a5\u6570\u672c\u8eab\u5c31\u5f88\u591a\uff0c\u6211\u4eec\u53ef\u4ee5\u518d\u4f18\u5316\u4e00\u4e0bTCP\u7684\u53ef\u4f7f\u7528\u7aef\u53e3\u8303\u56f4\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u670d\u52a1\u5668\u7684\u5e76\u53d1\u80fd\u529b\u3002\u4f9d\u7136\u662f\u5f80\u4e0a\u9762\u7684\u53c2\u6570\u6587\u4ef6\u4e2d\uff0c\u52a0\u5165\u4e0b\u9762\u8fd9\u4e9b\u914d\u7f6e\uff1a<\/p>\n<pre>net.ipv4.tcp_keepalive_time=&nbsp;1200\nnet.ipv4.ip_local_port_range=&nbsp;1024&nbsp;65535\nnet.ipv4.tcp_max_syn_backlog=&nbsp;8192\nnet.ipv4.tcp_max_tw_buckets=&nbsp;5000<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u8fd9\u51e0\u4e2a\u53c2\u6570\uff0c\u5efa\u8bae\u53ea\u5728\u6d41\u91cf\u975e\u5e38\u5927\u7684\u670d\u52a1\u5668\u4e0a\u5f00\u542f\uff0c\u4f1a\u6709\u663e\u8457\u7684\u6548\u679c\u3002\u4e00\u822c\u7684\u6d41\u91cf\u5c0f\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u6ca1\u6709\u5fc5\u8981\u53bb\u8bbe\u7f6e\u8fd9\u51e0\u4e2a\u53c2\u6570\u3002<\/p>\n<ul>\n<li>  net.ipv4.tcp_keepalive_time= 1200 \u8868\u793a\u5f53keepalive\u8d77\u7528\u7684\u65f6\u5019\uff0cTCP\u53d1\u9001keepalive\u6d88\u606f\u7684\u9891\u5ea6\u3002\u7f3a\u7701\u662f2\u5c0f\u65f6\uff0c\u6539\u4e3a20\u5206\u949f\u3002 <\/li>\n<li>  ip_local_port_range= 1024 65535 \u8868\u793a\u7528\u4e8e\u5411\u5916\u8fde\u63a5\u7684\u7aef\u53e3\u8303\u56f4\u3002\u7f3a\u7701\u60c5\u51b5\u4e0b\u5f88\u5c0f\uff0c\u6539\u4e3a1024\u523065535\u3002 <\/li>\n<li>  net.ipv4.tcp_max_syn_backlog= 8192 \u8868\u793aSYN\u961f\u5217\u7684\u957f\u5ea6\uff0c\u9ed8\u8ba4\u4e3a1024\uff0c\u52a0\u5927\u961f\u5217\u957f\u5ea6\u4e3a8192\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u66f4\u591a\u7b49\u5f85\u8fde\u63a5\u7684\u7f51\u7edc\u8fde\u63a5\u6570\u3002 <\/li>\n<li>  net.ipv4.tcp_max_tw_buckets= 5000 \u8868\u793a\u7cfb\u7edf\u540c\u65f6\u4fdd\u6301TIME_WAIT\u7684\u6700\u5927\u6570\u91cf\uff0c\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u6570\u5b57\uff0cTIME_WAIT\u5c06\u7acb\u523b\u88ab\u6e05\u9664\u5e76\u6253\u5370\u8b66\u544a\u4fe1\u606f\u3002\u9ed8\u8ba4\u4e3a180000\uff0c\u6539\u4e3a5000\u3002\u6b64\u9879\u53c2\u6570\u53ef\u4ee5\u63a7\u5236TIME_WAIT\u7684\u6700\u5927\u6570\u91cf\uff0c\u53ea\u8981\u8d85\u51fa\u4e86\u3002\u5185\u6838\u5176\u4ed6TCP\u53c2\u6570\u8bf4\u660e <\/li>\n<li>  net.ipv4.tcp_max_syn_backlog= 65536 \u8bb0\u5f55\u7684\u90a3\u4e9b\u5c1a\u672a\u6536\u5230\u5ba2\u6237\u7aef\u786e\u8ba4\u4fe1\u606f\u7684\u8fde\u63a5\u8bf7\u6c42\u7684\u6700\u5927\u503c\u3002\u5bf9\u4e8e\u6709128M\u5185\u5b58\u7684\u7cfb\u7edf\u800c\u8a00\uff0c\u7f3a\u7701\u503c\u662f1024\uff0c\u5c0f\u5185\u5b58\u7684\u7cfb\u7edf\u5219\u662f128\u3002 <\/li>\n<li>  net.core.netdev_max_backlog= 32768 \u6bcf\u4e2a\u7f51\u7edc\u63a5\u53e3\u63a5\u6536\u6570\u636e\u5305\u7684\u901f\u7387\u6bd4\u5185\u6838\u5904\u7406\u8fd9\u4e9b\u5305\u7684\u901f\u7387\u5feb\u65f6\uff0c\u5141\u8bb8\u9001\u5230\u961f\u5217\u7684\u6570\u636e\u5305\u7684\u6700\u5927\u6570\u76ee\u3002 <\/li>\n<li>  net.core.somaxconn= 32768 \u4f8b\u5982web\u5e94\u7528\u4e2dlisten\u51fd\u6570\u7684backlog\u9ed8\u8ba4\u4f1a\u7ed9\u6211\u4eec\u5185\u6838\u53c2\u6570\u7684net.core.somaxconn\u9650\u5236\u5230128\uff0c\u800cnginx\u5b9a\u4e49\u7684NGX_LISTEN_BACKLOG\u9ed8\u8ba4\u4e3a511\uff0c\u6240\u4ee5\u6709\u5fc5\u8981\u8c03\u6574\u8fd9\u4e2a\u503c\u3002 <\/li>\n<li>  net.core.wmem_default= 8388608 <\/li>\n<li>  net.core.rmem_default= 8388608 <\/li>\n<li>  net.core.rmem_max= 16777216 #\u6700\u5927socket\u8bfbbuffer,\u53ef\u53c2\u8003\u7684\u4f18\u5316\u503c:873200 <\/li>\n<li>  net.core.wmem_max= 16777216 #\u6700\u5927socket\u5199buffer,\u53ef\u53c2\u8003\u7684\u4f18\u5316\u503c:873200 <\/li>\n<li>  net.ipv4.tcp_timestsmps= 0 \u65f6\u95f4\u6233\u53ef\u4ee5\u907f\u514d\u5e8f\u5217\u53f7\u7684\u5377\u7ed5\u3002\u4e00\u4e2a1Gbps\u7684\u94fe\u8def\u80af\u5b9a\u4f1a\u9047\u5230\u4ee5\u524d\u7528\u8fc7\u7684\u5e8f\u5217\u53f7\u3002\u65f6\u95f4\u6233\u80fd\u591f\u8ba9\u5185\u6838\u63a5\u53d7\u8fd9\u79cd\u201c\u5f02\u5e38\u201d\u7684\u6570\u636e\u5305\u3002\u8fd9\u91cc\u9700\u8981\u5c06\u5176\u5173\u6389\u3002 <\/li>\n<li>  net.ipv4.tcp_synack_retries= 2 \u4e3a\u4e86\u6253\u5f00\u5bf9\u7aef\u7684\u8fde\u63a5\uff0c\u5185\u6838\u9700\u8981\u53d1\u9001\u4e00\u4e2aSYN\u5e76\u9644\u5e26\u4e00\u4e2a\u56de\u5e94\u524d\u9762\u4e00\u4e2aSYN\u7684ACK\u3002\u4e5f\u5c31\u662f\u6240\u8c13\u4e09\u6b21\u63e1\u624b\u4e2d\u7684\u7b2c\u4e8c\u6b21\u63e1\u624b\u3002\u8fd9\u4e2a\u8bbe\u7f6e\u51b3\u5b9a\u4e86\u5185\u6838\u653e\u5f03\u8fde\u63a5\u4e4b\u524d\u53d1\u9001SYN+ACK\u5305\u7684\u6570\u91cf\u3002 <\/li>\n<li>  net.ipv4.tcp_syn_retries= 2 \u5728\u5185\u6838\u653e\u5f03\u5efa\u7acb\u8fde\u63a5\u4e4b\u524d\u53d1\u9001SYN\u5305\u7684\u6570\u91cf\u3002 <\/li>\n<li>  #net.ipv4.tcp_tw_len= 1 <\/li>\n<li>  net.ipv4.tcp_tw_reuse= 1 \u5f00\u542f\u91cd\u7528\u3002\u5141\u8bb8\u5c06TIME-WAITsockets\u91cd\u65b0\u7528\u4e8e\u65b0\u7684TCP\u8fde\u63a5\u3002 <\/li>\n<li>  net.ipv4.tcp_wmem= 8192 436600 873200 TCP\u5199buffer,\u53ef\u53c2\u8003\u7684\u4f18\u5316\u503c:8192 436600 873200 <\/li>\n<li>  net.ipv4.tcp_rmem = 32768 436600 873200 TCP\u8bfbbuffer,\u53ef\u53c2\u8003\u7684\u4f18\u5316\u503c:32768 436600 873200 <\/li>\n<li>  net.ipv4.tcp_mem= 94500000 91500000 92700000 \u540c\u6837\u67093\u4e2a\u503c,\u610f\u601d\u662f: <\/li>\n<li>  net.ipv4.tcp_mem[0]:\u4f4e\u4e8e\u6b64\u503c\uff0cTCP\u6ca1\u6709\u5185\u5b58\u538b\u529b\u3002 <\/li>\n<li>  net.ipv4.tcp_mem[1]:\u5728\u6b64\u503c\u4e0b\uff0c\u8fdb\u5165\u5185\u5b58\u538b\u529b\u9636\u6bb5\u3002 <\/li>\n<li>  net.ipv4.tcp_mem[2]:\u9ad8\u4e8e\u6b64\u503c\uff0cTCP\u62d2\u7edd\u5206\u914dsocket\u3002\u4e0a\u8ff0\u5185\u5b58\u5355\u4f4d\u662f\u9875\uff0c\u800c\u4e0d\u662f\u5b57\u8282\u3002\u53ef\u53c2\u8003\u7684\u4f18\u5316\u503c\u662f:7864321048576 1572864 <\/li>\n<li>  net.ipv4.tcp_max_orphans= 3276800 \u7cfb\u7edf\u4e2d\u6700\u591a\u6709\u591a\u5c11\u4e2aTCP\u5957\u63a5\u5b57\u4e0d\u88ab\u5173\u8054\u5230\u4efb\u4f55\u4e00\u4e2a\u7528\u6237\u6587\u4ef6\u53e5\u67c4\u4e0a\u3002\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u6570\u5b57\uff0c\u8fde\u63a5\u5c06\u5373\u523b\u88ab\u590d\u4f4d\u5e76\u6253\u5370\u51fa\u8b66\u544a\u4fe1\u606f\u3002\u8fd9\u4e2a\u9650\u5236\u4ec5\u4ec5\u662f\u4e3a\u4e86\u9632\u6b62\u7b80\u5355\u7684DoS\u653b\u51fb\uff0c\u4e0d\u80fd\u8fc7\u5206\u4f9d\u9760\u5b83\u6216\u8005\u4eba\u4e3a\u5730\u51cf\u5c0f\u8fd9\u4e2a\u503c\uff0c \u66f4\u5e94\u8be5\u589e\u52a0\u8fd9\u4e2a\u503c(\u5982\u679c\u589e\u52a0\u4e86\u5185\u5b58\u4e4b\u540e)\u3002 <\/li>\n<li>  net.ipv4.tcp_fin_timeout= 30 \u5982\u679c\u5957\u63a5\u5b57\u7531\u672c\u7aef\u8981\u6c42\u5173\u95ed\uff0c\u8fd9\u4e2a\u53c2\u6570\u51b3\u5b9a\u4e86\u5b83\u4fdd\u6301\u5728FIN-WAIT-2\u72b6\u6001\u7684\u65f6\u95f4\u3002\u5bf9\u7aef\u53ef\u4ee5\u51fa\u9519\u5e76\u6c38\u8fdc\u4e0d\u5173\u95ed\u8fde\u63a5\uff0c\u751a\u81f3\u610f\u5916\u5f53\u673a\u3002\u7f3a\u7701\u503c\u662f60\u79d2\u30022.2 \u5185\u6838\u7684\u901a\u5e38\u503c\u662f180\u79d2\uff0c\u4f60\u53ef\u4ee5\u6309\u8fd9\u4e2a\u8bbe\u7f6e\uff0c\u4f46\u8981\u8bb0\u4f4f\u7684\u662f\uff0c\u5373\u4f7f\u4f60\u7684\u673a\u5668\u662f\u4e00\u4e2a\u8f7b\u8f7d\u7684WEB\u670d\u52a1\u5668\uff0c\u4e5f\u6709\u56e0\u4e3a\u5927\u91cf\u7684\u6b7b\u5957\u63a5\u5b57\u800c\u5185\u5b58\u6ea2\u51fa\u7684\u98ce\u9669\uff0cFIN-WAIT-2\u7684\u5371\u9669\u6027\u6bd4FIN-WAIT-1\u8981\u5c0f\uff0c\u56e0\u4e3a\u5b83\u6700\u591a\u53ea\u80fd\u5403\u63891.5K\u5185\u5b58\uff0c\u4f46\u662f\u5b83\u4eec\u7684\u751f\u5b58\u671f\u957f\u4e9b\u3002 <\/li>\n<\/ul>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u540c\u65f6\u8fd8\u6d89\u53ca\u5230\u4e00\u4e2aTCP \u62e5\u585e\u7b97\u6cd5\u7684\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u67e5\u770b\u672c\u673a\u63d0\u4f9b\u7684\u62e5\u585e\u7b97\u6cd5\u63a7\u5236\u6a21\u5757\uff1a<\/p>\n<ul>\n<li>  sysctl net.ipv4.tcp_available_congestion_control <\/li>\n<\/ul>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5bf9\u4e8e\u51e0\u79cd\u7b97\u6cd5\u7684\u5206\u6790\uff0c\u8be6\u60c5\u53ef\u4ee5\u53c2\u8003\u4e0b\uff1aTCP\u62e5\u585e\u63a7\u5236\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u3001\u9002\u7528\u73af\u5883\u3001\u6027\u80fd\u5206\u6790\uff0c\u6bd4\u5982\u9ad8\u5ef6\u65f6\u53ef\u4ee5\u8bd5\u7528hybla\uff0c\u4e2d\u7b49\u5ef6\u65f6\u53ef\u4ee5\u8bd5\u7528htcp\u7b97\u6cd5\u7b49\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5982\u679c\u60f3\u8bbe\u7f6eTCP \u62e5\u585e\u7b97\u6cd5\u4e3ahybla net.ipv4.tcp_congestion_control=hybla<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u989d\u5916\u7684\uff0c\u5bf9\u4e8e\u5185\u6838\u7248\u9ad8\u4e8e\u4e8e3.7.1\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u542ftcp_fastopen\uff1anet.ipv4.tcp_fastopen= 3<\/p>\n<h3><span style=\"padding-left: 20px;font-size: 1em\">IO\u4e8b\u4ef6\u5206\u914d\u673a\u5236<\/span><\/h3>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u5728Linux\u542f\u7528\u9ad8\u5e76\u53d1TCP\u8fde\u63a5\uff0c\u5fc5\u987b\u786e\u8ba4\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u4f7f\u7528\u4e86\u5408\u9002\u7684\u7f51\u7edcI\/O\u6280\u672f\u548cI\/O\u4e8b\u4ef6\u5206\u6d3e\u673a\u5236\u3002\u53ef\u7528\u7684I\/O\u6280\u672f\u6709\u540c\u6b65I\/O\uff0c\u975e\u963b\u585e\u5f0f\u540c\u6b65I\/O\uff0c\u4ee5\u53ca\u5f02\u6b65I\/O\u3002\u5728\u9ad8TCP\u5e76\u53d1\u7684\u60c5\u5f62\u4e0b\uff0c\u5982\u679c\u4f7f\u7528\u540c\u6b65I\/O\uff0c\u8fd9\u4f1a\u4e25\u91cd\u963b\u585e\u7a0b\u5e8f\u7684\u8fd0\u8f6c\uff0c\u9664\u975e\u4e3a\u6bcf\u4e2aTCP\u8fde\u63a5\u7684I\/O\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u3002\u4f46\u662f\uff0c\u8fc7\u591a\u7684\u7ebf\u7a0b\u53c8\u4f1a\u56e0\u7cfb\u7edf\u5bf9\u7ebf\u7a0b\u7684\u8c03\u5ea6\u9020\u6210\u5de8\u5927\u5f00\u9500\u3002\u56e0\u6b64\uff0c\u5728\u9ad8TCP\u5e76\u53d1\u7684\u60c5\u5f62\u4e0b\u4f7f\u7528\u540c\u6b65I\/O\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u975e\u963b\u585e\u5f0f\u540c\u6b65I\/O\u6216\u5f02\u6b65I\/O\u3002\u975e\u963b\u585e\u5f0f\u540c\u6b65I\/O\u7684\u6280\u672f\u5305\u62ec\u4f7f\u7528select()\uff0cpoll()\uff0cepoll\u7b49\u673a\u5236\u3002\u5f02\u6b65I\/O\u7684\u6280\u672f\u5c31\u662f\u4f7f\u7528AIO\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u4eceI\/O\u4e8b\u4ef6\u5206\u6d3e\u673a\u5236\u6765\u770b\uff0c\u4f7f\u7528select()\u662f\u4e0d\u5408\u9002\u7684\uff0c\u56e0\u4e3a\u5b83\u6240\u652f\u6301\u7684\u5e76\u53d1\u8fde\u63a5\u6570\u6709\u9650(\u901a\u5e38\u57281024\u4e2a\u4ee5\u5185)\u3002\u5982\u679c\u8003\u8651\u6027\u80fd\uff0cpoll()\u4e5f\u662f\u4e0d\u5408\u9002\u7684\uff0c\u5c3d\u7ba1\u5b83\u53ef\u4ee5\u652f\u6301\u7684\u8f83\u9ad8\u7684TCP\u5e76\u53d1\u6570\uff0c\u4f46\u662f\u7531\u4e8e\u5176\u91c7\u7528\u201c\u8f6e\u8be2\u201d\u673a\u5236\uff0c\u5f53\u5e76\u53d1\u6570\u8f83\u9ad8\u65f6\uff0c\u5176\u8fd0\u884c\u6548\u7387\u76f8\u5f53\u4f4e\uff0c\u5e76\u53ef\u80fd\u5b58\u5728I\/O\u4e8b\u4ef6\u5206\u6d3e\u4e0d\u5747\uff0c\u5bfc\u81f4\u90e8\u5206TCP\u8fde\u63a5\u4e0a\u7684I\/O\u51fa\u73b0\u201c\u9965\u997f\u201d\u73b0\u8c61\u3002\u800c\u5982\u679c\u4f7f\u7528epoll\u6216AIO\uff0c\u5219\u6ca1\u6709\u4e0a\u8ff0\u95ee\u9898(\u65e9\u671fLinux\u5185\u6838\u7684AIO\u6280\u672f\u5b9e\u73b0\u662f\u901a\u8fc7\u5728\u5185\u6838\u4e2d\u4e3a\u6bcf\u4e2aI\/O\u8bf7\u6c42\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u7684\uff0c\u8fd9\u79cd\u5b9e\u73b0\u673a\u5236\u5728\u9ad8\u5e76\u53d1TCP\u8fde\u63a5\u7684\u60c5\u5f62\u4e0b\u4f7f\u7528\u5176\u5b9e\u4e5f\u6709\u4e25\u91cd\u7684\u6027\u80fd\u95ee\u9898\u3002\u4f46\u5728\u6700\u65b0\u7684Linux\u5185\u6838\u4e2d\uff0cAIO\u7684\u5b9e\u73b0\u5df2\u7ecf\u5f97\u5230\u6539\u8fdb)\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7efc\u4e0a\u6240\u8ff0\uff0c\u5728\u5f00\u53d1\u652f\u6301\u9ad8\u5e76\u53d1TCP\u8fde\u63a5\u7684Linux\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528epoll\u6216AIO\u6280\u672f\u6765\u5b9e\u73b0\u5e76\u53d1\u7684TCP\u8fde\u63a5\u4e0a\u7684I\/O\u63a7\u5236\uff0c\u8fd9\u5c06\u4e3a\u63d0\u5347\u7a0b\u5e8f\u5bf9\u9ad8\u5e76\u53d1TCP\u8fde\u63a5\u7684\u652f\u6301\u63d0\u4f9b\u6709\u6548\u7684I\/O\u4fdd\u8bc1\u3002<\/p>\n<p style=\"margin-bottom: 20px;line-height: 1.8em\">\u7ecf\u8fc7\u8fd9\u6837\u7684\u4f18\u5316\u914d\u7f6e\u4e4b\u540e\uff0c\u670d\u52a1\u5668\u7684TCP\u5e76\u53d1\u5904\u7406\u80fd\u529b\u4f1a\u663e\u8457\u63d0\u9ad8\u3002\u4ee5\u4e0a\u914d\u7f6e\u4ec5\u4f9b\u53c2\u8003\uff0c\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u8bf7\u6839\u636e\u81ea\u5df1\u7684\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u89c2\u5bdf\u518d\u8c03\u6574\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b Linux \u7cfb\u7edf\u53ca kernel \u53c2\u6570\u4f18\u5316\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>\u4f17\u6240\u5468\u77e5\u5728\u9ed8\u8ba4\u53c2\u6570\u60c5\u51b5\u4e0bLinux\u5bf9\u9ad8\u5e76\u53d1\u652f\u6301\u5e76\u4e0d\u597d\uff0c\u4e3b\u8981\u53d7\u9650\u4e8e\u5355\u8fdb\u7a0b\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u3001\u5185\u6838TCP\u53c2\u6570\u65b9\u9762\u548cIO\u4e8b\u4ef6\u5206\u914d\u673a\u5236\u7b49\u3002 \u4e0b\u9762\u5c31\u4ece\u51e0\u65b9\u9762\u6765\u8c03\u6574\u4f7fLinux\u7cfb\u7edf\u80fd\u591f\u652f\u6301\u9ad8\u5e76\u53d1\u73af\u5883\u3002 Iptables\u76f8\u5173 \u5982\u975e\u5fc5\u987b\uff0c\u5173\u6389\u6216\u5378\u8f7diptables\u9632\u706b\u5899\uff0c\u5e76\u963b\u6b62kernel\u52a0\u8f7diptables\u6a21\u5757\u3002\u8fd9\u4e9b\u6a21\u5757\u4f1a\u5f71\u54cd\u5e76\u53d1\u6027\u80fd\u3002 \u5355\u8fdb\u7a0b\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236 \u4e00\u822c\u7684\u53d1\u884c\u7248\uff0c\u9650\u5236\u5355\u8fdb\u7a0b\u6700\u5927\u53ef\u4ee5\u6253\u5f001024\u4e2a\u6587\u4ef6\uff0c\u8fd9\u662f\u8fdc\u8fdc\u4e0d\u80fd\u6ee1\u8db3\u9ad8\u5e76\u53d1\u9700\u6c42\u7684\uff0c\u8c03\u6574\u8fc7\u7a0b\u5982\u4e0b\uff1a\u5728#\u53f7\u63d0\u793a\u7b26\u4e0b\u6572\u5165\uff1a #&nbsp;ulimit\u2013n&nbsp;65535 \u767b\u5f55\u540e\u590d\u5236 \u5c06root\u542f\u52a8\u7684\u5355\u4e00\u8fdb\u7a0b\u7684\u6700\u5927\u53ef\u4ee5\u6253\u5f00\u7684\u6587\u4ef6\u6570\u8bbe\u7f6e\u4e3a65535\u4e2a\u3002\u5982\u679c\u7cfb\u7edf\u56de\u663e\u7c7b\u4f3c\u4e8e\u201cOperation not permitted\u201d\u4e4b\u7c7b\u7684\u8bdd\uff0c\u8bf4\u660e\u4e0a\u8ff0\u9650\u5236\u4fee\u6539\u5931\u8d25\uff0c\u5b9e\u9645\u4e0a\u662f\u56e0\u4e3a\u6307\u5b9a\u7684\u6570\u503c\u8d85\u8fc7\u4e86Linux\u7cfb\u7edf\u5bf9\u8be5\u7528\u6237\u6253\u5f00\u6587\u4ef6\u6570\u7684\u8f6f\u9650\u5236\u6216\u786c\u9650\u5236\u3002\u56e0\u6b64\uff0c\u5c31\u9700\u8981\u4fee\u6539Linux\u7cfb\u7edf\u5bf9\u7528\u6237\u7684\u5173\u4e8e\u6253\u5f00\u6587\u4ef6\u6570\u7684\u8f6f\u9650\u5236\u548c\u786c\u9650\u5236\u3002 \u7b2c\u4e00\u6b65\uff0c\u4fee\u6539limits.conf\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\uff1a #&nbsp;vim&nbsp;\/etc\/security\/limits.conf *&nbsp;softnofile&nbsp;65536 *&nbsp;hard&nbsp;nofile65536 \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d&#8217;*&#8217;\u53f7\u8868\u793a\u4fee\u6539\u6240\u6709\u7528\u6237\u7684\u9650\u5236\uff1bsoft\u6216hard\u6307\u5b9a\u8981\u4fee\u6539\u8f6f\u9650\u5236\u8fd8\u662f\u786c\u9650\u5236\uff1b65536\u5219\u6307\u5b9a\u4e86\u60f3\u8981\u4fee\u6539\u7684\u65b0\u7684\u9650\u5236\u503c\uff0c\u5373\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570(\u8bf7\u6ce8\u610f\u8f6f\u9650\u5236\u503c\u8981\u5c0f\u4e8e\u6216\u7b49\u4e8e\u786c\u9650\u5236)\u3002\u4fee\u6539\u5b8c\u540e\u4fdd\u5b58\u6587\u4ef6\u3002\u7b2c\u4e8c\u6b65\uff0c\u4fee\u6539\/etc\/pam.d\/login\u6587\u4ef6\uff0c\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u884c\uff1a #&nbsp;vim&nbsp;\/etc\/pam.d\/login sessionrequired&nbsp;\/lib\/security\/pam_limits.so \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u662f\u544a\u8bc9Linux\u5728\u7528\u6237\u5b8c\u6210\u7cfb\u7edf\u767b\u5f55\u540e\uff0c\u5e94\u8be5\u8c03\u7528pam_limits.so\u6a21\u5757\u6765\u8bbe\u7f6e\u7cfb\u7edf\u5bf9\u8be5\u7528\u6237\u53ef\u4f7f\u7528\u7684\u5404\u79cd\u8d44\u6e90\u6570\u91cf\u7684\u6700\u5927\u9650\u5236(\u5305\u62ec\u7528\u6237\u53ef\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u9650\u5236)\uff0c\u800cpam_limits.so\u6a21\u5757\u5c31\u4f1a\u4ece\/etc\/security\/limits.conf\u6587\u4ef6\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6765\u8bbe\u7f6e\u8fd9\u4e9b\u9650\u5236\u503c\u3002\u4fee\u6539\u5b8c\u540e\u4fdd\u5b58\u6b64\u6587\u4ef6\u3002 \u7b2c\u4e09\u6b65\uff0c\u67e5\u770bLinux\u7cfb\u7edf\u7ea7\u7684\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\uff0c\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a #&nbsp;cat\/proc\/sys\/fs\/file-max 32568 \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u8868\u660e\u8fd9\u53f0Linux\u7cfb\u7edf\u6700\u591a\u5141\u8bb8\u540c\u65f6\u6253\u5f00(\u5373\u5305\u542b\u6240\u6709\u7528\u6237\u6253\u5f00\u6587\u4ef6\u6570\u603b\u548c)32568\u4e2a\u6587\u4ef6\uff0c\u662fLinux\u7cfb\u7edf\u7ea7\u786c\u9650\u5236\uff0c\u6240\u6709\u7528\u6237\u7ea7\u7684\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u90fd\u4e0d\u5e94\u8d85\u8fc7\u8fd9\u4e2a\u6570\u503c\u3002\u901a\u5e38\u8fd9\u4e2a\u7cfb\u7edf\u7ea7\u786c\u9650\u5236\u662fLinux\u7cfb\u7edf\u5728\u542f\u52a8\u65f6\u6839\u636e\u7cfb\u7edf\u786c\u4ef6\u8d44\u6e90\u72b6\u51b5\u8ba1\u7b97\u51fa\u6765\u7684\u6700\u4f73\u7684\u6700\u5927\u540c\u65f6\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\uff0c\u5982\u679c\u6ca1\u6709\u7279\u6b8a\u9700\u8981\uff0c\u4e0d\u5e94\u8be5\u4fee\u6539\u6b64\u9650\u5236\uff0c\u9664\u975e\u60f3\u4e3a\u7528\u6237\u7ea7\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u8bbe\u7f6e\u8d85\u8fc7\u6b64\u9650\u5236\u7684\u503c\u3002\u4fee\u6539\u6b64\u786c\u9650\u5236\u7684\u65b9\u6cd5\u662f\u4fee\u6539\/etc\/sysctl.conf\u6587\u4ef6\u5185fs.file-max= 131072 \u8fd9\u662f\u8ba9Linux\u5728\u542f\u52a8\u5b8c\u6210\u540e\u5f3a\u884c\u5c06\u7cfb\u7edf\u7ea7\u6253\u5f00\u6587\u4ef6\u6570\u786c\u9650\u5236\u8bbe\u7f6e\u4e3a131072\u3002\u4fee\u6539\u5b8c\u540e\u4fdd\u5b58\u6b64\u6587\u4ef6\u3002 \u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\u91cd\u542f\u7cfb\u7edf\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u5c31\u53ef\u4ee5\u5c06Linux\u7cfb\u7edf\u5bf9\u6307\u5b9a\u7528\u6237\u7684\u5355\u4e00\u8fdb\u7a0b\u5141\u8bb8\u540c\u65f6\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u9650\u5236\u8bbe\u4e3a\u6307\u5b9a\u7684\u6570\u503c\u3002\u5982\u679c\u91cd\u542f\u540e\u7528ulimit-n\u547d\u4ee4\u67e5\u770b\u7528\u6237\u53ef\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u4ecd\u7136\u4f4e\u4e8e\u4e0a\u8ff0\u6b65\u9aa4\u4e2d\u8bbe\u7f6e\u7684\u6700\u5927\u503c\uff0c\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u5728\u7528\u6237\u767b\u5f55\u811a\u672c\/etc\/profile\u4e2d\u4f7f\u7528ulimit-n\u547d\u4ee4\u5df2\u7ecf\u5c06\u7528\u6237\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6587\u4ef6\u6570\u505a\u4e86\u9650\u5236\u3002 \u7531\u4e8e\u901a\u8fc7ulimit-n\u4fee\u6539\u7cfb\u7edf\u5bf9\u7528\u6237\u53ef\u540c\u65f6\u6253\u5f00\u6587\u4ef6\u7684\u6700\u5927\u6570\u9650\u5236\u65f6\uff0c\u65b0\u4fee\u6539\u7684\u503c\u53ea\u80fd\u5c0f\u4e8e\u6216\u7b49\u4e8e\u4e0a\u6b21ulimit-n\u8bbe\u7f6e\u7684\u503c\uff0c\u56e0\u6b64\u60f3\u7528\u6b64\u547d\u4ee4\u589e\u5927\u8fd9\u4e2a\u9650\u5236\u503c\u662f\u4e0d\u53ef\u80fd\u7684\u3002\u6240\u4ee5\uff0c\u5982\u679c\u6709\u4e0a\u8ff0\u95ee\u9898\u5b58\u5728\uff0c\u5c31\u53ea\u80fd\u53bb\u6253\u5f00\/etc\/profile\u811a\u672c\u6587\u4ef6\uff0c\u5728\u6587\u4ef6\u4e2d\u67e5\u627e\u662f\u5426\u4f7f\u7528\u4e86ulimit-n\u9650\u5236\u4e86\u7528\u6237\u53ef\u540c\u65f6\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u91cf\uff0c\u5982\u679c\u627e\u5230\uff0c\u5219\u5220\u9664\u8fd9\u884c\u547d\u4ee4\uff0c\u6216\u8005\u5c06\u5176\u8bbe\u7f6e\u7684\u503c\u6539\u4e3a\u5408\u9002\u7684\u503c\uff0c\u7136\u540e\u4fdd\u5b58\u6587\u4ef6\uff0c\u7528\u6237\u9000\u51fa\u5e76\u91cd\u65b0\u767b\u5f55\u7cfb\u7edf\u5373\u53ef\u3002 \u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u5c31\u4e3a\u652f\u6301\u9ad8\u5e76\u53d1TCP\u8fde\u63a5\u5904\u7406\u7684\u901a\u8baf\u5904\u7406\u7a0b\u5e8f\u89e3\u9664\u5173\u4e8e\u6253\u5f00\u6587\u4ef6\u6570\u91cf\u65b9\u9762\u7684\u7cfb\u7edf\u9650\u5236\u3002 \u5185\u6838TCP\u53c2\u6570\u65b9\u9762 Linux\u7cfb\u7edf\u4e0b\uff0cTCP\u8fde\u63a5\u65ad\u5f00\u540e\uff0c\u4f1a\u4ee5TIME_WAIT\u72b6\u6001\u4fdd\u7559\u4e00\u5b9a\u7684\u65f6\u95f4\uff0c\u7136\u540e\u624d\u4f1a\u91ca\u653e\u7aef\u53e3\u3002\u5f53\u5e76\u53d1\u8bf7\u6c42\u8fc7\u591a\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u4ea7\u751f\u5927\u91cf\u7684TIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\uff0c\u65e0\u6cd5\u53ca\u65f6\u65ad\u5f00\u7684\u8bdd\uff0c\u4f1a\u5360\u7528\u5927\u91cf\u7684\u7aef\u53e3\u8d44\u6e90\u548c\u670d\u52a1\u5668\u8d44\u6e90\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u4f18\u5316TCP\u7684\u5185\u6838\u53c2\u6570\uff0c\u6765\u53ca\u65f6\u5c06TIME_WAIT\u72b6\u6001\u7684\u7aef\u53e3\u6e05\u7406\u6389\u3002 \u4e0b\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u53ea\u5bf9\u62e5\u6709\u5927\u91cfTIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u6d88\u8017\u6709\u6548\uff0c\u5982\u679c\u4e0d\u662f\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6548\u679c\u53ef\u80fd\u4e0d\u660e\u663e\u3002\u53ef\u4ee5\u4f7f\u7528netstat\u547d\u4ee4\u53bb\u67e5TIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\u72b6\u6001\uff0c\u8f93\u5165\u4e0b\u9762\u7684\u7ec4\u5408\u547d\u4ee4\uff0c\u67e5\u770b\u5f53\u524dTCP\u8fde\u63a5\u7684\u72b6\u6001\u548c\u5bf9\u5e94\u7684\u8fde\u63a5\u6570\u91cf\uff1a #&nbsp;netstat-n&nbsp;|&nbsp;awk&nbsp;\u2018\/^tcp\/&nbsp;{++S[$NF]}&nbsp;END&nbsp;{for(a&nbsp;in&nbsp;S)&nbsp;print&nbsp;a,&nbsp;S[a]}\u2019 \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4e0b\u9762\u7684\u7ed3\u679c\uff1a LAST_ACK16 SYN_RECV348 ESTABLISHED70 FIN_WAIT1229 FIN_WAIT230 CLOSING33 TIME_WAIT18098 \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u53ea\u7528\u5173\u5fc3TIME_WAIT\u7684\u4e2a\u6570\uff0c\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0c\u670918000\u591a\u4e2aTIME_WAIT\uff0c\u8fd9\u6837\u5c31\u5360\u7528\u4e8618000\u591a\u4e2a\u7aef\u53e3\u3002\u8981\u77e5\u9053\u7aef\u53e3\u7684\u6570\u91cf\u53ea\u670965535\u4e2a\uff0c\u5360\u7528\u4e00\u4e2a\u5c11\u4e00\u4e2a\uff0c\u4f1a\u4e25\u91cd\u7684\u5f71\u54cd\u5230\u540e\u7ee7\u7684\u65b0\u8fde\u63a5\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c31\u6709\u5fc5\u8981\u8c03\u6574\u4e0bLinux\u7684TCP\u5185\u6838\u53c2\u6570\uff0c\u8ba9\u7cfb\u7edf\u66f4\u5feb\u7684\u91ca\u653eTIME_WAIT\u8fde\u63a5\u3002 \u7f16\u8f91\u914d\u7f6e\u6587\u4ef6:\/etc\/sysctl.conf\uff0c\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u52a0\u5165\u4e0b\u9762\u7684\u51e0\u884c\u5185\u5bb9\uff1a #&nbsp;vim&nbsp;\/etc\/sysctl.conf net.ipv4.tcp_syncookies=&nbsp;1 net.ipv4.tcp_tw_reuse=&nbsp;1 net.ipv4.tcp_tw_recycle=&nbsp;1 net.ipv4.tcp_fin_timeout=&nbsp;30 \u767b\u5f55\u540e\u590d\u5236 \u8f93\u5165\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u8ba9\u5185\u6838\u53c2\u6570\u751f\u6548\uff1a #&nbsp;sysctl-p [&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-16696","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16696","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=16696"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16696\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}