Creating and Managing User Passwords in Linux
Managing users is a fundamental part of Linux administration. The process
usually begins with creating a new user and then setting or changing their
password. In this article, we will focus on two key aspects: creating new users
and managing their passwords. We'll look at the commands involved,
primarily useradd
and passwd
, and explore the various options you can use
with these commands.
User Creation with useradd
The useradd
command in Linux is used to create a new user. The basic syntax
for creating a new user is:
sudo useradd [OPTIONS] USERNAME
You can replace USERNAME
with the username you want to assign to the new user.
Example
To create a new user called alice
, run:
sudo useradd alice
This will create a new user with the default settings. To set a password for
this user, we use the passwd
command, which we'll explore in detail below.
Managing Passwords with passwd
The passwd
command is used to change the password for a user account. A
superuser can change the password for any account, whereas a regular user can
only change their own password.
Syntax
The basic syntax of the passwd
command is:
passwd [OPTIONS] [USERNAME]
Options and Their Descriptions
Here's a table of commonly used options with the passwd
command:
Option | Shorthand | Description |
---|---|---|
--delete | -d | Delete the password for the specified user. |
--expire | -e | Force expire the user's password immediately. |
--lock | -l | Lock the password of the specified account. |
--unlock | -u | Unlock the locked password of the specified user. |
--status | -S | Display the status of the user's password. |
Example: Setting a Password
To set a password for the user alice
:
sudo passwd alice
You will be prompted to enter the new password twice for confirmation.
Example: Deleting a Password
To delete a user's password:
sudo passwd -d alice
Example: Locking and Unlocking a User Account
To lock a user account:
sudo passwd -l alice
To unlock a user account:
sudo passwd -u alice
Example: Checking Password Status
To check the password status of a user:
sudo passwd -S alice
Listing Users and Their Permissions in Linux
Sometimes, as an administrator or even as a curious user, you may want to list the users and their permissions on your Linux system. Various commands and files can help you accomplish these tasks.
getent
One of the most reliable ways to list users is by using the getent
command:
getent passwd
This command will list all the users along with some other details like home directory, default shell, etc.
/etc/passwd
Alternatively, you can also view the /etc/passwd
file, which contains details
of all user accounts:
cat /etc/passwd
Each line represents a user and contains information separated by colons.
Checking Permissions
ls -l
To check file or directory permissions, you can use the ls -l
command followed
by the file or directory name. For example:
ls -l /path/to/file
This will show you the permissions in the form -rwxr-xr-x
, along with the
owner and the group.
stat
Another command you can use is stat
, which provides detailed information about
files or directories, including permissions.
stat /path/to/file
id
To list a user's groups and other related information, you can use:
id [username]
Replace [username]
with the user you want to check. If left blank, it will
show information about the currently logged-in user.
/etc/group
The /etc/group
file contains details about groups and their memberships:
cat /etc/group
Examples
To check permissions for a file named example.txt
:
ls -l example.txt
To list all groups that a user named alice
is a part of:
id alice
Summary
Managing user accounts and passwords are crucial tasks for system
administrators. The useradd
and passwd
commands provide a range of options
to create new users and manage their passwords effectively. Understanding these
options and how to combine them is key to maintaining a secure Linux
environment.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.