|


| NAVIGATION |
|
Home |
|
Store |
|
|
| INSIDE MAC |
|
Television Shows |
|
Broadcast Shows |
|
Daily News Shows |
|
Special Shows |
|
|
| EVENTS |
|
|
|
|
|
|
|
|
| DAILY TIPS |
|
Design |
|
Mac OS X |
|
Mac OS X UNIX |
|
|
| COMMUNITY |
|
Forums |
|
Surveys |
|
|
| NEWS |
|
Current |
|
Press |
|
Archive |
|
|
| FEATURES |
|
Editorial |
|
Dr. Mac |
|
Reviews |
|
Reader Reports |
|
|
| RESOURCES |
|
FAQ |
|
Documentation |
|
Learning Center |
|
MAN pages |
|
Glossary |
|
Tutorials |
|
Tips |
|
Links |
|
|
|

|
|
|
OSXFAQ Mac OS X UNIX Tip-of-the-Day

Week 91 - User Accounts (6 December 2004)
by
Adrian Mayo - Editor, OSXFAQ
Wednesday - Add a New User
This script adds a new user account to OS X. Give the user's first and last names, the desired user id, and if they are to be a normal (staff) or admin user.
The user id is supplied because normal account creation in OS X does not give this option. The user's short name will be equal to their first name. A home directory will be created so the user is a fully-fledged OS X account holder and can log in in the normal manner.
The script will prompt for a password for the new user.
Lots of checks are made, as you can see from the script comments.
Grab the script from here.
NOTE: THIS SCRIPT IS WRITTEN FOR PANTHER (10.3)
#!/bin/bash
# Create a user.
# Takes the user's firstname (=shortname), lastname, uid, and staff|admin
# and creates:
# a new user in NetInfo passwd
# a new /Users/firstname home directory
usage ()
{
echo "Create a new staff or admin user"
echo "Usage: ${0##*/} firstname lastname uid staff|admin"
if [ "$*" != "" ]; then echo " Error: $*"; fi
exit 1
}
# The script must be run by root
#
if [ "$USER" != "root" ]; then
echo "Must be run as root."
exit 1
fi
# Check parameters
#
if [ $# -ne 4 ]; then
usage
fi
first=$1; last=$2; uid=$3; accnt=$4
# check that the users does not already have a home directory
if [ -e /Users/$first ]; then
usage "User $first already exists at /Users/$first"
fi
# search NetInfo for the given user - it should not exist
str="$(nireport . /users name | grep -w $first)"
if [ ! -z "$str" ]; then
usage "User $first already exists (but does not have a home directory)"
fi
# search NetInfo for the given uid - it should not exist
str="$(nireport . /users uid | grep -w $uid)"
if [ ! -z "$str" ]; then
usage "User ID $uid already exists"
fi
# search NetInfo for the given group - it should not exist
str="$(nireport . /groups name | grep -w $first)"
if [ ! -z "$str" ]; then
usage "Group $first already exists"
fi
# search NetInfo for the given gid - it should not exist
str="$(nireport . /groups gid | grep -w $uid)"
if [ ! -z "$str" ]; then
usage "Group ID $uid already exists"
fi
# ensure either staff or admin is given
if [ $4 != staff ] && [ $4 != admin ]; then
usage "Give account type as 'staff' or 'admin'"
fi
# Add the new user to NetInfo
#
# add user and essential properties
dscl . create /users/$first
dscl . create /users/$first name $first
dscl . create /users/$first passwd "*"
dscl . create /users/$first hint ""
dscl . create /users/$first uid $uid
dscl . create /users/$first gid $uid
dscl . create /users/$first home /Users/$first
dscl . create /users/$first shell /bin/bash
dscl . create /users/$first realname "$first $last"
dscl . create /users/$first picture "/Library/User Pictures/Fun/Smack.tif"
dscl . create /users/$first sharedDir Public
# add some other properties that are usually in NetInfo
dscl . create /users/$first _shadow_passwd ""
dscl . create /users/$first _writers_hint $first
dscl . create /users/$first _writers_real_name $first
# add the new group
dscl . create /groups/$first
dscl . create /groups/$first name $first
dscl . create /groups/$first passwd "*"
dscl . create /groups/$first gid $uid
echo "New user and group $first created"
# Add admin users to the admin group
#
if [ $4 = admin ]; then
dscl . merge /groups/admin users $first
dscl . merge /groups/appserverusr users $first
dscl . merge /groups/appserveradm users $first
echo "$first added to groups admin, appserverusr, appserveradm"
fi
# Create the home directory, populate from the template, and set owners
#
mkdir /Users/$first
if [ ! -d /Users/$first ]; then
echo "Unable to create the user's home directory /Users/$first"
exit
fi
ditto -rsrc /System/Library/User\ Template/English.lproj/ /Users/$first
chown -R ${first}:$first /Users/$first
echo "Home directory /Users/$first created and populated"
# Now give the user a password
#
echo "A password for this account must be given, it is currently blank"
passwd $first
exit 0
If you want to learn more about Mac OS X Unix visit the Learning Center
click.
- For beginners: Mac OS X Unix Tutorials
- For detailed information on specific topics: Advanced Unix
- For OS X in gereral: Mac OS X Tutorials
|





|
 |
|
 |
Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved. |
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. |
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |
|