user and group management commands in linux

These user and group management commands should work on linux, unix and mac os, only a few might not work across all systems.

Show all existing users

awk -F: '{ print $1 }' /etc/passwd

Add a new user “ken”

sudo adduser ken

Grant root access to the user “ken”
Type

sudo /usr/sbin/visudo

Add the below

ken ALL=(ALL:ALL) ALL

after

root ALL=(ALL:ALL) ALL

To delete the user “ken”

sudo userdel ken

Check if anyone can execute commands as root

cat /etc/sudoers

Change the password of the existing user “ken”

sudo passwd ken

To lock the user account “ken”

sudo passwd -l ken

To unlock the user account “ken”

sudo passwd -u ken

Add the user “ken” to the user group “users”

sudo usermod -a -G users ken

Show the groups the user “ken” belongs to

groups ken

Find all the existing groups

cat /etc/group | cut -d: -f1

Change the primary group of the user “ken” to the group “users”

sudo usermod -g users ken

To add a group “testers”

groupadd testers

To delete a group “testers”, users under this group needs to be removed first if any.

groupdel testers

List all users under the user group “users”. It will not list users to whom this group is primary.

egrep "^users" /etc/group

Search within Codexpedia

Custom Search

Search the entire web

Custom Search