macOS 下使用命令列進行使用者(組)管理,就是這麼簡單! dscl 你很有必要了解一下!!

小猿來也發表於2021-06-16

大家好,我是@小猿來也...

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

修改使用者

我們可以使用 dsclcreate指令來新增或者修改使用者的屬性。

$ 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

刪除使用者

我們可以使用 dscldelete指令來刪除使用者。

$ 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/jenkins PrimaryGroupID GroupMembership
GroupMembership: jenkins
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 同樣支援對遠端機器上的使用者以及使用者組的管理,但是我們基本上用不到,所以這裡就不做介紹,感興趣的同學可以自己去嘗試一下。

相關文章