{"id":211602,"date":"2023-12-22T10:42:28","date_gmt":"2023-12-22T02:42:28","guid":{"rendered":"https:\/\/www.idc.net\/help\/211602\/"},"modified":"2023-12-22T10:42:28","modified_gmt":"2023-12-22T02:42:28","slug":"linux-%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%99%90%e5%88%b6%e6%af%8f%e4%b8%aa%e7%94%a8%e6%88%b7%e7%9a%84%e7%99%bb%e5%bd%95%e6%ac%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/211602\/","title":{"rendered":"Linux \u670d\u52a1\u5668\u5b89\u5168\u7b56\u7565\u6280\u5de7\uff1a\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570"},"content":{"rendered":"<p><title id=\"linux-%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%99%90%e5%88%b6%e6%af%8f%e4%b8%aa%e7%94%a8%e6%88%b7%e7%9a%84%e7%99%bb%e5%bd%95%e6%ac%a1%e6%95%b0-BcYLcMIoVN\">Linux \u670d\u52a1\u5668\u5b89\u5168\u7b56\u7565\u6280\u5de7\uff1a\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570<\/title><\/p>\n<h1 id=\"linux-%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%89%e5%85%a8%e7%ad%96%e7%95%a5%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%99%90%e5%88%b6%e6%af%8f%e4%b8%aa%e7%94%a8%e6%88%b7%e7%9a%84%e7%99%bb%e5%bd%95%e6%ac%a1%e6%95%b0-BcYLcMIoVN\">Linux \u670d\u52a1\u5668\u5b89\u5168\u7b56\u7565\u6280\u5de7\uff1a\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570<\/h1>\n<p>\u5728Linux\u670d\u52a1\u5668\u4e0a\uff0c\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570\u662f\u4e00\u79cd\u91cd\u8981\u7684\u5b89\u5168\u7b56\u7565\u3002\u901a\u8fc7\u9650\u5236\u767b\u5f55\u6b21\u6570\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u6076\u610f\u7528\u6237\u5c1d\u8bd5\u66b4\u529b\u7834\u89e3\u5bc6\u7801\u6216\u8fdb\u884c\u5176\u4ed6\u672a\u7ecf\u6388\u6743\u7684\u6d3b\u52a8\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u670d\u52a1\u5668\u4e0a\u5b9e\u65bd\u8fd9\u4e00\u5b89\u5168\u7b56\u7565\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8pam%e6%a8%a1%e5%9d%97%e9%99%90%e5%88%b6%e7%99%bb%e5%bd%95%e6%ac%a1%e6%95%b0-BcYLcMIoVN\">\u4f7f\u7528PAM\u6a21\u5757\u9650\u5236\u767b\u5f55\u6b21\u6570<\/h2>\n<p>PAM\uff08Pluggable Authentication Modules\uff09\u662fLinux\u7cfb\u7edf\u4e2d\u7528\u4e8e\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u7684\u6a21\u5757\u5316\u6846\u67b6\u3002\u901a\u8fc7\u914d\u7f6ePAM\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6253\u5f00PAM\u914d\u7f6e\u6587\u4ef6<code>\/etc\/pam.d\/sshd<\/code>\uff1a<\/p>\n<pre><code>sudo vi \/etc\/pam.d\/sshd\n<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre><code>auth required pam_tally2.so deny=3 unlock_time=1800\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570\u4e3a3\u6b21\u3002\u5982\u679c\u7528\u6237\u8fde\u7eed3\u6b21\u767b\u5f55\u5931\u8d25\uff0c\u5c06\u4f1a\u88ab\u9501\u5b9a1800\u79d2\uff0830\u5206\u949f\uff09\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8fail2ban%e5%b7%a5%e5%85%b7%e9%99%90%e5%88%b6%e7%99%bb%e5%bd%95%e6%ac%a1%e6%95%b0-BcYLcMIoVN\">\u4f7f\u7528fail2ban\u5de5\u5177\u9650\u5236\u767b\u5f55\u6b21\u6570<\/h2>\n<p>fail2ban\u662f\u4e00\u6b3e\u7528\u4e8e\u9632\u6b62\u66b4\u529b\u7834\u89e3\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u76d1\u63a7\u7cfb\u7edf\u65e5\u5fd7\u5e76\u6839\u636e\u9884\u5b9a\u4e49\u7684\u89c4\u5219\u6765\u9650\u5236\u767b\u5f55\u6b21\u6570\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5b89\u88c5fail2ban\uff1a<\/p>\n<pre><code>sudo apt-get install fail2ban\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u7f16\u8f91fail2ban\u914d\u7f6e\u6587\u4ef6<code>\/etc\/fail2ban\/jail.local<\/code>\uff1a<\/p>\n<pre><code>sudo vi \/etc\/fail2ban\/jail.local\n<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre><code>[sshd]\nenabled = true\nport = ssh\nfilter = sshd\nlogpath = \/var\/log\/auth.log\nmaxretry = 3\nbantime = 1800\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570\u4e3a3\u6b21\u3002\u5982\u679c\u7528\u6237\u8fde\u7eed3\u6b21\u767b\u5f55\u5931\u8d25\uff0c\u5c06\u4f1a\u88ab\u7981\u6b62\u767b\u5f551800\u79d2\uff0830\u5206\u949f\uff09\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e7%99%bb%e5%bd%95%e5%a4%b1%e8%b4%a5%e8%ae%a1%e6%95%b0%e5%99%a8%e8%84%9a%e6%9c%ac%e9%99%90%e5%88%b6%e7%99%bb%e5%bd%95%e6%ac%a1%e6%95%b0-BcYLcMIoVN\">\u4f7f\u7528\u767b\u5f55\u5931\u8d25\u8ba1\u6570\u5668\u811a\u672c\u9650\u5236\u767b\u5f55\u6b21\u6570<\/h2>\n<p>\u9664\u4e86\u4f7f\u7528PAM\u6a21\u5757\u548cfail2ban\u5de5\u5177\uff0c\u8fd8\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u7684\u811a\u672c\u6765\u5b9e\u73b0\u9650\u5236\u767b\u5f55\u6b21\u6570\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<pre><code>#!\/bin\/bash\n\nMAX_LOGIN_ATTEMPTS=3\nBAN_TIME=1800\n\nFAILED_LOGIN_COUNT=$(grep \"Failed password\" \/var\/log\/auth.log | wc -l)\n\nif [ $FAILED_LOGIN_COUNT -ge $MAX_LOGIN_ATTEMPTS ]; then\n    echo \"Too many failed login attempts. User is banned for $BAN_TIME seconds.\"\n    iptables -A INPUT -p tcp --dport ssh -m state --state NEW -m recent --set --name SSH\n    iptables -A INPUT -p tcp --dport ssh -m state --state NEW -m recent --update --seconds $BAN_TIME --hitcount $MAX_LOGIN_ATTEMPTS --rttl --name SSH -j DROP\nfi\n<\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0a\u811a\u672c\u4fdd\u5b58\u4e3a<code>login_attempts.sh<\/code>\uff0c\u5e76\u6dfb\u52a0\u6267\u884c\u6743\u9650\uff1a<\/p>\n<pre><code>chmod +x login_attempts.sh\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5c06\u811a\u672c\u6dfb\u52a0\u5230<code>\/etc\/crontab<\/code>\u6587\u4ef6\u4e2d\uff0c\u4ee5\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\uff1a<\/p>\n<pre><code>* * * * * root \/path\/to\/login_attempts.sh\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u6bcf\u5206\u949f\u68c0\u67e5\u4e00\u6b21\u7cfb\u7edf\u65e5\u5fd7\u4e2d\u7684\u767b\u5f55\u5931\u8d25\u6b21\u6570\uff0c\u5982\u679c\u8d85\u8fc7\u8bbe\u5b9a\u7684\u9608\u503c\uff0c\u5219\u7981\u6b62\u7528\u6237\u767b\u5f55\u4e00\u6bb5\u65f6\u95f4\u3002<\/p>\n<h2 id=\"%e6%80%bb%e7%bb%93-BcYLcMIoVN\">\u603b\u7ed3<\/h2>\n<p>\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570\u662f\u4fdd\u62a4Linux\u670d\u52a1\u5668\u5b89\u5168\u7684\u91cd\u8981\u7b56\u7565\u4e4b\u4e00\u3002\u901a\u8fc7\u4f7f\u7528PAM\u6a21\u5757\u3001fail2ban\u5de5\u5177\u6216\u81ea\u5b9a\u4e49\u811a\u672c\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62\u6076\u610f\u7528\u6237\u7684\u66b4\u529b\u7834\u89e3\u884c\u4e3a\u3002\u8bf7\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u4fdd\u62a4\u60a8\u7684\u670d\u52a1\u5668\u3002<\/p>\n<h3 id=\"%e9%a6%99%e6%b8%af%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%a6%96%e9%80%89%e5%90%8e%e6%b5%aa%e4%ba%91-BcYLcMIoVN\">\u9999\u6e2f\u670d\u52a1\u5668\u9996\u9009\u540e\u6d6a\u4e91<\/h3>\n<p>\u540e\u6d6a\u4e91\u63d0\u4f9b\u9ad8\u6027\u80fd\u7684\u9999\u6e2f\u670d\u52a1\u5668\uff0c\u4e3a\u60a8\u7684\u4e1a\u52a1\u63d0\u4f9b\u7a33\u5b9a\u53ef\u9760\u7684\u6258\u7ba1\u73af\u5883\u3002\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u8bbf\u95ee<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux \u670d\u52a1\u5668\u5b89\u5168\u7b56\u7565\u6280\u5de7\uff1a\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570 Linux \u670d\u52a1\u5668\u5b89\u5168\u7b56\u7565\u6280\u5de7\uff1a\u9650\u5236\u6bcf\u4e2a\u7528\u6237\u7684\u767b\u5f55\u6b21\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-211602","post","type-post","status-publish","format-standard","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/211602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=211602"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/211602\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=211602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=211602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=211602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}