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.14 Copy Files to Multiple Home Directories

5.14.1 Description

5.14.1 Description

There are times when a file might need to be propagated to every user's home directory.

Example One: Stripping the Password File

Flavors: AT&T, BSD

Shells: sh

This method of propagating files to all users' home directories is a two-step process. The first step is to copy the password file to a secure area and strip out all system-related accounts. This includes bin, ftp, root, sync, nobody, and so on--any account that is not a physical user. Then you run the propagation script against it.

# /etc/passwd /usr/private/admin/passwd

Copy the password to a secure (700) area owned by root.

# vi /usr/private/admin/passwd
root:NqM5kgsU0o./6:0:0:root:/root:/bin/tcsh bin:*:1:1:bin:/bin: daemon:*:2:2:daemon:/sbin: adm:*:3:4:adm:/var/adm: lp:*:4:7:lp:/var/spool/lpd: sync:*:5:0:sync:/sbin:/bin/sync shutdown:*:6:0:shutdown:/sbin:/sbin/shutdown postmaster:*:14:12:postmaster:/var/spool/mail:/bin/bash nobody:*:65534:100:nobody:/dev/null: ftp:*:404:1::/home/ftp:/bin/bash guest:*:405:100:guest:/dev/null:/dev/null

Strip out all the system-related accounts that are not physical users.

# vi cphome
#! /bin/sh cat /usr/private/admin/passwd | while read line do USER=`echo $line | awk -F":" '{print $1}'` DIR=`echo $line | awk -F":" '{print $6}'` cp $1 $DIR chown $USER $DIR/$1 chmod 750 $DIR/$1 done

Line 1: Define the shell.

Line 2: Begin processing through stripped password file.

Line 4: Get each username.

Line 5: Get each home directory.

Line 6: Copy the files to each home directory.

Line 7: Change the ownership to the user.

Line 8: Change the permissions for the user.

The stripped password file gets processed line by line, collecting the username and the home directory. Then, as it processes each line, the script copies the files to the user's home directory, chowns ownership to the user, and grants the permission 750 to the files.

This script can be easily modified to support the changing of UIDs or even the GIDs if needed. You can use it as a building block for modifying and manipulating users' accounts. See if you can think of other possibilities for which the script could be used.

Reasons

Some applications and files live in the home directories of the users. These files can be anything from configuration files to the application startup script. When new versions are loaded, these files often need updating.

Real World Experience

It is easy to modify one of these scripts so that it can do some real damage, really quickly. A script similar to this one was once created to remove a single file out of all the home directories on a system. When the administrator executed the script, he wondered why it was taking over 30 seconds to remove the files from 30 users' home directories. He broke out of the script only to find that all the files in the top level of each of the home directories were getting wiped out. Did he test the script before running it? No. Did he have a typo? Yes. He then had to spend the next day, a Saturday, restoring the files. The good news is that only one of the 30 users was logged in at the time.

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.