In this tutorial we will learn about creating and deleting the user in Red Hat and CentOS.
In this post we will explore the useradd and userdel commands.
In this post we will explore the useradd and userdel commands.
Basic command to create user
In Red Hat and CentOS,create a user and set password as per given below command
Now, lets see what happen when you simply use the useradd command in Red Hat and CentOS.
Here I am taking an eg. of creating a user called sharad
Here I am taking an eg. of creating a user called sharad
When we create a user by using command “useradd sharad”,the following things are happened
Explore default options of useradd command
To find default options of useradd,use the below given command
See the below reference of output
[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#[root@localhost ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes[root@localhost ~]#
useradd -n test
Now here Question comes, Why UID and GID was 500 when we created the first user called sharad.
Answer: It get the value from /etc/login.defs file.
Answer: It get the value from /etc/login.defs file.
Tip: If you set the value
CREATE_HOME no
in /etc/login.defs file, the home directory of user will not be created.See in below reference the newly created user’s home directory and /etc/skel contents are same
Question: Where is the mail spool directory
Answer: It is mentioned in /etc/login.defs file.
Answer: It is mentioned in /etc/login.defs file.
How to change default value of useradd command
You can also change the default value of useradd
Method 1 : By using command line
Method 1 : By using command line
examples:
For changing default shell use command
useradd -D -s /shell/path
For Changing Default Home Directory,use command useradd -D -b /new/home_dir/path
Likewise you can also do other changes.
Method 2: By editing /etc/default/useradd .
Using useradd command with many options
(1) Changing login shell at useradd command. Bydefault the login shell is /bin/bash
Use -s with useradd command
Use -s with useradd command
(2) Changing default home directory to other path.
Use -d option here,
Use -d option here,
useradd -d /Path/username username
(3) Changing userid , use -u option here
(3) Changing group id with useradd command, use -g option.
Note 1: Group must already exist so that we can use its GID. See below example.
GID of hr group is 601
Note 1: Group must already exist so that we can use its GID. See below example.
GID of hr group is 601
Note 2: hr group has GID 600 . User tester taken bydefault UID 601 also because there was no user exist with this UID. If exist than it would get the different UID as per increment pattern.
(4) You can use available options in single line. Here I have added -c for GECOS or comment
See below example
See below example
(5) Set password in single line with -p option. But here you have to get encrypt passwd.
useradd -p #$#@encrypted@#$ username
see below example how you will do. Here I will use the password PaaSS2ord
Get encrypted password by using command openssl
after using openssl command we get the encrypted value of PaaSS2ord as gYqytYyfGxwII
Now use this value with -p option
Now use this value with -p option
You can check by login user testred using the password PaaSS2ord
Below given are options which you can use with useradd command
Delete User in Red hat and CentOS
(1) To delete the user ,use below given command
Note: The above command will not remove user’s home directory and mail spool
(2) Delete user with its home directory and mail spool. Use option -r
Other options which you can also use
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost ~]# userdel --help
Usage: userdel [options] LOGIN
Options:
-f, --force force removal of files,
even if not owned by user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-Z, --selinux-user remove SELinux user from SELinux user mapping
[root@localhost ~
|
Examples:-
Example 1: Linux useradd Command — Create User With Default Configurations
This is a fundamental low level tool for user creation. To create user with default configurations use useradd as shown below.
Syntax: # useradd LOGIN-NAME
While creating users as mentioned above, all the default options will be taken except group id. To view the default options give the following command with the option -D.
$ useradd -D GROUP=1001 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/sh SKEL=/etc/skel CREATE_MAIL_SPOOL=no
- GROUP: This is the only option which will not be taken as default. Because if you don’t specify -n option a group with same name as the user will be created and the user will be added to that group. To avoid that and to make the user as the member of the default group you need to give the option -n.
- HOME: This is the default path prefix for the home directory. Now the home directory will be created as /home/USERNAME.
- INACTIVE: -1 by default disables the feature of disabling the account once the user password has expired. To change this behavior you need to give a positive number which means if the password gets expired after the given number of days the user account will be disabled.
- EXPIRE: The date on which the user account will be disabled.
- SHELL: Users login shell.
- SKEL: Contents of the skel directory will be copied to the users home directory.
- CREATE_MAIL_SPOOL: According to the value creates or does not create the mail spool.
Example 1: Creating user with all the default options, and with his own group.
Following example creates user ramesh with group ramesh. Use Linux passwd command to change the password for the user immediately after user creation.
# useradd ramesh # passwd ramesh Changing password for user ramesh. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. # grep ramesh /etc/passwd ramesh:x:500:500::/home/ramesh:/bin/bash # grep ramesh /etc/group ramesh:x:500: [Note: default useradd command created ramesh as username and group]
Example 2: Creating an user with all the default options, and with the default group.
# useradd -n sathiya # grep sathiya /etc/passwd sathiya:x:511:100::/home/sathiya:/bin/bash # grep sathiya /etc/group [Note: No rows returned, as group sathiya was not created] # grep 100 /etc/group users:x:100: [Note: useradd -n command created user sathiya with default group id 100] # passwd sathiya Changing password for user sathiya. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. [Note: Always set the password immediately after user creation]
Example 3: Editing the default options used by useradd.
The following example shows how to change the default shell from /bin/bash to /bin/ksh during user creation.
Syntax: # useradd -D --shell=<SHELLNAME> # useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/bash SKEL=/etc/skel [Note: The default shell is /bin/bash] # useradd -D -s /bin/ksh # useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/ksh SKEL=/etc/skel [Note: Now the default shell changed to /bin/ksh] # adduser priya # grep priya /etc/passwd priya:x:512:512::/home/priya:/bin/ksh [Note: New users are getting created with /bin/ksh] # useradd -D -s /bin/bash [Note: Set it back to /bin/bash, as the above is only for testing purpose]
Method 2: Linux useradd Command — Create Users With Custom Configurations
Instead of accepting the default values (for example, group, shell etc.) that is given by the useradd command as shown in the above method, you can specify custom values in the command line as parameters to the useradd command.
Syntax: # useradd -s <SHELL> -m -d <HomeDir> -g <Group> UserName
- -s SHELL : Login shell for the user.
- -m : Create user’s home directory if it does not exist.
- -d HomeDir : Home directory of the user.
- -g Group : Group name or number of the user.
- UserName : Login id of the user.
Example 4: Crate Linux User with Custom Configurations Using useradd Command
The following example creates an account (lebron) with home directory /home/king, default shell as /bin/csh and with comment “LeBron James”.
# useradd -s /bin/csh -m -d /home/king -c "LeBron James" -g root lebron # grep lebron /etc/passwd lebron:x:513:0:LeBron James:/home/king:/bin/csh
Note: You can give the password using -p option, which should be encrypted password. Or you can use the passwd command to change the password of the user.
No comments:
Post a Comment