Creating User and Groups

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Creating User and Groups

Adding a new User

The useradd username command creates a new user named username. It sets up the
user's home directory and account information and creates a private group for the user named
username. At this point, the account does not have a valid password set, and the user cannot log in until a password
is set.

Modifying Existing Users from the Command Line

• The usermod command.

Some common options include:

usermod options: Usage


-c, --comment COMMENT Add the user's real name to the comment field.
-g, --gid GROUP Specify the primary group for the user account.
-G, --groups GROUPS Specify a comma-separated list of supplementary groups for
the user account.
-a, --append Used with the -G option to add the supplementary groups
to the user's current set of group memberships instead of
replacing the set of supplementary groups with a new set.
-d, --home HOME_DIR Specify a particular home directory for the user account.
-m, --move-home Move the user's home directory to a new location. Must be
used with the -d option.
-s, --shell SHELL Specify a particular login shell for the user account.
-L, --lock Lock the user account.
-U, --unlock Unlock the user account.

Deleting Users from the Command Line

• The userdel username command removes the details of username from /etc/passwd, but
leaves the user's home directory intact.
• The userdel -r username command removes

1. You can use the #id command to check the information about the currently logged-in user.

# id
uid=1000(user01) gid=1000(user01) groups=1000(user01)
context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

To view basic information about another user, pass the username to the id command as an
argument.

# id user02
uid=1002(user02) gid=1001(user02) groups=1001(user02)
context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
2. Adding a user

The syntax for useradd command

# useradd [options] user

# useradd username

 Creating a different directory for the user

# useradd -d /data/projects username

 Create a User with a Specific User ID

# useradd -u 1002 username

 Create a User with a Specific Group ID

# useradd -u 1005 -g groupname username

 Add a User with Custom Comments

# useradd -c "I am user 5" username

 Create a User Login Shell in Linux

# useradd -s /sbin/nologin username

 Add a User with Home Directory, Shell, Custom Comment, and UID/GID

# useradd -m -d /var/www/username -s /bin/bash -c "Apache user" -u 1006 -g 1006 username

3. Setting up a password for the user


# passwd username

Changing password for user user01.


New password: xyz
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password: zyz
passwd: all authentication tokens updated successfully.

4. Adding a group
The syntax for groupadd command

# groupadd [options] group

 Create a new group

# groupadd groupname

 Create a new group with a specific group ID


# groupadd -g 1010 groupname

 Creating a System Group

# groupadd -r mysystemgroup

 Setting up password for the group while creation

#groupadd -p 123123 groupname

5. Modify User Accounts with Usermod Command

The syntax for usermod command


# usermod [options] username

 Change user name

# usermod -l new_username old_username

 Change the home directory of user

# usermod -d new_home_dir user_name

A better way to change the user's home directory is to use the -m option with -d. This way, it will
create a new home directory if it doesn’t exist. It will also move the content of the old home directory
to the new home directory with correct file permissions and ownership.

# usermod -md new_home_dir user_name

 Change the login shell of the user

# usermod -s /bin/zsh username

 Change the default user group

# usermod -g new_default_group_name username

 Add the user to other groups

# usermod -aG group_name username

 Lock and unlock the user account

# usermod -L username - Lock Account

# usermod -U username - Unlock Account

 Set an expiry date for the user account

# usermod -e 2020-04-01 username

 Change the UID of a user

# usermod -u UID username

You might also like