UNIX Hints & Hacks

ContentsIndex

Chapter 5: Account Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

5.1 User Account Names

 

5.5 GECOS Field

 

5.9 User Account Startup Files

 

5.13 Finding My Display

5.2 Passwords

 

5.6 Home Directories

 

5.10 Using Aliases

 

5.14 Copy Files to Multiple Home Directories

5.3 UID

 

5.7 Shells and the Password File

 

5.11 MS-DOS Users

 

5.15 Kill an Account

5.4 Group IDs and /etc/group

 

5.8 Configuring an Account

 

5.12 Changing Shells

 

5.16 Nulling the Root Password Without vi

 

5.13 Finding My Display

5.13.1 Description

5.13.1 Description

A little hack that determines where your display actually is.

Example

Flavor: BSD

Shells: bash, ksh, sh

Do you work in a large networked environment and find yourself frequently running rlogin or telnet across the network? Are you always annoyed by constantly needing to set the DISPLAY variable every time you want to point at the X display of the machine you are working on? Put the following lines into the .profile of your startup file in your home directory to automatically set the DISPLAY variable for you:

if [ "$DISPLAY" = "" ]; then
   if [ "`tty`" != "/dev/console" -o "$TERM" != "console" ]; then
      REMOTEHOSTNAME="`who am i | cut -d'(' -f2,2 | cut -d')'-f1,1`"
      DISPLAY="$REMOTEHOSTNAME:0"
      export DISPLAY
      fi
   fi

Line 1: Check whether the $DISPLAY has no value and continue.

Line 2: If the tty is not the console device and the terminal doesn't equal the console, continue.

Line 3: Get the name of the host you came in from. This is determined by taking the output of the who am I command and outputting the hostname from what's between the parenthesis.

Line 4: Set the DISPLAY to the system you came from.

Line 5: Exit.

As you log in to a local workstation, the $DISPLAY variable is checked to see whether it has no string defined. If there is none, the routine then checks whether you are local to the console. In this case you are local and nothing is done. If you telnet into a remote host, the $DISPLAY variable is checked to see whether it has a string defined. If there is no string defined, the routine checks to see whether you are local to the console. In this case you are not, so the routine does a who am i. This command outputs information about who is logged in, the terminal device, a time stamp, and the reverse name lookup of the machine that you are coming from:

rocket 3% who am i
roger        pts/5        Nov  2 07:39    (plane)

It then cuts the output from this command and collects only the hostname of the machine that you're coming from (in parentheses). The DISPLAY variable is then set to this host and exported as a global variable.

Reason

If you get a chance to work at a large installation, you tend to bounce around logging in to one machine after another as you support various users on multiple platforms. It is nice not having to set the DISPLAY variable every time.

Real World Experience

The nice thing about having the DISPLAY set at startup like this is that you can log in from any machine on the network. As you saw earlier (in section 5.10, "Using Aliases"), an alias can be set up to set the DISPLAY variable on your most common machine.

alias setrocket  'setenv DISPLAY rocket:0'  # Display X back on rocket

The only bad thing about this is that you need to have an entry for each of the systems you plan to log in from on the remote systems.

UNIX Hints & Hacks

ContentsIndex

Chapter 5: Account Management

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

5.1 User Account Names

 

5.5 GECOS Field

 

5.9 User Account Startup Files

 

5.13 Finding My Display

5.2 Passwords

 

5.6 Home Directories

 

5.10 Using Aliases

 

5.14 Copy Files to Multiple Home Directories

5.3 UID

 

5.7 Shells and the Password File

 

5.11 MS-DOS Users

 

5.15 Kill an Account

5.4 Group IDs and /etc/group

 

5.8 Configuring an Account

 

5.12 Changing Shells

 

5.16 Nulling the Root Password Without vi

 

© Copyright Macmillan USA. All rights reserved.