New User Creation - Mac OS

Hi all,

Is there an easy way to create new users for Mac OS devices? Theres no direct terminal access via Syncro and scripting seems to be difficult because there is no simple ‘net user’ equivalent like in Windows to create a new user.

Hopefully terminal access is on the roadmap for the future.

Any help appreciated

Thank you!

Tonight I learnt how easy it is to use ChatGPT.
Truely amazing!!!

So I asked how to create a user on the commandline on a Mac, and how to create a user using a script on a Mac.

Here are the two Queries and Responses. The bold text is my own. Looks much better at ChatGPT

ChatGPT Query
easy way to create users on Mac commandline?

ChatGPT Response
To create a new user on a Mac using the command line, you can use the dscl (Directory Service Command Line utility) command.

  1. Open Terminal
  2. Type sudo dscl . -create /Users/username and press Enter
  3. Replace “username” with the desired username for the new user
  4. Type sudo dscl . -create /Users/username UserShell /bin/bash and press Enter
  5. Type sudo dscl . -create /Users/username RealName "Full Name" and press Enter
  6. Replace “Full Name” with the new user’s full name
  7. Type sudo dscl . -create /Users/username UniqueID "505" and press Enter
  8. Replace “505” with a unique numerical ID for the new user.
  9. Type sudo dscl . -create /Users/username PrimaryGroupID 20 and press Enter
  10. Type sudo dscl . -create /Users/username NFSHomeDirectory /Users/username and press Enter
  11. Type sudo dscl . -passwd /Users/username "password" and press Enter
  12. Replace “password” with the desired password for the new user.

This will create a new user with the specified username, full name, and password. You can use dscl . -list /Users command to check the new user.
End of ChatGPT Response

ChatGPT Query
bash script to create user for Mac

ChatGPT Response
Here is a sample Bash script that you can use to create a new user on a Mac:

Copy code

#!/bin/bash

# Get the username
echo "Enter the username for the new user:"
read username

# Get the full name
echo "Enter the full name for the new user:"
read fullname

# Get the user ID
echo "Enter the user ID for the new user:"
read userid

# Get the password
echo "Enter the password for the new user:"
read -s password

# Create the user
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

echo "User $username has been created with ID $userid and password $password"

Make sure you run it with super user permission.

You can run this script by saving it to a file (e.g. create_user.sh) and then running sudo bash create_user.sh in the Terminal. The script will prompt you for the desired username, full name, user ID, and password for the new user, and then create the user using the dscl command.

End of ChatGPT response.

So the issue with just doing things that ChatGPT says could lead to issues, and certainly, at the very least, not understanding what you are actually running against devices.

  1. For example, in the above, it’s asking for input, which isn’t going to work if you’re running it from Syncro.
  2. It’s also telling you things like $userid, but doesn’t explain what or how you get that, you cannot just pick any number, it has to be a number above 500 but one that isn’t already in use.
  3. PrimaryGroupID 20 is a standard user, if you want it to be an admin, use 80.
  4. After much trial with those commands in the past, it doesn’t always create the proper home directories needed for proper GUI login.

So I’ll add a few adjustments to the above. I’ve noted the sections for what they do. You can feel free to modify it more for your needs. Also, creating users via script doesn’t enable securetoken for them, so only the originally created admin account at time of set up usually has securetoken enabled. You cannot reset a password via script, or create a new user with securetoken enabled, without knowing and supplying the password for the current securetoken enabled account.

Anyway, the below will create the account or reset the password using openssl random password generation function, and create an admin user. Change to 20 if you want standard user.

I also have a 2nd way to create users that doesn’t utilize dscl but I haven’t tested it as much, in case this doesn’t work for some reason.

EDIT - You might want to echo “$Randompw” if you decide to use the random function, so that you actually can record what the password is, or modify it to pass it somewhere, I stripped out the sections where I integrate this with other stuff.

#!/bin/bash

##Make these runtime variables##
UserName="$UserName"
FullName="$FullName"
################################
GetUser=$(dscl . list /Users | grep -i -x "$UserName")
Randompw=$(openssl rand -base64 24)

###Just resets password if user already exists###
if [ $GetUser ]; then
    echo "User $UserName already exists. Resetting password."
    dscl . -passwd /Users/$UserName $Randompw
    exit 0
fi
#################################################

###Gets the next useable "user ID"###
FindLastID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1)
UniqueID=$((FindLastID + 1))
################################################

dscl . create /Users/$UserName IsHidden 0
dscl . create /Users/$UserName RealName "$FullName"
dscl . passwd /Users/$UserName $Randompw
dscl . create /Users/$UserName UniqueID $UniqueID
dscl . create /Users/$UserName PrimaryGroupID 80
dscl . create /Users/$UserName UserShell /bin/bash
dscl . create /Users/$UserName NFSHomeDirectory /Users/$UserName
createhomedir -u $UserName -c


GetUser=$(dscl . list /Users | grep -i -x "$UserName")

if [ $GetUser ]; then
    echo "User $UserName created successfully."
    exit 0
fi

echo "Failed to create user"
exit 1

completely agree.
Best used as a starting point to build on to.