BIOS,Windows, Linux, Software,Security, Domains, Web Hosting

How to Create User and Group in Linux

Create a user or a group in linux is easy, if you are using ubuntu with the lastest edition, you can add user in a friendly UI, but this post will introduce how to add user and group with a series command. This might be a common chioce for most linux edition.

Here I assume we will create a tstusr and a tstgroup, so I must have a root account or root permissoin with the next comming command. All the samples tested in Ubuntu. If you want to view users and groups existed in current system, try to see /etc/passwd.

1. Common commands about user and group management

About users management:

  • useradd - add user
  • adduser - add user
  • passwd - set password for a user
  • usermod - modify user, for example, the user name or his/her home directory, etc
  • id - view user UID, GID and his group
  • chfn - modify user information
  • su - switch user

About group management:

  • groupadd - add group
  • groupdel - delete group
  • groupmod - modify group information
  • groups - view users's group

2. Create group in linux

groupadd tstgroup

This command will add a group named tstgroup in linux.

3. Create user in linux

useradd -g tstgroup -d /home/tstusr -s /etc/bash -m tstusr

More usage:

useradd -g [groupname] -d [home folder] -s [its SHELL]

If you want to add a bash user(cmdusr in bashgrp), just like this:

groupadd bashgrp

useradd -r -s /bin/bash -g bashgrp cmdusr

Refer to the notes for more information.

3. Delete user in linux

userdel -r tstusr

or, replaced by these two commands:

userdel tstusr
rm -rf /home/tstusr

4. Usage of useradd and usermod

1. Usage: useradd [options] LOGIN

-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
-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


2. Usage: usermod [options] LOGIN

-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
-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
-Z, --selinux-user new SELinux user mapping for the user account

Related Articles


Leave a Reply

Subscribe to BootBeta Comments RSS feed to receive notification of latest comments posted.


  • Enter Email Address:

Recent Post

Copyright@ 2010-2014 Bootbeta All Rights Reserved.
HooBlog Ver 1.5 Build 20140530