Articles‎ > ‎

Adding a user from the command line with dscl

posted Dec 19, 2008, 5:53 AM by Philip Rinehart   [ updated Dec 19, 2008, 6:11 AM by Greg Neagle ]
Written by MacEnterprise mailing list   
Friday, 09 March 2007

From Jeff McCune via the Macenterprise mailing list, he posted a short script showing how to create a user using the command line utility dscl 

#!/bin/bash
## create users in DirectoryService.
DS_ROOT_NODE="/NetInfo"if [ -z "${USERNAME:=$1}" ]; thenUSERNAME="testuser"fi
# We'll leave UniqueUID and GID calculations as an exercise for the reader...
UNIQUE_ID="505"
PRIMARY_GID="80"
REAL_NAME="Test User"
dscl localhost -create "${DS_ROOT_NODE}"/Users/"${USERNAME}" \
 || exit 1dscl localhost -append "${DS_ROOT_NODE}"/Groups/admin GroupMembership "${USERNAME}" \
 || exit 1for PROPERTY in \
    _writers_hint \
   _writers_passwd \ 
   _writers_picture \ 
   _writers_realname \ 
   _writers_tim_passworddo 
dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" "${PROPERTY}" "${USERNAME}" \
|| exit 1donedscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" \
"AuthenticationAuthority" ";ShadowHash;" || exit 1dscl localhost \
-append "${DS_ROOT_NODE}"/Users/"${USERNAME}" "AuthenticationHint" "Some Hint..." \
|| exit 1
dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" \
"NFSHomeDirectory" "/Users/${USERNAME}" \
|| exit 1dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" \
"Picture" "/Library/User Pictures/Fun/Fortune Cookie.tif" || exit 1
dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" "PrimaryGroupID" "${PRIMARY_GID}" \
 || exit 1dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" "RealName" "${REAL_NAME}" \ 
|| exit 1dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" "UniqueID" "${UNIQUE_ID}" || exit 1
dscl localhost -append "${DS_ROOT_NODE}"/Users/"${USERNAME}" "UserShell" "/bin/bash" || exit 1
# Create the home directory
mkdir -p "/Users/${USERNAME}"chown ${UNIQUE_ID}:${PRIMARY_GID} "/Users/${USERNAME}"
# From here, you need to set the password 
Comments