Linux menu

Wednesday, September 24, 2014

Configuring User's in Linux

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.
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
When we create a user by using command “useradd sharad”,the following things are happened



  • Same name of group is created , here new group name is sharad and user called sharad is member of it. Means sharad user is member of group called sharad
  • The user’s home directory of user called sharad is created in /home , see the command ls -ld /home/sharad
  • User sharad got a login shell /bin/bash which you can find by using command grep sharad /etc/passwd
  • In Red Hat and CentOS,when first user is created the uid and gid starts with 500 (uid= user id and gid= group id)
  • We have not set the password for user sharad,hence some content is missing in output of command “grep sharad /etc/shadow”


  • 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 ~]#




  • GROUP: Bydefault this option is not taken by useradd command. To get default group as peruseradd -D command, you have to use -n option with useradd command. For eg. 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.
  • HOME: This is the default path prefix for the home directory. The user’s home directory will be created as /home/USER-Login-Name.
    Tip: If you set the value CREATE_HOME no in /etc/login.defs file, the home directory of user will not be created.
  • INACTIVE: The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1 by default.
  • EXPIRE: The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD
  • SHELL: Users login shell.
  • SKEL: Contents inside skel directory will be copied to the users home directory.

    See in below reference the newly created user’s home directory and /etc/skel contents are same
  • CREATE_MAIL_SPOOL: Here the bydefault value is CREATE_MAIL_SPOOL=yes means mail spool directory will be created.
    Question: Where is the mail spool directory
    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
    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
    (2) Changing default home directory to other path.
    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 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
    (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
    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

    No comments: