sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Review
module list

Gathering Information about users

Often the administrator needs to ask questions about the system or about a specific user. This section briefly reviews simple commands to accomplish this. We will add many more system monitoring skills as the course develops.
Preview question: If you need information on a particular user, where do you look? Suppose you need to know what shell they use? when they last logged on? what they are working on currently? Can you find out if anyone is currently logged in remotely?

Getting information about a specific user

For the human eye, the simplest and oldest way to request information about a user is to use finger. Its use, however, has fallen from favor as it appears to be slow and its use across a network is insecure. On linux, finger on the local machine has been replaced by pinky. To get most of the traditional output of finger with pinky, use pinky -l:

-bash$ pinky -l gboyd
Login name: gboyd                       In real life:  God of Pluto
Directory: /users/gboyd                 Shell:  /bin/bash
Project: Unix/Linux Instructor
Plan:
To learn everything about Unix
and teach some of it.
$

In earlier Unix days, finger was used to communicate your current work to other users on the system. This was done using two static data files in the user's home directory. The .project file was a single line of information that indicated what you were currently working on (e.g., Unix system administrator). The .plan file was arbitrarily long and included whatever other information you wanted to share with the world. Both of these files are displayed by pinky.

The consistent output format of finger (now pinky) allows the extraction of pieces of its information using the tools we have discussed. When writing a shell program, however, it is often more useful to request specific pieces of information from individual programs.

Individual statistics about a user

The master repository for user information is /etc/passwd. It contains the user's shell, home directory, and, assuming it has been set by the administrator or by the user using chfn, the user's real name. (On linux, the chfn program is still used even though finger is named pinky.)  However, the /etc/passwd file only contains information about local users - network accounts are not included. This makes it less useful in most environments.

System programs such as id and the shell do not distinguish between local and networked accounts. Thus, they are much more useful as a source for a user's id numbers or groups.

The path to the user's home directory, irrespective of whether the user is a local or networked account, can also be found at the command-line using ~login.

$ echo ~root
/root
$

This can be difficult to use in a shell program, however,  since, in a program, the id of the user you are interested in is usually kept in a variable. This means that the expansion

$ user=root
$ echo ~$user
~root
$

does not work. This is because there are two substitutions to be performed on this line, the variable and the tilde, and, unfortunately, the tilde substitution is performed first. To circumvent this, you must force the shell to evaluate the line twice using eval:

$ user=root
$ eval echo ~$user
/root
$

This is a handy trick, but it has a drawback when using it in a shell program: you cannot easily tell whether the user exists! To determine this requires a careful grep of the /etc/passwd file, or, better, a run of the program id:

$ id gboyd
uid=501(gboyd) gid=502(gboyd) groups=502(gboyd)
$ id xxx
id: xxx: No such user
$

The first run of id above succeeds, while the second fails. The failure status of id can be used to determine whether the user exists, then the ~id method used to find their home directory.

Whether the user is logged in currently can be obtained by grepping the output of who for the user's name, so long as it is done carefully:

-bash-3.2$ who | grep '^gboyd '
gboyd    pts/11       2012-03-30 12:56 (xxxx)
-bash-3.2$      

Note the anchor and space to ensure only that user is found. (the xxxx is the remote user's IP address that I have suppressed)

If the user is not currently logged on, last -1 user may be used. The -1 requests only the last login, and will significantly speed up the operation as opposed to last user


System information

Information about who is using the system currently is easily obtained from who. The -T option, which is now the default on some systems, adds information about whether the individual users are remote or local users, and where the connection originated. (XWindows users are indicated by the DISPLAY parameter appearing in the position of the remote user's IP address)

$ who -T
dxy002   + pts/0        2012-03-30 13:25 (xxxx)
awick    + pts/2        2012-03-25 00:35 (xxxx)
mhasle   + pts/3        2012-03-30 09:18 (xxxx)

The current IP configuration can be obtained by running ifconfig, which is usually in /sbin

$ /sbin/ifconfig | grep '[0-9]\.'
          inet addr:147.144.1.2  Bcast:147.144.1.255  Mask:255.255.255.0
          RX bytes:18704115199 (17.4 GiB)  TX bytes:726810017252 (676.8 GiB)
          inet addr:10.4.0.73  Bcast:10.4.0.255  Mask:255.255.255.0
          RX bytes:489498900617 (455.8 GiB)  TX bytes:123838277863 (115.3 GiB)
          inet addr:127.0.0.1  Mask:255.0.0.0
          RX bytes:1391684619 (1.2 GiB)  TX bytes:1391684619 (1.2 GiB)

You can see the result of a poor choice of pattern with grep in the output of this command. (The more modern command to investigate IP addresses is ip addr. Try it. Can you grep it to just get the information you want?)

For a thumbnail view of how busy the system is, use uptime. The last part of the output of this command indicates the system load. A simple rule is that until the system load exceeds 1, you have as much of the machine as you want to yourself!

-bash$ uptime
  13:31:56 up 30 days, 19:00, 10 users,  load average: 0.13, 0.15, 0.1
-bash$ 

Last, if you have a need to generate a mail message for a user automatically, use one of the simple mail programs such as mail and feed the message to it using standard input using either a pipe or a here document. If you want to generate the mail message from within a shell script, you can customize it using variables:

$ user=gboyd
$ meal=lunch
$ mail $user << EOF
> Subject: $meal
> how about $meal today?
> -- andy
> EOF


Prev This page was made entirely with free software on linux:  
the Mozilla Project
and Openoffice.org    
Next

Copyright 2012 Greg Boyd - All Rights Reserved.