create user from command line using a template in macOS

0

I would like to create users from the command line (or using a shell script) in macOS High Sierra and Mojave. The user accounts should be copies of a user template. To create the template, I made changes in the folder /System/Library/User Template/

The template works if I create the users manually. However, if I use an already existing shell script to create the user, the template preferences are ignored.

Here's the code that is used to create the user accounts

#!/bin/bash

if [ $# -ne 3 ]; then
echo "Only 3 Arguments allowed"
else

account=$1
password=$2
uid=$3

username=$account
realname=$account
usershell=/bin/bash
uniqueid=$uid
primarygroupid=20
passwd=$password
pic="/Library/User Pictures/Fun/Chalk.tif"

upath="/Users/${username}"

echo "Create User ${upath}? (Y) to go on ..."
read yes

if [ "$yes" == Y ]; then

    dscl . create $upath
    dscl . create $upath UserShell $usershell
    dscl . create $upath RealName "${realname}"
    dscl . create $upath UniqueID $uniqueid
    dscl . create $upath PrimaryGroupID $primarygroupid
    dscl . create $upath NFSHomeDirectory $upath
    dscl . passwd $upath $passwd
    dscl . create $upath picture "${pic}"

else
    echo "Nothing created"
fi
fi

Is there anything I could do? Maybe an alternative to dscl to create user accounts? The well known Linux command adduser doesn't exists here.

Patrick

Patrizio Sommatinese

Posted 2019-08-13T12:02:11.250

Reputation: 1

dscl creates entries in the accounts database, but not the user home folder. There's a createhomedir command that's supposed to handle this, but I can't get it to work. What you can do is use cp to copy the template and then fix its permissions; see my answer here. – Gordon Davisson – 2019-08-16T07:33:13.530

I tried the copy trick. It doesn't work when I copy the plist-files directly after creating the user from the command line. It works after the first login of the user, though. – Patrizio Sommatinese – 2019-09-12T13:41:42.903

No answers