2

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)"
  • Where/how exactly does the script not do what you want it to do? – nohillside Jan 18 at 15:58
  • This is creating a user but I believe there is an issue with the home directory creation. Perhaps a permission issue. For instance I can not view any folder in finder. – John Vincent Jan 18 at 16:11
  • I get this error while logged in to the account created: The folder "Desktop" can't be opened because you don't have permission to see its contents. – John Vincent Jan 18 at 16:18
  • Well, without looking at everything in detail: How do you expect createhomedir to know for which user it should create a home directory? – nohillside Jan 18 at 17:33
  • Thank you! I added the user and the script works now – John Vincent Jan 18 at 20:55
3
#!/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)"
0

John Vincents answer is very useful as it can create the user for you via a script...

You will also want to consider secureToken status creation for the new user as well if you plan on activating FileVault for the user.

Some more important information regarding SecureToken: https://derflounder.wordpress.com/2018/01/20/secure-token-and-filevault-on-apple-file-system/

You can add the secureToken using the following with root:

sysadminctl -adminUser root -adminPassword <rootPassword> -secureTokenOn <user> -password <userPassword>

You can check secureToken status this way:

sysadminctl -secureTokenStatus username_goes_here

You must log in to answer this question.

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