User commands and logs

Keeping Log

Article from Issue 152/2013
Author(s):

Many logging and user-related commands can help you check on your system and keep track of security matters. We guide you through several helpful tools.

You can tell that Unix and Linux were designed for multiple logins by all the logs and commands that provide information about users. These tools often detect the first signs of an attempt to break into your system. In an institutional environment, they can be used to check not only what resources are being used but also whether users are doing what they are supposed to be doing.

If you have done any systems administration at all, you have probably used the adduser command. The adduser command (Figure 1) provides a simple wizard to help you create a new account. By default, much of the work is done for you, such as automatically creating a user ID (UID). However, you can choose to specify many details for yourself with options like --gid ID and --shell SHELL.

Figure 1: The command-line wizard with which most user accounts are added.

Or, you might have used useradd instead, which is less convenient for creating a new user than adduser but can be used for editing the details of an already existing user. Both of these commands have counterparts in deluser and userdel, which are similar in function, but whose options are different. For example, userdel has a -f or --force option that deluser lacks. And, userdel uses -r or --remove to delete the user's home directory and mail and printing spools instead of deluser's --remove-home and automatic removal of an account's spools.

But, these are just some of the items in the basic toolkit for users and their groups. At least 10 other logs and commands present information for administrator views – some of them overlap in functionality, but all provide detailed information.

User Information in Logs

Linux stores vast quantities of information about every aspect of the system, and users are no exception. Many logs are kept in plaintext files and are viewable with commands like less or cat. However, many are also long, so, when you are trying to pinpoint problems, you are usually better off using the command tail -p LOGFILE. This command displays only the last dozen lines or so of the file, which are slowly replaced as new system messages are generated.

Other logs are stored in databases, so they are best viewed using the command that has the same name as the file.

lastlog

The /var/log/lastlog file is a database that records the previous logins of users. Entered by itself, the lastlog command gives a complete list of all users who are currently on the system, including system-created ones used by some daemons. Deleted users are not shown. For every user, it shows the port they are (or were) logged into and either the date of their last login or   Never logged in   – at least, not since the password was last changed.

However, the resulting list is long, so you'll probably want to modify it with |less at the end of the command. Better yet, you can use -u or --user, followed by a login name to specify each user to include in the result by name or UID, or to specify a number of users by giving a range of UIDs with the start and end points separated by a hyphen (Figure 2).

Figure 2: The lastlog command shows the last time a user was logged in to the system.

Results can also be modified with -b DAYS or --before DAYS, which prints records that are older than the number of days entered. Alternatively, you can use -t DAYS or --time DAYS to view records that are more recent than the specified number of days.

faillog

The /var/log/faillog database records the number of time that users have tried unsuccessfully to log in. The faillog command makes two uses of the faillog database.

First, the faillog command is used by the root user to check the number of times in which a specific user has failed to log in. This statistic is a possible sign of a partially cracked system or of a user whose account needs to be reenabled, because the system is set to disable users who make a certain number of successive login failures.

You can use either the -a option to display information for all users or the options -u NAME or -u UID to specify a specific user (Figure 3). The results show the number of failed login attempts, followed by the maximum permitted on the system and the date and time of the last unsuccessful attempt. If a user has never had an unsuccessful login attempt, the date given is the earliest the CPU can display – for example: 12/31/69 16:00:00.

Figure 3: Failed logins may indicate an attempt to break into your system. The faillog command shows those failures. It can also disable an account.

The second use of the faillog command is to change how the system responds to failed logins. The options -m NUMBER or --maximum NUMBER set how many login failures can occur before an account is disabled; this is often set to 0 in most distributions, meaning that any number of login failures can occur. Additionally, the number of login failures for a specific account can be reset to 0 with -r or --reset.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Introducing Bash

    Beyond all the splash screens, screen savers, and vivid rock-star wallpaper is the simple yet powerful Bash shell.

  • Command Line: More Secure Passwords

    Even with encrypted drives, account passwords remain one of the prime security methods for any Linux system. With a little knowledge of the available commands and their options, you can make your account passwords more secure.

  • Bash Tricks

    The Bash shell is powerful and infinitely expressive. In this article we describe some tricky techniques you can use to enhance and customize your Bash environment.

  • Command Line – Killing Processes

    Linux offers a variety of tools for shutting down systems and processes. We describe some important commands.

  • Command Line: User Accounts

    Linux is a multiuser system: many users can work at the same machine at the same time on separate accounts. In this month’s column, we take a close look at how to create and manage Linux user accounts from the command line.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News