How to create a new user in Linux and add it to a group

Introduction:

The idea is to organize user management in ubuntu in a simple way so just by looking at it, you are able to know what to do.

Getting Started:

Users:

This section is for user management.

Create User

sudo adduser yourname

Change User password

sudo passwd user

Delete User

sudo deluser yourname

Add User to Group

sudo adduser username groupname

Make user own a directory

  • This will change ownership of all files and directories inside of directory and directory itself: sudo chown -R $USER ~/.myFolder
  • This will only change the permission of the folder directory but will leave the files and folders inside the directory alone: sudo chown -R $USER ~/.myFolder

Deleting the user account doen't remove the respective home folder. It's up to you whether or not you wish to delete the folder manually or keep it.

Remember, any user added later on with the same UID/GID as the previous owner will now have access to this folder if you have not taken the necessary precautions.

Move deleted User to archived folder

If you'd like to change that, so you can have an achieved folder for those who you deleted, you can do the following to avoid future conflicts:

sudo chown -R root:root /home/username/
sudo mkdir /home/archived_users/
sudo mv /home/username /home/archived_users/

Temporarily lock or unlock a user account

Lock: sudo passwd -l username
Unlock: sudo passwd -u username

Groups

This section is for group management.

Show Groups

groups

Add or Delete a Group

Add: sudo addgroup groupname
Delete: sudo delgroup groupname

Make a group own a directory

The following example will change the ownership of all files and subdirectories under the /var/www directory to a new owner and group named www-data:
The -R is to include subdirectories and the -h if the directory contains symbolic links:
chown -hR :www-data /var/www

Remove group ownership of a directory

Either delete the group or assign a new dummy group?