Site icon Narayana Tutorial

Create a User – Create a Group – Assign User to Group in Linux

delete files in linux

delete files in linux

Create a User – Create a Group – Assign User to Group in Linux

This article explained to create a user, create a group, assign a user to a group, delete a user, user password change, create a user with a home directory and without a home directory.

 

Create a User without Home Directory

By using the below command, we can create the user without a home directory and locked out of login.

Syntax
sudo useradd <username>

Example
sudo useradd narayanatutorial

Validate a User

By using the following command, we can validate the whether the user-created or not

Syntax
grep <user-name> /etc/passwd

Example
grep narayanatutorial  /etc/passwd

Create a User with Home Directory

By using the below command, we can create the user with a home directory and locked out of login.

Syntax
sudo useradd -m <username>

Example
sudo useradd -m narayanatutorial

Unlock User login

To unlock the user to log in, we need to assign the password then only the user will be able to log in.

Syntax
sudo passwd <username>

Example
sudo passwd narayanatutorial

Create a User with Home Directory and with password

By using the following command, we can create the user with a home directory, with a password and unlocked the user to log in

Syntax
sudo useradd -m <username>-p <password>

Example
sudo useradd -m narayanatutorial -p Admin1234

Create a Group

By using the following command, we can create the group.

Syntax 
sudo groupadd <group-name> 

Example 
sudo groupadd narayanatutorialGroup

Assign a User to a Group

By using the below command, we can assign a user to a group. Before executing the below command, we need to create the group by using the above command

Syntax 
sudo usermod -a -G <group-name> <username>

Example 
sudo usermod -a -G narayanatutorialGroup narayanatutorial

Assign a User to a Group by Creating User

First up all we need to create a group and then using the below command, we can create a user and assign to a group

Syntax 
 sudo useradd -s /bin/false -g <group-name> -d <home-directory> <username>

Example 
sudo useradd -s /bin/false -g narayanatutorialGroup -d /opt/narayanatutorial narayanatutorial

Validate the Group

By using the below command, we can validate the above group was created or not.

Syntax
grep <group-name> /etc/group

Example
grep narayanatutorialGroup  /etc/group

Change User Password

By using the following command we can change/add the user password.

Syntax
sudo passwd <username>

Example
sudo passwd narayanatutorial

Change Own Password

By using the following command, our own user password can be changed.

Syntax 
passwd

Example 
passwd

Delete a User

By using the following command, we can delete the user

Syntax
sudo userdel <username>

Example
sudo userdel narayanatutorial

Delete a User and Home Directory

To remove the home directory and mail spool too.

Syntax
sudo userdel -r <username>

Example
sudo userdel -r narayanatutorial

Change User Ownership

By using the following command, we can change the ownership of the one user to another user.

Syntax
sudo chown -R <username>:<groupname> /data/testFolder

Example
sudo chown -R narayanatutorial:narayanatutorialGroup /data/testFolder