Create or Modify user in Linux

In this post we will look at the commands used to create a new user in Linux as-well-as modifying the same.

  • useradd – Create new user
  • usermod – Modify existing user

Creating new user

Typical command used to create user is

$sudo useradd -m -c "Tux User" -s /bin/bash tuxuser

This will create a tuxuser acccount with home directory and default shell as bash.

After creating user make sure password is set using “passwd” command

$sudo passwd tuxuser
New Password:
Retype new password:
passwd: password updated successfully

Useradd options

$sudo useradd 
 Usage: useradd [options] LOGIN
        useradd -D
        useradd -D [options]
 Options:
   -b, --base-dir BASE_DIR       base directory for the home directory of the
                                 new account
   -c, --comment COMMENT         GECOS field of the new account
   -d, --home-dir HOME_DIR       home directory of the new account
   -D, --defaults                print or change default useradd configuration
   -e, --expiredate EXPIRE_DATE  expiration date of the new account
   -f, --inactive INACTIVE       password inactivity period of the new account
   -g, --gid GROUP               name or ID of the primary group of the new
                                 account
   -G, --groups GROUPS           list of supplementary groups of the new
                                 account
   -h, --help                    display this help message and exit
   -k, --skel SKEL_DIR           use this alternative skeleton directory
   -K, --key KEY=VALUE           override /etc/login.defs defaults
   -l, --no-log-init             do not add the user to the lastlog and
                                 faillog databases
   -m, --create-home             create the user's home directory
   -M, --no-create-home          do not create the user's home directory
   -N, --no-user-group           do not create a group with the same name as
                                 the user
   -o, --non-unique              allow to create users with duplicate
                                 (non-unique) UID
   -p, --password PASSWORD       encrypted password of the new account
   -r, --system                  create a system account
   -R, --root CHROOT_DIR         directory to chroot into
   -s, --shell SHELL             login shell of the new account
   -u, --uid UID                 user ID of the new account
   -U, --user-group              create a group with the same name as the user
   -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

Modify User

Let’s say we now want to modify an user option, the command used for that is usermod.

Let’s look at an example, if I want to modify the group of tuxuser and add tuxuser to tuxadmin group.

$ id tuxuser 
uid=1003(tuxuser) gid=1004(tuxuser) groups=1004(tuxuser)
$ sudo usermod -aG tuxadmin tuxuser
$ id tuxuser 
uid=1004(tuxuser) gid=1005(tuxuser) groups=1005(tuxuser),1003(tuxadmin)

Usermod Options

Here is the full list of options available on usermod.

$ sudo usermod 
 Usage: usermod [options] LOGIN
 Options:
   -c, --comment COMMENT         new value of the GECOS field
   -d, --home HOME_DIR           new home directory for the user account
   -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
   -f, --inactive INACTIVE       set password inactive after expiration
                                 to INACTIVE
   -g, --gid GROUP               force use GROUP as new primary group
   -G, --groups GROUPS           new list of supplementary GROUPS
   -a, --append                  append the user to the supplemental GROUPS
                                 mentioned by the -G option without removing
                                 him/her from other groups
   -h, --help                    display this help message and exit
   -l, --login NEW_LOGIN         new value of the login name
   -L, --lock                    lock the user account
   -m, --move-home               move contents of the home directory to the
                                 new location (use only with -d)
   -o, --non-unique              allow using duplicate (non-unique) UID
   -p, --password PASSWORD       use encrypted password for the new password
   -R, --root CHROOT_DIR         directory to chroot into
   -s, --shell SHELL             new login shell for the user account
   -u, --uid UID                 new UID for the user account
   -U, --unlock                  unlock the user account
   -v, --add-subuids FIRST-LAST  add range of subordinate uids
   -V, --del-subuids FIRST-LAST  remove range of subordinate uids
   -w, --add-subgids FIRST-LAST  add range of subordinate gids
   -W, --del-subgids FIRST-LAST  remove range of subordinate gids
   -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

0 Comments on Create or Modify user in Linux

Leave a Comment

Your email address will not be published. Required fields are marked *

You Might Be Interested In