How to Add or Delete Users in Debian?

A user in a Debian Linux-based system refers to an individual or process that interacts with the operating system. Each user in an operating system is defined with unique identifiers (UIDs) and specific permissions to access resources. This post will help you learn how to add or delete users in Debian.

Table of Contents
  1. Adding a User in Debian
  2. Checking UID and GID with the id Command
  3. Deleting a User in Debian

Adding a User in Debian

To add a user to a Debian system, you can use either useradd or adduser command. While both can be used for creating new users, adduser is generally more user-friendly, as it provides interactive prompts to guide you through the process.

Note- Replace [username] with the desired username of the new user.

1. Add a user in Debian using adduser:

The adduser command is a higher-level tool that prompts you for necessary details, such as the user’s password, full name, and other optional information. It’s recommended for most users, especially if you’re new to managing users in Linux to create a new user using adduser.

Syntax:
$> sudo adduser [username]

$> sudo adduser temp

You’ll be asked to enter a password for the new user, along with optional information such as the full name. Once confirmed, the user is added to the system, and their home directory is created automatically.

adding a new user using adduser
2. Add a user in Debian using useradd:

This command doesn’t ask for as much input as adduser, but it provides more flexibility and control for advanced users.

Syntax
$> sudo useradd [username]

$> sudo useradd temp

This creates the user, but you’ll likely need to set the password manually.

adding a new user using useradd
Setting User Passwords

Once you’ve created a user, you need to set their password. if you use useradd command to add a user, the password for the use can be set using passwd command.

$> sudo passwd temp

The user will be prompted to enter and confirm their password. If you’re an administrator, you can change another user’s password, but regular users can only change their own passwords.

setting password of new user

Using useradd to add a User with Specific Options:

The useradd command offers several options to customize the user account. Here’s how to modify the user creation process for different scenarios:

a.) Adding a User with a Comment:
You can add a descriptive comment about the user, such as their real name or job title.

$> sudo useradd -c "Comment" [username]

b.) Adding a User with a Specific Home Directory:
If you want the user to have a home directory in a location other than the default, you can specify it with the -d option.

$> sudo useradd -d /path/to/directory [username]

c.) Adding a User with a Specific UID:
If you need to assign a specific UID to a user, you can do so with the -u option.

$> sudo useradd -u UID [username]

d.) Adding a User with a Default Shell:
You can specify which shell the user will use by default, such as with the -s option.

$> sudo useradd -s /path/to/shell [username]

e.) Adding a User Without a Home Directory:
If you do not want the user to have a home directory, use the -M option.

$> sudo useradd -M [username]

f.) Adding a User to Specific Groups:
You can assign the user to additional groups using the -G option, which allows users to inherit group-specific permissions.

$> sudo useradd -G group1,group2,groupN [username]

Checking UID and GID with the id Command

To check the UID (User ID) and GID (Group ID) of any user, you can use the id command. This provides a quick overview of the user’s unique identifiers.

$> id username

It will display the UID, GID, and groups the user belongs to.


Deleting a User in Debian

When a user is no longer needed, you can delete their account. There are two main tools for removing users: deluser and userdel. The preferred command in Debian is deluser, as it is more user-friendly.

1. Using deluser:

a.) To delete a user without removing their home directory, use:

$> sudo deluser username 

b.) To delete the user and their home directory, use the –remove-home option:

$> sudo deluser --remove-home username 

c.) If you want to completely remove all files associated with the user, including system files, you can use the –remove-all-files option:

$> sudo deluser --remove-all-files username
deleting a user using deluser
2. Using userdel:

a.) To delete a user without removing their home directory, use:

$> sudo userdel username 

b.) To delete the user and their home directory, use -r option:

$> sudo userdel -r username