I would like to be able to create new users in Mac OS X 10.5 remotely after ssh'ing into the machine. How do I do this?
6 Answers
Use the dscl command. This example would create the user "luser", like so:
dscl . -create /Users/luser
dscl . -create /Users/luser UserShell /bin/bash
dscl . -create /Users/luser RealName "Lucius Q. User"
dscl . -create /Users/luser UniqueID "1010"
dscl . -create /Users/luser PrimaryGroupID 80
dscl . -create /Users/luser NFSHomeDirectory /Users/luser
You can then use passwd to change the user's password, or use:
dscl . -passwd /Users/luser password
You'll have to create /Users/luser for the user's home directory and change ownership so the user can access it, and be sure that the UniqueID is in fact unique.
This line will add the user to the administrator's group:
dscl . -append /Groups/admin GroupMembership luser
-
7
-
3There's no automatic way; if you script this you can just have it run the "id ####" command and make sure it returns "No such user" or some such hack.– palmerJun 5, 2009 at 18:54
-
1
-
BTW, since this is way more verbose than it should be, I ported these commands into the
useradd
syntax. Apr 26, 2014 at 20:50 -
1Note that
PrimaryGroupID 80
isadmin
, soluser
can usesudo
even if you not add him to administrators's group.– rusloJul 3, 2014 at 6:45
(This answer should be considered an addendum to fill in some blanks in palmer's procedure)
To pick an unused UniqueID for you new user, you could use:
maxid=$(dscl . -list /Users UniqueID | awk 'BEGIN { max = 500; } { if ($2 > max) max = $2; } END { print max + 1; }')
newid=$((maxid+1))
...then use the sequence of dscl commands palmer gave to create the account, and then create the new user's home directory with:
cp -R /System/Library/User\ Template/English.lproj /Users/luser
chown -R luser:staff /Users/luser
if [[ "$(sw_vers -productVersion)" != 10.[0-5].* ]]; then
# Set ACL on Drop Box in 10.6 and later
chmod +a "user:luser allow list,add_file,search,delete,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,writesecurity,chown,file_inherit,directory_inherit" /Users/luser/Public/Drop\ Box
fi
(there is a createhomedir command, but it didn't work when I tested it.)
-
1I created a script that populates our Open Directory Master with new users, and then calls
sudo createhomedir -s
(IIRC) on the Open Directory Replicas / Fileshares, and they happily create the home directories. Jun 5, 2009 at 23:07 -
2
dscl -list
is limited to 256 results, so if you have more than 256 users, this technique will fail to guarantee a unique UID.– smokrisSep 14, 2012 at 18:57 -
1Little trick to evict 2 additional pipes (use only awk): dscl . -list /Users UniqueID | awk '{ if ($2 > max) max = $2; } END { print max + 1; }' Aug 16, 2019 at 1:37
-
@ValtoniBoaventura Nice. I incorporated that in by answer (with a default in case there are no regular accounts). I also added a step to set the Drop Box ACL (which was added in 10.6). Aug 16, 2019 at 7:27
Another way to pick and choose a unique user ID before creating an account is just to look through the list and check that the one you want to use is not there:
sudo dscl . list /Users uid
sudo dscl . list groups gid
Handy if you need to use a certain ID
-
dscl -list
is limited to 256 results, so if you have more than 256 users, this technique will fail to guarantee a unique UID.– smokrisSep 14, 2012 at 18:58 -
yup that's the shortest path to add that user right here right now, just
grep
it with theid
you had in mind to use for the new user– yairAug 5, 2015 at 14:13
I've leveraged the different answers here to come up with what I think is a nice script to create user accounts. Admittedly, this isn't designed for running a command at a time from ssh; it is moreso designed to be a script run when compiling a package-based image of OS X (as created by Casper Imaging or InstaDMG).
#!/bin/bash
# This script creates a user account under Mac OS X
# (tested with 10.5 and 10.6; likely works with 10.4 but not earlier)
# Written by Clinton Blackmore, based on work at
# http://serverfault.com/questions/20702
# === Typically, this is all you need to edit ===
USERNAME=joeadmin
FULLNAME="Joe Admin"
PASSWORD="hard_to_hack"
# A list of (secondary) groups the user should belong to
# This makes the difference between admin and non-admin users.
# Leave only one uncommented
#SECONDARY_GROUPS="" # for a non-admin user
SECONDARY_GROUPS="admin _lpadmin _appserveradm _appserverusr" # for an admin user
# ====
if [[ $UID -ne 0 ]]; then echo "Please run $0 as root." && exit 1; fi
# Find out the next available user ID
MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
USERID=$((MAXID+1))
# Create the user account
dscl . -create /Users/$USERNAME
dscl . -create /Users/$USERNAME UserShell /bin/bash
dscl . -create /Users/$USERNAME RealName "$FULLNAME"
dscl . -create /Users/$USERNAME UniqueID "$USERID"
dscl . -create /Users/$USERNAME PrimaryGroupID 20
dscl . -create /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME
dscl . -passwd /Users/$USERNAME $PASSWORD
# Add use to any specified groups
for GROUP in $SECONDARY_GROUPS ; do
dseditgroup -o edit -t user -a $USERNAME $GROUP
done
# Create the home directory
createhomedir -c > /dev/null
echo "Created user #$USERID: $USERNAME ($FULLNAME)"
The script does let you specify which groups a user should belong to. It appears to me that this might differ depending upon the version of OS X you are running. I get different results when I run id
as an admin on OS X 10.6 than I do when running as an admin on OS X 10.5.
-
In another answer, Elliott warns that the 'createhomedir -c' command, toward the end, will create accounts for all users in the directory you are bound to. (The '-c' option, in the man page, 'creates home directories for local home paths only.') There is another option, '-u username', that may work better. Sep 21, 2010 at 18:52
-
hello i'm using the script above. So it adds my new user but when the user will connect to his calendar it says that he doesn't have the permissions! Can you help me ?– jimmyJan 24, 2011 at 2:50
-
1@jimmy: I'd recommend asking a new question on serverfault and link to this answer rather than just asking a question in the comments, which will be seldom seen. I would expect the permissions to be right. Using 'chown' and 'chmod' can help if you can identify a file that does not have the correct permissions ... but I don't know which one it would be. Jan 26, 2011 at 18:31
-
dscl -list
is limited to 256 results, so if you have more than 256 users, this technique will fail to guarantee a unique UID.– smokrisSep 14, 2012 at 18:58
If you have a bunch of users to create, it is possible to create a structured text file and pass it to dsimport
to do the job.
Apple's Command-Line Administration Guide has a whole chapter on users and groups.
-
Odd. Apple seems to have done away with v10.5 of the document you referred to initially, but kept v10.3 and a less comprehensive Snow Leopard version– chbOct 16, 2017 at 22:37
-
I started a little wrapper about dscl
that takes useradd
's parameters - it's not complete (nor do I think it can be, as some things are not possible on OS X), but I used it to do some user creation.
The framework is there for all the parameters, so if you want to take advantage of GitHub's awesome social features, it's easy to do.
-
Running
# gem install osx-useradd.gemspec
on OS X 10.5 results inERROR: While executing gem ... (Gem::RemoteSourceException)
and the second line isHTTP Response 301 fetching http://gems.rubyforge.org/yaml
.– user145545May 8, 2019 at 4:50
single_user
mode (a security reset) then you'll need to reboot holding downCommand-S
, gain write access to the disk per on-screen details, and thenrm /var/db/.AppleSetupDone
which will let you create a new admin account. Hope this helps someone/Volumes/Macintosh HD/var/db/
.AppleSetupDone with the Boot Recovery volume's/var/db/
dir. You won't find it in the latter.