29

What commands (or script) can create a fully functional user account on Mountain Lion, running only from the command line via terminal or ssh.

It looks like an existing pair of answers might work, but I wondered if there were a short script or tool for creating fully functional user accounts on Mountain Lion?

4 Answers 4

37

Here is a shell script I wrote at work to handle this as part of the NetInstall process (creating a local administrator account automatically during imaging process).

#!/bin/sh
. /etc/rc.common
dscl . create /Users/administrator
dscl . create /Users/administrator RealName "Administrator Account"
dscl . create /Users/administrator hint "Password Hint"
dscl . create /Users/administrator picture "/Path/To/Picture.png"
dscl . passwd /Users/administrator thisistheaccountpassword
dscl . create /Users/administrator UniqueID 501
dscl . create /Users/administrator PrimaryGroupID 80
dscl . create /Users/administrator UserShell /bin/bash
dscl . create /Users/administrator NFSHomeDirectory /Users/administrator
cp -R /System/Library/User\ Template/English.lproj /Users/administrator
chown -R administrator:staff /Users/administrator

Some notes to mention:

  • I have this saved as an executable ".sh" file.
  • Since it executes during NetInstall it runs as root, and needs to run as root to work properly. You can also subtract the first two lines, add a "sudo" to the beginning of each subsequent line, and manually run these as individual commands in Terminal.
  • Modify UniqueID from 501 to a number that you will know is safe on all systems (501 is taken by the first account created on a Mac, generally something higher like 550 will probably be safe, depending on how many users you have on your system).
  • PrimaryGroupID of 80 creates an Admin user. Change to PrimaryGroupID of 20 to create a Standard user.
  • I've imaged well over 50 Macs this way with no issues. I use this account to run commands via SSH, to push out patches via ARD and to do local desk-side administration.
6
  • Looks promising - will test it out soon!
    – bmike
    Mar 4, 2013 at 0:43
  • The problem I seem to have when I try and adapt this script to create regular users by passing variables to the script is that everything except for the last line works just fine. That is, dscl creates the user, and when I use dscl . list /users, the user shows up just fine, but chown does not recognize the user, and since I can't change the home directory permissions properly, it won't let that user log in. Any ideas?
    – user52842
    Jul 8, 2013 at 3:36
  • I wish I could. One of my weaknesses when it comes to shell scripting is that I never learned the usage of variables very well, mostly because I haven't actually needed to use them for anything at this point in my job. Maybe @bmike or someone else might be able to offer advice.
    – bispymusic
    Jul 10, 2013 at 15:33
  • @HackintoshCoat can you post your script? In your command, you've typed lowercase "/users", when it is in fact "/Users". I doubt that's the issue with your script; but, fresh eyes checking it might be able to find the problem fairly quickly. I've got a similar script that actually sets up multiple users, so the usage of variables itself is not a problem.
    – Kent
    Jul 24, 2013 at 8:16
  • 3
    Instead of the last two lines (cp -R ...; chown -R ...), you can use the command createhomedir -u administrator. createhomedir has a man page, too.
    – Alex Leach
    Feb 5, 2014 at 12:13
14

To further automate this, the following line can be used to get the next "available" user id if you are running on a mac which already has users set up.

LastID=`dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1`

NextID=$((LastID + 1))

Then, the corresponding line in bispymusic's answer above could be changed:

 dscl . create /Users/administrator UniqueID $NextID
2

I have some updates on the above answer.

dscl / -append /Groups/admin GroupMembership newUserName

This command can used for making the user have Administrative access. If this command is not provided the user will automatically will be set as standard user.

dscl . create /Users/newUserName PrimaryGroupID 80

This sets user's primary group id. 80 means admin and 20 means staff. And setting to 20 will not make the user standard. Unless the first command is not mentioned.

2
  • To clarify, either of these commands will accomplish the same thing?
    – MaxGabriel
    Sep 6, 2013 at 21:19
  • I prefer the first command, appending to /Groups/admin . Sep 27, 2013 at 14:16
0

Here is a shell script I wrote to add new users in Max OS X using command line. Works the same as if it were Linux. Feel free to edit one for your personal needs. Just name it useradd.sh and set correct permissions (chmod 0700 useradd.sh in my case). Thanks to bispymusic, Alex Leach, Kent for their contributions.

OS X High Sierra tested

#!/bin/sh

if [[ `id -u` != 0 ]]; then
    echo "Must be root to run script"
    exit
fi

read -p "Enter user name and press [ENTER]: " UserName

if [[ $UserName == `dscl . -list /Users UniqueID | awk '{print $1}' | grep -w $UserName` ]]; then
    echo "User already exists!"
    exit 0
fi

read -p "Enter real name and press [ENTER]: " RealName
read -p "Enter PrimaryGroupID (80 - admin, 20 - user) and press [ENTER]: " PrimaryGroupID
LastID=`dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1`
NextID=$((LastID + 1))

. /etc/rc.common
dscl . create /Users/$UserName
dscl . create /Users/$UserName RealName $RealName

read -p "Enter password hint and press [ENTER]: " PasswordHint
dscl . create /Users/$UserName hint $PasswordHint
PasswordHint=0

echo " "
read -s -p "Enter Account Password and press [ENTER]: " AccountPassword
echo " "
read -s -p "Enter Account Password again and press [ENTER]: " AccountPasswordRepeat
if [[ $AccountPassword == $AccountPasswordRepeat ]]; then
  dscl . passwd /Users/$UserName $AccountPassword
  AccountPassword=0
else
  echo "Passwords do not match!"
  exit 1
fi
echo " "
dscl . create /Users/$UserName UniqueID $NextID
dscl . create /Users/$UserName PrimaryGroupID $PrimaryGroupID
dscl . create /Users/$UserName UserShell /bin/bash
dscl . create /Users/$UserName NFSHomeDirectory /Users/$UserName
createhomedir -u $UserName -c

echo " "
echo "New user `dscl . -list /Users UniqueID | awk '{print $1}' | grep -w $UserName` has been created with unique ID `dscl . -list /Users UniqueID | grep -w $UserName | awk '{print $2}'`"

you can get same one from my gist

1
  • It could be NextID=$((`dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1` + 1)) but it just looks better and more clear the way it is right now. May 17, 2018 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .