Managing Users and groups in Linux

Managing Users

Create new user

useradd <username>

The -d option can be used to set the home directory for the user. The -m option will force to create the home directory:

useradd -d /home/<username> -m <username>

Change user’s password

passwd <username>

Delete user

deluser <username>

View user’s account info

id <username>

Change ownership of folder/files recursively

chown -R username somedir

Managing Groups

Create new group

groupadd <groupname>

Add an existing User to an existing Group

The -a param means “append” group. Otherwise the new group will overwrite the previous ones.

usermod -a -G <groupname> username

View user’s groups

groups <username>

Add a new user and assign it to an existing group

useradd -G <groupname> <username>

Add user to multiple groups

usermod -a -G group1,group2,group3 <username>

Modify user’s primary group

usermod -g <groupname> <username>

Change folders/files group recursively

chgrp -R <groupname> <folder/file>

Make all new folders/files created inside a directory to inherit the group of such directory

chmod g+s <folder/file>

Leave a Reply

Your email address will not be published. Required fields are marked *