大家好,我是@小猿來也...
Command-line interface to Directory Services。
在 linux 系統中我們習慣了使用 useradd,userdel,usermod 等指令進行用戶管理,使用 groupadd,groupdel,groupmod 等指令進行用戶組管理。
但是在 macOS 下這些指令是沒有的。
所以今天分享的主題是在 macOS 下如何在命令行里進行用戶組、用戶管理?
macOS 下有什么命令行工具可以用來進行用戶組、用戶管理的呢,它就是今天的主角 dscl 。
一、dscl 簡介
dscl 是一個目錄服務的命令行,用來創建、讀取和管理目錄服務數據。它還提供了基本的編輯器命令,如列表、搜索、創建、讀取、追加、合並、更改和刪除。
我們可以認為它是存儲訪問OS X用戶授權數據的工具。
在沒有任何命令的情況下調用,dscl 將以交互模式運行,從標准輸入讀取命令。
進入 dscl
在終端內輸入dscl .
即可進入本機的 dscl 。
$ dscl .
>
退出 dscl
進入 dscl 后輸入 q 或者 exit 即可退出 dscl 。
$ dscl .
> q
Goodbye
$ dscl .
> exit
Goodbye
dscl 所支持的指令集
$ dscl .
> help
dscl (v11.2)
usage: dscl [options] [<datasource> [<command>]]
datasource:
localhost (default) or
localonly (activates a DirectoryService daemon process
with Local node only - daemon quits after use
<hostname> (requires DS proxy support, >= DS-158) or
<nodename> (Directory Service style node name) or
<domainname> (NetInfo style domain name)
options:
-u <user> authenticate as user (required when using DS Proxy)
-P <password> authentication password
-p prompt for password
-f <filepath> targeted file path for DS daemon running in localonly mode
(example: /Volumes/Build100/var/db/dslocal/nodes/Default)
(NOTE: Nodename to use is fixed at /Local/Target)
-raw don't strip off prefix from DS constants
-plist print out record(s) or attribute(s) in XML plist format
-url print record attribute values in URL-style encoding
-q quiet - no interactive prompt
commands:
-read <path> [<key>...]
-readall <path> [<key>...]
-readpl <path> <key> <plist path>
-readpli <path> <key> <value index> <plist path>
-create <record path> [<key> [<val>...]]
-createpl <record path> <key> <plist path> <val1> [<val2>...]
-createpli <record path> <key> <value index> <plist path> <val1> [<val2>...]
-delete <path> [<key> [<val>...]]
-deletepl <record path> <key> <plist path> [<val>...]
-deletepli <record path> <key> <value index> <plist path> [<val>...]
-list <path> [<key>]
-append <record path> <key> <val>...
-merge <record path> <key> <val>...
-change <record path> <key> <old value> <new value>
-changei <record path> <key> <value index> <new value>
-diff <first path> <second path>
-search <path> <key> <val>
-auth [<user> [<password>]]
-authonly [<user> [<password>]]
-passwd <user path> [<new password> | <old password> <new password>]
MCX Extensions:
-mcxread <record path> [optArgs] [<appDomain> [<keyName>]]
-mcxset <record path> [optArgs] <appDomain> <keyName> [<mcxDomain> [<keyValue>]]
-mcxedit <record path> [optArgs] <appDomain> <keyPath> [<keyValue>]
-mcxdelete <record path> [optArgs] [<appDomain> [<keyName>]]
-mcxdeleteall <record path> [optArgs] [<appDomain> [<keyName>]]
-mcxexport <record path> [optArgs] [<appDomain> [<keyName>]]
-mcximport <record path> [optArgs] <file path>
-mcxhelp
>
列出 dscl 中所有的數據目錄
$ dscl .
> ls
AFPUserAliases
Aliases
Automount
AutomountMap
ComputerGroups
ComputerLists
Computers
Config
Ethernets
Groups
Hosts
Mounts
NetGroups
Networks
People
PresetComputerGroups
PresetComputerLists
PresetComputers
PresetGroups
PresetUsers
Protocols
Services
SharePoints
Users
二、管理用戶
獲取所有用戶列表
$ dscl .
# 進入用戶的數據目錄
> cd Users/
/Users >ls
...
yeah
nobody
root
...
也可以直接使用下面的指令
$ dscl . -ls /Users
ls
前的"-"是可以省去的,同樣其他指令也是一樣的,下面的演示中,將不會在給出具體指令前的"-"。
創建一個用戶
使用 dscl
在 /Users
數據目錄下,創建一個用戶的實例,我們可以設置它的 uid、 gid、 shell、realname、home 目錄。
創建一個名為 yeah 的用戶並設置用戶的 uid 為 8888。
# sudo dscl . create /Users/yeah UniqueID 8888
$ sudo dscl . create /Users/yeah uid 8888
gid該字段必須設置,如果不設置在更改文件宿主為該用戶時會報 illegal user name
錯誤。
因此你需要先跳到『管理用戶組』的部分,參考用戶組創建的命令先創建一個用戶組,這里我們已經提前創建好了一個 gid 為 6666 名稱同樣也為 yeah 的用戶組。
准備好用戶組之后我們來設置用戶的 gid。
# sudo dscl . create /Users/yeah PrimaryGroupID 6666
$ sudo dscl . create /Users/yeah gid 6666
創建並設置用戶所使用的 shell,如果 shell 不進行設置則用戶在終端中將會無法使用。
# sudo dscl . create /Users/yeah UserShell /bin/bash
$ sudo dscl . create /Users/yeah shell /bin/bash
創建並設置用戶的 realname
$ sudo dscl . create /Users/yeah realname "coding yeah"
設置用戶的初始密碼為空
# '*' 表示空密碼
# sudo dscl . create /Groups/yeah passwd \*
$ sudo dscl . create /Groups/yeah passwd '*'
修改用戶的密碼
$ sudo passwd yeah
Changing password for yeah.
New password: ********
Retype new password: ********
創建或者指定用戶的 home 目錄
dscl
在創建用戶的時候並不會自動為用戶創建 home 目錄,因此需要我們為用戶創建 home 目錄。
$ sudo mkdir /Users/yeah
在用戶 yeah 已經有所屬的用戶組的前提下,使用 chown
指令設定 /Users/yeah
目錄以及其子目錄的宿主為用戶 yeah
$ sudo chown -R yeah:yeah /Users/yeah
這樣新目錄 /Users/yeah
的以及其目錄的宿主就變成了用戶 yeah
聲明用戶的 home 目錄
# sudo dscl . create /Users/yeah NFSHomeDirectory /Users/yeah
$ sudo dscl . create /Users/yeah home /Users/yeah
這樣用戶 yeah 就可以以 /Users/yeah
為 home 目錄進行工作了。
授予用戶管理員權限
為了授予用戶管理員權限,我們只需要把用戶加入到 admin(/Groups/admin)
用戶組即可,這樣用戶就具有了 sudo 權限。
$ sudo dscl . merge /Groups/admin users yeah
修改用戶
我們可以使用 dscl
的 create
指令來添加或者修改用戶的屬性。
$ sudo dscl . create /Users/yeah shell /bin/zsh
比如我們可以用上面的指令來修改用戶的 shell 為 zsh。
獲取用戶的指定屬性
# 查看用戶yeah的所有屬性
$ dscl . read /Users/yeah
...
NFSHomeDirectory: /Users/yeah
Password: ********
PrimaryGroupID: 6666
RealName:
coding yeah
RecordName: yeah
RecordType: dsRecTypeStandard:Users
UniqueID: 8888
UserShell: /bin/zsh
# 查看用戶yeah的組ID和用戶ID
$ dscl . read /Users/yeah PrimaryGroupID UniqueID
PrimaryGroupID: 6666
UniqueID: 8888
# 列出所有用戶的的組ID
$ dscl . list /Users PrimaryGroupID
# 列出所有用戶的ID
$ dscl . list /Users UniqueID
刪除用戶
我們可以使用 dscl
的 delete
指令來刪除用戶。
$ sudo dscl . delete /Users/yeah
上面的指令可以刪除用戶 yeah 的所有屬性。但是需要說明的是用戶的 home 目錄需要手動刪除,用戶所屬的組也需要額外刪除。
三、管理用戶組
獲取所有用戶組列表
使用 dscl
進入 /Groups
數據目錄並列出所有用戶組。
$ dscl .
> cd /Groups/
/Groups > ls
_amavisd
_appowner
_appserveradm
_appserverusr
...
也可以直接使用下面的指令
$ dscl . ls /Groups
創建用戶組
使用 dscl
創建用戶組,我們需要在dscl
的/Groups
目錄下創建一個目錄,同時設置它的 gid 屬性。
# sudo dscl . create /Groups/yeah PrimaryGroupID 6666
$ sudo dscl . create /Groups/yeah gid 6666
創建用戶組 yeah 並設置用戶組 yeah 的用戶組 ID 為6666,上面的三種方式是等效的。
給用戶組添加用戶
# 多次執行會被添加多次
# sudo dscl . append /Groups/yeah GroupMembership yeah
# 多次執行會被添加多次
$ sudo dscl . merge /Groups/yeah users yeah
刪除指定組內的指定用戶
從 yeah 用戶組內刪除用戶 yeah
sudo dscl . delete /Groups/yeah GroupMembership yeah
刪除用戶組
$ sudo dscl . delete /Groups/yeah
使用上面的指令將在dscl
中刪除/Groups/yeah
目錄下的所有信息,我們要謹慎使用。
獲取用戶組的指定屬性
# 查看用戶組yeah的所有屬性
$ dscl . read /Groups/yeah
AppleMetaNodeLocation: /Local/Default
GroupMembership: yeah
PrimaryGroupID: 6666
RecordName: yeah
RecordType: dsRecTypeStandard:Groups
# 查看用戶yeah的組ID和組內用戶
$ dscl . read /Groups/yeah PrimaryGroupID GroupMembership
GroupMembership: yeah
PrimaryGroupID: 6666
# 列出所有用組的組ID
$ dscl . list /Groups PrimaryGroupID
# 列出所有用組的組內用戶
$ dscl . list /Groups GroupMembership
四、總結
好了今天的分享就到這里啦,主要是分享 macOS 如何基於命令行進行用戶以及用戶組的管理的知識,希望對你有幫助。
如果你看了覺得對你有幫助,就給小猿來也點個贊吧。
如果你沒時間詳細的了解,那么你只需要看下下面的這些就可以了。
創建用戶資源
# 創建組
sudo dscl . create /Groups/yeah gid 6666
# 創建用戶
sudo dscl . create /Users/yeah uid 8888
sudo dscl . create /Users/yeah gid 6666
sudo dscl . create /Users/yeah shell /bin/bash
# 修改用戶的密碼
sudo passwd yeah
# 創建用戶的home目錄
sudo mkdir /Users/yeah
# 用戶的home目錄宿主
sudo chown -R yeah:yeah /Users/yeah
# 聲明用戶的home目錄
sudo dscl . create /Users/yeah home /Users/yeah
# 把用戶添加到用戶組內
sudo dscl . merge /Groups/admin users yeah
查看用戶信息
$ dscl . read /Users/yeah
...
AppleMetaNodeLocation: /Local/Default
GeneratedUID: xxxxxx
NFSHomeDirectory: /Users/yeah
Password: ********
PrimaryGroupID: 6666
RecordName: yeah
RecordType: dsRecTypeStandard:Users
UniqueID: 8888
UserShell: /bin/bash
清除用戶資源
# 清除用戶
$ sudo dscl . delete /Groups/yeah
# 清除用戶組
$ sudo dscl . delete /Users/yeah
# 刪除用戶home目錄
$ sudo rm -rf /Users/yeah
另外 dscl 同樣支持對遠程機器上的用戶以及用戶組的管理,但是我們基本上用不到,所以這里就不做介紹,感興趣的同學可以自己去嘗試一下。