• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • 10 practical examples to use USERADD command in linux

    Q: How do you know what default values would be assigned to a user when created using useradd command?

    A: These are the two files which contain the default values to be assigned to a user when created using useradd
    # less /etc/default/useradd

    You can also view the default parameters set for new user to be created using
    # useradd -D

    The second file containing values used by useradd command for UID, GID, password encryption method and expiry related information
    # less /etc/login.defs
    MAIL_DIR        /var/spool/mail

    PASS_MAX_DAYS   99999
    PASS_MIN_LEN    5

    UID_MIN                   500
    UID_MAX                 60000

    GID_MIN                   500
    GID_MAX                 60000

    CREATE_HOME     yes
    UMASK           077


    1. How to change default values of useradd command?

    Either you can open /etc/default/useradd file and edit the file or you can also do the same using CLI as shown below

    To change the default home directory location for all new users
    # useradd -D -b /opt/users
    # useradd -D | grep HOME

    To change the default login shell
    # useradd -D -s /bin/sh
    # useradd -D | grep -i shell

    Now what if you want to add custom arguments to your user while creating them. let us discuss in detail the different options which you can use along with useradd command

    2. Create multiple users with same UID

    # useradd -o deepak -u 501
    # useradd -o deep -u 501
    # useradd -o user -u 501

    Verify the UID of the newly create users
    # grep 501 /etc/passwd

    3. Manually assign a UID to the user

    By default a user automatically gets any free uid more than 500 when you run the useradd command. But what if you manually want to assign a uid to your user

    # useradd -u 550 deepak
    Let us verify the assigned uid to deepak
    # id deepak
    uid=550(deepak) gid=550(deepak) groups=550(deepak)

    4. Create user without home directory

    # useradd -M test
    # su - test
    su: warning: cannot change directory to /home/test: No such file or directory

    -bash-4.1$ pwd

    5. Create user with custom defined home directory

    # useradd -d /home/users/test test
    # su - test

    $ pwd

    6. Add user to different primary group

    By default when you run useradd command, a group with the same name is created inside /etc/group but what if you donot want a group to be created with the same name instead add the user to some different already existing group.

    Here we will create a user "deep" and add him to group "admin" without creating another "deep" group
    # useradd -g admin deep
    Verify the groups of user "deep"
    # groups deep
    deep : admin

    7. Add user to different secondary group

    In the above command you saw if we are mentioning a different primary group while using useradd command then a default group with the name of user is NOT created. Now what if you want a group with username's to be created but instead you want the user to add some secondary group.

    Here user deepak is created along with group deepak but also in the same command we are adding deepak to dba group
    # useradd  -G dba deepak
    Verify groups of deepak
    # groups deepak
    deepak : deepak dba

    8. Add user to multiple groups

    You can add the user to multiple secondary groups using single command
    # useradd -G admin,dba deepak
    # groups deepak
    deepak : deepak admin dba

    9. Manually assign a shell to user

    Be default when you create a user in Red Hat Linux the user gets /bin/bash shell. But in case you want to give them some other shell for login use the below command
    # useradd -s /bin/sh  deepak
    # su - deepak

    -sh-4.1$ echo $SHELL

    10. Creating a user along with encrypted password

    Now you can create a user with pre-defined password, but the condition is the password used should be encrypted which you can do with various methods. Here I will show you one method to do so

    Encrypt your password using below command
    # openssl passwd -crypt mypassw0rd
    Warning: truncating password to 8 characters

    Now you can use the encrypted password for your new user
    # useradd -p TuUFdiN1KaCHQ deepak
    Try to login to the user, for which the password would be "mypassw0rd"
    10 practical examples to use USERADD command in linux 10 practical examples to use USERADD command in linux Reviewed by admin on Sunday, April 13, 2014 Rating: 5


    1. how to switch root without password

    2. you can do it using sudo su

      and add this line in your sudoers file
      username ALL = NOPASSWD: /bin/su

      1. Hi Deepak,

        How to provide root access to a user

      2. Simply add the user to the wheel group,. Else, open vim /etc/sudoers file and add the user to whom you want to give root previlages

    3. It should be add in 'visudo" command


    Powered by Blogger.