User Management For Linux Administrator

November 26, 2024

By: ATB Team

User Management For Linux Administrator

User management in Linux is an essential task for system administrators. Linux provides a set of commands and tools to create, modify, delete, and manage users and groups on the system. Below is an overview of the most commonly used commands and techniques for user management in Linux.

1. Creating a New User

To create a new user, use the useradd command. Here’s a basic example:

sudo useradd -m -s /bin/bash new_username
  • -m: Creates a home directory for the user at /home/new_username.
  • -s /bin/bash: Specifies the shell the user will use (in this case, /bin/bash).

After creating the user, set a password with:

sudo passwd new_username

2. Modifying an Existing User

To modify an existing user’s details, you can use the usermod command. Common modifications include changing the username, home directory, shell, or group memberships.

Change the username:

sudo usermod -l new_username old_username

Change the home directory:

sudo usermod -d /new/home/directory new_username

Change the login shell:

sudo usermod -s /bin/zsh new_username

Add a user to a group:

sudo usermod -aG group_name new_username

The -aG option appends the user to the specified group (group_name) without removing them from other groups.

3. Deleting a User

To remove a user and their associated files, use the userdel command.

sudo userdel -r username
  • -r: Removes the user’s home directory and mail spool in addition to the user account.

Be cautious when using userdel -r, as it permanently deletes the user’s files.

4. Listing Users

To view all users on the system, you can inspect the /etc/passwd file, which contains user account information.

cat /etc/passwd

Each line represents a user, with fields like username, user ID (UID), group ID (GID), home directory, and shell.

To list only usernames:

cut -d: -f1 /etc/passwd

5. Changing User Passwords

To change the password of a user, use the passwd command:

sudo passwd username

This will prompt you to enter a new password for the user.

6. Locking and Unlocking User Accounts

You can lock a user’s account to prevent login using the passwd command with the -l option:

sudo passwd -l username

To unlock the account:

sudo passwd -u username

7. User Groups Management

Create a new group:

sudo groupadd group_name

Delete a group:

sudo groupdel group_name

Add a user to a group:

sudo usermod -aG group_name username

Remove a user from a group:

sudo gpasswd -d username group_name

8. Viewing User Information

To view the details of a specific user, you can use the id command:

id username

This will display the user’s UID, GID, and group memberships.

9. Managing User Permissions

Permissions in Linux are handled through user, group, and others. Each file has permissions that define who can read, write, or execute it.

Change file permissions:

Use the chmod command to modify file permissions:

chmod u+x file.txt
  • u+x grants execute permission to the user (owner) of the file.

Change file ownership:

Use the chown command to change the owner or group of a file:

sudo chown username:groupname file.txt

This command changes the owner of file.txt to username and the group to groupname.

Change group ownership:

sudo chgrp groupname file.txt

This command changes the group of file.txt to groupname.

10. Viewing Group Information

To view the groups on the system, you can check the /etc/group file:

cat /etc/group

Each line represents a group, with details like group name, group ID (GID), and member users.

View groups for a user:

To see which groups a user belongs to, use the groups command:

groups username

11. Checking Account Expiry and Aging

Linux allows you to set account expiry and password aging settings. Use the chage command to view or modify these settings.

  • View password aging details:
sudo chage -l username
  • Set password expiration:
sudo chage -M 30 username

This sets the maximum number of days (30) before the user must change their password.

12. Sudo (Superuser) Access

To grant a user the ability to execute commands as a superuser, add them to the sudo group:

sudo usermod -aG sudo username

On some distributions, the group may be called wheel instead of sudo.

13. Audit and Monitor User Activity

For security and auditing purposes, you may want to track user logins and activities. Common logs include:

  • /var/log/auth.log: Tracks authentication activities, including successful and failed login attempts.
  • /var/log/wtmp and /var/log/btmp: Track login sessions and login attempts, respectively.

To check login history:

last username

To check failed login attempts:

lastb

Summary

  1. Creating users: useradd, passwd
  2. Modifying users: usermod
  3. Deleting users: userdel
  4. Managing groups: groupadd, groupdel, usermod -aG
  5. Viewing information: id, groups, cat /etc/passwd
  6. Permissions: chmod, chown, chgrp
  7. Account expiry: chage
  8. Sudo access: Add user to sudo group
  9. Audit and logs: last, lastb

These commands form the core of user and group management in Linux, and mastering them is crucial for effective system administration.

Leave a Comment

Table Of Content