ADD USER in Linux
ADD USER in Linux
We all are aware about the most popular command called ‘useradd‘ or ‘adduser‘
in Linux. There are times when a Linux System Administrator asked to create user
accounts on Linux with some specific properties, limitations or comments.
In Linux, 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.
In some other Linux distributions, useradd command may comes with lightly
difference version. I suggest you to read your documentation, before using our
instructions to create new user accounts in Linux.
When we run ‘useradd‘ command in Linux terminal, it performs following major
things:
Itedits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly
created User account.
Creates and populate a home directory for the new user.
Sets permissions and ownerships to home directory.
List All Users in a Linux System
Method # 1: The “cat” command
To use the “cat” command to list all users
in a Linux system, the following steps
should be performed in order:
Use the “cat” command to list all the users
on the terminal to display all the user
account details and passwords stored in the
/etc/passwd file of the Linux system.
$ cat /etc/passwd
Method # 2: The “awk” command
The “awk” command is helpful if you want
to display usernames only, which may be
useful if you do not need all the technical
details returned with the “cat” command. To
use this command to list all users in a Linux
system, the following steps should be
performed in order:
Launch the terminal.
Run the following command:
$ awk –F: ‘{ print $1}’ /etc/passwd
Method # 3: The “compgen” command
Like the “awk” command, this command is
used to display only usernames, ignoring all
other details. To use the “compgen” command
to list all users of the Linux system, the
following steps should be performed in order:
Launch the terminal.
Run the following command:
compgen –u
Method # 4: The “getent” command
The output of the “getent” command is very
similar to that of the “cat” command, as it
displays a lot of details along with the
usernames. To use the “getent” command to
list all users in the Linux system, the following
steps should be performed in order:
Launch the terminal.
Run the following command:
$ getent passwd
Basic syntax of command is:
$ useradd [options] username
How to Add a New User in Linux
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.
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 a new user called ‘tecmint‘, use the following command.
[root@tecmint ~]# useradd tecmint
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.
root@tecmint ~]# passwd tecmint
Changing password for user tecmint.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
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.
tecmint:x:504:504:tecmint:/home/tecmint:/bin/bash
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.
Grep Command in Linux
Grep command is the most powerful and
regularly used Linux command-line utility.
Using Grep, you can search for useful
information by specifying a search criteria.
It searches for a particular expression
pattern in a specified file. When it finds a
match, it prints all the lines of a file that
matched the specified pattern. It comes in
handy when you have to filter through large
log files.
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 ‘tecmint‘ is
‘/home/tecmint‘.
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 ‘anusha‘ with a
home directory ‘/data/projects‘.
[root@tecmint ~]# useradd -d /data/projects anusha
You can see the user home directory and other user related
information like user id, group id, shell and comments.
[root@tecmint ~]# cat /etc/passwd | grep anusha
anusha:x:505:505::/data/projects:/bin/bash
Using Grep
Here is the basic syntax of grep command. It starts with grep
followed by some options and search criteria and then ends
with the file name.
$ grep [options] PATTERN [FILE...]
Search for files
To search for a file name in a directory that contains a
specific string in it, you can use grep in the following way:
$ ls -l | grep -i “string
For instance, to search for a filename that contains a string
“test“, the command would be:
$ ls –l | grep –i test
This command lists all the files that contain the string “test.
Create a User with Specific User ID
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 ‘navin‘ with
custom userid ‘999‘.
[root@tecmint ~]# useradd -u 999 navin
Now, let’s verify that the user created with a defined userid (999)
using following command.
[root@tecmint ~]# cat /etc/passwd | grep navin
navin:x:999:999::/home/navin:/bin/bash
NOTE: Make sure the value of a user ID must be unique from any
other already created users on the system.
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 ‘shilpi‘ without
a home directory.
[root@tecmint ~]# useradd -M shilpi
Now, let’s verify that the user is created without home directory, using
ls command.
[root@tecmint ~]# ls -l /home/shilpi
ls: cannot access /home/shilpi: No such file or directory
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
‘tecmint’ using ‘-e‘ and ‘-f‘ options.
[root@tecmint ~]# useradd -e 2014-04-27 -f
45 tecmint
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
‘mansi‘ and would insert that user’s full name, Manis
Khurana, into the comment field.
[root@tecmint ~]# useradd -c "Manis Khurana" mansi
You can see your comments in ‘/etc/passwd‘ file in
comments section.
[root@tecmint ~]# tail -1 /etc/passwd
mansi:x:1006:1008:Manis Khurana:/home/mansi:/bin/sh
Change User Login Shell:
Sometimes, we add users 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 ‘tecmint‘ without
login shell i.e. ‘/sbin/nologin‘ shell.
[root@tecmint ~]# useradd -s /sbin/nologin tecmint
You can check assigned shell to the user in ‘/etc/passwd‘
file.
[root@tecmint ~]# tail -1 /etc/passwd
tecmint:x:1002:1002::/home/tecmint:/sbin/nologin
Add a User with Specific Home Directory, Default
Shell and Custom Comment
The following command will create a user ‘ravi‘ with
home directory ‘/var/www/tecmint‘, default
shell /bin/bashand adds extra information about user.
[root@tecmint ~]# useradd -m -d /var/www/ravi -s
/bin/bash -c "TecMint Owner" -U ravi
In the above command ‘-m -d‘ option creates a user
with specified home directory and the ‘-s‘ option set
the user’s default shell i.e. /bin/bash. The ‘-c‘ option
adds the extra information about user and ‘-U‘
argument create/adds a group with the same name as
the user.
Add a User with Home Directory, Custom Shell,
Custom Comment and UID/GID
The command is very similar to above, but
here we defining shell as ‘/bin/zsh‘ and
custom UID and GID to a user ‘tarunika‘.
Where ‘-u‘ defines new
user’s UID (i.e. 1000) and whereas ‘-g‘
defines GID (i.e. 1000).
[root@tecmint ~]# useradd -m -d
/var/www/tarunika -s /bin/zsh -c
"TecMint Technical Writer" -u 1000 -g
1000 tarunika
Add a User with Home Directory, No
Shell, Custom Comment and User ID
The following command is very much similar to
above two commands, the only difference is here,
that we disabling login shell to a user called
‘avishek‘ with custom User ID (i.e. 1019).
Here ‘-s‘ option adds the default shell /bin/bash,
but in this case we set login to ‘/usr/sbin/nologin‘.
That means user ‘avishek‘ will not able to login
into the system.
[root@tecmint ~]# useradd -m -d
/var/www/avishek -s /usr/sbin/nologin -c
"TecMint Sr. Technical Writer" -u 1019 avishek
Add a User with Home Directory, Shell, Custom
Skell/Comment and User ID
The only change in this command is, we
used ‘-k‘ option to set custom skeleton
directory i.e. /etc/custom.skell, not the
default one /etc/skel. We also used ‘-s‘
option to define different shell
i.e. /bin/tcsh to user ‘navin‘.
[root@tecmint ~]# useradd -m -d
/var/www/navin -k /etc/custom.skell -s
/bin/tcsh -c "No Active Member of
TecMint" -u 1027 navin
Add a User with Home Directory, No Shell,
Custom Comment and User ID
The following command is very much similar to
above two commands, the only difference is here,
that we disabling login shell to a user called
‘avishek‘ with custom User ID (i.e. 1019).
Here ‘-s‘ option adds the default shell /bin/bash,
but in this case we set login to ‘/usr/sbin/nologin‘.
That means user ‘avishek‘ will not able to login
into the system.
[root@tecmint ~]# useradd -m -d
/var/www/avishek -s /usr/sbin/nologin -c
"TecMint Sr. Technical Writer" -u 1019 avishek
Add a User with Home Directory, Shell, Custom
Skell/Comment and User ID
The only change in this command is, we
used ‘-k‘ option to set custom skeleton
directory i.e. /etc/custom.skell, not the
default one /etc/skel. We also used ‘-s‘
option to define different shell
i.e. /bin/tcsh to user ‘navin‘.
[root@tecmint ~]# useradd -m -d
/var/www/navin -k /etc/custom.skell -s
/bin/tcsh -c "No Active Member of
TecMint" -u 1027 navin
Add a User without Home Directory, No Shell,
No Group and Custom Comment
This following command is very different
than the other commands explained above.
Here we used ‘-M‘ option to create user
without user’s home directory and ‘-N‘
argument is used that tells the system to only
create username (without group). The ‘-r‘
arguments is for creating a system user.
[root@tecmint ~]# useradd -M -N -r -s
/bin/false -c "Disabled TecMint Member"
clayton