Create a User – Create a Group – Assign User to Group in Linux
Table of Contents
- 1 Create a User – Create a Group – Assign User to Group in Linux
- 1.1 Create a User without Home Directory
- 1.2 Validate a User
- 1.3 Create a User with Home Directory
- 1.4 Unlock User login
- 1.5 Create a User with Home Directory and with password
- 1.6 Create a Group
- 1.7 Assign a User to a Group
- 1.8 Assign a User to a Group by Creating User
- 1.9 Validate the Group
- 1.10 Change User Password
- 1.11 Change Own Password
- 1.12 Delete a User
- 1.13 Delete a User and Home Directory
- 1.14 Change User Ownership
- 1.15 Related Posts
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
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.