| sections in this module | City
                College of San Francisco - CS260A Unix/Linux System Administration Module: Review  | 
          module list | 
| 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$       
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.
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!
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:
| Prev |  This page was made entirely
                with free software on linux:   the Mozilla Project and Openoffice.org  | 
          Next |