Notes: Unix Lab 02

  1. The hostname command
  2. The uname command
  3. The whoami command
  4. The who am i command
  5. The who command
  6. The users command
  7. The w command
  8. The finger command
  9. The date command
  10. The env command
  11. Common shell environmental variables
  12. export command
There are a number of commands which provide information about the machine a user is logged in on, about the user account and about other users on the host.
  1. The hostname command

    The hostname command returns the name of the unix machine which the user's shell is running on.
      -s returns just the short name of the host, no domain information
      -d returns just the domain name without the name of the host
      
  2. The uname command

    The uname command returns the name of the unix host on which the user's shell is running. It has several options which can display additional information about the host.

      -o returns the operating system name
      -m returns the machine type
      -a displays all information uname can determine about the host
      
  3. The whoami command

    whoami simply returns the login name of the user who is logged in and running the current shell. It only returns the login ID and no additional information.
  4. The who am i command

    This is not really a command but another way of running "who -m". On some systems this can be replaced with almost any odd text such as who did this.

    An example:
     
    mark@platypus:~>
    mark@platypus:~> who am i
    mark     pts/39       Jan 19 02:28 (dialup1952.wnskvtao.sover.net)
    mark@platypus:~> who found me
    mark     pts/39       Jan 19 02:28 (dialup1952.wnskvtao.sover.net)
    mark@platypus:~>
    

    The output lists the login name of the user running the shell, what terminal they are logged in on, when they logged in, and how they are connected to the host. In the above example the username is "mark", the terminal is identified as pts/39 (a pseudo terminal), the user logged in on Jan 19 and was logged in from a computer in the sover.net domain.

  5. The who command

    The who command returns a list of all users currently logged in on the host where the shell is running.

     
    mark@platypus:~> who
    mark     :0           Jan 18 12:55
    mark     pts/40       Jan 19 11:38 (dialup2139.wnskvtao.sover.net)
    mark@platypus:~>
    

  6. The users command

    The users command simply ists the login names of all accounts which are presently logged in.

     
    tuckerm@kangaroo:~> users
    fingerhutb fingerhutb fingerhutb fingerhutb fingerhutb fingerhutb tuckerm
    tuckerm@kangaroo:~>
    

    Notice that if the same user is logged on more than one terminal their username will be listed more than once.

  7. The w command

    The w command returns a list of usernames of who is logged in and some information about what they are doing.

     
     
    [loriotg@gollum ~] w
     16:53:39 up 68 days, 21:53,  3 users,  load average: 0.67, 0.38, 0.30
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    loriotg  tty8     :0               13:25   68days  7:14   0.23s gnome-session
    loriotg  pts/0    :0.0             16:14    4:55   0.88s  0.83s ssh -X -Y apoll
    loriotg  pts/1    :0.0             14:07    0.00s  0.04s  0.01s w
    [loriotg@gollum ~] 
    
    

    The output first line lists some statistics about the computer followed by row headings for the remainder of the output. The USER column lists the username(s) for all users logged in on the computer. TTY lists the terminal or graphical display the user is logged in on. FROM lists where the user is logged in from (the dash "-" indicates a login on the local graphical display. LOGIN@ is the time that the user logged in. IDLE lists how much time the user has been IDLE. The IDLE time is based on CPU utilization and may not really reflect the wall clock. JCPU lists how much cpu time the user has utilized from this terminal. PCPU list how much cpu time the user's process (listed in the WHAT column) has taken. The WHAT column lists the process currently being run by the user on the listed terminal at the time the w program was run.

  8. The finger command

    finger is a program that will display a list of users logged in on the system and some information about their account.

     
    mark@platypus:~> finger
    Login     Name          Tty      Idle  Login Time   Office     Office Phone
    mark      Mark Tucker  *:0             Jan 18 12:55 vail 444a  x6328
    mark      Mark Tucker  *pts/18     10  Jan 19 11:40 (:pts/40:S.0)
    mark      Mark Tucker   pts/30   9:26  Jan 19 11:40 (:pts/40:S.1)
    mark      Mark Tucker   pts/39         Jan 19 11:40 (:pts/40:S.2)
    mark      Mark Tucker  *pts/40         Jan 19 11:38 (dialup2139.wnskvtao.sover.net)
    mark@platypus:~> 
    

    finger may be used to get information about a specific user regardless of being logged in or not.
     
    tuckerm@kangaroo:~> finger fingerhutb
    Login: fingerhutb                       Name: Bill Fingerhut
    Directory: /mnt/homes/fingerhutb        Shell: /bin/tcsh
    Office: Faculty, 20030601
    On since Mon Jan 10 14:27 (GMT) on pts/0 (messages off)
    On since Mon Jan 10 14:28 (GMT) on pts/1 from :42.0 (messages off)
    On since Mon Jan 10 14:28 (GMT) on pts/2 from :42.0 (messages off)
    On since Thu Jan 13 18:21 (GMT) on pts/142 (messages off)
    On since Thu Jan 13 18:22 (GMT) on pts/143 from :42.0 (messages off)
    On since Thu Jan 13 18:22 (GMT) on pts/144 from :42.0 (messages off)
    Last login Thu Jan 13 18:21 (GMT) on pts/141 from vail433win.lsc.vsc.edu
    No mail.
    No Plan.
    tuckerm@kangaroo:~>
    

    finger also has the ability to report information about users on a remote host. This has proven to be a security problem over the years and is not usually available on most networks. Here is an example of how it doesn't work:
     
    mark@platypus:~> finger tuckerm@metlab16
    [metlab16.lsc.vsc.edu]
    finger: connect: Connection refused
    mark@platypus:~>
    

  9. The date command

    The date command will return the system date and time in various formats depending on what options and arguments are passed to the command. There are many, many ways of manipulating the output from date. By default, date returns the current date and time for the set time zone, formatted by the system default language.
     
    mark@platypus:~> date
    Wed Jan 19 13:38:06 GMT 2005
    mark@platypus:~> 
    

    date has a quite a few ways of specifying the format of the output so that the exact data requested can be printed.

       %Y     year (1970...)   
       %j     day of year (001..366)
       %a     locale's abbreviated weekday name (Sun..Sat)
       %b     locale's abbreviated month name (Jan..Dec)
       %d     day of month (01..31)
       %H     hour (00..23)
       %M     minute (00..59)
       %Z     time zone (e.g., EDT), or nothing if no time zone is determinable
       

    An example of how to specify these formats:
     
    mark@platypus:~> date +%Y
    2005
    mark@platypus:~> 
    

    or print the year and month:
     
    mark@platypus:~> date +%b%Y
    Jan2005
    mark@platypus:~>
    

  10. The env command

    The shell keeps numerous pieces of information stored in memory know as environment variables. These pieces of information are organized where each has a name to identify it. Each variable usually has some value, although it is not required. The env command will lists all of these name value pairs that are stored by the current shell. Note that each shell has its own set of environment variables and that changing a value in one shell will not have any impact on a shell running on another terminal - even for the same user on the same machine. These values are used by the shell to determine how some functions will behave. Many unix programs look to their shell environment to determine how to function, what to operate on, or where to find data files.

    A sample of env output:
     
    mark@platypus:~> env
    CPLUS_INCLUDE_PATH=/usr/lib/qt/include:/usr/lib/qt/include
    MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man:/usr/lib/java/man:/usr/lib/qt/doc
    /man:/usr/share/texmf/man
    SSH_AGENT_PID=8094
    HOSTNAME=platypus.lsc.vsc.edu
    MRXVT_TABTITLE=Terminal
    TERM=rxvt
    SHELL=/bin/bash
    DBUS_SESSION_BUS_PID=8107
    WINDOWID=31457282
    QTDIR=/usr/lib/qt
    USER=mark
    SSH_AUTH_SOCK=/tmp/ssh-Obqlxp8070/agent.8070
    KDEDIR=/opt/kde
    SESSION_MANAGER=local/platypus:/tmp/.ICE-unix/8109
    USERNAME=mark
    XPSERVERLIST=
    T1LIB_CONFIG=/usr/share/t1lib/t1lib.config
    MINICOM=-c on
    PATH=/software/mcidas/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games
    :/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:/usr/share
    /texmf/bin:.:/software/gempak/GEMPAK5.7.4/bin/linux:/software/gempak/GEMPAK5.7.4
    /bin/scripts:/opt/kde/bin:/software/vmware/bin
    DESKTOP_SESSION=xfce
    LC_COLLATE=C
    GDM_XSERVER_LOCATION=local
    INPUTRC=/etc/inputrc
    PWD=/home/mark
    

    The shell environment variables can be referenced individually by prefixing their name with a dollar sign ($). Here is an example using the USER environment variable: mark@platypus:~> echo $USER mark mark@platypus:~>

  11. Common shell environmental variables

    Some of the more commonly referenced environment variables and what they are used for: $HOSTNAME lists the name of the machine the shell is running on.
    $HOME contains the path to the user's home directory
    $PATH contains a colon separated lists of directory paths. These paths are used by the shell to search for executable programs.
    $USERNAME the login name of the user running the shell
    $LOGNAME generally the same as $USERNAME
    $PWD contains the path to the current working directory
    $TERM contains the name of the current terminal type
    $SHELL indicates what shell the user is currently running
    $PAGER contains the default program to use for browsing text files
  12. export

    Environment variables can be created and removed by the user. The export command will create a variable and assign it a value. If the variable already exists, its value is updated with the export command.

     
    mark@platypus:~> 
    mark@platypus:~> echo $TESTVAR
    TESTVAR: Undefined variable.
    mark@platypus:~> 
    mark@platypus:~> export TESTVAR=9999
    mark@platypus:~> echo $TESTVAR
    9999
    mark@platypus:~> 
    mark@platypus:~> export TESTVAR=zoom
    mark@platypus:~> echo $TESTVAR
    zoom
    mark@platypus:~> 
    

    Environment variables can be removed entirely with the unset command to remove the variable.

     
    mark@platypus:~> echo $TESTVAR
    9999
    mark@platypus:~> unset TESTVAR 
    mark@platypus:~> echo $TESTVAR
    TESTVAR: Undefined variable.
    mark@platypus:~> 
    

Lab 02 Homework
Last modified: Fri May 22 12:45:44 EDT 2009