How to: Add a user from the OS X command line, works with Leopard!
October 29th, 2007 - Command Line, How to, Leopard, Mac OS X, Tips & Tricks
Adding a user is something easily accomplished using the built in GUI tools that ship with OS X, however any power user can appreciate the possible efficiency gained from using the command line. So in the spirit of efficiency here are the steps necessary to add a user to your Mac OS X system all with our good friend, Terminal.app.
These commands need to be run as either the root user or with the “sudo” command. For more information on the sudo command see the sudo man page.
Create a new entry in the local (/) domain under the category /users.
dscl / -create /Users/toddharris
Create and set the shell property to bash.
dscl / -create /Users/toddharris UserShell /bin/bash
Create and set the user’s full name.
dscl / -create /Users/toddharris RealName "Dr. Todd Harris"
Create and set the user’s ID.
dscl / -create /Users/toddharris UniqueID 503
Create and set the user’s group ID property.
dscl / -create /Users/toddharris PrimaryGroupID 1000
Create and set the user home directory.
dscl / -create /Users/toddharris NFSHomeDirectory /Local/Users/toddharris
Set the password.
dscl / -passwd /Users/toddharris PASSWORD
or
passwd toddharris
If you would like Dr. Harris to be able to perform administrative functions:
dscl / -append /Groups/admin GroupMembership toddharris
Mac Deals from the Apple Store:
Comments:
Comments: 38
Comment from Zamgar
Time: October 30, 2007, 12:30 am
sweet thats really useful
Pingback from University Update - Yahoo - How to: Add a user from the OS X command line, works with Leopard!
Time: October 30, 2007, 1:47 am
[...] YouTube How to: Add a user from the OS X command line, works with Leopard! » This Summary is from an article posted at OS X Daily on Tuesday, October 30, 2007 Adding a user is something easily accomplished using the built in GUI tools that ship with OS X, … toddharris Ads by Yahoo! Summary Provided by Technorati.comView Original Article at OS X Daily » 10 Most Recent News Articles About Yahoo [...]
Comment from NB
Time: October 31, 2007, 3:35 pm
Unable to get it to work? Invalid Path (/)
Comment from david koff
Time: November 5, 2007, 6:08 pm
i love this. i’d created a script a while back for remotely creating a HIDDEN user called “ard” on 10.4.x systems, turning all ARD incoming functions on for that user and editing the login window and fast user-switching windows to NOT mention this user. and now, that script, below, will not work on on leopard. so i’m glad to have this listed here instead!
david
niutil -create / /users/ard
niutil -appendprop / /groups/admin GroupMembership ard
niutil -appendprop / /users/ard “shell” “/bin/bash”
niutil -appendprop / /users/ard “_writers_hint” ard
niutil -appendprop / /users/ard “NFSHomeDirectory” “/var/root”
niutil -appendprop / /users/ard “gid” “405″
niutil -appendprop / /users/ard “uid” “405″
niutil -appendprop / /users/ard “authentication_authority” “;ShadowHash;”
niutil -appendprop / /users/ard “_writers_realname” ard
niutil -appendprop / /users/ard “picture” “/Library/User Pictures/Nature/Zen.tif”
niutil -appendprop / /users/ard “passwd” “whatever-your-password-is”
niutil -appendprop / /users/ard “realname” “ARD”
niutil -appendprop / /users/ard “hint” “whatever-your-password-is”
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -users ard -privs -all -restart -agent -menu
sudo defaults write /Library/Preferences/com.apple.loginwindow \HiddenUsersList -array-add ard
sudo defaults write /Library/Preferences/com.apple.loginwindow SHOWOTHERUSERS_MANAGED -bool false
Comment from ilia
Time: November 10, 2007, 6:48 am
doesn’t work for me.
Data source (/) is not valid.
Comment from Pim
Time: November 13, 2007, 4:25 pm
It changed for leopard. Use “dscl localhost -whatever /Local/Users”, replacing every instance of a solitary “/” with “localhost” and changing /Users to /Local/users. Hope that helps.
Comment from kl
Time: November 14, 2007, 5:34 am
on Leopard I’ve had to use:
> sudo dscl localhost -create /Local/Default/Users/username
Comment from Ivan
Time: November 21, 2007, 2:59 am
On Leopard 10.5 final, substituting “.” for the “/” works. I needed to perform software updates and other admin functions on a Mac with Leopard that I didn’t have the admin password for. Adapting the above steps allowed me to do so (thanks!). What I did:
[boot into single-user mode (hold down cmd-S during early startup)]
/sbin/fsck -fy (to verify integrity of the filesystem)
/sbin/mount -uw / (to be able to make changes to the disk)
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist (to be able to use dscl)
dscl . -create /Users/testuser
dscl . -create /Users/testuser UserShell /bin/bash
dscl . -create /Users/testuser RealName “Test User”
dscl . -create /Users/testuser UniqueID 502 (see notes below about this)
dscl . -create /Users/testuser PrimaryGroupID 80 (I chose to make the user a member of the admin group)
dscl . -create /Users/testuser NFSHomeDirectory /Users/testuser (note this is changed from above)
dscl . -passwd /Users/testuser PASSWORD
dscl . -append /Groups/admin GroupMembership testuser
exit
[restart after Mac OS X loads or login window appears]
[log out if Mac OS X loads straight into a user rather than a login window]
[log in as testuser]
[update software, enable root user, do whatever you like]
And then, when I’m ready to delete the user:
[boot into single-user mode]
/sbin/fsck -fy (to verify integrity of the filesystem)
/sbin/mount -uw / (to be able to make changes to the disk)
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist (to be able to use dscl)
dscl . -delete /Groups/admin GroupMembership testuser
dscl . -delete /Users/testuser
cd Users
rm -rf testuser (warning: will delete any files in user home folder)
exit
[restart or shut down after Mac OS X loads]
I chose the UniqueID (502) in a way consistent with the OS: the number 5xx where xx is one greater than the number of users ever created. To figure out the best available number, before issuing the dscl commands to create the user (but after the launchctl command), I typed:
dsexport users.out /Local/Default dsRecTypeStandard:Users
more users
I then scanned the list of users to see any 5xx users and then chose the next highest one available. There was only one user on this system, so that meant 502. I then deleted the export user info file (rm users.out). (I imagine it probably doesn’t really matter what UniqueID you choose as long as it is unassigned, but I am not enough of a Unix guru to know for certain.)
Comment from Rusty Myers
Time: December 7, 2007, 8:49 pm
Here’s my script using all of your guys suggestions. I’m going to be using it in my work environment at the university. It’s working on 10.5.1
You need to run it as sudo like this:
sudo ./makeuser.sh
Copy this into the makeuser.sh file:
#!/bin/bash
echo “This script will create a user on Leopard”
echo “”
echo “Enter Fullname”
read fullname
echo “Enter username”
read username
echo “Enter usernumber”
read usernumber
echo “Enter Password”
read password
echo “Enter Password again”
read password2
echo “”
if [ $password != $password2 ]
then
echo “Passwords do not match. Exiting…”
exit 0
else
echo “Passwords Match!”
fi
echo “”
echo “Full Name”$fullname
echo “Username:”$username
echo “UID:” $usernumber
echo “Password:” $password
echo “Are these details correct? Y or N?”
read test
echo “”
if [ $test = Y -o $test = y ]
then
echo “Creating User “$username
dscl . -create /Users/$username
dscl . -create /Users/$username UserShell /bin/bash
dscl . -create /Users/$username RealName $fullname
dscl . -create /Users/$username UniqueID $usernumber
dscl . -create /Users/$username PrimaryGroupID 80
dscl . -create /Users/$username NFSHomeDirectory /Users/$username
dscl . -passwd /Users/$username $password
dscl . -append /Groups/admin GroupMembership $username
echo “”
echo $username” created! Enjoy!”
else
echo “Please run script again with correct information.”
fi
That should be it, enjoy! Email me with problems or suggestions. Thanks!
Comment from Bertrand
Time: December 14, 2007, 7:14 pm
Reply to Rusty’s script.
I am writing a similar script and have done pretty much all of the steps you have except I set functions so I can use the code over again easily. Also, in my script I am writing a function that compares the entered information against preexisting keys so that a user cannot get errors by creating users/UniqueIDs/PrimaryGroupIDs etc that already are assigned to other users I’m attempting to use dscl . -list but am having problems setting my variables (UniqueID etc) since the list command always wants to list the /Users as well as any other info you parse… any suggestions would be greatly appreciate.
Thanks
Comment from Bertrand
Time: December 14, 2007, 9:46 pm
this ended up working for me, if anybody has a more elegant way I’d love to learn. I’m just a humble padawan after all…
function checkusername {
local test=$(dscl . -list /Users | grep $SHORTNAME)
if [ "$test" = "$SHORTNAME" ]; then
echo the the username $SHORTNAME already exists
exit
else
echo This would continue the script
fi
}
Comment from Bertrand
Time: December 14, 2007, 9:46 pm
this ended up working for me, if anybody has a more elegant way I’d love to learn. I’m just a humble padawan after all…
function checkusername {
local test=$(dscl . -list /Users | grep $SHORTNAME)
if [ "$test" = "$SHORTNAME" ]; then
echo the the username $SHORTNAME already exists
exit
else
echo This would continue the script
fi
}
Comment from David
Time: December 24, 2007, 9:56 am
Hi Bertrand,
That’s a useful addition — could you integrate it into the script and show the whole thing? I’m feeling lazy this morning. Why define it as a function in the first place?
What I’m really looking for is how to create a new user group from the command line ….
Cheer
Comment from David Liontooth
Time: December 24, 2007, 11:46 am
How to change user and group numbers for an existing user to a non-existing group
- Change user liontooth group staff to user liontooth UID 1000 group liontooth UID 1000
- dscl . create /Groups/liontooth PrimaryGroupID 1000 GroupMembership liontooth
- next log in and in System Prefs, Users, Advanced settings (right-click), set UID and GID to 1000
- as root, cd /Users ; chown -R 1000:1000 liontooth
- reboot — should all be working
Comment from ichi
Time: January 2, 2008, 9:12 pm
all this did for me in 10.5.1 is make the Leopard style “Guest Account” into “Other…”
run it again using NO and it comes back.
Comment from ichi
Time: January 2, 2008, 9:15 pm
oops, i meant to add that this is what what i refer to:
defaults write /Library/Preferences/com.apple.loginwindow Hide500Users -bool YES
oops wrong website!
Comment from RS
Time: January 7, 2008, 2:48 pm
I am having difficulty with David Koff’s solution. I keep getting errors with the niutil stating that I do not have enough arguments. Everything I have tried has failed. Can anyone help?
Comment from RS
Time: January 7, 2008, 2:54 pm
Once I get this script working for creating the hidden user, how do I log in as him/her if their name does not appear anywhere.
Comment from Mert
Time: January 8, 2008, 2:22 am
i like to ask if it’s possible to change user in the script and continue the next steps with the changed user?
In the script, I write : su - example and the program stops to process following codes
Comment from jennj6
Time: January 8, 2008, 7:42 pm
i odnt g et it????
Comment from lucy
Time: January 12, 2008, 3:08 pm
OS X discovery every day!
Comment from Audun Frøysaa
Time: January 24, 2008, 3:51 am
Hello.
When i log in as the new user created with dscl i get a message that the home folder can’t be found.
sudo dscl . -create /Users/$userName
sudo dscl . -create /Users/$userName UniqueID 555
sudo dscl . -create /Users/$userName UserShell /bin/bash
sudo dscl . -create /Users/$userName RealName “$forNavn $etterNavn”
sudo dscl . -create /Users/$userName NFSHomeDirectory /Users/$userName
sudo dscl . -create /Users/$userName PrimaryGroupID 20
sudo dscl . -passwd /Users/$userName $passord
sudo cp -R /System/Library/User\ Template/no.lproj /Users/$userName
sudo chown -R $userName:staff /Users/$userName
I have checked and the /Users/ exist and all the folders are created.
What can be wrong?
Thanks
Comment from Jamie
Time: February 14, 2008, 1:20 pm
sudo dscl . -create /Users/$userName NFSHomeDirectory /Users/$userName
change to
sudo dscl . -create /Users/$userName home /Users/$userName
Pingback from blog.babytux.de » Erfolgreich
Time: February 25, 2008, 10:00 pm
[...] Gut - wie sollte es anders sein - dieser Weg fhrte bei mir nicht zum Ziel: Ich konnte im “Setup Assistent” meine Tastatur nicht mehr benutzen. Also mu es noch andere Wege geben - am einfachsten wre es ja, z.B. unter dem “Single User Mode” einen Benutzer mit Admin-Rechten zu erstellen, oder noch besser, den bestehenden Account wieder zur Admin-Gruppe hinzuzufgen. Ja, auch das ist mglich. [...]
Comment from Rusty Myers
Time: March 12, 2008, 8:22 am
I’m back!
Thanks for your update Bertrand! I took what you did and added it, plus a little dash of checking userID’s too! Here is the new script. So far, I have tested it very successfully. Email me if you have any problems running it or any questions I might be able to help with…rustymyers@gmail.com
I also added the ability to run it from single user mode, should you need to use it there. I bet there is an easier way to check if your in singleuser mode, but I decided just to ask.
I added the dsexport command to check existing userID’s in the script so you don’t have to exit. It also cleans its self afterwards.
I tried to make most of the checks functions.
There is no need to create the user home folder in this script because the system creates it when you log into the account.
You don’t see the user account in the login window if you use a list style, unless restarting. Change it to the username and password style to type it in and log in without restart. Once you log in the first time, you can change it back to list style, or you can just restart.
I will be pushing it out to all my new computers to make user account creation easier, and so I don’t have to go through the setup assistant every time I get a new computer. Hope this helps someone else!
#!/bin/bash
#This script will create a user of your choice using your credentials. The user will not show up in the login window until a restart.
#Function to check the current usernames against the new username.
function checkusername {
local testun=$(dscl . -list /Users | grep $userA)
if [ "$testun" == "$userA" ];
then
echo “The username “$userA” already exists”
exit
else
echo “Username is unique!”
fi
}
#Function to check the current userID’s against the new userID.
function checkuserid {
local testuid=$(dscl . -list /Users UniqueID | grep “$userid” | awk ‘{print $2}’ )
if [ "$testuid" == "$userid" ];
then
echo “The userid “$userid” already exists”
exit
else
echo “UserID is unique!”
fi
}
#Funtion to check that both passwords are the same.
function chkpasswd {
if [ $password != $password2 ]
then
echo “Passwords do not match or are blank. Passwords can’t be blank. Exiting…”
exit 0
else
echo “Passwords Match!”
fi
}
#Step 1, enter new credentials to create user with.
echo “Enter Real Name”
read realname
echo “Enter Username”
read userA
checkusername
echo “Enter Password”
read password
echo “Re-Enter Password”
read password2
chkpasswd
#Check to see if your sure you want to use the entered credentials.
echo “Use these creds?”
echo “Real Name: “$realname
echo “Username: “$userA
echo “Password: “$password
echo “Y or N:”
read creds
if [ $creds = y -o $creds = Y ];
then
echo “Let’s make a user named “$userA” with the password “$password
echo “Are you in Single User Mode? (Default N)”
read sum
#Checks to see if your in single user mode. If you are, it loads the directory services plist.
if [ $sum = y -o $sum = Y ];
then
echo “Loading Directory Services”
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
else
echo “Skipping launchctl load”
fi
#Asks if you need to see all the userID’s already used.
echo “Making user…”
echo “Do you know what userID is availible? (Default Y)”
read existuserid
if [ $existuserid = n -o $existuserid = N ];
then
#Prints the existing user account records and inserts them to users.out.
dsexport users.out /Local/Default dsRecTypeStandard:Users
#Prints users.out to screen.
tail -n 5 users.out
echo “Find the next userID available.”
#Removes users.out to clean up after printing to screen.
rm users.out
fi
#Asks for the new userID. Checks if userID exists using checkuserid function.
echo “OK, Enter new userID number:”
read userid
checkuserid
#The meat of the script. These are the commands that create the user with your specified credentials.
dscl . -create /Users/”"$userA”"
dscl . -create /Users/”"$userA”" UserShell /bin/bash
dscl . -create /Users/”"$userA”" RealName “$realname”
dscl . -create /Users/”"$userA”" UniqueID $userid
dscl . -create /Users/”"$userA”" PrimaryGroupID 80
dscl . -create /Users/”"$userA”" NFSHomeDirectory /Users/”"$userA”"
dscl . -passwd /Users/”"$userA”" $password
dscl . -append /Groups/admin GroupMembership “”$userA”"
echo “All Done, “$userA” was created!”
sleep 2
exit
else
echo “Please Try Script Again!”
exit 1
fi
Comment from Johny
Time: September 6, 2008, 6:12 am
I followed this guide, and it all seemed to work. But when I restarted apache I got: “PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php/extensions/no-debug-non-zts-20060613/gd.so’ - (null) in Unknown on line 0″. I checked the folder /usr/lib/php/extensions/no-debug-non-zts-20060613/ and found a gd.so that was not empty. Any ideas?
Comment from karlo
Time: September 25, 2008, 1:48 am
Maybe check out also these snippets:
Create & delete user accounts from the command line on Mac OS X
http://codesnippets.joyent.com/posts/show/1374
Creating & deleting system service agent accounts on Mac OS X
Comment from Mike
Time: October 26, 2008, 5:10 am
thanks for blogs
Comment from Jack
Time: November 8, 2008, 10:34 am
Unable to get it to work? Invalid Path (/)
Comment from Cherooo
Time: January 30, 2009, 12:44 pm
Articles very interested, thank you very much
Comment from Neil
Time: February 9, 2009, 3:34 pm
Rusty,
When i run your script (above) on 10.5.6 i get the following after typing a username that i for sure know doesn’t exist on my system:
“The username “?? already exists”
Any ideas?
-N
Pingback from PJ Kix > Hi-tek / Lo-life » mac os x leopard unix groups admin
Time: February 10, 2009, 7:42 pm
[...] the new commands use dscl (directory services client) for managing users and groups. its still a bit confusing and less straight forward than the normal unix commands , usermod, groupadd, etc. [...]
Comment from Farialima
Time: March 12, 2009, 5:32 pm
The script above works well (once you replace the non-ascii characters by ascii characters in it) but (sorry…) I’ve found script that does more things, such as creating automatically a new UniqueID, creating a default group for the user, etc…: http://codesnippets.joyent.com/tag/dscl#post1374
Comment from iruamjs
Time: May 7, 2009, 6:00 pm
Well, here’s my problem….
I have a macbook running OSX 10.5.5 that didn’t want to load in to osx…
First of all, when i turned on my macbook, it would just show the gray screen with either a “Prohibition Sign” or a flashing folder with a “?” on it.
I entered the Mac OSX installer from a Boot Disc, and i realized that my home folder is renamed to “root”, which I suppose is the problem (original name was MacMau).
I did all the steps recommended by Ivan (thx btw =]) and I think it loads osx, but still, all I get is a blue screen with the mouse pointer on it, which i can move. But still, it doesnt load any users, or a Login window.
I’m sort of a newbie at all this, so I would appreciate if any help would be given as if it were for a 10 yr old…
thanks for taking the time to read my problem, and thx in advance for any help that can be provided.
I really miss my computer and the delightful voice of Vicky telling me the time every half an hour.. xP
Comment from Джаля
Time: May 22, 2009, 2:09 am
Полностью согласна!
Comment from Власта
Time: May 22, 2009, 9:50 am
Спасибо за Ваш труд!!
Pingback from All my bookmarks ever | Daniel John Gayle
Time: May 29, 2009, 4:56 pm
[...] How to: Add a user from the OS X command line, works with Leopard! - OS X Daily [...]
Pingback from OS 10.5 - Script for adding users - Mac-Forums.com
Time: June 4, 2009, 3:01 pm
[...] users through bash from these two sites, Mac OS X: Creating a new user from the command-line and How to: Add a user from the OS X command line, works with Leopard! - OS X Daily. The important thing about the script is that it creates the user’s home directory on an external [...]
Social bookmarks:
- | del.icio.us | Digg it | Furl | Netscape | reddit | StumbleUpon | Yahoo MyWeb |
October 29th, 2007


Write a comment