Posted on
Administration

Creating a Bash Script for Managing User Accounts

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Creating a Bash Script for Managing User Accounts

Managing user accounts is a critical administrative task in Linux systems. Automating these tasks with Bash scripts can save time and reduce errors. In this guide, we will walk through creating a Bash script to handle common user account operations such as creating users, deleting users, and modifying user attributes.


Step 1: Writing a Basic User Management Script

Here’s a foundational Bash script to manage user accounts:

#!/bin/bash

# Variables
LOG_FILE="/path/to/user_management.log" # Log file for user management actions

# Function to create a user
create_user() {
  local USERNAME=$1
  if id "$USERNAME" &>/dev/null; then
    echo "[$(date)] ERROR: User $USERNAME already exists." >> "$LOG_FILE"
  else
    sudo useradd "$USERNAME"
    echo "[$(date)] INFO: User $USERNAME created successfully." >> "$LOG_FILE"
  fi
}

# Function to delete a user
delete_user() {
  local USERNAME=$1
  if id "$USERNAME" &>/dev/null; then
    sudo userdel "$USERNAME"
    echo "[$(date)] INFO: User $USERNAME deleted successfully." >> "$LOG_FILE"
  else
    echo "[$(date)] ERROR: User $USERNAME does not exist." >> "$LOG_FILE"
  fi
}

# Function to modify a user
modify_user() {
  local USERNAME=$1
  local OPTION=$2
  local VALUE=$3
  if id "$USERNAME" &>/dev/null; then
    sudo usermod "$OPTION" "$VALUE" "$USERNAME"
    echo "[$(date)] INFO: User $USERNAME modified with $OPTION $VALUE." >> "$LOG_FILE"
  else
    echo "[$(date)] ERROR: User $USERNAME does not exist." >> "$LOG_FILE"
  fi
}

# Main script
case $1 in
  create)
    create_user "$2"
    ;;
  delete)
    delete_user "$2"
    ;;
  modify)
    modify_user "$2" "$3" "$4"
    ;;
  *)
    echo "Usage: $0 {create|delete|modify} username [options]" >> "$LOG_FILE"
    ;;
esac

Step 2: Automating the Script with Cron

To automate user account tasks, schedule the script using cron. For instance, to create a backup user every day at midnight:

  1. Open the crontab editor:

    crontab -e
    
  2. Add an entry to schedule the task:

    0 0 * * * /path/to/user_management.sh create backup_user
    
  3. Save and exit. The task will now execute daily.


Step 3: Enhancements and Customizations

1. Password Management

Add functionality to set or reset passwords for users:

set_password() {
  local USERNAME=$1
  echo "Enter new password for $USERNAME:"
  sudo passwd "$USERNAME"
  echo "[$(date)] INFO: Password updated for $USERNAME." >> "$LOG_FILE"
}

2. Group Management

Include options to add or remove users from groups:

manage_group() {
  local USERNAME=$1
  local GROUPNAME=$2
  local ACTION=$3
  if id "$USERNAME" &>/dev/null && getent group "$GROUPNAME" &>/dev/null; then
    case $ACTION in
      add)
        sudo usermod -aG "$GROUPNAME" "$USERNAME"
        echo "[$(date)] INFO: User $USERNAME added to group $GROUPNAME." >> "$LOG_FILE"
        ;;
      remove)
        sudo gpasswd -d "$USERNAME" "$GROUPNAME"
        echo "[$(date)] INFO: User $USERNAME removed from group $GROUPNAME." >> "$LOG_FILE"
        ;;
      *)
        echo "[$(date)] ERROR: Invalid action $ACTION." >> "$LOG_FILE"
        ;;
    esac
  else
    echo "[$(date)] ERROR: User or group does not exist." >> "$LOG_FILE"
  fi
}

3. Bulk User Management

Allow the script to process a list of users from a file:

bulk_user_management() {
  local FILE=$1
  while IFS=, read -r ACTION USERNAME OPTION VALUE; do
    case $ACTION in
      create)
        create_user "$USERNAME"
        ;;
      delete)
        delete_user "$USERNAME"
        ;;
      modify)
        modify_user "$USERNAME" "$OPTION" "$VALUE"
        ;;
      *)
        echo "[$(date)] ERROR: Invalid action $ACTION in file." >> "$LOG_FILE"
        ;;
    esac
  done < "$FILE"
}

Conclusion

A Bash script for managing user accounts simplifies administrative tasks by automating repetitive actions. With enhancements like password management, group handling, and bulk processing, you can streamline your workflows while ensuring consistency. Start building your user management script today to save time and improve efficiency!