{"id":68677,"date":"2025-05-18T13:23:49","date_gmt":"2025-05-18T05:23:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=68677"},"modified":"2025-05-18T13:23:49","modified_gmt":"2025-05-18T05:23:49","slug":"%e4%b8%80%e9%94%ae%e5%ae%8c%e6%88%90linux%e7%b3%bb%e7%bb%9f%e5%85%a8%e9%9d%a2%e5%b7%a1%e6%a3%80%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/68677\/","title":{"rendered":"\u4e00\u952e\u5b8c\u6210Linux\u7cfb\u7edf\u5168\u9762\u5de1\u68c0\u7684Shell\u811a\u672c\u5b9e\u6218"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728\u65e5\u5e38\u7684\u8fd0\u7ef4\u5de5\u4f5c\u4e2d\uff0c\u5feb\u901f\u83b7\u53d6\u670d\u52a1\u5668\u5f53\u524d\u72b6\u6001\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u5de5\u4f5c\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u7b80\u5355\u4f46\u529f\u80fd\u9f50\u5168\u7684Shell\u811a\u672c\uff0c\u5bf9Linux\u7cfb\u7edf\u8fdb\u884c\u81ea\u52a8\u5316\u5de1\u68c0\uff0c\u6db5\u76d6\u7cfb\u7edf\u6027\u80fd\u3001\u7f51\u7edc\u914d\u7f6e\u3001\u5b89\u5168\u72b6\u51b5\u3001\u670d\u52a1\u8fd0\u884c\u60c5\u51b5\u7b49\u591a\u4e2a\u7ef4\u5ea6\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u638c\u63e1\u670d\u52a1\u5668\u8fd0\u884c\u72b6\u6001\u7684\u6280\u672f\u4eba\u5458\u548c\u670d\u52a1\u5668\u7ba1\u7406\u5458\u3002<\/p>\n<p>\u5bf9\u4e8e\u90e8\u7f72\u5728\u7f8e\u56fd\u5730\u533a\u7684\u6570\u636e\u4e2d\u5fc3\u7684\u7528\u6237\uff0c\u8fd9\u7c7b\u811a\u672c\u5728\u6392\u67e5\u7f51\u7edc\u6545\u969c\u6216\u8d44\u6e90\u74f6\u9888\u65f6\u5c24\u5176\u91cd\u8981\u3002\u82e5\u60a8\u6b63\u5728\u8003\u8651\u5347\u7ea7\u670d\u52a1\u5668\u6027\u80fd\u6216\u66f4\u6362\u673a\u623f\uff0c\u4e0d\u59a8\u4e86\u89e3\u4e00\u4e0b\u9ad8\u6027\u80fd\u3001\u4f4e\u5ef6\u8fdf\u7684<strong>\u7f8e\u56fd\u670d\u52a1\u5668<\/strong>\u89e3\u51b3\u65b9\u6848\uff0c\u52a9\u60a8\u5b9e\u73b0\u7cfb\u7edf\u9ad8\u53ef\u7528\u8fd0\u7ef4\u76ee\u6807\u3002<\/p>\n<hr \/>\n<h2>\u4e00\u3001Linux\u5de1\u68c0\u8303\u56f4\u6982\u8ff0<\/h2>\n<p>\u672c\u811a\u672c\u6db5\u76d6\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u4e3b\u8981\u7684\u7cfb\u7edf\u4fe1\u606f\u6a21\u5757\uff1a<\/p>\n<ul>\n<li>\u7cfb\u7edf\u57fa\u672c\u4fe1\u606f\uff08\u4e3b\u673a\u540d\u3001\u64cd\u4f5c\u7cfb\u7edf\u3001\u5185\u6838\u7b49\uff09<\/li>\n<li>CPU\u8d1f\u8f7d\u548c\u6838\u5fc3\u4fe1\u606f<\/li>\n<li>\u5185\u5b58\u4f7f\u7528\u7387\u548c\u7a7a\u95f2\u60c5\u51b5<\/li>\n<li>\u78c1\u76d8\u6302\u8f7d\u70b9\u53ca\u5360\u7528\u60c5\u51b5<\/li>\n<li>\u7f51\u7edc\u63a5\u53e3\u4e0e\u8fde\u63a5\u72b6\u6001<\/li>\n<li>\u5173\u952e\u670d\u52a1\u8fd0\u884c\u72b6\u6001\uff08\u5982SSH\u3001MySQL\u3001Nginx\u7b49\uff09<\/li>\n<li>\u57fa\u7840\u5b89\u5168\u68c0\u67e5\uff08\u7528\u6237\u3001\u767b\u5f55\u7b56\u7565\u3001SSH\u914d\u7f6e\u7b49\uff09<\/li>\n<li>\u767b\u5f55\u4e0e\u7cfb\u7edf\u65e5\u5fd7<\/li>\n<li>\u6027\u80fd\u5206\u6790\u6392\u884c\uff08\u6309\u5185\u5b58\u548cCPU\u5360\u7528\uff09<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e8c\u3001Shell\u5de1\u68c0\u811a\u672c\u5185\u5bb9\u8be6\u89e3<\/h2>\n<p>\u811a\u672c\u4fdd\u5b58\u5728 <code>\/opt\/linux_inspection.sh<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\r\n\r\nLOG_FILE=\"\/opt\/\u5de1\u68c0\u62a5\u544a_$(date +%F_%T).log\"\r\n\r\necho \"\u7cfb\u7edf\u5de1\u68c0\u62a5\u544a\" &gt; $LOG_FILE\r\necho \"\u751f\u6210\u65f6\u95f4: $(date)\" &gt;&gt; $LOG_FILE\r\n\r\nlog() {\r\n    echo \"$1\" | tee -a $LOG_FILE\r\n}\r\n\r\nlog \"=================[ \u7cfb\u7edf\u57fa\u672c\u4fe1\u606f ]=================\"\r\nlog \"\u4e3b\u673a\u540d: $(hostname)\"\r\nlog \"\u64cd\u4f5c\u7cfb\u7edf: $(grep PRETTY_NAME \/etc\/os-release | cut -d= -f2 | tr -d '\\\"')\"\r\nlog \"\u5185\u6838\u7248\u672c: $(uname -r)\"\r\nlog \"\u7cfb\u7edf\u542f\u52a8\u65f6\u95f4: $(uptime -s)\"\r\nlog \"\u7cfb\u7edf\u8fd0\u884c\u65f6\u957f: $(uptime -p)\"\r\nlog \"\u5f53\u524d\u65f6\u95f4: $(date)\"\r\n\r\nlog \"=================[ CPU \u4fe1\u606f ]=================\"\r\nlog \"CPU \u578b\u53f7: $(lscpu | grep 'Model name' | awk -F: '{print $2}' | sed 's\/^ *\/\/')\"\r\nlog \"\u903b\u8f91\u6838\u6570: $(nproc)\"\r\nlog \"CPU \u4f7f\u7528\u7387: $(top -bn1 | grep '%Cpu' | awk '{print $2}')%\"\r\n\r\nlog \"=================[ \u5185\u5b58\u4f7f\u7528\u60c5\u51b5 ]=================\"\r\nfree -h | tee -a $LOG_FILE\r\nlog \"\u5185\u5b58\u4f7f\u7528\u5360\u6bd4: $(free | awk '\/Mem\/{printf(\"%.2f\", $3\/$2 * 100)}')%\"\r\n\r\nlog \"=================[ \u78c1\u76d8\u4f7f\u7528\u60c5\u51b5 ]=================\"\r\ndf -hT | tee -a $LOG_FILE\r\n\r\nlog \"=================[ \u7f51\u7edc\u914d\u7f6e ]=================\"\r\nlog \"\u672c\u5730 IP: $(hostname -I)\"\r\nlog \"\u9ed8\u8ba4\u7f51\u5173: $(ip route | grep default | awk '{print $3}')\"\r\nifconfig | tee -a $LOG_FILE\r\nss -tunlp | tee -a $LOG_FILE\r\n\r\nlog \"=================[ \u670d\u52a1\u72b6\u6001 ]=================\"\r\nfor service in sshd nginx mysqld; do\r\n    systemctl is-active $service &amp;&gt;\/dev\/null &amp;&amp; status=\"\u8fd0\u884c\u4e2d\" || status=\"\u672a\u8fd0\u884c\"\r\n    log \"$service \u670d\u52a1\u72b6\u6001: $status\"\r\ndone\r\n\r\nlog \"=================[ \u5b89\u5168\u5ba1\u8ba1 ]=================\"\r\ngrep -E \"^#?PermitRootLogin|^#?PasswordAuthentication\" \/etc\/ssh\/sshd_config | tee -a $LOG_FILE\r\nlog \"\u666e\u901a\u7528\u6237\u5217\u8868:\"\r\nawk -F: '{if ($3 &gt;= 1000) print $1}' \/etc\/passwd | tee -a $LOG_FILE\r\n\r\nlog \"=================[ \u767b\u5f55\u4fe1\u606f ]=================\"\r\nwho | tee -a $LOG_FILE\r\nlast -a | head -10 | tee -a $LOG_FILE\r\n\r\nlog \"=================[ \u7cfb\u7edf\u65e5\u5fd7\u6458\u8981 ]=================\"\r\ngrep \"Failed password\" \/var\/log\/auth.log | tail -10 | tee -a $LOG_FILE || log \"\u672a\u68c0\u6d4b\u5230\u767b\u5f55\u5931\u8d25\u8bb0\u5f55\"\r\n\r\nlog \"=================[ \u6027\u80fd\u6392\u884c ]=================\"\r\nlog \"\u5185\u5b58\u5360\u7528 Top 5:\"\r\nps aux --sort=-%mem | head -6 | tee -a $LOG_FILE\r\nlog \"CPU \u5360\u7528 Top 5:\"\r\nps aux --sort=-%cpu | head -6 | tee -a $LOG_FILE\r\n\r\nlog \"============= \u5de1\u68c0\u5b8c\u6210\uff0c\u62a5\u544a\u4fdd\u5b58\u5728: $LOG_FILE =============\"\r\n<\/code><\/pre>\n<hr \/>\n<h2>\u4e09\u3001\u5982\u4f55\u4f7f\u7528\u8be5\u811a\u672c<\/h2>\n<ol>\n<li><strong>\u8d4b\u4e88\u6267\u884c\u6743\u9650\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-bash\">chmod +x \/opt\/linux_inspection.sh\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u624b\u52a8\u8fd0\u884c\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-bash\">sh \/opt\/linux_inspection.sh\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u5b9a\u65f6\u6267\u884c\uff08\u63a8\u8350\uff09\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u4f7f\u7528 <code>crontab -e<\/code> \u6dfb\u52a0\u8ba1\u5212\u4efb\u52a1\uff0c\u4f8b\u5982\u6bcf\u5929\u65e9\u4e0a8\u70b9\u81ea\u52a8\u8fd0\u884c\uff1a<\/p>\n<pre><code class=\"language-bash\">0 8 * * * \/bin\/sh \/opt\/linux_inspection.sh\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>\u67e5\u770b\u65e5\u5fd7\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u811a\u672c\u6267\u884c\u5b8c\u6210\u540e\uff0c\u53ef\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u5de1\u68c0\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"language-bash\">less \/opt\/\u5de1\u68c0\u62a5\u544a_2025-01-10_12:41:26.log\r\n<\/code><\/pre>\n<hr \/>\n<h2>\u56db\u3001\u7ed3\u5408\u9ad8\u6027\u80fd\u7f8e\u56fd\u4e91\u670d\u52a1\u5668\uff0c\u63d0\u5347\u7cfb\u7edf\u8fd0\u7ef4\u6548\u7387<\/h2>\n<p>\u81ea\u52a8\u5316\u5de1\u68c0\u811a\u672c\u53ef\u5e2e\u52a9\u6280\u672f\u56e2\u961f\u9ad8\u6548\u638c\u63e1\u670d\u52a1\u5668\u8fd0\u884c\u72b6\u6001\uff0c\u82e5\u7ed3\u5408<a href=\"https:\/\/fwq.ai\/\"><strong>\u7f8e\u56fdvps<\/strong><\/a>\u6216<strong>\u7f8e\u56fd\u4e91\u670d\u52a1\u5668<\/strong>\u90e8\u7f72\uff0c\u53ef\u4ee5\u5728\u4fdd\u8bc1\u8bbf\u95ee\u901f\u5ea6\u7684\u540c\u65f6\u589e\u5f3a\u7cfb\u7edf\u7a33\u5b9a\u6027\u3002\u7279\u522b\u662f\u5bf9\u4e2d\u5927\u578b\u9879\u76ee\u800c\u8a00\uff0c\u9009\u7528\u9ad8\u5e26\u5bbd\u3001\u4f4e\u5ef6\u8fdf\u7684<strong>\u7f8e\u56fd\u670d\u52a1\u5668<\/strong>\u5c06\u6781\u5927\u63d0\u5347\u4e1a\u52a1\u53ef\u9760\u6027\uff0c\u4f53\u9a8c\u4f01\u4e1a\u7ea7\u8ba1\u7b97\u8d44\u6e90\u652f\u6301\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u5f15\u8a00 \u5728\u65e5\u5e38\u7684\u8fd0\u7ef4\u5de5\u4f5c\u4e2d\uff0c\u5feb\u901f\u83b7\u53d6\u670d\u52a1\u5668\u5f53\u524d\u72b6\u6001\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u5de5\u4f5c\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u7b80\u5355\u4f46\u529f\u80fd\u9f50\u5168\u7684Shell\u811a\u672c\uff0c\u5bf9Linux\u7cfb\u7edf\u8fdb\u884c\u81ea\u52a8\u5316\u5de1\u68c0\uff0c\u6db5\u76d6\u7cfb\u7edf\u6027\u80fd\u3001\u7f51\u7edc\u914d\u7f6e\u3001\u5b89\u5168\u72b6\u51b5\u3001\u670d\u52a1\u8fd0\u884c\u60c5\u51b5\u7b49\u591a\u4e2a\u7ef4\u5ea6\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u638c\u63e1\u670d\u52a1\u5668\u8fd0\u884c\u72b6\u6001\u7684\u6280\u672f\u4eba\u5458\u548c\u670d\u52a1\u5668\u7ba1\u7406\u5458\u3002 \u5bf9\u4e8e\u90e8\u7f72\u5728\u7f8e\u56fd\u5730\u533a\u7684\u6570\u636e\u4e2d\u5fc3\u7684\u7528\u6237\uff0c\u8fd9\u7c7b\u811a\u672c\u5728\u6392\u67e5\u7f51\u7edc\u6545\u969c\u6216\u8d44\u6e90\u74f6\u9888\u65f6\u5c24\u5176\u91cd\u8981\u3002\u82e5\u60a8\u6b63\u5728\u8003\u8651\u5347\u7ea7\u670d\u52a1\u5668\u6027\u80fd\u6216\u66f4\u6362\u673a\u623f\uff0c\u4e0d\u59a8\u4e86\u89e3\u4e00\u4e0b\u9ad8\u6027\u80fd\u3001\u4f4e\u5ef6\u8fdf\u7684\u7f8e\u56fd\u670d\u52a1\u5668\u89e3\u51b3\u65b9\u6848\uff0c\u52a9\u60a8\u5b9e\u73b0\u7cfb\u7edf\u9ad8\u53ef\u7528\u8fd0\u7ef4\u76ee\u6807\u3002 \u4e00\u3001Linux\u5de1\u68c0\u8303\u56f4\u6982\u8ff0 \u672c\u811a\u672c\u6db5\u76d6\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u4e3b\u8981\u7684\u7cfb\u7edf\u4fe1\u606f\u6a21\u5757\uff1a \u7cfb\u7edf\u57fa\u672c\u4fe1\u606f\uff08\u4e3b\u673a\u540d\u3001\u64cd\u4f5c\u7cfb\u7edf\u3001\u5185\u6838\u7b49\uff09 CPU\u8d1f\u8f7d\u548c\u6838\u5fc3\u4fe1\u606f \u5185\u5b58\u4f7f\u7528\u7387\u548c\u7a7a\u95f2\u60c5\u51b5 \u78c1\u76d8\u6302\u8f7d\u70b9\u53ca\u5360\u7528\u60c5\u51b5 \u7f51\u7edc\u63a5\u53e3\u4e0e\u8fde\u63a5\u72b6\u6001 \u5173\u952e\u670d\u52a1\u8fd0\u884c\u72b6\u6001\uff08\u5982SSH\u3001MySQL\u3001Nginx\u7b49\uff09 \u57fa\u7840\u5b89\u5168\u68c0\u67e5\uff08\u7528\u6237\u3001\u767b\u5f55\u7b56\u7565\u3001SSH\u914d\u7f6e\u7b49\uff09 \u767b\u5f55\u4e0e\u7cfb\u7edf\u65e5\u5fd7 \u6027\u80fd\u5206\u6790\u6392\u884c\uff08\u6309\u5185\u5b58\u548cCPU\u5360\u7528\uff09 \u4e8c\u3001Shell\u5de1\u68c0\u811a\u672c\u5185\u5bb9\u8be6\u89e3 \u811a\u672c\u4fdd\u5b58\u5728 \/opt\/linux_inspection.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a #!\/bin\/bash LOG_FILE=&#8221;\/opt\/\u5de1\u68c0\u62a5\u544a_$(date +%F_%T).log&#8221; echo &#8220;\u7cfb\u7edf\u5de1\u68c0\u62a5\u544a&#8221; &gt; $LOG_FILE echo &#8220;\u751f\u6210\u65f6\u95f4: $(date)&#8221; &gt;&gt; $LOG_FILE log() { echo &#8220;$1&#8221; | tee -a $LOG_FILE } log &#8220;=================[ \u7cfb\u7edf\u57fa\u672c\u4fe1\u606f ]=================&#8221; log &#8220;\u4e3b\u673a\u540d: $(hostname)&#8221; log &#8220;\u64cd\u4f5c\u7cfb\u7edf: $(grep PRETTY_NAME \/etc\/os-release | cut -d= -f2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-68677","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/68677","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=68677"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/68677\/revisions"}],"predecessor-version":[{"id":68678,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/68677\/revisions\/68678"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=68677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=68677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=68677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}