How to Create & Add user in Linux
In Linux environment we all are aware about the most popular command called ‘useradd’ or ‘adduser’. There are times when a Linux System Administrator asked to create user accounts on Linux with some specific properties, limitations or comments.
For Add user in linux or Cerate a User in linux we want to know about what is user.
What is User?
Every process on the system runs as a particular user. Every file is owned by a particular user. Access to files & directories are restricted by user.
After knowing what is user and role of user. We are going to see how we use ‘useradd’ and what’s the difference between ‘useradd’ and ‘adduser’.
A ‘useradd’ command is a low-level utility that is used for adding/creating user accounts in Linux and other Unix-like operating systems. The ‘adduser’ is much similar to useradd command, because it is just a symbolic link to it.
When we run ‘useradd’ command in Linux terminal, it performs following major things:
- useradd username sets reasonable defaults for all fields in /etc/passwd when run without options. The useradd command does not set any valid password by default , add the user cannot log in until a password is set.
It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly create User in Linux.
- useradd –help will display the basic options that can be used to override the defaults. In most cases , the same option can be used with the usermod command to modify an existing user .
- Some defaults , such as the range of valid UID numbers and default password aging rules, are read from the /etc/login.defs file. Values in this file only used when we create new user in linux .
Basic Syntax of Command for add user in linux
useradd [options] username
10 Basic commands for Creating a New User & Adding a New User in linux by these basic commands and some basic examples for useradd , you can create user in Linux
1. How to Add New User
To add/create a new user, all you’ve to follow the command ‘useradd’ or ‘adduser’ with ‘username’. The ‘username’ is a user login name, that is used by user to login into the system.
For Creating / Adding a new user in linux user should be the root user and for getting root privilege using su command.
Only one user can be added and that username must be unique (different from other username already exists on the system).
For example – to add new user ‘jerry’,using command ‘useradd’
When we add a new user in Linux with ‘useradd’ command it gets created in locked state and to unlock that user account, we need to set a password for that account with ‘passwd’ command.
Once a new user created, it’s entry automatically added to the ‘/etc/passwd‘ file. The file is used to store users information and the entry should be.
The above entry contains a set of seven colon-separated fields, each field has it’s own meaning. Let’s see what are these fields:
- Username: User login name used to login into system. It should be between 1 to 32 charcters long.
- Password: User password (or x character) stored in /etc/shadow file in encrypted format.
- User ID (UID): Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID’s ranging from 1-99 are reserved for other predefined accounts. Further UID’s ranging from 100-999 are reserved for system accounts and groups.
- Group ID (GID): The primary Group ID (GID) Group Identification Number stored in /etc/group file.
- User Info: This field is optional and allow you to define extra information about the user. For example, user full name. This field is filled by ‘finger’ command.
- Home Directory: The absolute location of user’s home directory.
- Shell: The absolute location of a user’s shell i.e. /bin/bash.
2. Create a User with Different Home Directory
By default ‘useradd’ command creates a user’s home directory under /home directory with username. Thus, for example, we’ve seen above the default home directory for the user ‘jerry’ is ‘/home/jerry’.
However, this action can be changed by using ‘-d’ option along with the location of new home directory (i.e. /data/projects).
For example, the following command will create a user ‘ron’ with a home directory ‘/data/projects’.
You can see the user home directory and other user related information like user id, group id, shell and comments.
3. Create a User with specific User ID (UID)
In Linux, every user has its own UID (Unique Identification Number). By default, whenever we create a new user accounts in Linux, it assigns userid 500, 501, 502 and so on…
But, we can create user’s with custom userid with ‘-u’ option. For example, the following command will create a user ‘harry’ with custom userid ‘501’.
NOTE: Make sure the value of a user ID must be unique from any other already created users on the system.
4. Create a User with specific Group ID (GID)
Similarly, every user has its own GID (Group Identification Number). We can create users with specific group ID’s as well with -g option.
Here in this example, we will add a user ‘oggy’ with a specific UID and GID simultaneously with the help of ‘-u’ and ‘-g’ options.
5. Add a User to a Multiple Groups
The ‘-G’ option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.
Here in this example, we are adding a user ‘stuwart’ into multiple groups like ‘abc’ & ‘def’.
verify that the multiple groups assigned to the user with ‘id’ command.
6. Add a User without Home Directory
In some situations, where we don’t want to assign a home directories for a user’s, due to some security reasons. In such situation, when a user logs into a system that has just restarted, its home directory will be root. When such user uses su command, its login directory will be the previous user home directory.
To create user’s without their home directories, ‘-M’ is used. For example, the following command will create a user ‘ninja’ without a home directory.
And , let’s verify that the user is created without home directory, using ls command.
7. Create a User with Account Expiry Date
By default, when we add user’s with ‘useradd’ command user account never get expires i.e their expiry date is set to 0 (means never expired).
However, we can set the expiry date using ‘-e’ option, that sets date in YYYY-MM-DD format. This is helpful for creating temporary accounts for a specific period of time.
Here in this example, we create a user ‘hattori’ with account expiry date i.e. 27th April 2014 in YYYY-MM-DD format.
And ,verify the age of account and password with ‘chage’ command for user ‘hattori’ after setting account expiry date.
8. Create a User with Password Expiry Date
The ‘-f’ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expire.
Here in this example, we will set a account password expiry date i.e. 45 days on a user ‘brad’ using ‘-e’ and ‘-f’options.
9. Add a User with Custom Comments
The ‘-c’ option allows you to add custom comments, such as user’s full name, phone number, etc to /etc/passwd file. The comment can be added as a single line without any spaces.
For example, the following command will add a user ‘mahi’ and would insert that user’s full name, MS Dhoni, into the comment field.
You can see your comments in ‘/etc/passwd’ file in comments section.
10. Change User Login Shell
Sometimes, we add users by using command ‘useradd’ which has nothing to do with login shell or sometimes we require to assign different shells to our users. We can assign different login shells to a each user with ‘-s’ option.
Here in this example, will add a user ‘nobita’ without login shell i.e. ‘/sbin/nologin’ shell.
You can check assigned shell to the user in ‘/etc/passwd’ file.
After practicing all these 10 Commands you are knowing how to Add a User in linux or Creating a User in linux by the following commands-
- useradd – For adding the new user
- passwd – Give password to user
- su- – For superuser/ rootuser access
- cat – Full form of cat is Concatenate
- uses of cat – cat command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.
- /etc/passwd – This is directory where information about useradd and there password are saved by default.
- -d – creating a user from different directory.
- -u – This create user’s with custom userid.
- -g – This create group with custom rid.
- –G – This option is used to add a user to additional groups.
- -M – To create user’s without their home directories
- -e – We can set the expiry date using -e.
- -f – This argument is used to define the number of days after a password expires.
- -c – This option allows you to add custom comments.
- -s – We can assign different login shells to a each user with ‘-s’ option.