Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Creating and Managing Users in Linux

Introduction

Managing users is a fundamental skill for any Linux system administrator. This tutorial will cover the basics of creating and managing users on a Linux system. We will explore user creation, modification, and deletion using command-line tools.

Creating a User

The most common command for creating a user is useradd. This command allows you to add a new user to the system.

Example:

To add a new user with the username john, you would execute the following command:

sudo useradd john

By default, this command will create a user with default settings. You can customize the creation process with various options.

Setting a Password for the User

After creating a user, you must set a password. This is done using the passwd command.

Example:

To set the password for user john, use the following command:

sudo passwd john

You will be prompted to enter and confirm the new password:

$ sudo passwd john
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Modifying a User

You can modify an existing user's information using the usermod command.

Example:

To change the username from john to john_doe, use:

sudo usermod -l john_doe john

Other common modifications include changing the user's home directory or shell.

Deleting a User

To delete a user, you can use the userdel command. This will remove the user from the system.

Example:

To delete the user john_doe, execute:

sudo userdel john_doe

If you also want to remove the user's home directory and mail spool, use the -r option:

sudo userdel -r john_doe

Listing Users

To list all users on the system, you can view the /etc/passwd file. This file contains information about user accounts.

Example:

To display the contents of the /etc/passwd file, use:

cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
john:x:1001:1001::/home/john:/bin/bash

Conclusion

Managing users in Linux is a critical task for system administrators. With the commands covered in this tutorial, you can create, modify, and delete user accounts, as well as set passwords and list users. Mastering these commands will help you effectively manage user accounts on your Linux system.