USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Automating User and Permission Audits with Scripts

Lesson 24/31 | Study Time: 20 Min

Automated audits of user accounts and permissions are essential for maintaining secure Linux environments and ensuring compliance with security policies. Manual audits are time-consuming and prone to errors, but scripting these tasks enables consistent, repeatable, and timely reviews. 

Importance of Automated User and Permission Audits

Maintaining automated user and permission audits is essential for regulatory compliance and minimizing security risks. The following points highlight their role in identifying misconfigurations, unauthorized accounts, and unsafe file permissions.


1. Ensures compliance with security and regulatory requirements by regularly verifying system access controls.

2. Identifies inactive, unauthorized, or misconfigured user accounts proactively.

3. Detects improper file and directory permissions that could lead to privilege escalation or data leakage.

4. Facilitates swift security reviews and supports incident investigations.

Core Components of User and Permission Audits in Linux

Regular auditing of user accounts, groups, and file permissions is vital for compliance and reducing security risks. The following points outline the core areas to monitor and verify in Linux systems.


User Account Audits


1. Extract user account details from /etc/passwd and /etc/shadow.

2. Identify accounts:


With expired or disabled passwords.

Without login shells or home directories.

Belonging to privileged groups (e.g., root, sudo).


3. Detect inactive users by checking last login timestamps using lastlog command.


Group Membership Audits: Regular audits of group memberships involve listing all groups and their members from /etc/group to ensure users belong only to necessary groups. These audits help detect orphaned or unused groups and maintain proper access control across the system.


File and Directory Permission Audits: Critical system files and directories should be scanned for unexpected permission changes. Audits should identify world-writable files or directories that pose security risks and validate ownership and permissions of sensitive files such as /etc/passwd and /etc/shadow.

Example Audit Script Components


Extracting and Reporting User Info

bash
#!/bin/bash
echo "User Audit Report"
echo "-----------------"
awk -F: '{ print "User: "$1", UID: "$3", Home: "$6 }' /etc/passwd
lastlog -b 90 # Lists users who have not logged in for 90 days


Checking Group Memberships

bash
echo "Group Memberships"
echo "-----------------"
getent group | while IFS=: read group passwd gid members; do
echo "Group: $group - Members: $members"
done


Finding World-Writable Files

bash
echo "World Writable Files"
echo "---------------------"
find / -xdev -type f -perm -0002 -print 2>/dev/null


Andrew Foster

Andrew Foster

Product Designer
Profile

Class Sessions

1- Linux Security Model Overview 2- Kernel-Level Security Features (Namespaces, Capabilities, SELinux, AppArmor) 3- Linux File System Permissions and Extended Attributes (Xattr) 4- Secure User and Group Management Fundamentals 5- Best Practices for Sudo Configuration and Privilege Escalation Control 6- Disabling Unneeded Services and Configuring Secure Boot 7- Firewall Setup: Iptables/Nftables Basics and Advanced Rule Creation 8- Securing SSH: Key Management, Configuration, and Tunneling 9- Mandatory Access Control (SELinux/AppArmor Detailed Configuration) 10- Deployment of PAM for Enhanced Authentication 11- Linux Network Namespaces and Container Isolation Basics 12- TLS/SSL Configuration for Linux Services 13- VPN Setup for Secure Remote Access (OpenVPN, WireGuard) 14- Cryptographic Tools: GPG Encryption, Hashing Utilities, and Key Management 15- Intrusion Detection Systems and Log Monitoring Tools Overview 16- Linux Audit Framework (Auditd) Configuration and Log Analysis 17- Using Syslog, Journald, and Centralized Logging Solutions 18- File Integrity Monitoring with AIDE And Tripwire 19- Compliance Frameworks Introduction (PCI DSS, GDPR, HIPAA) 20- Incident Response Preparation and Forensic Readiness Basics 21- Bash Scripting Best Practices for Security and Automation 22- Conditional Logic, Loops, and Functions for Modular Scripts 23- Handling Errors, Signals, and Debugging Scripts Effectively 24- Automating User and Permission Audits with Scripts 25- Integrating Shell Scripts with System Tools (Cron Jobs, Systemd Timers) 26- Automating Log Analysis and Alerting Via Scripting 27- Writing Scripts for Automated Patch and Vulnerability Management 28- Automating Firewall and SSH Key Rotation Policies 29- Integrating Shell Scripts with Security Scanning Tools (Lynis, OpenVAS) 30- Case Studies on Automated Incident Detection and Response 31- Using Open-Source Tools for Orchestration with Scripting