How can I create a new user through terminal macOS 10.14
I am trying to write a script to add a user to a computer. These will not be admin accounts and should be staff accounts. I ran the following script and can login to the account but I can't access any files, create any files, or save any files to the computer.
Ultimately I would like to run this to remove 5 user accounts and all of their information and then create 5 new accounts (this is in a classroom where I have 36 computers) but I am struggling to create a single user correctly with the following script.
Any help would be much appreciated.
#!/bin/bash
USERNAME=per1
FULLNAME="Period 1"
PASSWORD="test"
SECONDARY_GROUPS="staff"
# ====
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)"
Solution 1:
#!/bin/bash
USERNAME=per1
FULLNAME="Period 1"
PASSWORD="test"
SECONDARY_GROUPS="staff"
# ====
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 -u $USERNAME > /dev/null
echo "Created user #$USERID: $USERNAME ($FULLNAME)"