Skip to content

Instantly share code, notes, and snippets.

@zlatko-minev
Last active August 13, 2018 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zlatko-minev/5e5f154a60f6cc624a9accc62075dec0 to your computer and use it in GitHub Desktop.
Save zlatko-minev/5e5f154a60f6cc624a9accc62075dec0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# If the top line line doesn't work, change to #!/bin/bash
# Name: list_users.sh
# Purpose: List all normal user and system groups in the system.
# Tested on RHEL / Debian Linux / BusyBox / GNU/Linux / Mac OS X Sierra Darwin
# Author: Zlatko Minev, based on Vivek Gite, under GPL v2.0+
# Original link: https://www.cyberciti.biz/faq/linux-list-users-command/
# See also: http://tuxthink.blogspot.com/2012/03/look-into-etcpasswdetcshadow-and.html
# -----------------------------------------------------------------------------------
clear
#source ./colors.sh
Bold='\033[1m' # Bold
BRed='\033[1;31m' # Bold Red
ResetTxt='\033[0m' # Text Reset
Bar='---------------------------------'
_g="/etc/group"
#echo "Content of $_g:" | cat - $_g # https://superuser.com/questions/1285136/cat-command-and-echo # echo " "
printf "%s${Bold}[ System User Groups ]${ResetTxt}%s\\n" ${Bar} ${Bar} # https://github.com/koalaman/shellcheck/wiki/SC2059
printf "${BRed}%-5s %-15s %-15s %-8s${ResetTxt}\\n" "GID" "Group Name" "Users in Group" "Group Pswd"
awk -F':' '{ printf "%-5s %-15s %-15s %-8s\n", $3, $1, $4, $2 }' "$_g"
printf '%s%s%s\n\n' "${Bar}" '------------------------' "${Bar}"
_p="/etc/passwd"
#echo "Content of $_g:" | cat - $_p # echo " "
printf "%s${Bold}[ System User Accounts ]${ResetTxt}%s\\n" ${Bar} ${Bar}
printf "${BRed}%-5s %-5s %-10s %-16s %-15s %-25s %-10s${ResetTxt}\\n" "GID" "UID" "User Name" "User Info" "User shell" "User Directory" "Has pswd"
awk -F':' '{ printf "%-5s %-5s %-10s %-16s %-15s %-25s %-10s\n", $4, $3, $1, $5, $7, $6, $2 }' "$_p"
printf '%s%s%s\n\n\n' "${Bar}" '------------------------' "${Bar}"
@zlatko-minev
Copy link
Author

Pretty print Linux user groups, users, and key properties ala *nix.

@zlatko-minev
Copy link
Author

Updated bugs fixed thanks to nixCraft Vivek, used lint vs code with shellchecker. Screenshot:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment