← All posts
August 1, 20242 min read

Linux User & Group Management

Creating groups and users, and locking a shared directory like /var/www down to a single group with the right permission bits.

#linux#users#permissions

Groups

Create a new group with a specific group id (-r instead gives a low, system-level id):

sudo groupadd -g 1002 mygroup

Restrict a shared directory to one group:

sudo usermod -a -G www-data someuser
sudo chgrp -R www-data /var/www
sudo chmod -R g+w /var/www
sudo find /var/www -type d -exec chmod 2775 {} \;
sudo find /var/www -type f -exec chmod ug+rw {} \;

The 2775 on directories sets the setgid bit, so new files created inside inherit the www-data group automatically.

Add an existing user to a group, then refresh the current shell's group membership:

sudo usermod -aG developers john
newgrp developers

Delete a group (fails if it's still a user's primary group):

sudo groupdel mygroup

Users

Create a user with a specific home directory, shell, and group memberships:

sudo useradd -m -d /home/subho -s /bin/bash -G standard,admin subho

Set or change that user's password:

sudo passwd subho

Delete a user and their home directory (-r removes the home dir and mail spool):

sudo userdel -r someuser

Inspect a user's numeric IDs and group memberships:

id someuser     # uid, gid, and every group they're in
groups someuser # group names only