个人工具

“UbuntuHelp:LinuxLogFiles”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(新页面: {{From|https://help.ubuntu.com/community/LinuxLogFiles}} {{Languages|UbuntuHelp:LinuxLogFiles}} === Introduction === One of the things which makes GNU/Linux a great operating system is ...)
 
 
(未显示同一用户的8个中间版本)
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/LinuxLogFiles}}
 
{{From|https://help.ubuntu.com/community/LinuxLogFiles}}
 
{{Languages|UbuntuHelp:LinuxLogFiles}}
 
{{Languages|UbuntuHelp:LinuxLogFiles}}
 
 
 
=== Introduction ===
 
=== Introduction ===
One of the things which makes GNU/Linux a great operating system is that virtually anything, and everything happening on, and to the system may be logged in some manner.  This information is invaluable to using the system in an informed manner, and should be one of the first resources used in trouble-shooting system, and application issues.  The logs can tell you almost anything you need to know, so long as you have an idea where to look first.
+
One of the things which makes GNU/Linux a great operating system is that virtually anything and everything happening on and to
 
+
the system may be logged in some manner.  This information is invaluable for using the system in an informed manner, and should
Your Ubuntu system provides vital information concerning events, operation, and other functions in various system log files.  These log files are typically plain ASCII text in a standard log file format, and the majority of them are located in the traditional system log subdirectory <code><nowiki>/var/log</nowiki></code>. Many of these log files are generated by the system log daemon, <code><nowiki>syslogd</nowiki></code> on behalf of the system, and certain applications, while some applications generate their own logs by writing directly to log files located in the <code><nowiki>/var/log</nowiki></code> subdirectory.
+
be one of the first resources you use to trouble-shoot system and application issues.  The logs can tell you almost anything you
 
+
need to know, as long as you have an idea where to look first.
This guide discusses several of these system log files, describes their content with examples, and further presents examples on extracting useful information from these system log files with such command-line utilities as <code><nowiki>grep</nowiki></code> and <code><nowiki>less</nowiki></code>.  This guide will also discuss the system logging daemon, <code><nowiki>syslogd</nowiki></code>, its configuration, and the concept of log rotation.  Additional information will be provided in the '''Resources''' section of this guide.
+
Your Ubuntu system provides vital information using various system log files.  These log files are typically plain ASCII text in
 
+
a standard log file format, and most of them sit in the traditional system log subdirectory <code><nowiki>/var/log</nowiki></code>. Many are generated
 +
by the system log daemon, <code><nowiki>syslogd</nowiki></code> on behalf of the system and certain applications, while some applications generate their
 +
own logs by writing directly to filesin <code><nowiki>/var/log</nowiki></code>.
 +
This guide talks about how to read and use several of these system log files, how to use and configure the system logging
 +
daemon, <code><nowiki>syslogd</nowiki></code>, and how log rotation worksSee the '''Resources''' section for additional information.
 
=== Target Audience ===
 
=== Target Audience ===
This guide is for anyone with sufficient experience using the GNU/Linux command-line, and particularly experience in executing command-line utilities, and editing system configuration files with a preferred console-based text editor.
+
This guide will be simple enough to use if you have any experience using the console and editing text files using a text editor.
 
+
See the end of this document for some essential commands that may help you find your way around these files if you're relatively
 +
new to the command line.
 
=== System Logs ===
 
=== System Logs ===
This section of the guide addresses so-called system logs, or logs which deal primarily with the functioning of the Ubuntu system, and not necessarily with additional applications added by the System Administrator, or users of the system.  Examples of such logs include the logging of authorization mechanisms, running system daemons, system messages, and the all-encompassing system log itself, or ''syslog'' as it is known. (not to be confused with the actual <code><nowiki>syslogd</nowiki></code> daemon'' which we'll cover later).
+
System logs deal primarily with the functioning of the Ubuntu system, not necessarily with additional applications added by
 
+
users.  Examples include authorization mechanisms, system daemons, system messages, and the all-encompassing system log itself,
 +
''syslog''.
 
==== Authorization Log ====
 
==== Authorization Log ====
The Authorization Log tracks usage of authorization systems, that is, all of the Ubuntu mechanisms for authorizing users which prompt for user passwords, such as the Pluggable Authentication Module (PAM) system, the <code><nowiki>sudo</nowiki></code> command, remote logins to <code><nowiki>sshd</nowiki></code>, and so on.  The Authorization Log file may be accessed at <code><nowiki>/var/log/auth.log</nowiki></code>.  This log is useful for learning about user logins, and usage of the sudo command on your Ubuntu system, for example.
+
The Authorization Log tracks usage of authorization systems, the mechanisms for authorizing users which prompt for user
 
+
passwords, such as the Pluggable Authentication Module (PAM) system, the <code><nowiki>sudo</nowiki></code> command, remote logins to <code><nowiki>sshd</nowiki></code> and so
You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
on.  The Authorization Log file may be accessed at <code><nowiki>/var/log/auth.log</nowiki></code>.  This log is useful for learning about user logins
 
+
and usage of the sudo command.
<code><nowiki>less /var/log/auth.log</nowiki></code>
+
Use <code><nowiki>grep</nowiki></code> to cut down on the volume.  For example, to see only information in the Authorization Log pertaining to
 
+
<code><nowiki>sshd</nowiki></code> logins, use this:
Press the '''SPACE BAR''' to advance to the next page, or the '''ENTER''' key to advance one line at-a-time.  The '''b''' key will scroll backwards one full page, and the '''q''' quits the <code><nowiki>less</nowiki></code> utility.
+
 
+
Specific information may be accessed from the Authorization Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Authorization Log pertaining to <code><nowiki>sshd</nowiki></code> logins, you might use a command such as the following at a terminal prompt:
+
 
+
 
<code><nowiki>grep sshd /var/log/auth.log | less</nowiki></code>
 
<code><nowiki>grep sshd /var/log/auth.log | less</nowiki></code>
 
 
==== Daemon Log ====
 
==== Daemon Log ====
The daemon log exists at <code><nowiki>/var/log/daemon.log</nowiki></code> and contains information specific to running system, and application daemons such as the Gnome Display Manager
+
A daemon is a program that runs in the background, generally without human intervention, performing some operation important to
daemon, (<code><nowiki>gdm</nowiki></code>) the Bluetooth HCI daemon, (<code><nowiki>hcid</nowiki></code>) or the MySQL Relational Database Management System (RDBMS) daemon, (<code><nowiki>mysqld</nowiki></code>).  This log is useful for gaining information about running daemons, and for trouble-shooting problems with a particular daemon.
+
the proper running of your system.  The daemon log at <code><nowiki>/var/log/daemon.log</nowiki></code> and contains information about running system
 
+
and application daemons such as the Gnome Display Manager daemon <code><nowiki>gdm</nowiki></code>, the Bluetooth HCI daemon <code><nowiki>hcid</nowiki></code>, or the MySQL
You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
database daemon <code><nowiki>mysqld</nowiki></code>.  This can help you trouble-shoot problems with a particular daemon.
 
+
Again, use grep to find specific information, plugging in the name of the daemon you're interested in.
<code><nowiki>less /var/log/daemon.log</nowiki></code>
+
 
+
Specific information may be accessed from the Daemon Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Daemon Log pertaining to the MySQL daemon, you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep mysqld /var/log/daemon.log | less</nowiki></code>
+
 
+
 
==== Debug Log ====
 
==== Debug Log ====
The debug log exists at <code><nowiki>/var/log/debug</nowiki></code> and provides detailed, debug messages from the Ubuntu system, and applications which log to <code><nowiki>syslogd</nowiki></code> at the DEBUG level. These messages are useful for debugging issues with everything from hardware drivers, to server daemons.
+
The debug log at <code><nowiki>/var/log/debug</nowiki></code> and provides detailed debug messages from the Ubuntu system and applications which log to
 
+
<code><nowiki>syslogd</nowiki></code> at the DEBUG level.
You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
 
+
<code><nowiki>less /var/log/debug</nowiki></code>
+
 
+
Specific information may be accessed from the Debug Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Debug Log pertaining to the Advanced Configuration and Power Interface (ACPI), you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep ACPI /var/log/debug | less</nowiki></code>
+
 
+
 
==== Kernel Log ====
 
==== Kernel Log ====
The kernel log at <code><nowiki>/var/log/kern.log</nowiki></code> provides a detailed log of messages from the Ubuntu Linux kernel.  These messages may prove useful for trouble-shooting a new, or custom-built kernel for example.
+
The kernel log at <code><nowiki>/var/log/kern.log</nowiki></code> provides a detailed log of messages from the Ubuntu Linux kernel.  These messages may
 
+
prove useful for trouble-shooting a new or custom-built kernel, for example.
You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
 
+
<code><nowiki>less /var/log/kern.log</nowiki></code>
+
 
+
Specific information may be accessed from the Kernel Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Kernel Log pertaining to the computer's Central Processing Unit (CPU), you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep CPU /var/log/kern.log | less</nowiki></code>
+
 
+
 
==== Kernel Ring Buffer ====
 
==== Kernel Ring Buffer ====
The kernel ring buffer is not really a log file per se, but rather an area in the running kernel which may be queried for kernel bootup messages via the <code><nowiki>dmesg</nowiki></code> utility.  You may see all kernel bootup messages by using a command such as the following at a terminal prompt:
+
The kernel ring buffer is not really a log file per se, but rather an area in the running kernel you can query for kernel bootup
 
+
messages via the <code><nowiki>dmesg</nowiki></code> utility.  To see the messages, use this:
 
<code><nowiki>dmesg | less</nowiki></code>
 
<code><nowiki>dmesg | less</nowiki></code>
 
+
Or to search for lines that mention the Plug & Play system, for example, use <code><nowiki>grep</nowiki></code> like this:
You may also use the <code><nowiki>dmesg</nowiki></code> utility to examine specific information from the kernel bootup messages such as Plug and Play (PNP) messages by using a command such as the following at a terminal prompt:
+
 
+
 
<code><nowiki>dmesg | grep pnp | less</nowiki></code>
 
<code><nowiki>dmesg | grep pnp | less</nowiki></code>
 
+
By default, the system initialization script <code><nowiki>/etc/init.d/bootmisc.sh</nowiki></code> sends all bootup messages to the file
In relation to the Kernel Ring Buffer, the default behavior of the <code><nowiki>/etc/init.d/bootmisc.sh</nowiki></code> system initialization script is to use the <code><nowiki>dmesg</nowiki></code> command to log all bootup messages to the file <code><nowiki>/var/log/dmesg</nowiki></code> as well.  This file may be used as any other log file for examining Kernel bootup messages via commands <code><nowiki>grep</nowiki></code>, <code><nowiki>less</nowiki></code>, and others.
+
<code><nowiki>/var/log/dmesg</nowiki></code> as well.  You can view and search this file the usual way.
 
+
 
==== Messages Log ====
 
==== Messages Log ====
The messages log contains informational messages from applications, and system facilities, and is available at <code><nowiki>/var/log/messages</nowiki></code>.  This log is useful for examining message output from applications, and system facilities which log to the syslog / sysklog daemon at the INFO level.
+
The messages log contains informational messages from applications, and system facilities, and is available at
 
+
<code><nowiki>/var/log/messages</nowiki></code>.  This log is useful for examining message output from applications, and system facilities which log to
You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
the syslog / sysklog daemon at the INFO level.
 
+
<code><nowiki>less /var/log/messages</nowiki></code>
+
 
+
Specific information may be accessed from the Messages Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Messages Log pertaining to the Gnome Configuration Daemon (<code><nowiki>gconfd</nowiki></code>), you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep gconfd /var/log/messages | less</nowiki></code>
+
 
+
 
==== System Log ====
 
==== System Log ====
The system log typically contains the greatest deal of information by default about your Ubuntu system.  It is located at <code><nowiki>/var/log/syslog</nowiki></code>, and may contain information other logs do not.  You may wish to consult the System Log when you are unable to locate the desired log information in another log.
+
The system log typically contains the greatest deal of information by default about your Ubuntu system.  It is located at
 
+
<code><nowiki>/var/log/syslog</nowiki></code>, and may contain information other logs do not.  Consult the System Log when you can't locate the desired
 
+
log information in another log.
 
=== Application Logs ===
 
=== Application Logs ===
In addition to the myriad of system-specific logs available on your Ubuntu system, you may also access individual logs which may be used by certain applications.   If you list the contents of your <code><nowiki>/var/log</nowiki></code> subdirectory, you will see familiar names of applications you may have installed, such as <code><nowiki>/var/log/apache2</nowiki></code> representing the logs for the Apache 2 Hyper Text Transport Protocol (HTTP) server, or <code><nowiki>/var/log/samba</nowiki></code>, which contains the logs for the Samba Server Message Block (SMB) server.  This section of the guide introduces some specific examples of application logs, and information contained within them.
+
Many applications also create logs in <code><nowiki>/var/log</nowiki></code>. If you list the contents of your <code><nowiki>/var/log</nowiki></code> subdirectory, you will
 
+
see familiar names, such as <code><nowiki>/var/log/apache2</nowiki></code> representing the logs for the Apache 2 web server, or <code><nowiki>/var/log/samba</nowiki></code>,
 +
which contains the logs for the Samba server.  This section of the guide introduces some specific examples of application logs,
 +
and information contained within them.
 
==== Apache HTTP Server Logs ====
 
==== Apache HTTP Server Logs ====
The default installation for Apache2 on Ubuntu creates a log subdirectory: <code><nowiki>/var/log/apache2</nowiki></code>, and within this subdirectory, are two log files with two distinct purposes:
+
The default installation for Apache2 on Ubuntu creates a log
 
+
subdirectory: <code><nowiki>/var/log/apache2</nowiki></code>.  Within this subdirectory
* <code><nowiki>/var/log/apache2/access.log</nowiki></code> : Contains records of all access to the HTTP server by clients
+
are two log files with two distinct purposes:
* <code><nowiki>/var/log/apache2/error.log</nowiki></code> : Contains records of all error conditions reported by the HTTP server
+
* <code><nowiki>/var/log/apache2/access.log</nowiki></code> - records of every page served and every file loaded by the web server.
 
+
* <code><nowiki>/var/log/apache2/error.log</nowiki></code> - records of all error conditions reported by the HTTP server
With this information in mind, and a command of the tools <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code> basic information gathering from these logs becomes possible.
+
By default, every time Apache accesses a file or page, the access logs record the IP address, time and date, browser
 
+
identification string, HTTP result code and the text of the actual query, which will generally be a GET for a page view. Look
For example, and in terms of access, if you wished to see log records for every recorded access to your Apache2 server from the client IP address ''82.211.81.166'', and display the results as one page per screen, you would simply use a command such as the following at a terminal prompt:
+
at the Apache documentation for a complete rundown; quite a lot can be gleaned from this file, and indeed many statistical
 
+
packages exist that perform analyses of these logs.
<code><nowiki>grep "82.211.81.166" /var/log/apache2/access.log | less</nowiki></code>
+
Also, every time any error occurs, Apache adds a line to the error log.  If you run PHP with error and warning messages
 
+
disabled, this can be your only way to identify bugs.
Or, if you wished to determine if any clients using your Apache2 server were using Mac OS X, a simple command such as the following, typed into a terminal window would suffice:
+
 
+
<code><nowiki>grep "Mac OS X" /var/log/apache2/access.log | less</nowiki></code>
+
 
+
On the other side of the coin, suppose you wished to see information from the <code><nowiki>/var/log/apache2/error.log</nowiki></code>This log can be used to search for instances of the Apache2 server being shut down, by using a command such as the following from a terminal prompt:
+
 
+
<code><nowiki>grep "shutting down" /var/log/apache2/error.log | less</nowiki></code>
+
 
+
You may also see all log entries which are considered errors by Apache2 with a command such as the following entered at a terminal prompt:
+
 
+
<code><nowiki>grep error /var/log/apache2/error.log | less</nowiki></code>
+
 
+
 
==== CUPS Print System Logs ====
 
==== CUPS Print System Logs ====
The Common Unix Printing System (CUPS) uses the default log file <code><nowiki>/var/log/cups/error_log</nowiki></code> to store informational and error messages.  If you need to solve a printing issue in Ubuntu, then this log may be a good place to start. You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
The Common Unix Printing System (CUPS) uses the default log file <code><nowiki>/var/log/cups/error_log</nowiki></code> to store informational and error
 
+
messages.  If you need to solve a printing issue in Ubuntu, this log may be a good place to start.  
<code><nowiki>less /var/log/cups/error_log</nowiki></code>
+
 
+
Specific information may be accessed from the CUPS Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the CUPS Log pertaining to Full reloads, you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep reload /var/log/cups/error_log | less</nowiki></code>
+
 
+
 
==== Rootkit Hunter Log ====
 
==== Rootkit Hunter Log ====
The Rootkit Hunter utility (<code><nowiki>rkhunter</nowiki></code>) checks your Ubuntu system for backdoors, sniffers, and so-called "rootkits", which are all signs of compromise of your system.  The log which  rkhunter uses is located at <code><nowiki>/var/log/rkhunter.log</nowiki></code> You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
The Rootkit Hunter utility (<code><nowiki>rkhunter</nowiki></code>) checks your Ubuntu system for backdoors, sniffers and rootkits, which are all signs
 
+
of compromise of your system.  The log rkhunter uses is located at <code><nowiki>/var/log/rkhunter.log</nowiki></code>.
<code><nowiki>less /var/log/rkhunter.log</nowiki></code>
+
 
+
Specific information may be accessed from the Rootkit Hunter Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Rootkit Hunter Log pertaining to Warnings, you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep WARNING /var/log/rkhunter.log | less</nowiki></code>
+
 
+
 
==== Samba SMB Server Logs ====
 
==== Samba SMB Server Logs ====
The Server Message Block Protocol (SMB) server, Samba is popularly used for sharing files from your Ubuntu computer to other computers which support the SMB protocol.  Samba keeps three distinct types of logs in the subdirectory <code><nowiki>/var/log/samba</nowiki></code>:
+
The Server Message Block Protocol (SMB) server, Samba is popularly used for sharing files between your Ubuntu computer and other
 
+
computers which support the SMB protocol.  Samba keeps three distinct types of logs in the subdirectory <code><nowiki>/var/log/samba</nowiki></code>:
* <code><nowiki>log.nmbd</nowiki></code> : Logs all messages related to Samba's NETBIOS over IP functionality (the network stuff)
+
* <code><nowiki>log.nmbd</nowiki></code> - messages related to Samba's NETBIOS over IP functionality (the network stuff)
* <code><nowiki>log.smbd</nowiki></code> : Logs all messages related to Samba's SMB/CIFS functionality (the file, print, etc. sharing stuff)
+
* <code><nowiki>log.smbd</nowiki></code> - messages related to Samba's SMB/CIFS functionality (the file and print sharing stuff)
* <code><nowiki>log.[IP_ADDRESS]</nowiki></code> : Logs messages related to requests for services from the IP address contained in the log file name, for example, <code><nowiki>log.192.168.1.1</nowiki></code>.
+
* <code><nowiki>log.[IP_ADDRESS]</nowiki></code> - messages related to requests for services from the IP address contained in the log file name, for example, <code><nowiki>log.192.168.1.1</nowiki></code>.
 
+
To view all information related to Samba's networking, you could use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>less /var/log/samba/log.nmbd</nowiki></code>
+
 
+
If you wanted only to see information logged about Master Browsers, then you might use a command like this at a terminal prompt:
+
 
+
<code><nowiki>grep "master browser" /var/log/samba/log.nmbd | less</nowiki></code>
+
 
+
If you would like to see details related to the SMB functionality of Samba, you can view the appropriate log in its entirety with a command similar to the following at a terminal prompt:
+
 
+
<code><nowiki>less /var/log/samba/log.smbd</nowiki></code>
+
 
+
To see only messages related to the start up of the Samba server, a command like the following at a terminal prompt may be used:
+
 
+
<code><nowiki>grep started /var/log/samba/log.smbd | less</nowiki></code>
+
 
+
To view all the details on connections from the client system with IP address ''192.168.99.99'' you could use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>less /var/log/samba/log.192.168.99.99</nowiki></code>
+
 
+
 
==== X11 Server Log ====
 
==== X11 Server Log ====
The default X11 Windowing Server in use with Ubuntu is the Xorg X11 server, and assuming your computer has but one display defined, it stores log messages in the file <code><nowiki>/var/log/Xorg.0.log</nowiki></code>.  This log is helpful for diagnosing issues with your X11 environment.  You may examine the log, from its most recent rotation point, (if applicable) one page per screen, using a command such as the following from a terminal prompt:
+
The default X11 Windowing Server in use with Ubuntu is the Xorg X11 server, and assuming your computer has only one display
 
+
defined, it stores log messages in the file <code><nowiki>/var/log/Xorg.0.log</nowiki></code>.  This log is helpful for diagnosing issues with your X11
<code><nowiki>less /var/log/Xorg.0.log</nowiki></code>
+
environment.   
 
+
Specific information may be accessed from the Xorg Log by using such commands as <code><nowiki>grep</nowiki></code>, and <code><nowiki>less</nowiki></code>.  For example, to see only information in the Xorg Log pertaining to the freetype font engine, you might use a command such as the following at a terminal prompt:
+
 
+
<code><nowiki>grep freetype /var/log/Xorg.0.log | less</nowiki></code>
+
 
+
 
=== Non-Human-Readable Logs ===
 
=== Non-Human-Readable Logs ===
Some log files found in the <code><nowiki>/var/log</nowiki></code> subdirectory are designed to be readable by applications, and not necessarily by humans.  Some examples of such log files which appear in <code><nowiki>/var/log</nowiki></code> follow.
+
Some log files found in the <code><nowiki>/var/log</nowiki></code> subdirectory are designed to be readable by applications, not necessarily by
 
+
humans.  Some examples of such log files which appear in <code><nowiki>/var/log</nowiki></code> follow.
 
==== Login Failures Log ====
 
==== Login Failures Log ====
The login failures log located at <code><nowiki>/var/log/faillog</nowiki></code> is actually designed to be parsed, and output by the <code><nowiki>faillog</nowiki></code> command.  For example, to print recent login failures using the <code><nowiki>faillog</nowiki></code> command, simply enter the following at a terminal prompt:
+
The login failures log located at <code><nowiki>/var/log/faillog</nowiki></code> is actually designed to be parsed and displayed by the <code><nowiki>faillog</nowiki></code>
 
+
command.  For example, to print recent login failures, use this:
 
<code><nowiki>faillog</nowiki></code>
 
<code><nowiki>faillog</nowiki></code>
 
 
==== Last Logins Log ====
 
==== Last Logins Log ====
The last logins log at <code><nowiki>/var/log/lastlog</nowiki></code> should not typically be parsed, and examined by humans, but rather should be used in conjunction with the <code><nowiki>lastlog</nowiki></code> command.  For example to see a listing of logins with the <code><nowiki>lastlog</nowiki></code> command, displayed one page per screen with the <code><nowiki>less</nowiki></code> command, use the following command at a terminal prompt:
+
The last logins log at <code><nowiki>/var/log/lastlog</nowiki></code> should not typically be parsed and examined by humans, but rather should be used
 
+
in conjunction with the <code><nowiki>lastlog</nowiki></code> command.  For example to see a listing of logins with the <code><nowiki>lastlog</nowiki></code> command, displayed
 +
one page per screen with the <code><nowiki>less</nowiki></code> command, use the following command:
 
<code><nowiki>lastlog | less</nowiki></code>
 
<code><nowiki>lastlog | less</nowiki></code>
 
 
==== Login Records Log ====
 
==== Login Records Log ====
The file <code><nowiki>/var/log/wtmp</nowiki></code> contains login records, but unlike <code><nowiki>/var/log/lastlog</nowiki></code> above, <code><nowiki>/var/log/wtmp</nowiki></code> is not used to show a list of recent logins, but is instead used by other utilities such as the <code><nowiki>who</nowiki></code> command to present a listed of currently logged in users.  For example, if you wish to see who is currently logged in on the machine you are currently using, issue the following at a terminal prompt:
+
The file <code><nowiki>/var/log/wtmp</nowiki></code> contains login records, but unlike <code><nowiki>/var/log/lastlog</nowiki></code> above, <code><nowiki>/var/log/wtmp</nowiki></code> is not used to
 
+
show a list of recent logins, but is instead used by other utilities such as the <code><nowiki>who</nowiki></code> command to present a listed of
 +
currently logged in users.  This command will show the users currently logged in to your machine:
 
<code><nowiki>who</nowiki></code>
 
<code><nowiki>who</nowiki></code>
 
 
=== System Logging Daemon (syslogd) ===
 
=== System Logging Daemon (syslogd) ===
The system logging daemon, or <code><nowiki>syslogd</nowiki></code> (also known as <code><nowiki>sysklogd</nowiki></code>) is a system daemon, or special application which executes silently, in the background and does good things for your system.  Specifically, <code><nowiki>syslogd</nowiki></code> awaits logging messages from numerous system, and application sources, and routes the messages to their proper target, be it a standard log file, a First In First Out (FIFO) pipe for use by a log analyzation application, or even across the network to another system's <code><nowiki>syslogd</nowiki></code>.
+
The system logging daemon <code><nowiki>syslogd</nowiki></code>, also known as <code><nowiki>sysklogd</nowiki></code>, awaits logging messages from numerous sources and routes
 
+
the messages to the appropriate file or network destination. Messages logged to <code><nowiki>syslogd</nowiki></code> usually contain common
Messages logged to <code><nowiki>syslogd</nowiki></code> usually contain important common elements, such as system hostnames, and time-stamps in addition to the specific log information from a system source, the Linux kernel, or a user application.
+
elements like system hostnames and time-stamps in addition to the specific log information.
 
+
 
==== Configuration of syslogd ====
 
==== Configuration of syslogd ====
The detailed configuration of <code><nowiki>syslogd</nowiki></code> is beyond the scope of this guide, and the reader is encouraged to seek additional information via the '''Resources''' section of this guide for information on correctly configuring, and modifying the configuration of <code><nowiki>syslogd</nowiki></code>.  The file which configures the behavior of the <code><nowiki>syslogd</nowiki></code> daemon is <code><nowiki>/etc/syslog.conf</nowiki></code> and consists primarily of two fields, the selector, and the action.  The selector field consists of a facility, to be logged, such as for example the '''auth''' facility which deals with authorization, and a priority, or level to log such information at, such as '''info''', or '''warning''' priorities, which would log all messages at the informational priority and higher, or only at the warning level and higher respectively.  The action field consists of a target for the log information, such as a standard log file (i.e. <code><nowiki>/var/log/syslog</nowiki></code>), or the hostname of a remote computer to send the log information to (e.g. @myotherubuntu).
+
The <code><nowiki>syslogd</nowiki></code> daemon's configuration file is <code><nowiki>/etc/syslog.conf</nowiki></code>.  Each entry in this file consists of two fields, the
 
+
selector and the action.  The selector field specifies a facility to be logged, such as for example the '''auth''' facility
The configuration file is very flexible, and powerful in nature, allowing a seemingly infinite combination of logging to take place to fit the particular requirements your installation may have.
+
which deals with authorization, and a priority level to log such information at, such as '''info''', or '''warning'''.  The
 
+
action field consists of a target for the log information, such as a standard log file (i.e. <code><nowiki>/var/log/syslog</nowiki></code>), or the
 +
hostname of a remote computer to send the log information to.
 
==== Echoing Messages to syslogd With Logger ====
 
==== Echoing Messages to syslogd With Logger ====
A neat utility exists in the <code><nowiki>logger</nowiki></code> tool, which allows one to place messages into the System Log (i.e. <code><nowiki>/var/log/syslog</nowiki></code>) arbitrarily.  This is a powerful tool, which you may use in Administrative scripts, such as Perl, or shell scripts to provide them with standard logging capabilities, or you may use it just to place things in the system log as needed.  For example, assume your user name is <code><nowiki>buddha</nowiki></code>, and you would like to enter a message into the syslog about a particularly delicious pizza you're eating, you could use a command such as the following at a terminal prompt:
+
A neat utility exists in the <code><nowiki>logger</nowiki></code> tool, which allows one to place messages into the System Log
 
+
(i.e. <code><nowiki>/var/log/syslog</nowiki></code>) arbitrarily.  For example, assume your user name is <code><nowiki>buddha</nowiki></code>, and you would like to enter a
 +
message into the syslog about a particularly delicious pizza you're eating, you could use a command such as the following at a
 +
terminal prompt:
 
<code><nowiki>logger This Pizza from Vinnys Gourmet Rocks</nowiki></code>
 
<code><nowiki>logger This Pizza from Vinnys Gourmet Rocks</nowiki></code>
 
+
and you would end up with a line in the <code><nowiki>/var/log/syslog</nowiki></code> file like this:
and you would find a line in the <code><nowiki>/var/log/syslog</nowiki></code> file such as this afterward:
+
 
+
 
<pre><nowiki>
 
<pre><nowiki>
Jan 12 23:17:02 localhost buddha: This Pizza from Vinnys Gourmet Rocks
+
Jan 12 23:34:45 localhost buddha: This Pizza from Vinnys Gourmet Rocks
 
</nowiki></pre>
 
</nowiki></pre>
 
+
You can even specify a tag the messages come from, and redirect the output standard error too.
Used in a little more professional manner in shell scripts, you can even specify a tag the messages come from, and redirect the output standard error too. This lets you have excellent error logging in a script, such as in this example snippet:
+
 
+
 
<pre><nowiki>
 
<pre><nowiki>
 
#!/bin/bash
 
#!/bin/bash
第223行: 第139行:
 
# test for the existence of Fred's home dir on this machine
 
# test for the existence of Fred's home dir on this machine
 
if [ -d /home/fred ]; then
 
if [ -d /home/fred ]; then
$logmsg "I. Fred's Home Directory Found"
+
  $logmsg "I. Fred's Home Directory Found"
 
else
 
else
$logmsg "E. Fred's Home Directory was NOT Found. Boo Hoo."
+
  $logmsg "E. Fred's Home Directory was NOT Found. Boo Hoo."
exit 1
+
  exit 1
 
fi
 
fi
 
</nowiki></pre>
 
</nowiki></pre>
 
+
Executing this script as <code><nowiki>chkdir.sh</nowiki></code> on the machine <code><nowiki>butters</nowiki></code> where Fred does not have a home directory,
Executing this script as <code><nowiki>chkdir.sh</nowiki></code> on the machine <code><nowiki>butters</nowiki></code> where Fred does not have a home directory, <code><nowiki>/home/fred</nowiki></code>, gives the following results:
+
<code><nowiki>/home/fred</nowiki></code>, gives the following results:
 
+
 
<pre><nowiki>
 
<pre><nowiki>
 
bumpy@butters:~$./chkdir.sh
 
bumpy@butters:~$./chkdir.sh
第240行: 第155行:
 
Jan 12 23:23:11 localhost MyScript: E. Fred's Home Directory was NOT Found. Boo Hoo.
 
Jan 12 23:23:11 localhost MyScript: E. Fred's Home Directory was NOT Found. Boo Hoo.
 
</nowiki></pre>
 
</nowiki></pre>
 
+
So, as you can see, we received the messages both via standard error, at the terminal prompt, and they also appear in our
So, as you can see, we received the messages both via standard error, at the terminal prompt, and they also appear in our syslog!
+
syslog.
 
+
 
==== Log Rotation ====
 
==== Log Rotation ====
When viewing directory listings in <code><nowiki>/var/log</nowiki></code> or any of its subdirectories, you may encounter log files with names such as <code><nowiki>daemon.log.0</nowiki></code>, <code><nowiki>daemon.log.1.gz</nowiki></code>, and so on.  What are these log files?  They are 'rotated' log files.  That is, they have automatically been renamed after a predefined time-frame, and a new original log started.  After even more time the log files are compressed with the <code><nowiki>gzip</nowiki></code> utility as in the case of the example <code><nowiki>daemon.log.1.gz</nowiki></code>.  The purpose of log rotation is to archive and compress old logs so that they consume less disk space, but are still available for inspection as needed.  What handles this functionality? Why, the <code><nowiki>logrotate</nowiki></code> command of course!  Typically, logrotate is called from the system-wide cron script <code><nowiki>/etc/cron.daily/logrotate</nowiki></code>, and further defined by the configuration file <code><nowiki>/etc/logrotate.conf</nowiki></code>.
+
When viewing directory listings in <code><nowiki>/var/log</nowiki></code> or any of its subdirectories, you may encounter log files with names such as
 
+
<code><nowiki>daemon.log.0</nowiki></code>, <code><nowiki>daemon.log.1.gz</nowiki></code>, and so on.  What are these log files?  They are 'rotated' log files.  That is, they
This guide will not cover the myriad of ways logrotate may be configured to handle the automatic rotation of any log file on your Ubuntu system, but rather the reader is encouraged to use the '''Resources''' section of this guide, and study the requisite manual pages to determine how to configure logrotate for a particular log file, and needs.
+
have automatically been renamed after a predefined time-frame, and a new original log started.  After even more time the log
 
+
files are compressed with the <code><nowiki>gzip</nowiki></code> utility as in the case of the example <code><nowiki>daemon.log.1.gz</nowiki></code>.  The purpose of log
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png '''NOTE:''' You may also rotate system log files via the <code><nowiki>cron.daily</nowiki></code> script <code><nowiki>/etc/cron.daily/sysklogd</nowiki></code> instead of using logrotate.  Actually, the utility <code><nowiki>savelog</nowiki></code> may produce unexpected results on log rotation which configuring <code><nowiki>logrotate</nowiki></code> seems to have no effect on.  In those cases, you should check the cron.daily <code><nowiki>sysklogd</nowiki></code> script in <code><nowiki>/etc/cron.daily/sysklogd</nowiki></code> and read the <code><nowiki>savelog</nowiki></code> manual page to see if <code><nowiki>savelog</nowiki></code> is not in fact doing the rotation in a way that is not what you are specifying with <code><nowiki>logrotate</nowiki></code>.
+
rotation is to archive and compress old logs so that they consume less disk space, but are still available for inspection as
 
+
needed.  What handles this functionality? Why, the <code><nowiki>logrotate</nowiki></code> command of course!  Typically, logrotate is called from the
=== Additional Tips ===
+
system-wide cron script <code><nowiki>/etc/cron.daily/logrotate</nowiki></code>, and further defined by the configuration file
Some additional tips for quickly viewing logs manually, (i.e. without a log file analyzer application) which may help you expediently locate the information you require.
+
<code><nowiki>/etc/logrotate.conf</nowiki></code>. Individual configuration files can be added into <code><nowiki>/etc/logrotate.d</nowiki></code> (where the apache2 and mysql
 
+
configurations are stored for example).
==== Just the Beginning ====
+
This guide will not cover the myriad of ways <code><nowiki>logrotate</nowiki></code> may be configured to handle the automatic rotation of any log file
You may look at just the beginning of any log file by using the <code><nowiki>head</nowiki></code> command.  by default, <code><nowiki>head</nowiki></code> shows the first ten lines of any text file, so for example, if you wished to see the oldest entries in your Authorization Log file, a command such as the following could be used at a terminal prompt:
+
on your Ubuntu system.  For more detail, check the '''Resources''' section of this guide.
 
+
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png '''NOTE:''' You may also rotate system log files via the <code><nowiki>cron.daily</nowiki></code> script
<code><nowiki>head /var/log/auth.log</nowiki></code>
+
<code><nowiki>/etc/cron.daily/sysklogd</nowiki></code> instead of using logrotate.  Actually, the utility <code><nowiki>savelog</nowiki></code> may produce unexpected results
 
+
on log rotation which configuring <code><nowiki>logrotate</nowiki></code> seems to have no effect on.  In those cases, you should check the cron.daily
If ten lines is not enough, and you need to see the first twenty-five (25) lines, then use <code><nowiki>head</nowiki></code> with the <code><nowiki>-n</nowiki></code> switch as such:
+
<code><nowiki>sysklogd</nowiki></code> script in <code><nowiki>/etc/cron.daily/sysklogd</nowiki></code> and read the <code><nowiki>savelog</nowiki></code> manual page to see if <code><nowiki>savelog</nowiki></code> is not in
 
+
fact doing the rotation in a way that is not what you are specifying with <code><nowiki>logrotate</nowiki></code>.
<code><nowiki>head -n 25 /var/log/auth.log</nowiki></code>
+
=== Essential Commands ===
 
+
If you're new to the console and the Linux command line, these commands will get you up and running to the point where you can
==== Just the End ====
+
work with log files at a basic level.
The compliment to <code><nowiki>head</nowiki></code> of course is none other than the <code><nowiki>tail</nowiki></code> command.  Can you guess what <code><nowiki>tail</nowiki></code> allows you to do?  Say you need the last ten lines of the Kernel log for important messages from the kernel of late.  A command such as the following entered into a terminal prompt should do:
+
==== Getting Started ====
 
+
To change to the log directory, where most of these files sit, use the <code><nowiki>cd</nowiki></code> command.  This saves having to type out a full
<code><nowiki>tail /var/log/kern.log</nowiki></code>
+
path name for every subsequent command:
 
+
<code><nowiki>cd /var/log</nowiki></code>
Again, and as with <code><nowiki>head</nowiki></code>, you may get more than the default ten lines of output with <code><nowiki>tail</nowiki></code> by specifying the <code><nowiki>-n</nowiki></code> switch as such:
+
==== Editing Files ====
 
+
You can view and edit files in GEdit or Kate, the simple text editors that come with Ubuntu and Kubuntu respectively, but these
<code><nowiki>tail -n 30 /var/log/kern.log</nowiki></code>
+
can be overkill when all you want to do is look at a file or make simple changes.  The easiest editor to use from the console is
 
+
nano, which is less powerful but also less complicated than vim or emacs.  The command to edit a particular logfile
to see the last thirty (30) lines of the Kernel log instead.
+
<code><nowiki>/var/log/example.log</nowiki></code> using nano is:
 
+
<code><nowiki>nano example.log</nowiki></code>
===== Real-Time Tail =====
+
Press Ctrl+X to exit.  It will ask if you want to save your changes when you exit, but unless you run it with the <code><nowiki>sudo</nowiki></code>
Another neat use for the <code><nowiki>tail</nowiki></code> command is to use it for watching a log in 'real-time' by specifying the <code><nowiki>-f</nowiki></code> switch.  For example, if you wished to watch in real-time as clients access your Apache2 server, issuing a command such as this from a terminal prompt would allow you to do so:
+
command the files won't be writable.  In general, you won't want to save your changes to log files, of course.
 
+
==== Viewing Files ====
<code><nowiki>tail -f /var/log/apache2/access.log</nowiki></code>
+
To simply look at a file, an editor is overkill.  Use the <code><nowiki>less</nowiki></code> command, which pages through a file one screen at a time:
 
+
<code><nowiki>less example.log</nowiki></code>
You will see the log spit out, then stop, and as the Apache2 server is accessed, log entries will fly by in real-time! If you have a very busy server, they will fly by too fast for you to read them!  You can use the <code><nowiki>-f</nowiki></code> switch to view any log file in this manner.
+
You don't need <code><nowiki>sudo</nowiki></code> to look at a file.  Press h for help, or q to quit.  The cursor keys and page up/down keys will work
 
+
as expected, and the slash key ("/") will do a case-sensitive search; the n key repeats the last search.
 +
==== Viewing the Beginning of Files ====
 +
To see the first ten lines of a file, use the <code><nowiki>head</nowiki></code> command:
 +
<code><nowiki>head example.log</nowiki></code>
 +
To see some other number of lines from the beginning of the file, add the -n switch, thus:
 +
<code><nowiki>head -n 20 example.log</nowiki></code>
 +
==== Viewing the End of Files ====
 +
To see the final ten lines of a file, the analogous command is <code><nowiki>tail</nowiki></code>:
 +
<code><nowiki>tail example.log</nowiki></code>
 +
Again, the -n switch gives you control over how many lines it displays:
 +
<code><nowiki>tail -n 20 example.log</nowiki></code>
 +
==== Watching a Changing File ====
 +
Also, the -f ("follow") switch puts tail into a loop, constantly waiting for new additions to the file it's displaying.  This is
 +
useful for monitoring files that are being updated in real time:
 +
<code><nowiki>tail -f example.log</nowiki></code>
 +
Press Ctrl+C to quit the loop.
 +
==== Searching Files ====
 +
Because log files can be large and unwieldy, it helps to be able to focus.  The <code><nowiki>grep</nowiki></code> command helps you strip out only the
 +
content you care about.  To find all the lines in a file containing the word "system", for example, use this:
 +
<code><nowiki>grep "system" example.log</nowiki></code>
 +
To find all the lines containing "system" at the beginning of the line, use this:
 +
<code><nowiki>grep "^system" example.log</nowiki></code>
 +
Note the caret symbol, a regular expression that matches only the start of a line.  This is less useful for standard log files,
 +
which always start with a date and time, but it can be handy otherwise. Not all files have a standard format.
 +
Any time the result of a <code><nowiki>grep</nowiki></code> is still too long, you can pipe it through <code><nowiki>less</nowiki></code>:
 +
<code><nowiki>grep "system" example.log | less</nowiki></code>
 
=== Resources ===
 
=== Resources ===
Additional information on system, and application logs, and syslogd is available via the following resources:
+
Additional information on system and application logs and <code><nowiki>syslogd</nowiki></code> is available via the following resources:
 
+
 
==== Local System Resources ====
 
==== Local System Resources ====
 
 
{|border="1" cellspacing="0"
 
{|border="1" cellspacing="0"
 
|<code><nowiki>man dmesg</nowiki></code>|| System manual page for the <code><nowiki>dmesg</nowiki></code> kernel ring buffer utility
 
|<code><nowiki>man dmesg</nowiki></code>|| System manual page for the <code><nowiki>dmesg</nowiki></code> kernel ring buffer utility
第312行: 第249行:
 
|<code><nowiki>man tail</nowiki></code>|| System manual page for the <code><nowiki>tail</nowiki></code> utility
 
|<code><nowiki>man tail</nowiki></code>|| System manual page for the <code><nowiki>tail</nowiki></code> utility
 
|}
 
|}
 
 
==== WWW Resources ====
 
==== WWW Resources ====
 
 
[http://www.samag.com/documents/s=1146/sam0109m/0109m.htm Checking Your System Logs with awk]
 
[http://www.samag.com/documents/s=1146/sam0109m/0109m.htm Checking Your System Logs with awk]
 
 
[http://www.linuxvoodoo.com/resources/howtos/syslog/ Syslog - Watching Your Logs]
 
[http://www.linuxvoodoo.com/resources/howtos/syslog/ Syslog - Watching Your Logs]
 
 
[http://www-128.ibm.com/developerworks/linux/library/l-roadmap5/ Windows-to-Linux roadmap: Part 5. Linux logging (IBM)]
 
[http://www-128.ibm.com/developerworks/linux/library/l-roadmap5/ Windows-to-Linux roadmap: Part 5. Linux logging (IBM)]
 
 
[http://www.linuxsecurity.com/content/view/116430/151/ Sawing Linux Logs With Simple Tools]
 
[http://www.linuxsecurity.com/content/view/116430/151/ Sawing Linux Logs With Simple Tools]
 
----
 
----
[[category:CategoryDocumentation]]
 
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2009年5月12日 (二) 17:43的最新版本

Introduction

One of the things which makes GNU/Linux a great operating system is that virtually anything and everything happening on and to the system may be logged in some manner. This information is invaluable for using the system in an informed manner, and should be one of the first resources you use to trouble-shoot system and application issues. The logs can tell you almost anything you need to know, as long as you have an idea where to look first. Your Ubuntu system provides vital information using various system log files. These log files are typically plain ASCII text in a standard log file format, and most of them sit in the traditional system log subdirectory /var/log. Many are generated by the system log daemon, syslogd on behalf of the system and certain applications, while some applications generate their own logs by writing directly to filesin /var/log. This guide talks about how to read and use several of these system log files, how to use and configure the system logging daemon, syslogd, and how log rotation works. See the Resources section for additional information.

Target Audience

This guide will be simple enough to use if you have any experience using the console and editing text files using a text editor. See the end of this document for some essential commands that may help you find your way around these files if you're relatively new to the command line.

System Logs

System logs deal primarily with the functioning of the Ubuntu system, not necessarily with additional applications added by users. Examples include authorization mechanisms, system daemons, system messages, and the all-encompassing system log itself, syslog.

Authorization Log

The Authorization Log tracks usage of authorization systems, the mechanisms for authorizing users which prompt for user passwords, such as the Pluggable Authentication Module (PAM) system, the sudo command, remote logins to sshd and so on. The Authorization Log file may be accessed at /var/log/auth.log. This log is useful for learning about user logins and usage of the sudo command. Use grep to cut down on the volume. For example, to see only information in the Authorization Log pertaining to sshd logins, use this: grep sshd /var/log/auth.log | less

Daemon Log

A daemon is a program that runs in the background, generally without human intervention, performing some operation important to the proper running of your system. The daemon log at /var/log/daemon.log and contains information about running system and application daemons such as the Gnome Display Manager daemon gdm, the Bluetooth HCI daemon hcid, or the MySQL database daemon mysqld. This can help you trouble-shoot problems with a particular daemon. Again, use grep to find specific information, plugging in the name of the daemon you're interested in.

Debug Log

The debug log at /var/log/debug and provides detailed debug messages from the Ubuntu system and applications which log to syslogd at the DEBUG level.

Kernel Log

The kernel log at /var/log/kern.log provides a detailed log of messages from the Ubuntu Linux kernel. These messages may prove useful for trouble-shooting a new or custom-built kernel, for example.

Kernel Ring Buffer

The kernel ring buffer is not really a log file per se, but rather an area in the running kernel you can query for kernel bootup messages via the dmesg utility. To see the messages, use this: dmesg | less Or to search for lines that mention the Plug & Play system, for example, use grep like this: dmesg | grep pnp | less By default, the system initialization script /etc/init.d/bootmisc.sh sends all bootup messages to the file /var/log/dmesg as well. You can view and search this file the usual way.

Messages Log

The messages log contains informational messages from applications, and system facilities, and is available at /var/log/messages. This log is useful for examining message output from applications, and system facilities which log to the syslog / sysklog daemon at the INFO level.

System Log

The system log typically contains the greatest deal of information by default about your Ubuntu system. It is located at /var/log/syslog, and may contain information other logs do not. Consult the System Log when you can't locate the desired log information in another log.

Application Logs

Many applications also create logs in /var/log. If you list the contents of your /var/log subdirectory, you will see familiar names, such as /var/log/apache2 representing the logs for the Apache 2 web server, or /var/log/samba, which contains the logs for the Samba server. This section of the guide introduces some specific examples of application logs, and information contained within them.

Apache HTTP Server Logs

The default installation for Apache2 on Ubuntu creates a log subdirectory: /var/log/apache2. Within this subdirectory are two log files with two distinct purposes:

  • /var/log/apache2/access.log - records of every page served and every file loaded by the web server.
  • /var/log/apache2/error.log - records of all error conditions reported by the HTTP server

By default, every time Apache accesses a file or page, the access logs record the IP address, time and date, browser identification string, HTTP result code and the text of the actual query, which will generally be a GET for a page view. Look at the Apache documentation for a complete rundown; quite a lot can be gleaned from this file, and indeed many statistical packages exist that perform analyses of these logs. Also, every time any error occurs, Apache adds a line to the error log. If you run PHP with error and warning messages disabled, this can be your only way to identify bugs.

CUPS Print System Logs

The Common Unix Printing System (CUPS) uses the default log file /var/log/cups/error_log to store informational and error messages. If you need to solve a printing issue in Ubuntu, this log may be a good place to start.

Rootkit Hunter Log

The Rootkit Hunter utility (rkhunter) checks your Ubuntu system for backdoors, sniffers and rootkits, which are all signs of compromise of your system. The log rkhunter uses is located at /var/log/rkhunter.log.

Samba SMB Server Logs

The Server Message Block Protocol (SMB) server, Samba is popularly used for sharing files between your Ubuntu computer and other computers which support the SMB protocol. Samba keeps three distinct types of logs in the subdirectory /var/log/samba:

  • log.nmbd - messages related to Samba's NETBIOS over IP functionality (the network stuff)
  • log.smbd - messages related to Samba's SMB/CIFS functionality (the file and print sharing stuff)
  • log.[IP_ADDRESS] - messages related to requests for services from the IP address contained in the log file name, for example, log.192.168.1.1.

X11 Server Log

The default X11 Windowing Server in use with Ubuntu is the Xorg X11 server, and assuming your computer has only one display defined, it stores log messages in the file /var/log/Xorg.0.log. This log is helpful for diagnosing issues with your X11 environment.

Non-Human-Readable Logs

Some log files found in the /var/log subdirectory are designed to be readable by applications, not necessarily by humans. Some examples of such log files which appear in /var/log follow.

Login Failures Log

The login failures log located at /var/log/faillog is actually designed to be parsed and displayed by the faillog command. For example, to print recent login failures, use this: faillog

Last Logins Log

The last logins log at /var/log/lastlog should not typically be parsed and examined by humans, but rather should be used in conjunction with the lastlog command. For example to see a listing of logins with the lastlog command, displayed one page per screen with the less command, use the following command: lastlog | less

Login Records Log

The file /var/log/wtmp contains login records, but unlike /var/log/lastlog above, /var/log/wtmp is not used to show a list of recent logins, but is instead used by other utilities such as the who command to present a listed of currently logged in users. This command will show the users currently logged in to your machine: who

System Logging Daemon (syslogd)

The system logging daemon syslogd, also known as sysklogd, awaits logging messages from numerous sources and routes the messages to the appropriate file or network destination. Messages logged to syslogd usually contain common elements like system hostnames and time-stamps in addition to the specific log information.

Configuration of syslogd

The syslogd daemon's configuration file is /etc/syslog.conf. Each entry in this file consists of two fields, the selector and the action. The selector field specifies a facility to be logged, such as for example the auth facility which deals with authorization, and a priority level to log such information at, such as info, or warning. The action field consists of a target for the log information, such as a standard log file (i.e. /var/log/syslog), or the hostname of a remote computer to send the log information to.

Echoing Messages to syslogd With Logger

A neat utility exists in the logger tool, which allows one to place messages into the System Log (i.e. /var/log/syslog) arbitrarily. For example, assume your user name is buddha, and you would like to enter a message into the syslog about a particularly delicious pizza you're eating, you could use a command such as the following at a terminal prompt: logger This Pizza from Vinnys Gourmet Rocks and you would end up with a line in the /var/log/syslog file like this:

Jan 12 23:34:45 localhost buddha: This Pizza from Vinnys Gourmet Rocks

You can even specify a tag the messages come from, and redirect the output standard error too.

#!/bin/bash
#
# sample logger error jive
#
logmsg="/usr/bin/logger -s -t MyScript "

# announce what this script is, even to the log
$logmsg "Directory Checker FooScript Jive 1.0"

# test for the existence of Fred's home dir on this machine
if [ -d /home/fred ]; then
   $logmsg "I. Fred's Home Directory Found"
else
   $logmsg "E. Fred's Home Directory was NOT Found. Boo Hoo."
   exit 1
fi

Executing this script as chkdir.sh on the machine butters where Fred does not have a home directory, /home/fred, gives the following results:

bumpy@butters:~$./chkdir.sh
MyScript: Directory Checker FooScript Jive 1.0
MyScript: E. Fred's Home Directory was NOT Found. Boo Hoo.
bumpy@butters:~$tail -n 2 /var/log/syslog
Jan 12 23:23:11 localhost MyScript: Directory Checker FooScript Jive 1.0
Jan 12 23:23:11 localhost MyScript: E. Fred's Home Directory was NOT Found. Boo Hoo.

So, as you can see, we received the messages both via standard error, at the terminal prompt, and they also appear in our syslog.

Log Rotation

When viewing directory listings in /var/log or any of its subdirectories, you may encounter log files with names such as daemon.log.0, daemon.log.1.gz, and so on. What are these log files? They are 'rotated' log files. That is, they have automatically been renamed after a predefined time-frame, and a new original log started. After even more time the log files are compressed with the gzip utility as in the case of the example daemon.log.1.gz. The purpose of log rotation is to archive and compress old logs so that they consume less disk space, but are still available for inspection as needed. What handles this functionality? Why, the logrotate command of course! Typically, logrotate is called from the system-wide cron script /etc/cron.daily/logrotate, and further defined by the configuration file /etc/logrotate.conf. Individual configuration files can be added into /etc/logrotate.d (where the apache2 and mysql configurations are stored for example). This guide will not cover the myriad of ways logrotate may be configured to handle the automatic rotation of any log file on your Ubuntu system. For more detail, check the Resources section of this guide. IconsPage?action=AttachFile&do=get&target=IconNote.png NOTE: You may also rotate system log files via the cron.daily script /etc/cron.daily/sysklogd instead of using logrotate. Actually, the utility savelog may produce unexpected results on log rotation which configuring logrotate seems to have no effect on. In those cases, you should check the cron.daily sysklogd script in /etc/cron.daily/sysklogd and read the savelog manual page to see if savelog is not in fact doing the rotation in a way that is not what you are specifying with logrotate.

Essential Commands

If you're new to the console and the Linux command line, these commands will get you up and running to the point where you can work with log files at a basic level.

Getting Started

To change to the log directory, where most of these files sit, use the cd command. This saves having to type out a full path name for every subsequent command: cd /var/log

Editing Files

You can view and edit files in GEdit or Kate, the simple text editors that come with Ubuntu and Kubuntu respectively, but these can be overkill when all you want to do is look at a file or make simple changes. The easiest editor to use from the console is nano, which is less powerful but also less complicated than vim or emacs. The command to edit a particular logfile /var/log/example.log using nano is: nano example.log Press Ctrl+X to exit. It will ask if you want to save your changes when you exit, but unless you run it with the sudo command the files won't be writable. In general, you won't want to save your changes to log files, of course.

Viewing Files

To simply look at a file, an editor is overkill. Use the less command, which pages through a file one screen at a time: less example.log You don't need sudo to look at a file. Press h for help, or q to quit. The cursor keys and page up/down keys will work as expected, and the slash key ("/") will do a case-sensitive search; the n key repeats the last search.

Viewing the Beginning of Files

To see the first ten lines of a file, use the head command: head example.log To see some other number of lines from the beginning of the file, add the -n switch, thus: head -n 20 example.log

Viewing the End of Files

To see the final ten lines of a file, the analogous command is tail: tail example.log Again, the -n switch gives you control over how many lines it displays: tail -n 20 example.log

Watching a Changing File

Also, the -f ("follow") switch puts tail into a loop, constantly waiting for new additions to the file it's displaying. This is useful for monitoring files that are being updated in real time: tail -f example.log Press Ctrl+C to quit the loop.

Searching Files

Because log files can be large and unwieldy, it helps to be able to focus. The grep command helps you strip out only the content you care about. To find all the lines in a file containing the word "system", for example, use this: grep "system" example.log To find all the lines containing "system" at the beginning of the line, use this: grep "^system" example.log Note the caret symbol, a regular expression that matches only the start of a line. This is less useful for standard log files, which always start with a date and time, but it can be handy otherwise. Not all files have a standard format. Any time the result of a grep is still too long, you can pipe it through less: grep "system" example.log | less

Resources

Additional information on system and application logs and syslogd is available via the following resources:

Local System Resources

man dmesg System manual page for the dmesg kernel ring buffer utility
man faillog System manual page for the faillog command (and also the faillog configuration file via man 5 faillog)
man grep System manual page for the grep pattern searching utility
man head System manual page for the head utility
man klogd System manual page for the kernel log daemon (klogd)
man last System manual for the last command which shows last logged in users
man less System manual page for the less paging utility
man logger System manual page for the logger command-line interface to syslog utility
man logrotate System manual page for the the logrotate utility
man savelog System manual page for the savelog log file saving utility
man syslogd System manual page for the system log daemon (syslogd)
man syslog.conf System manual page for the syslogd configuration file
man tail System manual page for the tail utility

WWW Resources

Checking Your System Logs with awk Syslog - Watching Your Logs Windows-to-Linux roadmap: Part 5. Linux logging (IBM) Sawing Linux Logs With Simple Tools