Creating a New User in Linux

It is a required and basic configuration task that every Linux system admin should be familiar with. We have completed similar configuration setup in prequisite CIT class.

In this blog, I will post basic linux commands to create a new user and assign a new group or existing group to the new user.

Adding a new user

useradd username

To allow this command to execute, we need to give root priveleage using sudo.

sudo useradd <username>

Making sure that new user has been added to the /etc/passwd

ls /etc/passwd

Logging as a new user

The following commands will prompt to enter and confirm the password.

sudo passwd <username>

Creating a new user with Specific Directory

When creating a new user without specifying the directory will automatically creates user’s home directory in /home.

However, we can specify the directory name when creating a new user.

sudo useradd -m -d /opt/username username 

Assigning Specific ID to user

In linux system, users are identiefied by unique ID. When creating a new user, the new user is assigned with next available user ID.

We also can assign specific UID for a new user.

sudo useradd -u **ID** username

Creating user with specific Group ID

When creating a new user we can assign specific Group ID number. This group ID should be created before we can run the following command.

sudo useradd -g users username

We can check if the user with desired Group ID has been assigned.

id -gn username

Assigning a User to multiple Groups

In Linux OS, there are two types of groups: Primary group and Secondary group. So, if the new user is created without assigning a group, then by default the new user’s home directory will be in /home.

Following commands will assign different groups to the new user. We can assign supplementary groups by using the tag -G along the new user.

sudo useradd -g users -G developer, analyst username

Creating a New User by Specifying Login Shell

In most Linux OS, the default shell is set to /bin/sh or else /bin/bash. We can also assign specific Login shell to the new user.

sudo useradd -s /usr/bin/shell username

We can verify if the commands has been executed by following commands.

ls /etc/passwd | grep <username>

cat /etc/passwd/<username>