Linux Journalctl命令詳解
Linux提供了一個(gè)強(qiáng)大的日志系統(tǒng),它可以跟蹤和記錄系統(tǒng)的各種活動(dòng)。在這個(gè)系統(tǒng)中,journalctl
是一個(gè)非常重要的工具,用于查詢和操作由systemd進(jìn)程管理的日志。
本文將深入探討journalctl
命令,介紹其基本使用、高級(jí)選項(xiàng)及示例等內(nèi)容。
1. Journalctl簡(jiǎn)介
Systemd是Linux發(fā)行版的初始化系統(tǒng),負(fù)責(zé)啟動(dòng)系統(tǒng)后的所有服務(wù),并監(jiān)視它們?cè)谙到y(tǒng)運(yùn)行期間的狀態(tài)。Journal是Systemd的一部分,主要負(fù)責(zé)收集和存儲(chǔ)日志數(shù)據(jù)。
journalctl
是Journal的主要接口,提供豐富的功能來(lái)檢索和顯示日志條目。它能從磁盤上的二進(jìn)制文件或者其他傳輸目標(biāo)(如syslog)獲取日志。
引用自Man page of journalctl:
journalctl may be used to query the contents of the systemd(1) journal as written by systemd-journald.service(8).
“journalctl可以用來(lái)查詢由systemd-journald.service(8)寫入的systemd(1)日志的內(nèi)容。”
2. Journalctl基礎(chǔ)使用
下面是一些基本的journalctl
命令。
-
查看所有日志:
(分頁(yè)輸出)journalctl
或者:
(不分頁(yè)輸出)journalctl --no-pager
-
按時(shí)間倒序查看所有日志:
journalctl -r
-
查看最新的10條日志:
journalctl -n 10
-
實(shí)時(shí)查看新添加的日志條目:
journalctl -f
3. 過(guò)濾日志條目
journalctl
提供了多種方式來(lái)過(guò)濾和查找特定的日志條目。
以下是一些過(guò)濾日志的例子:
根據(jù)服務(wù)名稱過(guò)濾:
journalctl -u nginx
根據(jù)進(jìn)程ID過(guò)濾:
journalctl _PID=2001
根據(jù)優(yōu)先級(jí)過(guò)濾(0-7,0表示最重要):
journalctl -p err -b
4. 時(shí)間戳和日志輪轉(zhuǎn)
對(duì)于大型系統(tǒng),日志文件可能會(huì)非常大。為了解決這個(gè)問(wèn)題,journalctl
提供了日志輪轉(zhuǎn)和時(shí)間戳功能。
日志輪轉(zhuǎn)是一個(gè)過(guò)程,其中舊的日志條目被刪除以釋放空間給新的日志條目。默認(rèn)情況下,Journal將根據(jù)配置在/var/log/journal中保存日志文件,并在磁盤空間不足時(shí)執(zhí)行日志輪轉(zhuǎn)。
時(shí)間戳允許你查找特定時(shí)間范圍內(nèi)的日志條目。例如:
journalctl --since="2021-07-01" --until="2021-07-31 03:00"
5. 高級(jí)應(yīng)用
盡管journalctl
提供了豐富的功能,但在某些情況下,你可能需要更高級(jí)的工具或技術(shù)。例如,你可能需要將日志發(fā)送到遠(yuǎn)程服務(wù)器,或者在多個(gè)系統(tǒng)上集中處理日志。
對(duì)于這些需求,你可以考慮使用像Logstash、Graylog或Fluentd這樣的日志聚合工具,它們可以與journalctl
結(jié)合使用,提供更強(qiáng)大的功能。
6. journalctl --help指令文檔
英文
journalctl [OPTIONS...] [MATCHES...]
Query the journal.
Options:
--system Show the system journal
--user Show the user journal for the current user
-M --machine=CONTAINER Operate on local container
-S --since=DATE Show entries not older than the specified date
-U --until=DATE Show entries not newer than the specified date
-c --cursor=CURSOR Show entries starting at the specified cursor
--after-cursor=CURSOR Show entries after the specified cursor
--show-cursor Print the cursor after all the entries
--cursor-file=FILE Show entries after cursor in FILE and update FILE
-b --boot[=ID] Show current boot or the specified boot
--list-boots Show terse information about recorded boots
-k --dmesg Show kernel message log from the current boot
-u --unit=UNIT Show logs from the specified unit
--user-unit=UNIT Show logs from the specified user unit
-t --identifier=STRING Show entries with the specified syslog identifier
-p --priority=RANGE Show entries with the specified priority
--facility=FACILITY... Show entries with the specified facilities
-g --grep=PATTERN Show entries with MESSAGE matching PATTERN
--case-sensitive[=BOOL] Force case sensitive or insenstive matching
-e --pager-end Immediately jump to the end in the pager
-f --follow Follow the journal
-n --lines[=INTEGER] Number of journal entries to show
--no-tail Show all lines, even in follow mode
-r --reverse Show the newest entries first
-o --output=STRING Change journal output mode (short, short-precise,
short-iso, short-iso-precise, short-full,
short-monotonic, short-unix, verbose, export,
json, json-pretty, json-sse, json-seq, cat,
with-unit)
--output-fields=LIST Select fields to print in verbose/export/json modes
--utc Express time in Coordinated Universal Time (UTC)
-x --catalog Add message explanations where available
--no-full Ellipsize fields
-a --all Show all fields, including long and unprintable
-q --quiet Do not show info messages and privilege warning
--no-pager Do not pipe output into a pager
--no-hostname Suppress output of hostname field
-m --merge Show entries from all available journals
-D --directory=PATH Show journal files from directory
--file=PATH Show journal file
--root=ROOT Operate on files below a root directory
--namespace=NAMESPACE Show journal data from specified namespace
--interval=TIME Time interval for changing the FSS sealing key
--verify-key=KEY Specify FSS verification key
--force Override of the FSS key pair with --setup-keys
Commands:
-h --help Show this help text
--version Show package version
-N --fields List all field names currently used
-F --field=FIELD List all values that a specified field takes
--disk-usage Show total disk usage of all journal files
--vacuum-size=BYTES Reduce disk usage below specified size
--vacuum-files=INT Leave only the specified number of journal files
--vacuum-time=TIME Remove journal files older than specified time
--verify Verify journal file consistency
--sync Synchronize unwritten journal messages to disk
--relinquish-var Stop logging to disk, log to temporary file system
--smart-relinquish-var Similar, but NOP if log directory is on root mount
--flush Flush all journal data from /run into /var
--rotate Request immediate rotation of the journal files
--header Show journal header information
--list-catalog Show all message IDs in the catalog
--dump-catalog Show entries in the message catalog
--update-catalog Update the message catalog database
--setup-keys Generate a new FSS key pair
See the journalctl(1) man page for details.
中文
journalctl [選項(xiàng)...] [匹配項(xiàng)...]
查詢?nèi)罩尽?
選項(xiàng):
--system 顯示系統(tǒng)日志
--user 顯示當(dāng)前用戶的用戶日志
-M --machine=CONTAINER 對(duì)本地容器進(jìn)行操作
-S --since=DATE 顯示不早于指定日期的條目
-U --until=DATE 顯示不晚于指定日期的條目
-c --cursor=CURSOR 從指定的游標(biāo)開(kāi)始顯示條目
--after-cursor=CURSOR 顯示指定游標(biāo)之后的條目
--show-cursor 在所有條目后打印游標(biāo)
--cursor-file=FILE 顯示文件中游標(biāo)后的條目并更新文件
-b --boot[=ID] 顯示當(dāng)前啟動(dòng)或指定的啟動(dòng)
--list-boots 顯示有關(guān)記錄啟動(dòng)的簡(jiǎn)潔信息
-k --dmesg 顯示當(dāng)前啟動(dòng)的內(nèi)核消息日志
-u --unit=UNIT 顯示指定單元的日志
--user-unit=UNIT 顯示指定用戶單元的日志
-t --identifier=STRING 顯示具有指定syslog標(biāo)識(shí)符的條目
-p --priority=RANGE 顯示具有指定優(yōu)先級(jí)的條目
--facility=FACILITY... 顯示具有指定設(shè)施的條目
-g --grep=PATTERN 顯示與PATTERN匹配的MESSAGE的條目
--case-sensitive[=BOOL] 強(qiáng)制進(jìn)行大小寫敏感或不敏感的匹配
-e --pager-end 在分頁(yè)器中立即跳到末尾
-f --follow 跟蹤日志
-n --lines[=INTEGER] 要顯示的日志條目數(shù)量
--no-tail 即使在跟蹤模式下也顯示所有行
-r --reverse 先顯示最新的條目
-o --output=STRING 改變?nèi)罩据敵瞿J?(short, short-precise,
short-iso, short-iso-precise, short-full,
short-monotonic, short-unix, verbose, export,
json, json-pretty, json-sse, json-seq, cat,
with-unit)
--output-fields=LIST 在verbose/export/json模式下選擇要打印的字段
--utc 以協(xié)調(diào)世界時(shí)(UTC)表示時(shí)間
-x --catalog 在可用的地方添加消息解釋
--no-full 縮略字段
-a --all 顯示所有字段,包括長(zhǎng)和不可打印的
-q --quiet 不顯示信息消息和權(quán)限警告
--no-pager 不將輸出管道輸出到分頁(yè)器
--no-hostname 抑制主機(jī)名字段的輸出
-m --merge 顯示所有可用日志的條目
-D --directory=PATH 顯示來(lái)自目錄的日志文件
--file=PATH 顯示日志文件
--root=ROOT 在根目錄下操作文件
--namespace=NAMESPACE 顯示指定命名空間的日志數(shù)據(jù)
--interval=TIME 更改FSS密封鍵的時(shí)間間隔
--verify-key=KEY 指定FSS驗(yàn)證鍵
--force 使用--setup-keys覆蓋FSS密鑰對(duì)
命令:
-h --help 顯示此幫助文本
--version 顯示包版本
-N --fields 列出當(dāng)前使用的所有字段名稱
-F --field=FIELD 列出指定字段采取的所有值
--disk-usage 顯示所有日志文件的總磁盤使用量
--vacuum-size=BYTES 將磁盤使用量減少到指定大小以下
--vacuum-files=INT 只保留指定數(shù)量的日志文件
--vacuum-time=TIME 刪除早于指定時(shí)間的日志文件
--verify 驗(yàn)證日志文件的一致性
--sync 將未寫入的日志消息同步到磁盤
--relinquish-var 停止記錄到磁盤,記錄到臨時(shí)文件系統(tǒng)
--smart-relinquish-var 類似,但如果日志目錄在根掛載上,則無(wú)操作
--flush 將所有日志數(shù)據(jù)從 /run 刷新到 /var
--rotate 請(qǐng)求立即旋轉(zhuǎn)日志文件
--header 顯示日志頭信息
--list-catalog 在目錄中顯示所有消息ID
--dump-catalog 顯示消息目錄中的條目
--update-catalog 更新消息目錄數(shù)據(jù)庫(kù)
--setup-keys 生成新的FSS密鑰對(duì)
有關(guān)詳細(xì)信息,請(qǐng)參閱 journalctl(1) 手冊(cè)頁(yè)。
7. 補(bǔ)充
清空所有日志
清除所有的系統(tǒng)日志,可使用 journalctl
的 --vacuum-time
或 --vacuum-size
選項(xiàng)。
-
清除所有日志:
sudo journalctl --vacuum-time=1s
這將會(huì)刪除所有時(shí)間戳早于現(xiàn)在的日志條目,基本上等同于清空所有日志。
journalctl --vacuum-time
命令通常接受一個(gè)相對(duì)時(shí)間值,如 “1year”, “2months”, “3weeks”, “4days”, “1s” 等。這個(gè)命令會(huì)刪除所有在指定時(shí)間長(zhǎng)度之前的日志。 -
清除超過(guò)特定大小的日志:
journalctl --vacuum-size=1
這將刪除所有日志,直到系統(tǒng)日志的總大小降到1(單位為BYTE)。
可以使用
journalctl --disk-usage
查看日志占用空間大小。 -
如果以上兩個(gè)都沒(méi)有效果,可以采用暴力方式直接刪除日志文件。
sudo systemctl stop systemd-journald sudo rm -rf /var/log/journal/* sudo systemctl start systemd-journald
暴力刪除有風(fēng)險(xiǎn),使用需謹(jǐn)慎。
過(guò)濾掉某個(gè)服務(wù)日志
比如systemd服務(wù)日志太多太煩雜,可以過(guò)濾掉它:
journalctl | grep -v systemd
過(guò)濾前:
過(guò)濾后:
注意事項(xiàng)
journal日志不會(huì)將程序輸出的空行顯示,日志會(huì)被壓縮得滿滿當(dāng)當(dāng)。
journal日志不會(huì)自動(dòng)持久化,重啟系統(tǒng)后,歷史日志將被清除。可參考journal日志持久化配置。
參考文章:systemd journalctl日志持久化配置(systemd日志)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-719314.html
總結(jié)
journalctl
是一個(gè)強(qiáng)大而靈活的工具,可以幫助你管理和查詢Linux系統(tǒng)的日志。通過(guò)學(xué)習(xí)和理解journalctl
的各種功能和選項(xiàng),你將能夠更有效地管理你的系統(tǒng),并在出現(xiàn)問(wèn)題時(shí)快速找到原因。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-719314.html
參考資料
- Man page of journalctl
- Mastering systemd: Using journalctl for Troubleshooting
- An introduction to systemd’s journalctl utility
- journalctl 日志問(wèn)題
到了這里,關(guān)于Linux journalctl命令詳解(journalctl指令)(systemd服務(wù)默認(rèn)日志管理工具)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!