Linux How Tos

How To List and Manage Users in CentOS

In CentOS, you can list and manage users using various commands and tools. Here are some common commands to list and manage users:

List Users:

To list all users on your CentOS system, you can use the cat command to read the /etc/passwd file:

cat /etc/passwd
Each line in this file represents a user account.

There is another quick and effective way to obtain a list of usernames on a Linux system.

cut -d: -f1 /etc/passwd

The cut -d: -f1 /etc/passwd command is a concise way to list all the usernames on a Linux system. Here's what each part of the command does:

cut: This is the command used to cut and extract sections from each line of text.

-d:: The -d flag specifies the delimiter that separates fields on each line. In this case, the delimiter is a colon (:), which is the standard delimiter used in the /etc/passwd file to separate different fields like username, password, user ID, group ID, home directory, and shell.

-f1: The -f flag specifies which field to extract after splitting the line using the delimiter specified with -d. In this case, -f1 indicates that we want to extract the first field, which is the username.

/etc/passwd: This is the input file from which cut reads the data. In this case, it's the /etc/passwd file, which contains information about user accounts on the system.

So, when you run cut -d: -f1 /etc/passwd, it extracts and lists all the usernames from the /etc/passwd file by splitting each line based on the colon delimiter and selecting the first field (the username).

This command is a quick and effective way to obtain a list of usernames on a Linux system.

List Users with Home Directories:

To list users along with their home directories, you can use the awk command:

awk -F: '{print $1, $6}' /etc/passwd
This command extracts the username (field 1) and home directory (field 6) from the /etc/passwd file.

List Users in a Group:

To list users belonging to a specific group, you can use the getent command:

getent group group_name
Replace group_name with the name of the group you want to list.

Create a New User:

To create a new user, use the useradd command:

sudo useradd username
Replace username with the desired username. You can set additional options like home directory and shell using flags like -d and -s.

Set Password for a User:

To set a password for a user, use the passwd command:

sudo passwd username
Replace username with the user you want to set the password for. You will be prompted to enter and confirm the password.

Delete a User:

To delete a user, use the userdel command:

sudo userdel username
This will delete the user account, but it won't remove the home directory by default. To remove the home directory as well, use the -r flag: sudo userdel -r username.

Modify User Properties:

You can modify user properties, such as the username or home directory, using the usermod command:

sudo usermod -l new_username old_username
This command changes the username from old_username to new_username.

Create the New Home Directory:

sudo mkdir /new_home
Decide on the new location for the user's home directory. Typically, home directories are located in the /home directory. For example, if you want to change the home directory for a user named "username" to /new_home.

Change the User's Home Directory:

sudo usermod -d /new_home username
To change the home directory for a user, you can use the usermod command with the -d option.

Replace:
/new_home with the path to the new home directory.
username with the username of the user whose home directory you want to change.
For example:
sudo usermod -d /new_home username

Update Permissions (if needed):

sudo chown -R username:username /new_home
Depending on the situation, you may need to update permissions for the user's new home directory. You can use the chown command to change ownership if necessary.
Replace username with the actual username.

Verify the Changes:

To ensure that the changes have taken effect, you can use the grep command to check the user's entry in the /etc/passwd file:

grep username /etc/passwd
Make sure the entry shows the correct path to the new home directory.

Log in as the User:

It's a good practice to log in as the user to verify that they can access their new home directory without any issues.

 

These are some of the common commands for listing and managing users in CentOS. Be sure to run administrative commands with sudo to ensure you have the necessary privileges.