Add files via upload

This commit is contained in:
LINUXexpert.org
2025-05-17 10:06:49 -07:00
committed by GitHub
parent e7f89c23ca
commit b6cb2d31f7
13 changed files with 617 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
# Linux System Administration Scripts
Linux SysAdmin Bash Scripts Library
Automate routine tasks: This project provides a collection of Bash scripts to automate common Linux system administration duties. Automating daily sysadmin tasks improves efficiency and consistency by reducing manual repetition and the risk of human error. Each script is designed to be distribution-agnostic, using only standard base utilities (e.g. rsync, tar, awk, grep, netstat/ss, systemctl) available on most Linux systems. All scripts are released under the GNU GPL v3.0 license and include usage information in their headers.
## Included Scripts
- `backup.sh` Backup Utility: Archive directories into compressed tarballs for backups.
- `restore.sh` Restore Utility: Restore files from backup archives.
- `disk_cleanup.sh` Disk Usage & Cleanup: Report disk usage and identify large files; optionally clean package caches and temporary files to free space.
- `log_inspect.sh` Log Inspection: Search within log files or tail the latest system logs for troubleshooting.
- `log_rotate.sh` Log Rotation: Compress and rotate old log files to prevent excessive disk usage
- `network_info.sh` Network & Firewall Info: Show network interface details, routing table, open listening ports, and basic firewall (iptables) rules.
- `process_monitor.sh` Process Management: List top resource-consuming processes and allow termination of processes by name or PID.
- `security_audit.sh` Security Audit: Scan for security issues like world-writable files, SUID/SGID executables, and open network ports.
- `service_manager.sh` Service Management: Start, stop, restart, or check status of system services, and enable/disable services at boot.
- `sys_monitor.sh` System Monitoring: Display system uptime, resource utilization (CPU, memory, disk), and top processes.
- `update_system.sh` System Updates: Apply available package updates and patches (works with apt, yum/dnf, zypper, pacman).
- `user_manage.sh` User and Group Management: Create or remove user accounts and groups, modify user group memberships, and lock/unlock accounts.
+43
View File
@@ -0,0 +1,43 @@
#!/bin/bash
# backup.sh - Backup directory to compressed tar archive
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: backup.sh <source_directory> <destination_directory>
# Description: Creates a tar.gz archive of the source directory in the destination.
SRC="$1"
DEST="$2"
if [ -z "$SRC" ] || [ -z "$DEST" ]; then
echo "Usage: $0 <source_directory> <destination_directory>"
exit 1
fi
if [ ! -d "$SRC" ]; then
echo "Source directory '$SRC' not found!"; exit 1
fi
if [ ! -d "$DEST" ]; then
# create destination dir if it doesn't exist
mkdir -p "$DEST" || { echo "Failed to create destination '$DEST'"; exit 1; }
fi
base_name="$(basename "$SRC")"
date_str="$(date +%Y%m%d)"
archive_name="${base_name}-backup-${date_str}.tar.gz"
tar -czf "$DEST/$archive_name" -C "$(dirname "$SRC")" "$base_name"
if [ $? -eq 0 ]; then
echo "Backup successful: $DEST/$archive_name"
else
echo "Backup failed for $SRC"
fi
+55
View File
@@ -0,0 +1,55 @@
#!/bin/bash
# disk_cleanup.sh - Show disk usage and optionally clean temporary files
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: disk_cleanup.sh [--clean]
# Description: Without --clean, displays disk usage and largest files. With --clean, performs cleanup of caches and /tmp.
if [ "$1" != "--clean" ]; then
echo "==== Disk Usage Overview ===="
df -h -x tmpfs -x devtmpfs
echo -e "\n==== Top 10 Largest Files ===="
# List top 10 largest files (size in MB)
find / -type f -printf '%s %p\n' 2>/dev/null | sort -nr | head -n 10 | awk '{size=$1/1024/1024; printf("%.1f MB - ", size); $1=""; print $0;}'
echo -e "\n(To actually free space, run: $0 --clean)"
exit 0
fi
# If --clean is specified:
echo "Cleaning package caches and temporary files..."
# Package manager cache cleanup
if [ $EUID -ne 0 ]; then
echo "Run as root to perform cleanup."; exit 1
fi
if command -v apt-get &> /dev/null; then
apt-get clean
elif command -v dnf &> /dev/null; then
dnf clean all
elif command -v yum &> /dev/null; then
yum clean all
elif command -v pacman &> /dev/null; then
pacman -Scc --noconfirm
fi
# Clean /tmp and /var/tmp files older than 7 days
find /tmp -type f -mtime +7 -exec rm -f {} \;
find /tmp -type d -empty -mtime +7 -exec rmdir {} \;
find /var/tmp -type f -mtime +7 -exec rm -f {} \;
echo "Temporary files older than 7 days removed from /tmp and /var/tmp."
echo "Disk cleanup completed."
+51
View File
@@ -0,0 +1,51 @@
#!/bin/bash
# log_inspect.sh - Search or tail system log files
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: log_inspect.sh [search <pattern> | tail <logfile>]
# Description: Searches across /var/log for a pattern, or tails a specific log file.
if [ "$1" = "search" ]; then
pattern="$2"
if [ -z "$pattern" ]; then
echo "Usage: $0 search <pattern>"; exit 1
fi
echo "Searching for '$pattern' in /var/log..."
grep -R -i --color=auto "$pattern" /var/log 2>/dev/null
exit 0
elif [ "$1" = "tail" ]; then
logfile="$2"
if [ -z "$logfile" ]; then
echo "Usage: $0 tail <log_file_path>"; exit 1
fi
if [ ! -f "$logfile" ]; then
echo "Log file '$logfile' not found."; exit 1
fi
echo "== Last 100 lines of $logfile =="
tail -n 100 "$logfile"
exit 0
else
# Default: tail the main system log (syslog or messages)
if [ -f /var/log/syslog ]; then
echo "== Last 50 lines of /var/log/syslog =="
tail -n 50 /var/log/syslog
elif [ -f /var/log/messages ]; then
echo "== Last 50 lines of /var/log/messages =="
tail -n 50 /var/log/messages
else
echo "No syslog or messages log found."
fi
fi
+38
View File
@@ -0,0 +1,38 @@
#!/bin/bash
# log_rotate.sh - Compress and remove old log files
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: log_rotate.sh [days]
# days: rotate logs older than this many days (default 7).
# Description: Compresses .log files older than X days in /var/log and deletes archives older than 90 days.
DAYS="$1"
if ! [[ "$DAYS" =~ ^[0-9]+$ ]]; then
DAYS=7
fi
if [ $EUID -ne 0 ]; then
echo "Please run as root to rotate system logs."
exit 1
fi
echo "Rotating logs older than $DAYS days..."
# Compress uncompressed .log files older than $DAYS days
find /var/log -type f -name "*.log" -mtime +$DAYS ! -name "*.gz" -exec gzip {} \;
echo "Compressed logs older than $DAYS days."
# Remove very old compressed logs (older than 90 days)
find /var/log -type f -name "*.gz" -mtime +90 -exec rm -f {} \;
echo "Removed log archives older than 90 days."
+53
View File
@@ -0,0 +1,53 @@
#!/bin/bash
# network_info.sh - Show network interfaces, routes, open ports, firewall rules
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: network_info.sh (no arguments)
# Description: Displays network interface addresses, routing table, open ports, and iptables rules.
echo "==== Network Interfaces (IP addresses) ===="
if command -v ip &> /dev/null; then
ip -brief addr show # brief output of interfaces and addresses
elif command -v ifconfig &> /dev/null; then
ifconfig -a
else
echo "No network interface tool (ip or ifconfig) available."
fi
echo -e "\n==== Routing Table ===="
if command -v ip &> /dev/null; then
ip route show
elif command -v route &> /dev/null; then
route -n
else
echo "No routing tool (ip or route) available."
fi
echo -e "\n==== Listening Ports (TCP/UDP) ===="
if command -v ss &> /dev/null; then
ss -tulwn # show TCP/UDP ports in listen state with numeric addresses
elif command -v netstat &> /dev/null; then
netstat -tuln
else
echo "No socket listing tool (ss or netstat) available."
fi
echo -e "\n==== Firewall Rules (iptables) ===="
if command -v iptables &> /dev/null; then
iptables -L -n -v # list firewall rules with numeric addresses and packet counts
else
echo "iptables command not found (no firewall rules to show or using nftables)."
fi
+40
View File
@@ -0,0 +1,40 @@
#!/bin/bash
# process_monitor.sh - Show top processes and allow killing by name or PID
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: process_monitor.sh [kill <process_name|PID>]
# Description: Without args, shows top CPU & memory processes. With "kill", terminates process by name or PID.
if [ "$1" = "kill" ]; then
target="$2"
if [ -z "$target" ]; then
echo "Usage: $0 kill <process_name|PID>"; exit 1
fi
# If target is numeric (PID), kill that PID, else kill by name
if [[ "$target" =~ ^[0-9]+$ ]]; then
kill "$target" && echo "Process $target killed." || echo "Failed to kill process $target."
else
# Use pkill to kill by name (match full process name)
pkill -x "$target" && echo "Processes named '$target' killed." || echo "No process '$target' found or kill failed."
fi
exit 0
fi
echo "==== Top 5 CPU-consuming processes ===="
ps -eo pid,user,comm,%cpu --sort=-%cpu | head -n 6
echo -e "\n==== Top 5 Memory-consuming processes ===="
ps -eo pid,user,comm,%mem --sort=-%mem | head -n 6
+45
View File
@@ -0,0 +1,45 @@
#!/bin/bash
# restore.sh - Restore files from a backup archive
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: restore.sh <backup_archive.tar.gz> [target_directory]
# Description: Extracts the tar.gz archive into the target directory (current dir if not specified).
ARCHIVE="$1"
TARGET="$2"
if [ -z "$ARCHIVE" ]; then
echo "Usage: $0 <archive.tar.gz> [target_directory]"
exit 1
fi
if [ ! -f "$ARCHIVE" ]; then
echo "Backup archive '$ARCHIVE' not found!"; exit 1
fi
if [ -z "$TARGET" ]; then
TARGET="."
else
if [ ! -d "$TARGET" ]; then
mkdir -p "$TARGET" || { echo "Failed to create target directory '$TARGET'"; exit 1; }
fi
fi
tar -xzf "$ARCHIVE" -C "$TARGET"
status=$?
if [ $status -eq 0 ]; then
echo "Restore successful to directory: $TARGET"
else
echo "Restore failed with error code $status"
fi
+41
View File
@@ -0,0 +1,41 @@
#!/bin/bash
# security_audit.sh - Check for common security issues (permissions, open ports)
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: security_audit.sh (no arguments)
# Description: Lists world-writable files/dirs, SUID/SGID files, and listening ports.
# World-writable files (perm bits: others have write)
echo "==== World-Writable Files (potentially unsafe) ===="
find / -xdev -type f -perm -0002 -printf '%M %u %g %p\n' 2>/dev/null
# World-writable directories without sticky bit
echo -e "\n==== World-Writable Directories (no sticky bit) ===="
find / -xdev -type d -perm -0002 ! -perm -1000 -printf '%M %u %g %p\n' 2>/dev/null
# SUID/SGID files (files with setuid or setgid bits)
echo -e "\n==== SUID/SGID Files ===="
find / -xdev \( -perm -4000 -o -perm -2000 \) -printf '%M %u %g %p\n' 2>/dev/null
# Open listening ports
echo -e "\n==== Listening Network Ports ===="
if command -v ss &> /dev/null; then
ss -tulwn
elif command -v netstat &> /dev/null; then
netstat -tuln
else
echo "No command available to list network ports."
fi
+71
View File
@@ -0,0 +1,71 @@
#!/bin/bash
# service_manager.sh - Start/stop/restart and manage system services
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: service_manager.sh <action> <service_name>
# Actions: start, stop, restart, status, enable, disable, list
# Description: Uses systemctl or service to control services.
action="$1"
service="$2"
if [ "$action" = "list" ]; then
# List running services
if command -v systemctl &> /dev/null; then
systemctl list-units --type=service --state=running
elif command -v service &> /dev/null; then
service --status-all 2>&1 | grep '+' # shows running services with [+]
else
echo "No service management tool available."
fi
exit 0
fi
if [ -z "$action" ] || [ -z "$service" ]; then
echo "Usage: $0 {start|stop|restart|status|enable|disable|list} <service_name>"
exit 1
fi
if command -v systemctl &> /dev/null; then
case "$action" in
start|stop|restart|status)
systemctl $action "$service"
;;
enable|disable)
systemctl $action "$service"
;;
*)
echo "Invalid action. Use start, stop, restart, status, enable, disable, or list."
exit 1
;;
esac
elif command -v service &> /dev/null; then
case "$action" in
start|stop|restart)
service "$service" $action
;;
status)
service "$service" status
;;
*)
echo "Action '$action' not supported with legacy service command."
exit 1
;;
esac
else
echo "No known service manager found (systemctl/service)."
exit 1
fi
+36
View File
@@ -0,0 +1,36 @@
#!/bin/bash
# sys_monitor.sh - System resource monitoring script
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: sys_monitor.sh (no arguments)
# Description: Prints system uptime, memory, disk usage, and top CPU/mem processes.
echo "==== System Uptime and Load ===="
uptime
echo -e "\n==== Memory Usage ===="
free -h
echo -e "\n==== Disk Usage ===="
# Exclude temporary filesystems (tmpfs) for clarity
df -h -x tmpfs -x devtmpfs
echo -e "\n==== Top 5 Processes by CPU Usage ===="
# Display header and top 5 CPU-consuming processes
ps -eo pid,user,comm,%cpu --sort=-%cpu | head -n 6
echo -e "\n==== Top 5 Processes by Memory Usage ===="
ps -eo pid,user,comm,%mem --sort=-%mem | head -n 6
+48
View File
@@ -0,0 +1,48 @@
#!/bin/bash
# update_system.sh - Apply system package updates
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: update_system.sh (no arguments, run as root)
# Description: Detects the Linux distro's package manager and installs all updates.
# Ensure running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root to apply system updates."
exit 1
fi
if command -v apt-get &> /dev/null; then
echo "Updating with apt-get..."
apt-get update && apt-get upgrade -y
elif command -v apt &> /dev/null; then
echo "Updating with apt..."
apt update && apt upgrade -y
elif command -v dnf &> /dev/null; then
echo "Updating with dnf..."
dnf upgrade -y
elif command -v yum &> /dev/null; then
echo "Updating with yum..."
yum update -y
elif command -v zypper &> /dev/null; then
echo "Updating with zypper..."
zypper refresh && zypper update -y
elif command -v pacman &> /dev/null; then
echo "Updating with pacman..."
pacman -Syuu --noconfirm
else
echo "Error: No supported package manager found on this system."
exit 1
fi
+76
View File
@@ -0,0 +1,76 @@
#!/bin/bash
# user_manage.sh - User and Group Management Script
#
# Copyright (C) 2025 LINUXexpert.org
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Usage: user_manage.sh <subcommand> [arguments...]
# Subcommands: adduser <user>, deluser <user>, addgroup <group>, delgroup <group>,
# addtogroup <user> <group>, lock <user>, unlock <user>,
# listusers, listgroups
# Description: Automates user/group creation, deletion, and modifications.
# Requires root privileges for most operations.
subcmd="$1"
case "$subcmd" in
adduser)
user="$2"
if [ -z "$user" ]; then echo "Username required. Usage: $0 adduser <username>"; exit 1; fi
# Create user with a home directory (-m) and default settings
useradd -m "$user" && echo "User '$user' created." || echo "Failed to create user."
;;
deluser)
user="$2"
if [ -z "$user" ]; then echo "Username required. Usage: $0 deluser <username>"; exit 1; fi
# Delete user and remove home directory (-r)
userdel -r "$user" && echo "User '$user' deleted." || echo "Failed to delete user."
;;
addgroup)
group="$2"
if [ -z "$group" ]; then echo "Group name required. Usage: $0 addgroup <group>"; exit 1; fi
groupadd "$group" && echo "Group '$group' created." || echo "Failed to create group."
;;
delgroup)
group="$2"
if [ -z "$group" ]; then echo "Group name required. Usage: $0 delgroup <group>"; exit 1; fi
groupdel "$group" && echo "Group '$group' deleted." || echo "Failed to delete group."
;;
addtogroup)
user="$2"; group="$3"
if [ -z "$user" ] || [ -z "$group" ]; then
echo "Usage: $0 addtogroup <user> <group>"; exit 1;
fi
usermod -aG "$group" "$user" && echo "Added user '$user' to group '$group'." || echo "Failed to modify group membership."
;;
lock)
user="$2"
if [ -z "$user" ]; then echo "Username required. Usage: $0 lock <username>"; exit 1; fi
usermod -L "$user" && echo "User '$user' account locked." || echo "Failed to lock account."
;;
unlock)
user="$2"
if [ -z "$user" ]; then echo "Username required. Usage: $0 unlock <username>"; exit 1; fi
usermod -U "$user" && echo "User '$user' account unlocked." || echo "Failed to unlock account."
;;
listusers)
cut -d: -f1 /etc/passwd
;;
listgroups)
cut -d: -f1 /etc/group
;;
*)
echo "Usage: $0 {adduser|deluser|addgroup|delgroup|addtogroup|lock|unlock|listusers|listgroups}"
exit 1
;;
esac