Linux is a multi-user operating system, which means that more than one user can use Linux at the same time. Linux provides a beautiful mechanism to manage users in a system. One of the most important roles of a system administrator is to manage the users and groups in a system.
In this article I will explain how to add and remove user account from command line in Linux System.
Adding New User to an Linux System
1. Run the below command to get the user manual.
# man useradd
2. Create the new user account with their home directory.
# useradd --home /home/sagar sagar
# adduser sagar
3. Useraddd command does not set any valid password by default, and user cannot log in until a password is set.To set the password user the following command:
# passwd sagar Changing password for user sagar. New password: Retype new password: passwd: all authentication tokens updated successfully.
4. Verify the values in /etc/password:
# cat /etc/passwd | grep sagar sagar:x:501:501::/home/sagar:/bin/bash
5. Verify the values in /etc/group :
# cat /etc/group | grep sagar sagar:x:501:
6. Verify email user created for id sagar:
# ls /var/spool/mail | grep sagar sagar
More other options:
-c, –comment COMMENT
Add a value, such as a full name, to the GECOS field.
-g, –gid GROUP
Specify the primary group for the user account.
-G, –groups GROUPS
Specify a list if supplementary groups for the user account.
Used with the -G option to append the user to the supplemental groups mentioned without removing the user from other groups.
-d, –home HOME_DIR
Specify a new home directory to a new location. Must be used with the -d option.
Move a user home directory to a new location. Must be used with the -d option.
s, –shell SHELL
Specify a new login shell for the user account.
Lock a user account.
Unlock a user account.
Deleting User from Linux System
1. Run the below command to get userdel manual.
# man userdel
2. userdel username removes the user from /etc/passwd, but leaves the home directory intact by default. Proper command to remove the user’s account, user’s home directory and mail spool as part of the deletion process.
# userdel --remove sagar Or # userdel -r sagar