Created
October 31, 2024 20:26
-
-
Save filipnet/f0d381b3d9b41f1d2316cabb5abc2749 to your computer and use it in GitHub Desktop.
This Bash script scans the system's /etc/passwd file to list user accounts with their real names, associated groups, and home directories. It allows filtering based on home directory names and user groups. Additionally, it can modify the ownership and permissions of home directories recursively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Filter parameter for the home directory | |
| filter_string="home" | |
| # Optional filter parameter for the user group (default is empty) | |
| group_filter="users" | |
| # Default dry-run flag (true) | |
| dry_run=true | |
| # Function to display usage information | |
| usage() { | |
| echo "Usage: $0 [--dry-run] [--filter <string>] [--group <group>]" | |
| echo " --dry-run Show the commands that would be executed without making changes." | |
| echo " --filter Filter home directories containing the specified string." | |
| echo " --group Filter by user group." | |
| exit 1 | |
| } | |
| # Parse command-line arguments | |
| while [[ "$#" -gt 0 ]]; do | |
| case "$1" in | |
| --dry-run) | |
| dry_run=true | |
| shift | |
| ;; | |
| --filter) | |
| filter_string="$2" | |
| shift 2 | |
| ;; | |
| --group) | |
| group_filter="$2" | |
| shift 2 | |
| ;; | |
| *) | |
| usage | |
| ;; | |
| esac | |
| done | |
| # Function to query home directories | |
| get_home_directories() { | |
| local -n result_array=$1 # Reference to the passed array | |
| result_array=() # Reset the array | |
| while IFS=: read -r username _ uid gid realname home shell; do | |
| # Get the group name from the group ID | |
| group_name=$(getent group "$gid" | cut -d: -f1) | |
| # Check if the home directory exists and matches the filters | |
| if [[ -d "$home" && "$home" == *"$filter_string"* ]]; then | |
| if [[ -z "$group_filter" || "$group_name" == "$group_filter" ]]; then | |
| # Add the data as an array element (including group name) | |
| result_array+=("$username:$realname:$group_name:$home") | |
| fi | |
| fi | |
| done < /etc/passwd | |
| } | |
| # Function to print home directories | |
| print_home_directories() { | |
| local -n array=$1 # Reference to the passed array | |
| # Header for the table | |
| printf "%-20s %-35s %-15s %-50s\n" "Account Name" "Realname" "Group" "Home Directory" | |
| printf "%-20s %-35s %-15s %-50s\n" "-------------" "--------" "-----" "----------------" | |
| # Output the data | |
| for entry in "${array[@]}"; do | |
| IFS=':' read -r username realname group home <<< "$entry" | |
| printf "%-20s %-35s %-15s %-50s\n" "$username" "$realname" "$group" "$home" | |
| done | |
| } | |
| # Function to adjust permissions for user accounts | |
| adjust_permissions() { | |
| local -n array=$1 # Reference to the passed array | |
| for entry in "${array[@]}"; do | |
| IFS=':' read -r username _ group home <<< "$entry" | |
| # Change ownership recursively | |
| if $dry_run; then | |
| echo "Dry run: chown -R \"$username:$group\" \"$home\"" | |
| echo "Dry run: chmod -R 700 \"$home\"" | |
| else | |
| echo "chown -R \"$username:$group\" \"$home\"" | |
| chown -R "$username:$group" "$home" | |
| echo "chmod -R 700 \"$home\"" | |
| chmod -R 700 "$home" | |
| fi | |
| done | |
| } | |
| # Main program | |
| home_directories=() # Array for home directories | |
| get_home_directories home_directories # Query data | |
| print_home_directories home_directories # Output data | |
| # Call the adjust_permissions function to modify ownership and permissions | |
| adjust_permissions home_directories # Adjust permissions for the user accounts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment