macOS/Windows/Linux get/set sudo/admin users from Terminal
Each of macOS, Windows, and Linux has a way to get and set which users have administrative privileges (i.e. can use sudo or are in the Administrators group) from Terminal.
macOS get / set sudo users from Terminal
On macOS, the users who are
sudo
-capable (i.e. administrators) can be discovered from Terminal using the
Directory Service command line utility
dscl:
dscl . -read /Groups/admin GroupMembershipTo add a user to the sudoers list, use the following command:
dscl . -append /Groups/admin GroupMembership <username>Windows get Administrator users from Terminal
On Windows, the users who are Administrator capable can be discovered from PowerShell using:
Get-LocalGroupMember -Group "Administrators"Query a specific user to see if they are an Administrator:
$user = "myUsername"
# put the desired username in the $user variable, then run the following command:
Get-LocalGroupMember -Group "Administrators" | Where-Object { $_.Name -like "*\$user" }Make a user an Administrator:
Add-LocalGroupMember -Group "Administrators" -Member "myUsername"For Windows Subsystem for Linux (WSL), use the Linux commands below.
Linux get / set sudo users from Terminal
On Linux, the users who are sudo -capable (i.e. administrators) can be discovered from Terminal using:
getent group sudoAdd a user to the sudo group (allowing them to use sudo):
sudo usermod -aG sudo <username>