To find all available users on a macOS system from the command line, you can use a Bash script. The macOS operating system, like other Unix-like systems, stores user information in various system files, primarily in /etc/passwd. macOS uses Open Directory for user management, so you can use commands like dscl to query this information.
Listing Users from the CLI
The following Bash script shows all available users on your macOS system, along with their respective User ID numbers (UID):
#!/bin/bash
# Shows all system users with their UID.
echo "Users and their UIDs on this macOS system:"
# Loop through the user list obtained with dscl
for user in $(dscl . list /Users | grep -v '^_'); do
# Get the UID of the current user
uid=$(dscl . -read /Users/$user UniqueID | awk '{ print $2 }')
# Display the username and its UID
echo "User: $user, UID: $uid"
done
To run this script:
- Open Terminal on your Mac.
- Create a file for the script, for example,
mac_lista_usuarios.sh. - Paste the script code into this file using a text editor.
- Give the file execute permissions with the command
chmod +x mac_lista_usuarios.sh - Run the script with
./mac_lista_usuarios.sh
$ ./mac_lista_usuarios.sh
Users and their UIDs on this macOS system:
User: daemon, UID: 1
User: luis, UID: 501 <== My user
User: pruebas, UID: 502 <== Test USER
User: nobody, UID: -2
User: root, UID: 0
This script lists all users registered on the system, excluding those whose usernames start with an underscore (_), which are usually system or service accounts. It uses the dscl command that interacts with Open Directory. The script could be modified to show more information about each user if needed.
CLI Command to Force Another User’s Logout
You might need this occasionally, for example when performing administrative tasks on your Mac remotely, connecting via ssh, without access to the GUI, and needing to log out other users.
Assuming my user has admin rights, to log out another user you just need to find the other user’s UID using the previous command ./macusers.sh and then run the following script:
#!/bin/bash
# Check if a UID was provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <UID>"
exit 1
fi
UID=$1
# Log out the user with the provided UID
sudo launchctl bootout user /$UID
To run this script:
- Open Terminal on your Mac.
- Create a file for the script, for example,
mac_logout_usuario.sh. - Paste the script code into this file using a text editor.
- Give the file execute permissions with the command
chmod +x mac_logout_usuario.sh - Run the script with
./mac_logout_usuario.sh 502
This command uses launchctl, a tool for interacting with macOS, to terminate the specified user’s session. Keep in mind that forcing another user’s logout may result in loss of unsaved data in their open applications, so use it with caution.
A complete example would be:
$ ./mac_lista_usuarios.sh
Users and their UIDs on this macOS system:
User: daemon, UID: 1
User: luis, UID: 501
User: pruebas, UID: 502 <== TARGET USER FOR LOGOUT
User: nobody, UID: -2
User: root, UID: 0
$ ./mac_logout_usuario.sh 502