Introduction
Introduction
User management is one of the most important responsibilities of a Linux Administrator.
In an enterprise environment, administrators need to manage:
Users and groups
Passwords and password aging
File ownership and permissions
ACLs
Controlled administrative access using sudo
SSH authentication
User access troubleshooting
A Linux Administrator should be able to:
Create and delete users
Create and manage groups
Configure file and directory permissions
Manage passwords
Configure password aging
Provide controlled sudo access
Configure SSH key-based authentication
Troubleshoot user-access issues
User management is one of the most important responsibilities of a Linux Administrator.
In an enterprise environment, administrators need to manage:
Users and groups
Passwords and password aging
File ownership and permissions
ACLs
Controlled administrative access using
sudoSSH authentication
User access troubleshooting
A Linux Administrator should be able to:
Create and delete users
Create and manage groups
Configure file and directory permissions
Manage passwords
Configure password aging
Provide controlled
sudoaccessConfigure SSH key-based authentication
Troubleshoot user-access issues
1. Types of Users in Linux
Linux generally has three main categories of users.
Linux generally has three main categories of users.
1.1 Root User
The root user is the Linux superuser and has almost unlimited privileges.
[root@rocky8 ~]# id root
uid=0(root) gid=0(root) groups=0(root)
Because root has extensive privileges, direct root access should be carefully controlled in an enterprise environment.
The root user is the Linux superuser and has almost unlimited privileges.
[root@rocky8 ~]# id root
uid=0(root) gid=0(root) groups=0(root)Because root has extensive privileges, direct root access should be carefully controlled in an enterprise environment.
1.2 System Users
System users are normally created for applications and services.
Examples:
apache
nginx
mysql
These accounts are generally used to run services rather than for interactive human login.
System users are normally created for applications and services.
Examples:
apache
nginx
mysqlThese accounts are generally used to run services rather than for interactive human login.
1.3 Regular Users
Regular users are created for people who need to log in and work on the system.
Examples:
john
admin
developer
support
Regular users are created for people who need to log in and work on the system.
Examples:
john
admin
developer
support2. Creating Linux Users
The basic syntax for creating a user is:
[root@rocky8 ~]# useradd user1
Create a user with a home directory and Bash shell:
[root@rocky8 ~]# useradd -m -s /bin/bash user2
Create a user with a comment:
[root@rocky8 ~]# useradd -m -s /bin/bash -c "DevOps Engineer" john
Create a user and assign a primary group:
[root@rocky8 ~]# useradd -m -g devops -s /bin/bash -c "DevOps Engineer" user4
The basic syntax for creating a user is:
[root@rocky8 ~]# useradd user1Create a user with a home directory and Bash shell:
[root@rocky8 ~]# useradd -m -s /bin/bash user2Create a user with a comment:
[root@rocky8 ~]# useradd -m -s /bin/bash -c "DevOps Engineer" johnCreate a user and assign a primary group:
[root@rocky8 ~]# useradd -m -g devops -s /bin/bash -c "DevOps Engineer" user4Common useradd Options
Option Description -mCreate the user's home directory -uSpecify UID -gSpecify primary group -GSpecify supplementary groups -cAdd a comment -sSpecify login shell -dSpecify home directory
| Option | Description |
|---|---|
-m | Create the user's home directory |
-u | Specify UID |
-g | Specify primary group |
-G | Specify supplementary groups |
-c | Add a comment |
-s | Specify login shell |
-d | Specify home directory |
3. Enterprise Example – Creating a User with Specific UID and Groups
In enterprise environments, user information may be provided in a predefined format.
For example:
Parameter Value Username a181361User ID 9181361Primary Group ID 3100000Secondary Group ID 3100004Comment Vallabh DaroleHome Directory Automatically created
The corresponding command is:
[root@rocky8 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
This is useful when an organization maintains standardized UID/GID mappings.
Enterprise Tip: Before assigning a specific UID or GID, verify that the ID is not already being used on the system.
Check whether a UID exists:
[root@rocky8 ~]# getent passwd 9181361
Check whether a GID exists:
[root@rocky8 ~]# getent group 3100000
In enterprise environments, user information may be provided in a predefined format.
For example:
| Parameter | Value |
|---|---|
| Username | a181361 |
| User ID | 9181361 |
| Primary Group ID | 3100000 |
| Secondary Group ID | 3100004 |
| Comment | Vallabh Darole |
| Home Directory | Automatically created |
The corresponding command is:
[root@rocky8 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361This is useful when an organization maintains standardized UID/GID mappings.
Enterprise Tip: Before assigning a specific UID or GID, verify that the ID is not already being used on the system.
Check whether a UID exists:
[root@rocky8 ~]# getent passwd 9181361Check whether a GID exists:
[root@rocky8 ~]# getent group 31000004. Create User and Password
4.1 RHEL / Rocky Linux
On RHEL-based systems, passwd --stdin may be available depending on the OS version and configuration.
Example:
[root@rocky8 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
[root@rocky8 ~]# echo 'a181361:pass@1234' | chpasswd
[root@rocky8 ~]# passwd -e a181361
The passwd -e command forces the user to change the password at the next login.
An alternative often seen on older RHEL/Rocky environments is:
[root@rocky8 ~]# echo 'pass@1234' | passwd --stdin a181361
Note: passwd --stdin is not portable across Linux distributions. chpasswd is generally more portable for scripted password changes.
On RHEL-based systems, passwd --stdin may be available depending on the OS version and configuration.
Example:
[root@rocky8 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
[root@rocky8 ~]# echo 'a181361:pass@1234' | chpasswd
[root@rocky8 ~]# passwd -e a181361The passwd -e command forces the user to change the password at the next login.
An alternative often seen on older RHEL/Rocky environments is:
[root@rocky8 ~]# echo 'pass@1234' | passwd --stdin a181361Note:
passwd --stdinis not portable across Linux distributions.chpasswdis generally more portable for scripted password changes.
4.2 Ubuntu Linux
Ubuntu commonly uses chpasswd:
[root@ubuntu24 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
[root@ubuntu24 ~]# echo 'a181361:pass@1234' | chpasswd
Ubuntu commonly uses chpasswd:
[root@ubuntu24 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
[root@ubuntu24 ~]# echo 'a181361:pass@1234' | chpasswd4.3 SUSE Linux
SUSE Linux also commonly uses chpasswd:
[root@sles15 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
[root@sles15 ~]# echo 'a181361:pass@1234' | chpasswd
SUSE Linux also commonly uses chpasswd:
[root@sles15 ~]# useradd -u 9181361 -g 3100000 -G 3100004 -c "Vallabh Darole" -m a181361
[root@sles15 ~]# echo 'a181361:pass@1234' | chpasswdSecurity Note
The password shown above is only a lab example.
Avoid putting real passwords directly into:
Shell history
Scripts
Git repositories
Automation files
Documentation
In production, use a secure password-management or automation solution.
The password shown above is only a lab example.
Avoid putting real passwords directly into:
Shell history
Scripts
Git repositories
Automation files
Documentation
In production, use a secure password-management or automation solution.
5. Verify the User
After creating a user, always verify the account.
After creating a user, always verify the account.
Check User Information
[root@rocky8 ~]# id a181361
Example:
uid=9181361(a181361) gid=3100000 groups=3100000,3100004
[root@rocky8 ~]# id a181361Example:
uid=9181361(a181361) gid=3100000 groups=3100000,3100004Check Home Directory
[root@rocky8 ~]# ls -ld /home/a181361
[root@rocky8 ~]# ls -ld /home/a181361Check /etc/passwd
[root@rocky8 ~]# grep '^a181361:' /etc/passwd
[root@rocky8 ~]# grep '^a181361:' /etc/passwdCheck Login Shell
[root@rocky8 ~]# getent passwd a181361
[root@rocky8 ~]# getent passwd a1813616. Deleting a Linux User
Delete a user:
[root@rocky8 ~]# userdel user1
Delete a user and their home directory:
[root@rocky8 ~]# userdel -r user2
The -r option removes the user's home directory and associated local mail spool where applicable.
Delete a user:
[root@rocky8 ~]# userdel user1Delete a user and their home directory:
[root@rocky8 ~]# userdel -r user2The -r option removes the user's home directory and associated local mail spool where applicable.
Enterprise Tip
Before deleting an account, check whether the user owns:
Important files
Running processes
Scheduled jobs
Application files
Cron jobs
Other resources
Find files owned by a user:
[root@rocky8 ~]# find / -user user1 -ls 2>/dev/null
Before deleting an account, check whether the user owns:
Important files
Running processes
Scheduled jobs
Application files
Cron jobs
Other resources
Find files owned by a user:
[root@rocky8 ~]# find / -user user1 -ls 2>/dev/null7. Locking and Unlocking User Accounts
Administrators may need to temporarily disable an account.
Administrators may need to temporarily disable an account.
Lock the Account
[root@rocky8 ~]# passwd -l a181361
[root@rocky8 ~]# passwd -l a181361Check Account Status
[root@rocky8 ~]# passwd -S a181361
[root@rocky8 ~]# passwd -S a181361Unlock the Account
[root@rocky8 ~]# passwd -u a181361
This is useful when an employee is temporarily unavailable, an account is suspected of compromise, or access needs to be suspended during an investigation.
[root@rocky8 ~]# passwd -u a181361This is useful when an employee is temporarily unavailable, an account is suspected of compromise, or access needs to be suspended during an investigation.
8. Linux Group Management
Groups make it easier to manage permissions for multiple users.
For example, instead of assigning permissions individually to 20 users, an administrator can create one group and manage permissions for that group.
Groups make it easier to manage permissions for multiple users.
For example, instead of assigning permissions individually to 20 users, an administrator can create one group and manage permissions for that group.
Create a Group
[root@rocky8 ~]# groupadd developers
[root@rocky8 ~]# groupadd developersAdd a User to a Secondary Group
[root@rocky8 ~]# usermod -aG developers john
[root@rocky8 ~]# usermod -aG developers johnVerify Group Membership
[root@rocky8 ~]# id john
or:
[root@rocky8 ~]# groups john
Important: Always use -aG when adding a supplementary group. Without -a, existing supplementary group memberships can be replaced.
[root@rocky8 ~]# id johnor:
[root@rocky8 ~]# groups johnImportant: Always use
-aGwhen adding a supplementary group. Without-a, existing supplementary group memberships can be replaced.
9. Primary and Secondary Groups
Linux users can have:
One primary group
Zero or more supplementary/secondary groups
Check the user's groups:
[root@rocky8 ~]# id john
Example:
uid=1001(john) gid=1001(john) groups=1001(john),1005(developers),1006(devops)
Here:
Primary group : john
Secondary groups : developers, devops
Linux users can have:
One primary group
Zero or more supplementary/secondary groups
Check the user's groups:
[root@rocky8 ~]# id johnExample:
uid=1001(john) gid=1001(john) groups=1001(john),1005(developers),1006(devops)Here:
Primary group : john
Secondary groups : developers, devops10. Password Aging
Linux provides the chage command to manage password expiration.
Linux provides the chage command to manage password expiration.
Set Maximum Password Age
Set the maximum password age to 90 days:
[root@rocky8 ~]# chage -M 90 john
Set the maximum password age to 90 days:
[root@rocky8 ~]# chage -M 90 johnSet Password Expiration Warning
Warn the user 10 days before password expiration:
[root@rocky8 ~]# chage -W 10 john
Warn the user 10 days before password expiration:
[root@rocky8 ~]# chage -W 10 johnView Password Aging Information
[root@rocky8 ~]# chage -l john
Example:
Last password change : Sep 11, 2026
Password expires : Dec 10, 2026
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 90
Number of days of warning before password expires : 10
Password aging is commonly used to implement organizational password policies.
[root@rocky8 ~]# chage -l johnExample:
Last password change : Sep 11, 2026
Password expires : Dec 10, 2026
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 90
Number of days of warning before password expires : 10Password aging is commonly used to implement organizational password policies.
11. Understanding /etc/login.defs
The /etc/login.defs file contains default settings used by several user-management utilities.
View the file:
[root@rocky8 ~]# cat /etc/login.defs
Some important settings include:
PASS_MAX_DAYS
PASS_MIN_DAYS
PASS_WARN_AGE
UID_MIN
UID_MAX
GID_MIN
GID_MAX
CREATE_HOME
Example:
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_WARN_AGE 7
These settings can influence the default behavior when new users are created.
Note: Exact contents and defaults can vary between Linux distributions and versions. Modern systems may also use other mechanisms such as PAM configuration and authselect for authentication policies.
The /etc/login.defs file contains default settings used by several user-management utilities.
View the file:
[root@rocky8 ~]# cat /etc/login.defsSome important settings include:
PASS_MAX_DAYS
PASS_MIN_DAYS
PASS_WARN_AGE
UID_MIN
UID_MAX
GID_MIN
GID_MAX
CREATE_HOMEExample:
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_WARN_AGE 7These settings can influence the default behavior when new users are created.
Note: Exact contents and defaults can vary between Linux distributions and versions. Modern systems may also use other mechanisms such as PAM configuration and
authselectfor authentication policies.
12. Important Linux User and Group Files
Linux stores user and group information in several important files.
Linux stores user and group information in several important files.
/etc/passwd
Contains basic user-account information.
[root@rocky8 ~]# cat /etc/passwd
Example:
john:x:1001:1001:DevOps Engineer:/home/john:/bin/bash
The fields are:
Username
Password placeholder
UID
GID
Comment
Home directory
Login shell
Contains basic user-account information.
[root@rocky8 ~]# cat /etc/passwdExample:
john:x:1001:1001:DevOps Engineer:/home/john:/bin/bashThe fields are:
Username
Password placeholder
UID
GID
Comment
Home directory
Login shell/etc/shadow
Contains password hashes and password-aging information.
[root@rocky8 ~]# cat /etc/shadow
Access to this file should be restricted.
Contains password hashes and password-aging information.
[root@rocky8 ~]# cat /etc/shadowAccess to this file should be restricted.
/etc/group
Contains group information.
[root@rocky8 ~]# cat /etc/group
Contains group information.
[root@rocky8 ~]# cat /etc/group/etc/gshadow
Contains secure group information.
[root@rocky8 ~]# cat /etc/gshadow
Security Note: Do not modify these files manually unless you understand the consequences. Prefer commands such as useradd, usermod, groupadd, groupmod, and passwd.
Contains secure group information.
[root@rocky8 ~]# cat /etc/gshadowSecurity Note: Do not modify these files manually unless you understand the consequences. Prefer commands such as
useradd,usermod,groupadd,groupmod, andpasswd.
13. File Ownership and Permissions
Linux security is strongly based on file ownership and permissions.
Run:
[root@rocky8 ~]# ls -l
Example:
-rwxr-xr-- 1 alex developers 4096 May 10 12:30 deploy.sh
The permission string is:
-rwxr-xr--
It can be divided into:
- | rwx | r-x | r--
| | |
| | +---- Others
| +---------- Group
+---------------- Owner
Linux security is strongly based on file ownership and permissions.
Run:
[root@rocky8 ~]# ls -lExample:
-rwxr-xr-- 1 alex developers 4096 May 10 12:30 deploy.shThe permission string is:
-rwxr-xr--It can be divided into:
- | rwx | r-x | r--
| | |
| | +---- Others
| +---------- Group
+---------------- Owner14. Linux File Types
The first character represents the file type.
Character Meaning -Regular file dDirectory lSymbolic link cCharacter device bBlock device sSocket pNamed pipe
Example:
-rw-r--r-- Regular file
drwxr-xr-x Directory
lrwxrwxrwx Symbolic link
The first character represents the file type.
| Character | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
c | Character device |
b | Block device |
s | Socket |
p | Named pipe |
Example:
-rw-r--r-- Regular file
drwxr-xr-x Directory
lrwxrwxrwx Symbolic link15. Linux Permissions
There are three permission categories:
Owner
Group
Others
And three basic permissions:
r = read
w = write
x = execute
Example:
-rwxr-xr--
Means:
Owner : rwx
Group : r-x
Others : r--
There are three permission categories:
Owner
Group
OthersAnd three basic permissions:
r = read
w = write
x = executeExample:
-rwxr-xr--Means:
Owner : rwx
Group : r-x
Others : r--16. Numeric Permissions
Linux permissions can also be represented numerically.
Permission Value Read (r) 4 Write (w) 2 Execute (x) 1
Therefore:
7 = 4 + 2 + 1 = rwx
6 = 4 + 2 = rw-
5 = 4 + 1 = r-x
4 = 4 = r--
3 = 2 + 1 = -wx
2 = 2 = -w-
1 = 1 = --x
0 = 0 = ---
For example:
755 = rwxr-xr-x
Meaning:
Owner = 7 = rwx
Group = 5 = r-x
Others = 5 = r-x
Linux permissions can also be represented numerically.
| Permission | Value |
|---|---|
Read (r) | 4 |
Write (w) | 2 |
Execute (x) | 1 |
Therefore:
7 = 4 + 2 + 1 = rwx
6 = 4 + 2 = rw-
5 = 4 + 1 = r-x
4 = 4 = r--
3 = 2 + 1 = -wx
2 = 2 = -w-
1 = 1 = --x
0 = 0 = ---For example:
755 = rwxr-xr-xMeaning:
Owner = 7 = rwx
Group = 5 = r-x
Others = 5 = r-x17. Changing Permissions with chmod
Add Execute Permission
[root@rocky8 ~]# chmod +x script.sh
[root@rocky8 ~]# chmod +x script.shSet Permissions Numerically
[root@rocky8 ~]# chmod 755 script.sh
[root@rocky8 ~]# chmod 755 script.shRemove Write Permission from Others
[root@rocky8 ~]# chmod o-w sensitive_file.txt
[root@rocky8 ~]# chmod o-w sensitive_file.txt18. Symbolic chmod
Permissions can be modified using:
u = user/owner
g = group
o = others
a = all
Permissions can be modified using:
u = user/owner
g = group
o = others
a = allGive Owner Full Access
[root@rocky8 ~]# chmod u+rwx sensitive_file.txt
[root@rocky8 ~]# chmod u+rwx sensitive_file.txtGive Group Full Access
[root@rocky8 ~]# chmod g+rwx sensitive_file.txt
[root@rocky8 ~]# chmod g+rwx sensitive_file.txtGive Others Full Access
[root@rocky8 ~]# chmod o+rwx sensitive_file.txt
[root@rocky8 ~]# chmod o+rwx sensitive_file.txtRemove Owner Permissions
[root@rocky8 ~]# chmod u-rwx sensitive_file.txt
[root@rocky8 ~]# chmod u-rwx sensitive_file.txtRemove Group Permissions
[root@rocky8 ~]# chmod g-rwx sensitive_file.txt
[root@rocky8 ~]# chmod g-rwx sensitive_file.txtRemove Others' Permissions
[root@rocky8 ~]# chmod o-rwx sensitive_file.txt
Security Tip: Always grant the minimum permissions required.
[root@rocky8 ~]# chmod o-rwx sensitive_file.txtSecurity Tip: Always grant the minimum permissions required.
19. Common Permission Examples
Permission 644
rw-r--r--
Commonly used for regular files.
[root@rocky8 ~]# chmod 644 file.txt
The owner can read/write, while group and others can read.
rw-r--r--Commonly used for regular files.
[root@rocky8 ~]# chmod 644 file.txtThe owner can read/write, while group and others can read.
Permission 600
rw-------
Only the owner has read/write access.
[root@rocky8 ~]# chmod 600 private.txt
Useful for sensitive files such as private configuration files and SSH private keys.
rw-------Only the owner has read/write access.
[root@rocky8 ~]# chmod 600 private.txtUseful for sensitive files such as private configuration files and SSH private keys.
Permission 755
rwxr-xr-x
Commonly used for executable files, scripts, and directories where appropriate.
[root@rocky8 ~]# chmod 755 script.sh
rwxr-xr-xCommonly used for executable files, scripts, and directories where appropriate.
[root@rocky8 ~]# chmod 755 script.sh20. Change File Ownership with chown
Change the owner:
[root@rocky8 ~]# chown alex config.yaml
Change owner and group:
[root@rocky8 ~]# chown alex:developers config.yaml
Change directory ownership:
[root@rocky8 ~]# chown alex:developers app/
Change ownership recursively:
[root@rocky8 ~]# chown -R www-data:www-data /var/www/html
Caution: Always verify the target path before using chown -R. A recursive ownership change on the wrong directory can cause major application or system problems.
Change the owner:
[root@rocky8 ~]# chown alex config.yamlChange owner and group:
[root@rocky8 ~]# chown alex:developers config.yamlChange directory ownership:
[root@rocky8 ~]# chown alex:developers app/Change ownership recursively:
[root@rocky8 ~]# chown -R www-data:www-data /var/www/htmlCaution: Always verify the target path before using
chown -R. A recursive ownership change on the wrong directory can cause major application or system problems.
21. Security Rule – Avoid chmod 777
Avoid using:
[root@rocky8 ~]# chmod 777 file
as a quick fix for permission problems.
777 gives:
Owner : rwx
Group : rwx
Others : rwx
This can create a serious security risk.
Instead, identify:
Correct owner
Correct group
Required permissions
Whether ACLs are required
Whether SELinux is also involved
A better troubleshooting approach is:
[root@rocky8 ~]# ls -l file
[root@rocky8 ~]# namei -l /path/to/file
Avoid using:
[root@rocky8 ~]# chmod 777 fileas a quick fix for permission problems.
777 gives:
Owner : rwx
Group : rwx
Others : rwxThis can create a serious security risk.
Instead, identify:
Correct owner
Correct group
Required permissions
Whether ACLs are required
Whether SELinux is also involved
A better troubleshooting approach is:
[root@rocky8 ~]# ls -l file
[root@rocky8 ~]# namei -l /path/to/file22. Enterprise Access-Control Example
Let's create a simple enterprise scenario.
We have the following users:
webadmin1
webadmin2
dbadmin1
dbadmin2
help-desk
We will use:
web-grp
db-grp
Our requirement is:
User Requirement webadmin1Web application administration webadmin2Web application administration dbadmin1Database administration dbadmin2Database administration help-deskRead-only access to web and database data
Let's create a simple enterprise scenario.
We have the following users:
webadmin1
webadmin2
dbadmin1
dbadmin2
help-deskWe will use:
web-grp
db-grpOur requirement is:
| User | Requirement |
|---|---|
webadmin1 | Web application administration |
webadmin2 | Web application administration |
dbadmin1 | Database administration |
dbadmin2 | Database administration |
help-desk | Read-only access to web and database data |
23. Step 1 – Create Groups
[root@rocky8 ~]# groupadd web-grp
[root@rocky8 ~]# groupadd db-grp
Verify:
[root@rocky8 ~]# getent group web-grp
[root@rocky8 ~]# getent group db-grp
[root@rocky8 ~]# groupadd web-grp
[root@rocky8 ~]# groupadd db-grpVerify:
[root@rocky8 ~]# getent group web-grp
[root@rocky8 ~]# getent group db-grp24. Step 2 – Create Users
For a lab environment:
[root@rocky8 ~]# useradd -m webadmin1
[root@rocky8 ~]# useradd -m webadmin2
[root@rocky8 ~]# useradd -m dbadmin1
[root@rocky8 ~]# useradd -m dbadmin2
[root@rocky8 ~]# useradd -m help-desk
Set passwords interactively:
[root@rocky8 ~]# passwd webadmin1
[root@rocky8 ~]# passwd webadmin2
[root@rocky8 ~]# passwd dbadmin1
[root@rocky8 ~]# passwd dbadmin2
[root@rocky8 ~]# passwd help-desk
Using interactive passwd is preferable to exposing passwords directly in shell history.
For a lab environment:
[root@rocky8 ~]# useradd -m webadmin1
[root@rocky8 ~]# useradd -m webadmin2
[root@rocky8 ~]# useradd -m dbadmin1
[root@rocky8 ~]# useradd -m dbadmin2
[root@rocky8 ~]# useradd -m help-deskSet passwords interactively:
[root@rocky8 ~]# passwd webadmin1
[root@rocky8 ~]# passwd webadmin2
[root@rocky8 ~]# passwd dbadmin1
[root@rocky8 ~]# passwd dbadmin2
[root@rocky8 ~]# passwd help-deskUsing interactive passwd is preferable to exposing passwords directly in shell history.
25. Step 3 – Add Users to Groups
Add web administrators:
[root@rocky8 ~]# usermod -aG web-grp webadmin1
[root@rocky8 ~]# usermod -aG web-grp webadmin2
Add database administrators:
[root@rocky8 ~]# usermod -aG db-grp dbadmin1
[root@rocky8 ~]# usermod -aG db-grp dbadmin2
Verify:
[root@rocky8 ~]# id webadmin1
[root@rocky8 ~]# id dbadmin1
Add web administrators:
[root@rocky8 ~]# usermod -aG web-grp webadmin1
[root@rocky8 ~]# usermod -aG web-grp webadmin2Add database administrators:
[root@rocky8 ~]# usermod -aG db-grp dbadmin1
[root@rocky8 ~]# usermod -aG db-grp dbadmin2Verify:
[root@rocky8 ~]# id webadmin1
[root@rocky8 ~]# id dbadmin126. Step 4 – Create Application Directories
Create the web and database directories:
[root@rocky8 ~]# mkdir /web-data
[root@rocky8 ~]# mkdir /db-data
Create the web and database directories:
[root@rocky8 ~]# mkdir /web-data
[root@rocky8 ~]# mkdir /db-data27. Step 5 – Set Group Ownership
For web administrators:
[root@rocky8 ~]# chown root:web-grp /web-data
For database administrators:
[root@rocky8 ~]# chown root:db-grp /db-data
Verify:
[root@rocky8 ~]# ls -ld /web-data /db-data
For web administrators:
[root@rocky8 ~]# chown root:web-grp /web-dataFor database administrators:
[root@rocky8 ~]# chown root:db-grp /db-dataVerify:
[root@rocky8 ~]# ls -ld /web-data /db-data28. Step 6 – Set Directory Permissions
Give the owner and group full access:
[root@rocky8 ~]# chmod 770 /web-data
[root@rocky8 ~]# chmod 770 /db-data
The configuration becomes:
/web-data
Owner : root
Group : web-grp
Mode : 770
/db-data
Owner : root
Group : db-grp
Mode : 770
Therefore:
web-grp → Full access to /web-data
db-grp → Full access to /db-data
Others → No access
Give the owner and group full access:
[root@rocky8 ~]# chmod 770 /web-data
[root@rocky8 ~]# chmod 770 /db-dataThe configuration becomes:
/web-data
Owner : root
Group : web-grp
Mode : 770
/db-data
Owner : root
Group : db-grp
Mode : 770Therefore:
web-grp → Full access to /web-data
db-grp → Full access to /db-data
Others → No access29. Step 7 – Verify Access
Check directory permissions:
[root@rocky8 ~]# ls -ld /web-data
[root@rocky8 ~]# ls -ld /db-data
You can test access by switching users:
[root@rocky8 ~]# su - webadmin1
Then:
[webadmin1@rocky8 ~]$ cd /web-data
[webadmin1@rocky8 ~]$ touch test.txt
The user should be able to create files in /web-data.
The same concept applies to dbadmin1 and /db-data.
Check directory permissions:
[root@rocky8 ~]# ls -ld /web-data
[root@rocky8 ~]# ls -ld /db-dataYou can test access by switching users:
[root@rocky8 ~]# su - webadmin1Then:
[webadmin1@rocky8 ~]$ cd /web-data
[webadmin1@rocky8 ~]$ touch test.txtThe user should be able to create files in /web-data.
The same concept applies to dbadmin1 and /db-data.
30. Access Control Using ACL
Sometimes standard Linux permissions are not enough.
For example:
webadmin1 → Full access to /web-data
webadmin2 → Full access to /web-data
help-desk → Read/Traverse access to /web-data
The help-desk user is not a member of web-grp.
Instead of changing the primary group structure, we can use an ACL.
Sometimes standard Linux permissions are not enough.
For example:
webadmin1 → Full access to /web-data
webadmin2 → Full access to /web-data
help-desk → Read/Traverse access to /web-dataThe help-desk user is not a member of web-grp.
Instead of changing the primary group structure, we can use an ACL.
31. Check Existing ACL
[root@rocky8 ~]# getfacl /web-data
and:
[root@rocky8 ~]# getfacl /db-data
[root@rocky8 ~]# getfacl /web-dataand:
[root@rocky8 ~]# getfacl /db-data32. Give Help-Desk Access Using ACL
Give help-desk read and traverse permissions:
[root@rocky8 ~]# setfacl -m u:help-desk:rx /web-data
[root@rocky8 ~]# setfacl -m u:help-desk:rx /db-data
Verify:
[root@rocky8 ~]# getfacl /web-data
[root@rocky8 ~]# getfacl /db-data
Give help-desk read and traverse permissions:
[root@rocky8 ~]# setfacl -m u:help-desk:rx /web-data
[root@rocky8 ~]# setfacl -m u:help-desk:rx /db-dataVerify:
[root@rocky8 ~]# getfacl /web-data
[root@rocky8 ~]# getfacl /db-data33. Important ACL Concept
For a directory:
r = list directory contents
w = create/delete/rename directory entries
x = enter/traverse the directory
Therefore:
rx
allows a user to list and traverse the directory but does not allow them to modify directory entries.
However, directory ACLs alone do not automatically make every file inside readable.
If help-desk must actually read files inside /web-data, the files themselves must have appropriate read permissions/ACLs.
For newly created files and directories, default ACLs can be used.
Example:
[root@rocky8 ~]# setfacl -m u:help-desk:rx /web-data
[root@rocky8 ~]# setfacl -d -m u:help-desk:rx /web-data
For a directory:
r = list directory contents
w = create/delete/rename directory entries
x = enter/traverse the directoryTherefore:
rxallows a user to list and traverse the directory but does not allow them to modify directory entries.
However, directory ACLs alone do not automatically make every file inside readable.
If help-desk must actually read files inside /web-data, the files themselves must have appropriate read permissions/ACLs.
For newly created files and directories, default ACLs can be used.
Example:
[root@rocky8 ~]# setfacl -m u:help-desk:rx /web-data
[root@rocky8 ~]# setfacl -d -m u:help-desk:rx /web-data34. Expected Access
User /web-data/db-datawebadmin1Read/Write/Execute No access webadmin2Read/Write/Execute No access dbadmin1No access Read/Write/Execute dbadmin2No access Read/Write/Execute help-deskRead/Traverse Read/Traverse
This is a practical example of implementing role-based access control using:
Linux Users
↓
Linux Groups
↓
Ownership
↓
Permissions
↓
ACL
| User | /web-data | /db-data |
|---|---|---|
webadmin1 | Read/Write/Execute | No access |
webadmin2 | Read/Write/Execute | No access |
dbadmin1 | No access | Read/Write/Execute |
dbadmin2 | No access | Read/Write/Execute |
help-desk | Read/Traverse | Read/Traverse |
This is a practical example of implementing role-based access control using:
Linux Users
↓
Linux Groups
↓
Ownership
↓
Permissions
↓
ACL35. SUDO – Controlled Administrative Access
Instead of giving users the root password, Linux administrators can provide controlled administrative access using sudo.
Check sudo access:
[root@rocky8 ~]# sudo -l -U username
The main sudo configuration is:
[root@rocky8 ~]# cat /etc/sudoers
Important: Do not directly edit /etc/sudoers using a normal text editor.
Always use:
[root@rocky8 ~]# visudo
visudo checks the configuration syntax before saving it.
Instead of giving users the root password, Linux administrators can provide controlled administrative access using sudo.
Check sudo access:
[root@rocky8 ~]# sudo -l -U usernameThe main sudo configuration is:
[root@rocky8 ~]# cat /etc/sudoersImportant: Do not directly edit
/etc/sudoersusing a normal text editor.
Always use:
[root@rocky8 ~]# visudovisudo checks the configuration syntax before saving it.
36. SUDO Using Groups
Instead of granting administrative privileges to individual users, users can be placed into groups.
For example:
web-grp
db-grp
helpdesk-team
A sudo rule can be configured for a group.
Example:
%web-grp ALL=(ALL) /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpd
This allows members of web-grp to perform specific Apache service operations without automatically giving them unrestricted root access.
Best Practice: Restrict sudo permissions to the exact commands and arguments required by the user's job.
Instead of granting administrative privileges to individual users, users can be placed into groups.
For example:
web-grp
db-grp
helpdesk-teamA sudo rule can be configured for a group.
Example:
%web-grp ALL=(ALL) /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpdThis allows members of web-grp to perform specific Apache service operations without automatically giving them unrestricted root access.
Best Practice: Restrict sudo permissions to the exact commands and arguments required by the user's job.
37. Principle of Least Privilege
The Principle of Least Privilege means:
Give users only the permissions they actually need.
For example, if a web administrator only needs to restart Apache, avoid giving:
ALL=(ALL) ALL
when a more restrictive rule can be used.
A restricted rule is safer than unrestricted root access.
The Principle of Least Privilege means:
Give users only the permissions they actually need.
For example, if a web administrator only needs to restart Apache, avoid giving:
ALL=(ALL) ALLwhen a more restrictive rule can be used.
A restricted rule is safer than unrestricted root access.
38. SUDO Command Aliases
Command aliases can simplify sudo configuration.
Example:
Cmnd_Alias USERMGMT = /usr/sbin/useradd, /usr/sbin/usermod
Then:
%helpdesk-team ALL=(ALL) USERMGMT
This allows members of helpdesk-team to execute the specified user-management commands.
Always verify command paths first:
[root@rocky8 ~]# which useradd
[root@rocky8 ~]# which usermod
On some systems, command -v can also be used:
[root@rocky8 ~]# command -v useradd
[root@rocky8 ~]# command -v usermod
Command aliases can simplify sudo configuration.
Example:
Cmnd_Alias USERMGMT = /usr/sbin/useradd, /usr/sbin/usermodThen:
%helpdesk-team ALL=(ALL) USERMGMTThis allows members of helpdesk-team to execute the specified user-management commands.
Always verify command paths first:
[root@rocky8 ~]# which useradd
[root@rocky8 ~]# which usermodOn some systems, command -v can also be used:
[root@rocky8 ~]# command -v useradd
[root@rocky8 ~]# command -v usermod39. SSH Key-Based Authentication
SSH key-based authentication is commonly used for:
Linux administration
Ansible
Automation
Server-to-server communication
CI/CD pipelines
Instead of authenticating using a password, SSH uses a key pair:
Private Key → Stored securely on client
Public Key → Stored on destination server
SSH key-based authentication is commonly used for:
Linux administration
Ansible
Automation
Server-to-server communication
CI/CD pipelines
Instead of authenticating using a password, SSH uses a key pair:
Private Key → Stored securely on client
Public Key → Stored on destination server40. Generate an SSH Key
On the source server:
[root@rocky8 ~]# ssh-keygen
For a modern RSA key:
[root@rocky8 ~]# ssh-keygen -t ed25519
The key files are normally stored under:
~/.ssh/
For example:
id_ed25519
id_ed25519.pub
Security Rule: Never share the private key. The .pub file is the public key and can be copied to the destination server.
On the source server:
[root@rocky8 ~]# ssh-keygenFor a modern RSA key:
[root@rocky8 ~]# ssh-keygen -t ed25519The key files are normally stored under:
~/.ssh/For example:
id_ed25519
id_ed25519.pubSecurity Rule: Never share the private key. The
.pubfile is the public key and can be copied to the destination server.
41. Copy the Public Key to Another Server
Use:
[root@rocky8 ~]# ssh-copy-id user@server
For example:
[root@rocky8 ~]# ssh-copy-id ansadmin@ans01
The public key is added to:
~/.ssh/authorized_keys
on the destination server.
Use:
[root@rocky8 ~]# ssh-copy-id user@serverFor example:
[root@rocky8 ~]# ssh-copy-id ansadmin@ans01The public key is added to:
~/.ssh/authorized_keyson the destination server.
42. Test SSH Key Authentication
Connect to the destination:
[root@rocky8 ~]# ssh user@server
Example:
[root@rocky8 ~]# ssh ansadmin@ans01
If configured correctly, SSH can authenticate using the key instead of asking for the user's password.
This is particularly useful for Ansible automation.
Connect to the destination:
[root@rocky8 ~]# ssh user@serverExample:
[root@rocky8 ~]# ssh ansadmin@ans01If configured correctly, SSH can authenticate using the key instead of asking for the user's password.
This is particularly useful for Ansible automation.
43. SSH Troubleshooting
If key-based authentication does not work, check:
If key-based authentication does not work, check:
Check SSH Service
[root@rocky8 ~]# systemctl status sshd
[root@rocky8 ~]# systemctl status sshdCheck .ssh Directory
[root@rocky8 ~]# ls -ld ~/.ssh
[root@rocky8 ~]# ls -ld ~/.sshCheck authorized_keys
[root@rocky8 ~]# ls -l ~/.ssh/authorized_keys
Typical permissions are:
~/.ssh → 700
authorized_keys → 600
Fix permissions if required:
[root@rocky8 ~]# chmod 700 ~/.ssh
[root@rocky8 ~]# chmod 600 ~/.ssh/authorized_keys
Use verbose SSH output:
[root@rocky8 ~]# ssh -vvv user@server
This is one of the most useful commands for troubleshooting SSH authentication.
[root@rocky8 ~]# ls -l ~/.ssh/authorized_keysTypical permissions are:
~/.ssh → 700
authorized_keys → 600Fix permissions if required:
[root@rocky8 ~]# chmod 700 ~/.ssh
[root@rocky8 ~]# chmod 600 ~/.ssh/authorized_keysUse verbose SSH output:
[root@rocky8 ~]# ssh -vvv user@serverThis is one of the most useful commands for troubleshooting SSH authentication.
44. Troubleshooting Linux User Access
When troubleshooting Linux user-access problems, start with the user's identity and group membership.
When troubleshooting Linux user-access problems, start with the user's identity and group membership.
Check User Information
[root@rocky8 ~]# id username
[root@rocky8 ~]# id usernameCheck Account Status
[root@rocky8 ~]# passwd -S username
[root@rocky8 ~]# passwd -S usernameCheck Password Aging
[root@rocky8 ~]# chage -l username
[root@rocky8 ~]# chage -l usernameCheck SUDO Permissions
[root@rocky8 ~]# sudo -l -U username
[root@rocky8 ~]# sudo -l -U usernameCheck Recent Logins
[root@rocky8 ~]# last
[root@rocky8 ~]# lastCheck Failed Login Attempts
On systems using faillog:
[root@rocky8 ~]# faillog -u username
On systems using systemd:
[root@rocky8 ~]# journalctl -u sshd
On systems using faillog:
[root@rocky8 ~]# faillog -u usernameOn systems using systemd:
[root@rocky8 ~]# journalctl -u sshd45. Troubleshooting File Permission Problems
When a user cannot access a file or directory, check:
When a user cannot access a file or directory, check:
1. User identity
[root@rocky8 ~]# id username
[root@rocky8 ~]# id username2. File ownership
[root@rocky8 ~]# ls -l /path/to/file
[root@rocky8 ~]# ls -l /path/to/file3. Directory permissions
[root@rocky8 ~]# ls -ld /path/to/directory
[root@rocky8 ~]# ls -ld /path/to/directory4. Parent-directory permissions
[root@rocky8 ~]# namei -l /path/to/file
[root@rocky8 ~]# namei -l /path/to/file5. ACL
[root@rocky8 ~]# getfacl /path/to/file
[root@rocky8 ~]# getfacl /path/to/file6. SELinux
On RHEL/Rocky systems:
[root@rocky8 ~]# getenforce
Check the security context:
[root@rocky8 ~]# ls -Z /path/to/file
This is important because a Linux user may have correct Unix permissions but still be denied by SELinux.
On RHEL/Rocky systems:
[root@rocky8 ~]# getenforceCheck the security context:
[root@rocky8 ~]# ls -Z /path/to/fileThis is important because a Linux user may have correct Unix permissions but still be denied by SELinux.
46. Real-World Example – Unauthorized Access Attempt
Suppose a user attempts to execute an administrative command without having the required privileges.
The administrator can investigate systematically.
Suppose a user attempts to execute an administrative command without having the required privileges.
The administrator can investigate systematically.
Step 1 – Check User Identity
[root@rocky8 ~]# id username
[root@rocky8 ~]# id usernameStep 2 – Check Account Status
[root@rocky8 ~]# passwd -S username
[root@rocky8 ~]# passwd -S usernameStep 3 – Check SUDO Permissions
[root@rocky8 ~]# sudo -l -U username
[root@rocky8 ~]# sudo -l -U usernameStep 4 – Check Login Activity
[root@rocky8 ~]# last username
[root@rocky8 ~]# last usernameStep 5 – Check Authentication Logs
On RHEL/Rocky:
[root@rocky8 ~]# journalctl -u sshd
Depending on the system configuration, authentication information may also be available in:
/var/log/secure
The administrator can verify:
User identity
Group membership
Account status
Password expiration
Sudo permissions
Login activity
SSH authentication
File permissions
ACLs
SELinux context
Correct user and sudo configuration helps prevent unauthorized administrative access.
On RHEL/Rocky:
[root@rocky8 ~]# journalctl -u sshdDepending on the system configuration, authentication information may also be available in:
/var/log/secureThe administrator can verify:
User identity
Group membership
Account status
Password expiration
Sudo permissions
Login activity
SSH authentication
File permissions
ACLs
SELinux context
Correct user and sudo configuration helps prevent unauthorized administrative access.
No comments:
Post a Comment