Answers:
这是我在工作中编写的Shell脚本,它作为NetInstall流程的一部分(在映像过程中自动创建本地管理员帐户)进行处理。
#!/bin/sh
. /etc/rc.common
dscl . create /Users/administrator
dscl . create /Users/administrator RealName "Administrator Account"
dscl . create /Users/administrator hint "Password Hint"
dscl . create /Users/administrator picture "/Path/To/Picture.png"
dscl . passwd /Users/administrator thisistheaccountpassword
dscl . create /Users/administrator UniqueID 501
dscl . create /Users/administrator PrimaryGroupID 80
dscl . create /Users/administrator UserShell /bin/bash
dscl . create /Users/administrator NFSHomeDirectory /Users/administrator
cp -R /System/Library/User\ Template/English.lproj /Users/administrator
chown -R administrator:staff /Users/administrator
一些注意事项:
cp -R ...
; chown -R ...
)createhomedir -u administrator
。createhomedir
也有一个手册页。
我对以上答案有一些更新。
dscl / -append /Groups/admin GroupMembership newUserName
此命令可用于使用户具有管理访问权限。如果未提供此命令,则用户将自动设置为标准用户。
dscl . create /Users/newUserName PrimaryGroupID 80
设置用户的主要组ID。80表示管理员 ,20表示员工。并将其设置为20不会成为用户标准。除非没有提到第一个命令。
这是我编写的Shell脚本,用于使用命令行在Max OS X中添加新用户。就像在Linux上一样工作。可以根据自己的需要随意编辑。只需将其命名为useradd.sh并设置正确的权限即可(chmod 0700 useradd.sh
以我为例)。感谢bispymusic,Alex Leach,Kent的贡献。
OS X High Sierra测试
#!/bin/sh
if [[ `id -u` != 0 ]]; then
echo "Must be root to run script"
exit
fi
read -p "Enter user name and press [ENTER]: " UserName
if [[ $UserName == `dscl . -list /Users UniqueID | awk '{print $1}' | grep -w $UserName` ]]; then
echo "User already exists!"
exit 0
fi
read -p "Enter real name and press [ENTER]: " RealName
read -p "Enter PrimaryGroupID (80 - admin, 20 - user) and press [ENTER]: " PrimaryGroupID
LastID=`dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1`
NextID=$((LastID + 1))
. /etc/rc.common
dscl . create /Users/$UserName
dscl . create /Users/$UserName RealName $RealName
read -p "Enter password hint and press [ENTER]: " PasswordHint
dscl . create /Users/$UserName hint $PasswordHint
PasswordHint=0
echo " "
read -s -p "Enter Account Password and press [ENTER]: " AccountPassword
echo " "
read -s -p "Enter Account Password again and press [ENTER]: " AccountPasswordRepeat
if [[ $AccountPassword == $AccountPasswordRepeat ]]; then
dscl . passwd /Users/$UserName $AccountPassword
AccountPassword=0
else
echo "Passwords do not match!"
exit 1
fi
echo " "
dscl . create /Users/$UserName UniqueID $NextID
dscl . create /Users/$UserName PrimaryGroupID $PrimaryGroupID
dscl . create /Users/$UserName UserShell /bin/bash
dscl . create /Users/$UserName NFSHomeDirectory /Users/$UserName
createhomedir -u $UserName -c
echo " "
echo "New user `dscl . -list /Users UniqueID | awk '{print $1}' | grep -w $UserName` has been created with unique ID `dscl . -list /Users UniqueID | grep -w $UserName | awk '{print $2}'`"
你可以从我的主意中得到一个
NextID=$((`dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1` + 1))
但是看起来更好,现在更清晰了。