RHCSA Administration Script

RHCSA Administration Script

Β·

6 min read

Overview This Bash script provides a robust set of tools for Linux system administration, designed to simplify complex management tasks.

Features

πŸ” User and Group Management

πŸš€ Service Control - 🌐 Network Configuration

πŸ“¦ Package Management

πŸ”₯ Firewall Administration

⏰ Job Scheduling

πŸ’½ Advanced LVM Management

πŸ“‚ Filesystem Management

πŸ—ƒοΈ RAID Configuration

πŸ›‘οΈSELinux Management

Prerequisites

Linux distribution (CentOS/RHEL recommended)

sudo privileges

systemd

firewalld

mdadm

LVM tools

#!/bin/bash

# Define colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Helper function to pause and return to menu
pause() {
    read -p "Press [Enter] key to continue..."
}

# Manage Users and Groups
manage_users_groups() {
    echo -e "${GREEN}Manage Users and Groups...${NC}"
    echo "1. Add User"
    echo "2. Delete User"
    echo "3. Set User Password"
    echo "4. Add Group"
    echo "5. Delete Group"
    echo "6. Assign User to Group"
    read -p "Select an option: " ug_option
    case $ug_option in
        1)
            read -p "Enter username: " username
            sudo useradd "$username"
            echo "User $username added."
            ;;
        2)
            read -p "Enter username to delete: " username
            sudo userdel "$username"
            echo "User $username deleted."
            ;;
        3)
            read -p "Enter username to set password: " username
            sudo passwd "$username"
            ;;
        4)
            read -p "Enter group name: " group
            sudo groupadd "$group"
            echo "Group $group added."
            ;;
        5)
            read -p "Enter group name to delete: " group
            sudo groupdel "$group"
            echo "Group $group deleted."
            ;;
        6)
            read -p "Enter username: " username
            read -p "Enter group name: " group
            sudo usermod -aG "$group" "$username"
            echo "User $username added to group $group."
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Manage Services
manage_services() {
    echo -e "${GREEN}Manage Services...${NC}"
    echo "1. Start Service"
    echo "2. Stop Service"
    echo "3. Restart Service"
    echo "4. Enable Service"
    echo "5. Disable Service"
    echo "6. Check Service Status"
    read -p "Select an option: " service_option
    read -p "Enter service name: " service
    case $service_option in
        1) sudo systemctl start "$service" ;;
        2) sudo systemctl stop "$service" ;;
        3) sudo systemctl restart "$service" ;;
        4) sudo systemctl enable "$service" ;;
        5) sudo systemctl disable "$service" ;;
        6) sudo systemctl status "$service" ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Manage Network
manage_network() {
    echo -e "${GREEN}Manage Network...${NC}"
    echo "1. View Network Configuration"
    echo "2. Restart Network"
    read -p "Select an option: " net_option
    case $net_option in
        1) ip addr show ;;
        2) sudo systemctl restart NetworkManager ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Manage Packages
manage_packages() {
    echo -e "${GREEN}Manage Packages...${NC}"
    echo "1. Install Package"
    echo "2. Remove Package"
    echo "3. Update System"
    read -p "Select an option: " pkg_option
    case $pkg_option in
        1)
            read -p "Enter package name to install: " pkg
            sudo yum install -y "$pkg"
            ;;
        2)
            read -p "Enter package name to remove: " pkg
            sudo yum remove -y "$pkg"
            ;;
        3) sudo yum update -y ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Manage Firewall
manage_firewall() {
    echo -e "${GREEN}Manage Firewall...${NC}"
    echo "1. Start Firewall"
    echo "2. Stop Firewall"
    echo "3. Enable Firewall"
    echo "4. Disable Firewall"
    echo "5. Add Firewall Rule"
    echo "6. Remove Firewall Rule"
    read -p "Select an option: " fw_option
    case $fw_option in
        1) sudo systemctl start firewalld ;;
        2) sudo systemctl stop firewalld ;;
        3) sudo systemctl enable firewalld ;;
        4) sudo systemctl disable firewalld ;;
        5)
            read -p "Enter rule (e.g., port/service): " rule
            sudo firewall-cmd --permanent --add-service="$rule"
            sudo firewall-cmd --reload
            ;;
        6)
            read -p "Enter rule to remove (e.g., port/service): " rule
            sudo firewall-cmd --permanent --remove-service="$rule"
            sudo firewall-cmd --reload
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Manage Jobs (cron, at)
manage_jobs() {
    echo -e "${GREEN}Manage Jobs...${NC}"
    echo "1. Schedule Cron Job"
    echo "2. Schedule At Job"
    read -p "Select an option: " job_option
    case $job_option in
        1)
            echo "Enter cron job in the format: * * * * * /path/to/script"
            read -p "Enter cron job: " cronjob
            (crontab -l 2>/dev/null; echo "$cronjob") | crontab -
            echo "Cron job scheduled."
            ;;
        2)
            echo "Enter the command to schedule (e.g., ls)"
            read -p "Enter command: " cmd
            read -p "Enter time (e.g., now + 1 minute): " time
            echo "$cmd" | at "$time"
            echo "At job scheduled."
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Advanced LVM Management
manage_lvm_advanced() {
    echo -e "${GREEN}Advanced LVM Management...${NC}"
    echo "1. Create Physical Volume (PV)"
    echo "2. Create Volume Group (VG)"
    echo "3. Create Logical Volume (LV)"
    echo "4. Extend Logical Volume"
    read -p "Select an option: " lvm_option
    case $lvm_option in
        1)
            read -p "Enter disk (e.g., /dev/sdb): " disk
            sudo pvcreate "$disk"
            echo "Physical Volume created on $disk."
            ;;
        2)
            read -p "Enter volume group name: " vg_name
            read -p "Enter physical volume (e.g., /dev/sdb): " pv
            sudo vgcreate "$vg_name" "$pv"
            echo "Volume Group $vg_name created."
            ;;
        3)
            read -p "Enter logical volume name: " lv_name
            read -p "Enter volume group: " vg
            read -p "Enter size (e.g., 10G): " size
            sudo lvcreate -L "$size" -n "$lv_name" "$vg"
            echo "Logical Volume $lv_name created."
            ;;
        4)
            read -p "Enter logical volume path (e.g., /dev/vg/lv): " lv_path
            read -p "Enter size to extend (e.g., +5G): " size
            sudo lvextend -L "$size" "$lv_path"
            sudo resize2fs "$lv_path"
            echo "Logical Volume extended."
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Advanced Filesystem Management
manage_filesystems_advanced() {
    echo -e "${GREEN}Advanced Filesystem Management...${NC}"
    echo "1. Create Filesystem"
    echo "2. Resize Filesystem"
    read -p "Select an option: " fs_option
    case $fs_option in
        1)
            read -p "Enter partition or volume (e.g., /dev/sdb1): " partition
            sudo mkfs.ext4 "$partition"
            echo "Filesystem created on $partition."
            ;;
        2)
            read -p "Enter filesystem path (e.g., /dev/vg/lv): " fs_path
            sudo resize2fs "$fs_path"
            echo "Filesystem resized."
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Advanced RAID Management
manage_raid_advanced() {
    echo -e "${GREEN}Advanced RAID Management...${NC}"
    echo "1. Create RAID Array"
    echo "2. Remove RAID Array"
    read -p "Select an option: " raid_option
    case $raid_option in
        1)
            read -p "Enter RAID level (e.g., 0, 1, 5): " level
            read -p "Enter devices (e.g., /dev/sd{b,c,d}): " devices
            sudo mdadm --create --verbose /dev/md0 --level="$level" --raid-devices=$(echo "$devices" | wc -w) $devices
            echo "RAID array created."
            ;;
        2)
            read -p "Enter RAID array (e.g., /dev/md0): " array
            sudo mdadm --stop "$array"
            sudo mdadm --remove "$array"
            echo "RAID array removed."
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Manage SELinux
manage_selinux() {
    echo -e "${GREEN}SELinux Management...${NC}"
    echo "1. View SELinux Status"
    echo "2. Change SELinux Mode"
    echo "3. Manage SELinux Contexts"
    read -p "Select an option: " selinux_option
    case $selinux_option in
        1) sudo sestatus ;;
        2)
            echo "Change SELinux Mode (enforcing, permissive, disabled)"
            read -p "Enter mode: " mode
            sudo setenforce "$mode"
            ;;
        3)
            echo "Manage SELinux Contexts"
            # Add SELinux context management logic
            ;;
        *)
            echo -e "${RED}Invalid option.${NC}"
            ;;
    esac
    pause
}

# Main Menu
while true; do
    echo -e "${GREEN}RHCSA Administration Script${NC}"
    echo "1. Manage Users and Groups"
    echo "2. Manage Services"
    echo "3. Manage Network"
    echo "4. Manage Packages"
    echo "5. Manage Firewall"
    echo "6. Manage Jobs"
    echo "7. Advanced LVM Management"
    echo "8. Advanced Filesystem Management"
    echo "9. Advanced RAID Management"
    echo "10. SELinux Management"
    echo "11. Exit"
    read -p "Select an option: " choice
    case $choice in
        1) manage_users_groups ;;
        2) manage_services ;;
        3) manage_network ;;
        4) manage_packages ;;
        5) manage_firewall ;;
        6) manage_jobs ;;
        7) manage_lvm_advanced ;;
        8) manage_filesystems_advanced ;;
        9) manage_raid_advanced ;;
        10) manage_selinux ;;
        11) exit 0 ;;
        *) echo -e "${RED}Invalid option selected.${NC}" ;;
    esac
done

Usage

vim admin_script.sh

chmod +x admin_script.sh

sudo ./admin_script.sh

Β