• news
  • pics
  • linux
  • windows
  • proxmox
  • game
  • news
  • pics
  • linux
  • windows
  • proxmox
  • game
Home
linux

Viewing Linux Logs from the Command Line

At some point in your career as a Linux administrator, you are going to have to view log files. After all, they are there for one very important reason…to help you troubleshoot an issue. In fact, every seasoned administrator will immediately tell you that the first thing to be done, when a problem arises, is to view the logs.

And there are plenty of logs to be found: logs for the system, logs for the kernel, for package managers, for Xorg, for the boot process, for Apache, for MySQL… For nearly anything you can think of, there is a log file.

Most log files can be found in one convenient location:/var/log. These are all system and service logs, those which you will lean on heavily when there is an issue with your operating system or one of the major services. For desktop app-specific issues, log files will be written to different locations (e.g., Thunderbird writes crash reports to ‘~/.thunderbird/Crash Reports’). Where a desktop application will write logs will depend upon the developer and if the app allows for custom log configuration.

We are going to be focus on system logs, as that is where the heart of Linux troubleshooting lies. And the key issue here is, how do you view those log files?

Fortunately there are numerous ways in which you can view your system logs, all quite simply executed from the command line.

/var/log

This is such a crucial folder on your Linux systems. Open up a terminal window and issue the command cd /var/log. Now issue the command ls and you will see the logs housed within this directory (Figure 1).

Figure 1: A listing of log files found in /var/log/.

Now, let’s take a peek into one of those logs.

Viewing logs with less

One of the most important logs contained within /var/log is syslog. This particular log file logs everything except auth-related messages. Say you want to view the contents of that particular log file. To do that, you could quickly issue the command less /var/log/syslog. This command will open the syslog log file to the top. You can then use the arrow keys to scroll down one line at a time, the spacebar to scroll down one page at a time, or the mouse wheel to easily scroll through the file.

The one problem with this method is that syslog can grow fairly large; and, considering what you’re looking for will most likely be at or near the bottom, you might not want to spend the time scrolling line or page at a time to reach that end. Will syslog open in the less command, you could also hit the [Shift]+[g] combination to immediately go to the end of the log file. The end will be denoted by (END). You can then scroll up with the arrow keys or the scroll wheel to find exactly what you want.

This, of course, isn’t terribly efficient.

Viewing logs with dmesg

The dmesg command prints the kernel ring buffer. By default, the command will display all messages from the kernel ring buffer. From the terminal window, issue the command dmesg and the entire kernel ring buffer will print out (Figure 2).

Figure 2: A USB external drive displaying an issue that may need to be explored.

Fortunately, there is a built-in control mechanism that allows you to print out only certain facilities (such as daemon).

Say you want to view log entries for the user facility. To do this, issue the command dmesg –facility=user. If anything has been logged to that facility, it will print out.

Unlike the less command, issuing dmesgwill display the full contents of the log and send you to the end of the file. You can always use your scroll wheel to browse through the buffer of your terminal window (if applicable). Instead, you’ll want to pipe the output of dmesg to the less command like so:

dmesg | less

The above command will print out the contents of dmesgand allow you to scroll through the output just as you did viewing a standard log with theless command.

Viewing logs with tail

The tailcommand is probably one of the single most handy tools you have at your disposal for the viewing of log files. What tail does is output the last part of files. So, if you issue the command tail /var/log/syslog, it will print out only the last few lines of the syslog file.But wait, the fun doesn’t end there. The tail command has a very important trick up its sleeve, by way of the-f option. When you issue the command tail -f /var/log/syslog, tail will continue watching the log file and print out the next line written to the file. This means you can follow what is written to syslog, as it happens, within your terminal window (Figure 3).

Figure 3: Following /var/log/syslog using the tail command.

Using tail in this manner is invaluable for troubleshooting issues.

To escape thetail command (when following a file), hit the [Ctrl]+[x] combination.

You can also instruct tail to only follow a specific amount of lines. Say you only want to view the last five lines written to syslog; for that you could issue the command:

tail -f -n 5 /var/log/syslog

The above command would follow input to syslog and only print out the most recent five lines. As soon as a new line is written to syslog, it would remove the oldest from the top. This is a great way to make the process of following a log file even easier. I strongly recommend not using this to view anything less than four or five lines, as you’ll wind up getting input cut off and won’t get the full details of the entry.

Previous article How to Install and Use Wine to Run Windows Applications on Linux
Next article Understanding Linux File Permissions

therock

15 Comments

  1. keto quiche
    July 9, 2021 at 10:17 pm

    keto diet foods to eat

  2. speed keto diet
    July 9, 2021 at 8:27 pm

    keto diet downsides

  3. keto sugar substitute
    July 7, 2021 at 11:46 pm

    list of keto diet foods

  4. free dating site in california
    June 26, 2021 at 2:38 pm

    free safe online dating sites

  5. dating site with free messaging
    June 26, 2021 at 10:24 am

    free christan dating

  6. which dating site is completely free?
    June 26, 2021 at 5:38 am

    pof.com 100 free dating site service online

  7. free sites for people who use dating sites for a scams
    June 24, 2021 at 7:47 am

    free fuck dating sites

  8. free trial dating chat lines
    June 2, 2021 at 9:21 am

    safe dating websites for free

  9. free dating singles
    June 1, 2021 at 8:49 pm

    free online dating sites for college students

  10. hookup dating free
    May 27, 2021 at 4:39 am

    online dating free uk

  11. free online dating chat sites
    May 21, 2021 at 1:45 am

    free black dating service

  12. free dating simulator game
    May 20, 2021 at 10:41 pm

    whats the best free dating site

  13. free website for singles dating
    May 20, 2021 at 7:48 pm

    completely free christian dating site

  14. dating site free and no cost
    May 20, 2021 at 12:58 am

    free online dating chatting

  15. you have a secret admirer tinder
    May 14, 2021 at 8:58 pm

    you have a secret admirer tinder

    Viewing Linux Logs from the Command Line – rock funs

Meta
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Categories
  • game
  • linux
  • news
  • pics
  • proxmox
  • windows
Recent Posts
  • 紧身胶衣问谁看不下贱?“HK416”:真的不鼓包的哦!~…
  • Using local directory bind mount points
  • Proxmox中Lxc容器挂载远程目录
  • livego流媒体服务实现无插件播放视频(支持hls,flv)
  • PVE直通核显给虚拟机
Recent Comments
  • Ashvem on livego流媒体服务实现无插件播放视频(支持hls,flv)
  • tadalafil cialis on livego流媒体服务实现无插件播放视频(支持hls,flv)
  • cialis online on livego流媒体服务实现无插件播放视频(支持hls,flv)
  • buy cialis online on livego流媒体服务实现无插件播放视频(支持hls,flv)
  • Alanvem on livego流媒体服务实现无插件播放视频(支持hls,flv)
Archives
  • April 2022 (1)
  • February 2022 (5)
  • January 2022 (2)
  • December 2021 (3)
  • November 2021 (1)
  • October 2021 (2)
  • September 2021 (1)
  • August 2021 (1)
  • July 2021 (8)
  • June 2021 (14)
  • May 2021 (2)
  • April 2021 (2)
  • March 2021 (10)
  • January 2021 (4)
  • December 2020 (4)
  • November 2020 (13)
  • April 2020 (276)
  • March 2020 (1)
  • June 2019 (5)
  • May 2019 (10)
  • December 2015 (1)