{"id":210678,"date":"2026-02-11T19:55:08","date_gmt":"2026-02-11T11:55:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/?p=210678"},"modified":"2026-02-05T19:56:25","modified_gmt":"2026-02-05T11:56:25","slug":"ubuntu-%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e4%b8%8e%e5%88%86%e6%9e%90%e7%b3%bb%e7%bb%9f%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/210678\/","title":{"rendered":"Ubuntu \u670d\u52a1\u5668\u5982\u4f55\u67e5\u770b\u4e0e\u5206\u6790\u7cfb\u7edf\u65e5\u5fd7"},"content":{"rendered":"<p dir=\"auto\">\u5728 Ubuntu \u670d\u52a1\u5668\uff08\u5c24\u5176\u662f 20.04 \u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u4e2d\uff0c\u7cfb\u7edf\u65e5\u5fd7\u4e3b\u8981\u7531 <strong>systemd-journald<\/strong> \u7ba1\u7406\uff0c\u4f7f\u7528 <strong>journalctl<\/strong> \u547d\u4ee4\u67e5\u770b\u548c\u5206\u6790\u65e5\u5fd7\u5df2\u6210\u4e3a\u4e3b\u6d41\u65b9\u5f0f\u3002\u540c\u65f6\uff0c<strong>rsyslog<\/strong> \u4ecd\u8d1f\u8d23\u5c06\u90e8\u5206\u65e5\u5fd7\u5199\u5165\u4f20\u7edf\u6587\u672c\u6587\u4ef6\uff08\u5982 \/var\/log\/syslog\uff09\uff0c\u5f62\u6210\u53cc\u8f68\u5e76\u5b58\u7684\u5c40\u9762\u3002<\/p>\n<p dir=\"auto\">\u672c\u6587\u91cd\u70b9\u8bb2\u89e3\u751f\u4ea7\u73af\u5883\u4e2d\u6700\u5b9e\u7528\u3001\u6700\u9ad8\u6548\u7684\u67e5\u770b\u4e0e\u5206\u6790\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u6545\u969c\u3001\u76d1\u63a7\u5b89\u5168\u4e8b\u4ef6\u3001\u6392\u67e5\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<h3 dir=\"auto\">\u4e00\u3001Ubuntu \u65e5\u5fd7\u4f53\u7cfb\u5feb\u901f\u6982\u89c8<\/h3>\n<ul dir=\"auto\">\n<li><strong>journald<\/strong>\uff08systemd-journald\uff09\uff1a\u73b0\u4ee3\u6838\u5fc3\uff0c\u65e5\u5fd7\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u5b58\u50a8\u5728 \/var\/log\/journal\/\uff08\u6301\u4e45\u5316\uff09\u6216 \/run\/log\/journal\/\uff08\u5185\u5b58\u4e2d\uff0c\u91cd\u542f\u4e22\u5931\uff09\u3002\u652f\u6301\u7ed3\u6784\u5316\u67e5\u8be2\u3001\u5b9e\u65f6\u8ddf\u8e2a\u3001\u6309\u670d\u52a1\/\u65f6\u95f4\/\u4f18\u5148\u7ea7\u8fc7\u6ee4\u3002<\/li>\n<li><strong>rsyslog<\/strong>\uff1a\u4f20\u7edf syslog \u5b88\u62a4\u8fdb\u7a0b\uff0c\u5c06\u65e5\u5fd7\u8f6c\u53d1\u5230 \/var\/log\/ \u4e0b\u7684\u6587\u672c\u6587\u4ef6\uff08\u5982 syslog\u3001auth.log\u3001kern.log\uff09\uff0c\u4fbf\u4e8e grep \u7b49\u5de5\u5177\u5904\u7406\u3002<\/li>\n<li><strong>\u5173\u7cfb<\/strong>\uff1ajournald \u6536\u96c6\u6240\u6709\u65e5\u5fd7\uff0crsyslog \u53ef\u4ece journald \u5bfc\u5165\uff08\u9ed8\u8ba4\u914d\u7f6e\u4e0b ForwardToSyslog=yes\uff09\uff0c\u6240\u4ee5\u5f88\u591a\u65e5\u5fd7\u5728\u4e24\u8fb9\u90fd\u80fd\u770b\u5230\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u751f\u4ea7\u73af\u5883\u4f18\u5148\u4f7f\u7528 <strong>journalctl<\/strong>\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u66f4\u5f3a\u3001\u67e5\u8be2\u66f4\u7075\u6d3b\u3001\u652f\u6301\u5b9e\u65f6\u8ddf\u8e2a\u3002<\/p>\n<h3 dir=\"auto\">\u4e8c\u3001journalctl \u6838\u5fc3\u7528\u6cd5\uff08\u6700\u5e38\u7528\u547d\u4ee4\uff09<\/h3>\n<ol dir=\"auto\">\n<li><strong>\u67e5\u770b\u5168\u90e8\u65e5\u5fd7\uff08\u4ece\u65e7\u5230\u65b0\uff09<\/strong> journalctl \uff08\u8f93\u51fa\u5f88\u957f\uff0c\u9ed8\u8ba4\u7528 less \u5206\u9875\uff0c\u6309 q \u9000\u51fa\uff09<\/li>\n<li><strong>\u5b9e\u65f6\u8ddf\u8e2a\u65e5\u5fd7\uff08\u7c7b\u4f3c tail -f\uff09<\/strong> journalctl -f \u2192 \u9002\u5408\u76d1\u63a7\u6b63\u5728\u53d1\u751f\u7684\u9519\u8bef\u6216\u670d\u52a1\u542f\u52a8\u8fc7\u7a0b\u3002<\/li>\n<li><strong>\u53ea\u770b\u5f53\u524d\u5f00\u673a\u4ee5\u6765\u7684\u65e5\u5fd7<\/strong> journalctl -b \u2192 \u6392\u67e5\u672c\u6b21\u542f\u52a8\u540e\u7684\u95ee\u9898\u6700\u5e38\u7528\u3002<\/li>\n<li><strong>\u67e5\u770b\u4e0a\u4e00\u6b21\/\u6307\u5b9a boot \u7684\u65e5\u5fd7<\/strong> journalctl -b -1\uff08\u4e0a\u4e00\u6b21\uff09 journalctl &#8211;list-boots\uff08\u5217\u51fa\u6240\u6709 boot ID\uff09 journalctl -b boot-id\uff08\u6307\u5b9a ID\uff09<\/li>\n<li><strong>\u6309\u670d\u52a1\u67e5\u770b\u65e5\u5fd7<\/strong> journalctl -u nginx.service journalctl -u mysql -f\uff08\u5b9e\u65f6\u8ddf\u8e2a MySQL\uff09 \u2192 \u8fd0\u7ef4\u4e2d\u6700\u5e38\u7528\uff1a\u51fa\u95ee\u9898\u5148 journalctl -u \u670d\u52a1\u540d -n 200 -xe\uff08\u6700\u8fd1 200 \u884c + \u9519\u8bef\u89e3\u91ca\uff09<\/li>\n<li><strong>\u6309\u65f6\u95f4\u8303\u56f4\u8fc7\u6ee4<\/strong> journalctl &#8211;since &#8220;2026-02-01&#8221; journalctl &#8211;since &#8220;2 hours ago&#8221; &#8211;until &#8220;now&#8221; journalctl &#8211;since &#8220;today&#8221; -u ssh<\/li>\n<li><strong>\u6309\u4f18\u5148\u7ea7\uff08\u4e25\u91cd\u7a0b\u5ea6\uff09\u8fc7\u6ee4<\/strong> journalctl -p err -b\uff08\u53ea\u770b error \u53ca\u4ee5\u4e0a\uff09 journalctl -p warning..emerg\uff08warning \u5230 emergency\uff09 \u2192 \u5feb\u901f\u5b9a\u4f4d\u4e25\u91cd\u95ee\u9898\u3002<\/li>\n<li><strong>\u7ed3\u5408 grep \u641c\u7d22\u5173\u952e\u5b57<\/strong> journalctl -u nginx | grep -i &#8220;error|failed&#8221; journalctl &#8211;since &#8220;1 hour ago&#8221; | grep &#8220;denied&#8221;<\/li>\n<li><strong>\u67e5\u770b\u5185\u6838\u65e5\u5fd7\uff08dmesg \u66ff\u4ee3\uff09<\/strong> journalctl -k\uff08\u53ea\u5185\u6838\uff09 journalctl -k -b\uff08\u672c\u6b21 boot \u5185\u6838\uff09<\/li>\n<\/ol>\n<h3 dir=\"auto\">\u4e09\u3001\u4f20\u7edf\u6587\u672c\u65e5\u5fd7\u67e5\u770b\uff08\/var\/log\/ \u4e0b\uff09<\/h3>\n<p dir=\"auto\">\u867d\u7136 journalctl \u66f4\u5f3a\u5927\uff0c\u4f46\u5f88\u591a\u8fd0\u7ef4\u4e60\u60ef\u4ecd\u770b\u6587\u672c\u6587\u4ef6\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\/var\/log\/syslog<\/strong>\uff1a\u7cfb\u7edf\u7efc\u5408\u65e5\u5fd7\uff08\u5927\u90e8\u5206\u4e8b\u4ef6\uff09 tail -n 100 \/var\/log\/syslog | grep ssh<\/li>\n<li><strong>\/var\/log\/auth.log<\/strong>\uff1a\u8ba4\u8bc1\u3001\u5b89\u5168\u76f8\u5173\uff08\u767b\u5f55\u5931\u8d25\u3001sudo \u4f7f\u7528\uff09 grep &#8220;Failed password&#8221; \/var\/log\/auth.log | tail -20\uff08\u66b4\u529b\u7834\u89e3\u5c1d\u8bd5\uff09<\/li>\n<li><strong>\/var\/log\/kern.log<\/strong>\uff1a\u5185\u6838\u6d88\u606f<\/li>\n<li><strong>\/var\/log\/dmesg<\/strong>\uff1a\u542f\u52a8\u65f6\u786c\u4ef6\/\u9a71\u52a8\u4fe1\u606f\uff08journalctl -k \u7b49\u4ef7\uff09<\/li>\n<\/ul>\n<p dir=\"auto\">\u67e5\u770b\u6280\u5de7\uff1a<\/p>\n<ul dir=\"auto\">\n<li>tail -f \/var\/log\/syslog\uff08\u5b9e\u65f6\uff09<\/li>\n<li>less +F \/var\/log\/auth.log\uff08\u7c7b\u4f3c tail -f\uff0c\u6309 Ctrl+C \u505c\u6b62\u8ddf\u8e2a\uff09<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u56db\u3001\u65e5\u5fd7\u5206\u6790\u5b9e\u7528\u6280\u5de7\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<ol dir=\"auto\">\n<li><strong>\u5feb\u901f\u6545\u969c\u5b9a\u4f4d\u9ec4\u91d1\u7ec4\u5408<\/strong>\n<ul dir=\"auto\">\n<li>journalctl -b -p err -xe\uff08\u672c\u6b21 boot \u9519\u8bef\u65e5\u5fd7 + \u89e3\u91ca\uff09<\/li>\n<li>journalctl -u \u5acc\u7591\u670d\u52a1 -n 300 -f\uff08\u6700\u8fd1 300 \u884c\u5b9e\u65f6\u8ddf\u8e2a\uff09<\/li>\n<li>journalctl &#8211;since &#8220;30 minutes ago&#8221; -p warning..\uff08\u6700\u8fd1\u534a\u5c0f\u65f6\u8b66\u544a\u4ee5\u4e0a\uff09<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u65e5\u5fd7\u4f53\u79ef\u7ba1\u7406\uff08\u9632\u6b62 \/var \u5360\u6ee1\uff09<\/strong>\n<ul dir=\"auto\">\n<li>\u67e5\u770b\u5360\u7528\uff1ajournalctl &#8211;disk-usage<\/li>\n<li>\u6e05\u7406\u65e7\u65e5\u5fd7\uff1ajournalctl &#8211;vacuum-time=30d\uff08\u4fdd\u7559 30 \u5929\uff09 journalctl &#8211;vacuum-size=2G\uff08\u9650\u5236\u603b\u5927\u5c0f 2GB\uff09<\/li>\n<li>\u6c38\u4e45\u8bbe\u7f6e\uff1a\u7f16\u8f91 \/etc\/systemd\/journald.conf SystemMaxUse=4G MaxRetentionSec=90day \u7136\u540e sudo systemctl restart systemd-journald<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5bfc\u51fa\u65e5\u5fd7\u4fbf\u4e8e\u5206\u6790<\/strong> journalctl -u nginx -o json &gt; nginx.json\uff08JSON \u683c\u5f0f\uff0c\u5582\u7ed9 ELK \u6216\u811a\u672c\uff09 journalctl -o short-iso\uff08\u5e26\u5b8c\u6574\u65f6\u95f4\u6233\uff09<\/li>\n<li><strong>\u76d1\u63a7\u66b4\u529b\u7834\u89e3\/\u5f02\u5e38\u767b\u5f55<\/strong> journalctl -u ssh | grep &#8220;Failed&#8221; \u6216 tail -f \/var\/log\/auth.log | grep sshd<\/li>\n<li><strong>rsyslog \u72b6\u6001\u68c0\u67e5<\/strong> sudo systemctl status rsyslog \u5982\u679c\u65e5\u5fd7\u6ca1\u5199\u5230 \/var\/log\/\uff0c\u68c0\u67e5 \/etc\/rsyslog.conf \u662f\u5426\u6709 imjournal \u6a21\u5757\u542f\u7528\u3002<\/li>\n<\/ol>\n<h3 dir=\"auto\">\u4e94\u3001\u603b\u7ed3\uff1a\u670d\u52a1\u5668\u65e5\u5fd7\u6392\u67e5\u5fc3\u6cd5<\/h3>\n<ul dir=\"auto\">\n<li>\u4f18\u5148 <strong>journalctl<\/strong>\uff1a\u529f\u80fd\u5168\u9762\u3001\u7ed3\u6784\u5316\u3001\u6613\u8fc7\u6ee4<\/li>\n<li>\u5b9e\u65f6\u95ee\u9898 \u2192 -f \/ -u \u670d\u52a1 -f<\/li>\n<li>\u5386\u53f2\u95ee\u9898 \u2192 -b \/ &#8211;since + -p err<\/li>\n<li>\u5b89\u5168\u4e8b\u4ef6 \u2192 auth.log \u6216 journalctl _COMM=sshd<\/li>\n<li>\u78c1\u76d8\u544a\u8b66 \u2192 \u5148 vacuum-time \/ vacuum-size \u6e05\u7406<\/li>\n<\/ul>\n<p dir=\"auto\">\u719f\u7ec3\u638c\u63e1 journalctl \u540e\uff0c\u4f60\u80fd\u628a\u201c\u670d\u52a1\u5668\u51fa\u95ee\u9898\u4e86\u770b\u65e5\u5fd7\u201d\u4ece\u534a\u5c0f\u65f6\u7f29\u77ed\u5230 2-5 \u5206\u949f\u3002\u5efa\u8bae\u628a\u5e38\u7528\u7ec4\u5408\u505a\u6210 alias \u6216\u5c0f\u811a\u672c\uff0c\u957f\u671f\u503c\u5b88\u65f6\u975e\u5e38\u7701\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Ubuntu \u670d\u52a1\u5668\uff08\u5c24\u5176\u662f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-210678","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/210678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=210678"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/210678\/revisions"}],"predecessor-version":[{"id":210679,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/210678\/revisions\/210679"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=210678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=210678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=210678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}