在Mac OS X上等效于adduser命令?


20

以管理员身份登录后,我希望能够从命令行创建新用户。具体来说,我adduser在Linux上寻找与之等效的命令。

Answers:


12

http://wiki.freegeek.org/index.php/Mac_OSX_adduser_script有一个不错的脚本, 但是它有多种错别字,有时不够灵活,因此,这是我的改进版本,并做了一些改进:

#!/bin/bash
# =========================
# Add User OS X Interactive Command Line
# =========================

getHiddenUserUid()
{
    local __UIDS=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ugr)

    #echo $__UIDS
    local __NewUID
    for __NewUID in $__UIDS
    do
        if [[ $__NewUID -lt 499 ]] ; then
            break;
        fi
    done

    echo $((__NewUID+1))
}

getInteractiveUserUid()
{
    # Find out the next available user ID
    local __MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
    echo $((__MAXID+1))
}

if  [ $UID -ne 0 ] ; then echo "Please run $0 as root." && exit 1; fi

read -p "Enter your desired user name: " USERNAME

read -p "Enter a full name for this user: " FULLNAME

read -s -p "Enter a password for this user: " PASSWORD
echo
read -s -p "Validate a password: " PASSWORD_VALIDATE
echo

if [[ $PASSWORD != $PASSWORD_VALIDATE ]] ; then
    echo "Passwords do not match!"
    exit 1;
fi

# ====

# A list of (secondary) groups the user should belong to
# This makes the difference between admin and non-admin users.

read -p "Is this an administrative user? [y/n] (n): " GROUP_ADD
GROUP_ADD=${GROUP_ADD:-n}

if [ "$GROUP_ADD" = n ] ; then
    SECONDARY_GROUPS="staff"  # for a non-admin user
elif [ "$GROUP_ADD" = y ] ; then
    SECONDARY_GROUPS="admin _lpadmin _appserveradm _appserverusr" # for an admin user
else
    echo "You did not make a valid selection!"
    exit 1;
fi

# ====

# Create a UID that is not currently in use

read -p "Should this user have interactive access?  [y/n] (y): " IS_INTERACTIVE
IS_INTERACTIVE=${IS_INTERACTIVE:-y}

if [ "$IS_INTERACTIVE" = y ] ; then
    USERID=$(getInteractiveUserUid)
elif [ "$IS_INTERACTIVE" = n ] ; then
    USERID=$(getHiddenUserUid)
else
    echo "You did not make a valid selection!"
    exit 1;
fi

echo "Going to create user as:"
echo "User name: " $USERNAME
echo "Full name: " $FULLNAME
echo "Secondary groups: " $SECONDARY_GROUPS
echo "UniqueID: " $USERID

read -p "Is this information correct?  [y/n] (y): " IS_INFO_CORRECT
IS_INFO_CORRECT=${IS_INFO_CORRECT:-y}

if [ "$IS_INFO_CORRECT" = y ] ; then
    echo "Configuring Open Directory..."
elif [ "$IS_INFO_CORRECT" = n ] ; then
    echo "User creation cancelled!"
    exit 1;
else
    echo "You did not make a valid selection!"
    exit 1;
fi


# Create the user account by running dscl (normally you would have to do each of these commands one
# by one in an obnoxious and time consuming way.

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 user to any specified groups
echo "Adding user to specified groups..."

for GROUP in $SECONDARY_GROUPS ; do
    dseditgroup -o edit -t user -a $USERNAME $GROUP
done

# Create the home directory
echo "Creating home directory..."
createhomedir -c 2>&1 | grep -v "shell-init"

echo "Created user #$USERID: $USERNAME ($FULLNAME)"

至少在10.12 / Sierra上,这可能会出现问题。当我尝试它时,它正在为网络上的每个用户创建目录。特别是,在不指定“ -u用户名”的情况下使用“ createhomedir”可能是不明智的选择。
Jan Kyu Peblik

17

我认为您正在寻找dscl命令。Niutil听起来像NetInfo,在OS X中不推荐使用,而推荐使用Open Directory或LDAP。

Dscl或目录服务命令行可用于为本地或远程用户数据存储编辑Open Directory。可以使用sudo命令完成操作,但更容易用作root用户。

这是一个简短且非常不足的教程:在终端中,使用sudo -s将用户切换为root。要创建名为dscl2的功能用户帐户,您需要执行以下操作:

dscl . -create /Users/dscl2

dscl . -create /Users/dscl2 UserShell /bin/bash

dscl . -create /Users/dscl2 RealName "DSCL 2"

dscl . -create /Users/dscl2 UniqueID 8005

dscl . -create /Users/dscl2 PrimaryGroupID 20

dscl . -create /Users/dscl2 NFSHomeDirectory /Users/dscl2

dscl . -passwd /Users/dscl2 password

UUID通常约为501或更大。501是创建的第一个帐户的默认UID。小于500的UID默认情况下不会显示在“帐户”窗格中。选择所需的任何数字,但要确保它在本地系统上是唯一的。不要覆盖现有的UID,否则您将遇到很大的问题。

Dscl也具有交互模式,其工作方式略有不同。在提示符下仅输入“ dscl”即可进入交互模式。

如果您处于交互方式,请键入ls列出可用的目录。您应该看到BSD,LDAP和本地。您使用cd浏览目录。有关更多信息,请参见您的朋友的手册页。


2
我知道您警告过我们这是不够的,但是即使在完成dscl之后,也很高兴看到创建帐户所需的全部步骤。例如,是否需要创建/ Users / dscl2文件夹?如何应用ACL?
内森·加拉比迪安

1
这个答案是正确的,但是@ anton-matosov答案中的脚本很棒。运行相同的命令。
bmucklow
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.