What if you could find the root cause of a system crash in seconds instead of hours?
Why System logs (/var/log) in Linux CLI? - Purpose & Use Cases
Imagine you are a system administrator trying to find out why a server crashed last night. You have to open multiple log files scattered in different folders, read through thousands of lines, and try to piece together what happened.
Manually searching through logs is slow and tiring. It's easy to miss important clues hidden in huge files. Mistakes happen when you overlook timestamps or mix up log entries. This can delay fixing problems and cause frustration.
Using system logs in /var/log lets you quickly access organized records of system events. Tools and scripts can filter, search, and summarize logs automatically, saving time and reducing errors.
cat /var/log/syslog | grep -i error | less
journalctl -p err -b
Accessing and analyzing /var/log empowers you to spot issues fast and keep systems running smoothly.
A web server suddenly stops responding. By checking /var/log/apache2/error.log, you find a configuration error and fix it before users notice.
Manual log checks are slow and error-prone.
/var/log stores important system event records.
Automated log tools help find and fix problems quickly.
Practice
/var/log/ directory on a Linux system?Solution
Step 1: Understand the role of /var/log/
The /var/log/ directory is designed to keep logs about system and application activities.Step 2: Differentiate from other directories
Other directories like /home store user files, /etc holds configs, and /tmp stores temporary files.Final Answer:
To store system and application logs recording events and errors -> Option DQuick Check:
/var/log/ = system logs [OK]
- Confusing /var/log with /etc for configs
- Thinking /var/log stores user documents
- Mixing up temporary files with logs
/var/log/syslog file?Solution
Step 1: Recall command functions
Thetailcommand shows the last lines of a file; by default, it shows the last 10 lines.Step 2: Compare options
headshows the first lines,cat | headalso shows first lines, andlessopens a pager for browsing but does not directly show last lines.Final Answer:
tail /var/log/syslog -> Option AQuick Check:
tail = last 10 lines [OK]
- Using head instead of tail for last lines
- Confusing less with tail output
- Piping cat to head for last lines
grep 'error' /var/log/syslog | wc -l?Solution
Step 1: Understand grep and wc -l
grep 'error' /var/log/syslogfilters lines containing 'error'.wc -lcounts the number of lines from that output.Step 2: Combine command effects
The pipeline counts how many lines in syslog have the word 'error'.Final Answer:
It shows the number of lines containing the word 'error' in /var/log/syslog -> Option BQuick Check:
grep + wc -l = count matching lines [OK]
- Thinking grep excludes matching lines
- Confusing wc -l with word count
- Assuming output shows lines, not count
tail -n 20 /var/log/auth.log but get an error: "tail: cannot open '/var/log/auth.log' for reading: Permission denied". What is the best way to fix this?Solution
Step 1: Identify permission issue
The error says permission denied, meaning the user lacks rights to read the file.Step 2: Use elevated privileges safely
Usingsudoruns the command as root, allowing access without changing file permissions.Final Answer:
Run the command with sudo:sudo tail -n 20 /var/log/auth.log-> Option AQuick Check:
Permission denied? Use sudo [OK]
- Changing permissions insecurely with chmod 777
- Copying files without permissions
- Using head instead of fixing permissions
/var/log/syslog in real-time and filter only lines containing the word 'failed'. Which command achieves this?Solution
Step 1: Understand real-time monitoring
tail -fshows new lines added to a file live as they appear.Step 2: Filter output for 'failed'
Pipingtail -foutput togrep failedshows only lines containing 'failed' in real-time.Final Answer:
tail -f /var/log/syslog | grep failed -> Option CQuick Check:
tail -f + grep = live filtered log [OK]
- Using grep alone shows only current matches, no live updates
- Using cat with tail -f incorrectly
- Trying to grep inside less which is not live
Ready to prove it?
Pass the test and this pattern moves unknown
