How to add user in Linux/ Modify/Delete user in Linux

In the previous article, we have seen the theory of User management in this article we are going to understand user management as practical knowledge.

how to add users in linux

How to add user in linux

Before jumping how to create the user in the Linux system we should know the syntax of the command that is use to add the user in Linux.

useradd is the command that is use to add the user in linux system.

Syntax

useradd option… arguments…

1 How to create users

To create the users in Linux then we can execute useradd command with the username

The option is optional but arguments are compulsory the arguments are the name of the users that we want to add to the Linux system.

# useradd devopstutuser
Screenshot 1

2 – How to assign a password to the user

To assign the password to the user that we created then we can execute passwd command with the user name.

# passwd devopstutuser
Screenshot 2

3 – How to switch between user

To switch between users in Linux then we can execute su username – command with the username in which we want to switch. ‘

Please note that we need a password of a user in which we are switching

# su  user1 - 
Screenshot 3 1

4- How to log out from the current user

We can use the exit command to logged out from the current user that we logged in.

# exit 
Screenshot 4 1

5. # getent passswd root

The getent command is build to get the information of the user and display on the screen.

We don’t need to specify the full path to the file in order to get the information of the user.

Just write the file name then username.

# getent password devopstut
Screenshot 5 1

By default when we created the user the comment field is empty as follows

Screenshot 6 1

8. How to assign the comment at the time of user creation

To assign the comments at the time of the user creation then we can use ( -c ) option with useradd command and the comment that we want to assign

# useradd -c "IT-Admin" user1 
Screenshot 7 1

Now we can see that IT-Admin is filled in place of comment field in the file.

9. How to assign customized UserId for the at time of user creation

To assign the customized UID and GID of the user when we creating the user then we can use the ( -u ) option with the userid in useradd command.

# useradd -u 1500 user2 
Screenshot 8 1

Now we want to change the default home directory of the user3 to /user3data directory soo first make the directories as /user3data.

Screenshot 9 1

10 – If we want to change the default home directory of the user3 then we can use the ( -d ) option with the useradd command and the directory name where we want to make the home directory.

 # useradd -d /userdata3/userdata3 
Screenshot 10 1

By default when we create any user that user has /bin/bash shell where thy can logging in server.

Now we want to change the default shell of the user5 soo that user5 cannot login to the server.

11. How to change default login shell

To change the default login shell of the user at the time of creation then we can use the ( –s ) option with the useradd command after that we can change the default shell of the user4 now user4 will not able to login to the server.

 #useradd -s /sbin/nologin user5
Screenshot 11 1

How to Modify attributes a user in Linux

Before jumping how to modify the attributes user in the Linux system we should know the syntax of the command that is use to modify the user in Linux.

usermod is the command that is use to add the user in linux system.

Syntax

usermod option… arguments…

12. How to modify Userid of a user

To assign the customized UID of the user that we created then we can use usermod command with the ( -u ) option with the userid and username.

# usermod -u 1007 user1
Screenshot 12 1

13. How to change the username

To change the username then we can execute usermmod command with ( -l ) option.

In this first argument is the new name and the second is the username that we want to change

# usermod -l user5 user1 
Screenshot 13

14 . How to change default login shell of the user

To change the default login shell for the user then we can add usermod command with ( -s ) option and the login shell that we want to assign to the user

# usermod -s /sbin/nologin user1
Screenshot 14

15. How to modify the comments

To modify the comment for the user that we created then we can user usermod command with ( -c ) option and comment name.

# usermod -c "New-Comment" user1
Screenshot 15

16. How to change the default directory for the user

To change the default directory to the user then we can add ( -d ) option with the directory name.

Here We are changing the default directory to /user2dir for the user2 user.

# usermod -d /user2dir user2
Screenshot 16

How to delete the user in Linux

By default all the user related files is crated inside the /home directory

Screenshot 17

Before jumping how to delete user in the Linux system we should know the syntax of the command that is use to delete the user in Linux.

userdel is the command that is use to add the user in linux system.

Syntax

userdel option… arguments…

How to delete a user in Linux

To delete a user then we can execute userdel command with the username that we want to delete command without any option soo this command just deletes the user it’s a home directory is still exist in /home directory.

# userdel user1
Screenshot 18

How to delete the user with their home directory

If we add the ( -rf ) option in userdel command with the username then that will delete the home directory of the user add well.

# userdel -rf user2
Screenshot 19

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

If you want to read online manual for the useradd command click on this link

If you want to read online manual for the usermod command-click on this link

If you want to read the online manual for the userdel command-click on this link

This is the complete knowledge of how we can create / Modify / Delete user in the Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment