0Pricing
Linux Command Line Mastery · 课时

高级日志记录与故障排查技术

探索系统日志,使用 `journalctl`,并采用系统化方法排查复杂问题。

高级日志记录与故障排查技术 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Logs Matter

When something goes wrong on a Linux system, logs are your best friend! They are like a digital diary that records events and activities.

System logs help you understand what happened, when it happened, and often, why it happened. This information is crucial for fixing problems and maintaining system health.

The `/var/log` Directory

Traditionally, most system logs are stored in the /var/log directory. You'll find many files here, each typically dedicated to a specific service or type of event.

  • auth.log: Records authentication attempts.
  • syslog: General system activity messages.
  • kern.log: Messages from the Linux kernel.

It's a treasure trove of information, but navigating it can be complex.

Viewing Classic Logs

For older log files or those not managed by systemd, you can use basic commands like cat, less, or tail to view their contents.

tail -f is especially useful for watching logs in real-time as new entries are added.

Try viewing the end of the syslog file (if available on your system):

tail /var/log/syslog

Modern Logging with `journalctl`

Modern Linux systems often use systemd, which includes its own logging system called the Journal. The command-line tool to interact with this journal is journalctl.

journalctl provides a centralized way to access logs from the kernel, services, and applications, making troubleshooting much more efficient than sifting through many files.

Your First `journalctl` Command

Running journalctl without any arguments will display all log messages collected by the systemd journal, starting from the oldest available entry.

It's a lot of information! You can scroll with arrow keys, Page Up/Down, or 'q' to quit. This is your comprehensive system log:

journalctl

Narrowing Down Log Entries

The real power of journalctl comes from its filtering capabilities. You can specify exactly what you want to see:

  • -u <unit>: Show logs for a specific systemd unit (e.g., a service like nginx or sshd).
  • -b: Show logs from the current boot.
  • --since "YYYY-MM-DD HH:MM:SS": Filter by a specific time or date.

Let's check logs specifically for the `ssh` service (sshd unit), if it's running:

journalctl -u sshd

Watching Logs in Real-time

Just like tail -f, journalctl can also display new log entries as they happen. This is incredibly useful when you're trying to debug an issue in real-time, for example, when starting a service.

Use the -f (follow) option to continuously monitor the journal for new messages:

journalctl -f

A Plan for Problem Solving

Effective troubleshooting isn't just about looking at logs; it's about having a systematic approach. Here's a common methodology:

  • Observe: What are the symptoms? What is failing or acting strangely?
  • Define: Clearly state the problem. What exactly is not working as expected?
  • Isolate: Determine where the problem might be (e.g., network, specific service, configuration, hardware).
  • Test: Propose a solution based on your findings and test it.
  • Document: Record what you did, what worked, and what didn't.

Where to Start Looking

When a problem arises, start with these basic checks:

  • Recent Changes: Did anything change recently? New software installed, configuration edits, system updates?
  • Service Status: Is the relevant service running? (Use systemctl status <service>).
  • Resource Usage: Are you out of disk space, memory, or CPU? (df -h, free -h, top).
  • Relevant Logs: Use journalctl -u <service> -b to check logs for the affected service since the last boot.

`journalctl` Filtering Challenge

You need to find log entries related to the nginx web server that occurred since yesterday. Which journalctl command(s) would be most appropriate?

Logs & Troubleshooting Recap

Great job! You've learned how critical system logs are for diagnosing issues on Linux.

We explored the traditional /var/log directory and, more importantly, mastered journalctl for viewing, filtering, and following modern systemd logs.

Remember to combine these powerful tools with a systematic troubleshooting approach to efficiently identify and resolve complex system problems!

常见问题解答

「高级日志记录与故障排查技术」课时是免费的吗?

是的 — 「高级日志记录与故障排查技术」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。

「高级日志记录与故障排查技术」这节课中我会学到什么?

探索系统日志,使用 `journalctl`,并采用系统化方法排查复杂问题。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Command Line Mastery 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「高级日志记录与故障排查技术」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?

能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 磁盘输入输出监控:`iostat`、`iotop`
  2. 内存与 CPU 性能工具
  3. 高级日志记录与故障排查技术
  4. 使用 strace 与 ltrace 跟踪系统调用
← 返回 Linux Command Line Mastery