用 tail -f 实时查看日志文件
日志一有新行立刻滚动显示,排查服务问题最常用的命令。
运行 tail -f /var/log/syslog,先显示最后 10 行并持续滚动新内容,按 Ctrl+C 退出。
01命令
命令
$ tail -f /var/log/syslog02作用说明
tail -f 从文件末尾开始,有新内容写入就立即打印,可以实时看到服务的输出。加 -n 50 可以从更多历史行开始;日志会轮转时用大写的 tail -F,它会按文件名重新打开新文件。配合 grep 过滤,例如 tail -f /var/log/syslog | grep --line-buffered sshd,只看相关日志。
03风险
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- tail(1) — Ubuntu manpage依据: Ubuntu manpage describes --follow (-f) and -F, which follows the file by name and handles rotation
- tail(1) — man7.org依据: Man page documents tail's -f option to follow a file as it grows and -n to select the number of lines
最后验证: 2026-09-19 · 分类: 文件与文件夹