InitMate/InitMate.sh

215 lines
6.7 KiB
Bash
Raw Normal View History

2024-05-24 08:11:13 -06:00
#!/bin/bash
2024-05-24 08:29:43 -06:00
LOG_FILE="$(dirname "$0")/setup.log"
# Log function
log() {
echo "$1" | tee -a "$LOG_FILE"
}
2024-05-24 08:11:13 -06:00
# Check if the script is run as root
if [ "$EUID" -ne 0 ]; then
2024-05-24 08:29:43 -06:00
log "Please run as root"
2024-05-24 08:11:13 -06:00
exit 1
fi
# Function to detect the OS and package manager
detect_os() {
if [[ -f /etc/os-release ]]; then
2024-05-24 08:29:43 -06:00
# shellcheck source=/dev/null
2024-05-24 08:11:13 -06:00
source /etc/os-release
OS=$ID
else
2024-05-24 08:29:43 -06:00
log "Unable to detect the operating system. Please enter it manually (e.g., ubuntu, debian, centos):"
2024-05-24 08:11:13 -06:00
read -r OS
fi
case $OS in
ubuntu|debian)
PKG_MANAGER="apt"
;;
centos|fedora)
PKG_MANAGER="yum"
;;
*)
2024-05-24 08:29:43 -06:00
log "Unknown operating system. Please enter the package manager (e.g., apt, yum):"
2024-05-24 08:11:13 -06:00
read -r PKG_MANAGER
2024-05-24 08:29:43 -06:00
log "Please enter the command to update the package list (e.g., 'apt update && apt upgrade' or 'yum update'):"
read -r UPDATE_CMD
log "Please enter the command to install a package (e.g., 'apt install -y {package_name}' or 'yum install -y {package_name}'):"
log "Do not include the {package_name} part, just the 'apt install -y' part"
read -r INSTALL_CMD
2024-05-24 08:11:13 -06:00
;;
esac
}
# Function to secure SSH
secure_ssh() {
2024-05-24 08:29:43 -06:00
log "Securing SSH..."
2024-05-24 08:11:13 -06:00
SSH_PORT=$((RANDOM % 64512 + 1024))
2024-05-24 08:29:43 -06:00
log "New SSH port: $SSH_PORT"
2024-05-24 08:11:13 -06:00
sed -i "s/#Port 22/Port $SSH_PORT/" /etc/ssh/sshd_config
sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config
sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config
sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/" /etc/ssh/sshd_config
2024-05-24 08:29:43 -06:00
log "Please enter your public key:"
2024-05-24 08:11:13 -06:00
read -r PUBLIC_KEY
mkdir -p ~/.ssh
echo "$PUBLIC_KEY" > ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
systemctl restart sshd
}
# Function to setup firewall
setup_firewall() {
if command -v ufw &>/dev/null; then
2024-05-24 08:29:43 -06:00
log "UFW is already installed. Opening SSH port $SSH_PORT."
2024-05-24 08:11:13 -06:00
ufw allow "$SSH_PORT"/tcp
elif command -v firewall-cmd &>/dev/null; then
2024-05-24 08:29:43 -06:00
log "Firewalld is already installed. Opening SSH port $SSH_PORT."
2024-05-24 08:11:13 -06:00
firewall-cmd --permanent --add-port="$SSH_PORT"/tcp
firewall-cmd --reload
else
2024-05-24 08:29:43 -06:00
log "No firewall detected. Do you want to install UFW? (yes/no)"
2024-05-24 08:11:13 -06:00
read -r INSTALL_UFW
if [[ $INSTALL_UFW == "yes" ]]; then
if [[ $PKG_MANAGER == "apt" ]]; then
apt update && apt install -y ufw
elif [[ $PKG_MANAGER == "yum" ]]; then
yum install -y ufw
2024-05-24 08:29:43 -06:00
else
$UPDATE_CMD && $INSTALL_CMD ufw
2024-05-24 08:11:13 -06:00
fi
ufw allow "$SSH_PORT"/tcp
ufw enable
fi
fi
2024-05-24 08:29:43 -06:00
log "Do you want to open additional ports? (yes/no)"
2024-05-24 08:11:13 -06:00
read -r OPEN_PORTS
if [[ $OPEN_PORTS == "yes" ]]; then
2024-05-24 08:29:43 -06:00
log "Please enter the ports and protocols to open (comma-separated, e.g., 80/tcp,443/tcp,8080/udp):"
2024-05-24 08:11:13 -06:00
read -r ADDITIONAL_PORTS
IFS=',' read -r -a PORT_ARRAY <<< "$ADDITIONAL_PORTS"
2024-05-24 08:29:43 -06:00
for PORT_PROTOCOL in "${PORT_ARRAY[@]}"; do
IFS='/' read -r PORT PROTOCOL <<< "$PORT_PROTOCOL"
2024-05-24 08:11:13 -06:00
if command -v ufw &>/dev/null; then
2024-05-24 08:29:43 -06:00
ufw allow "$PORT/$PROTOCOL"
2024-05-24 08:11:13 -06:00
elif command -v firewall-cmd &>/dev/null; then
2024-05-24 08:29:43 -06:00
firewall-cmd --permanent --add-port="$PORT/$PROTOCOL"
2024-05-24 08:11:13 -06:00
fi
done
if command -v firewall-cmd &>/dev/null; then
firewall-cmd --reload
fi
fi
}
# Function to perform system update
system_update() {
if [[ $PKG_MANAGER == "apt" ]]; then
apt update && apt upgrade -y
elif [[ $PKG_MANAGER == "yum" ]]; then
yum update -y
2024-05-24 08:29:43 -06:00
else
$UPDATE_CMD && $INSTALL_CMD upgrade -y
fi
}
# Function to run additional setup
run_additional_setup() {
log "Do you want to run additional setup scripts? (yes/no)"
read -r RUN_ADDITIONAL_SETUP
if [[ $RUN_ADDITIONAL_SETUP == "yes" ]]; then
SCRIPT_DIR=$(dirname "$0")
if [[ -f "$SCRIPT_DIR/extender.sh" ]]; then
bash "$SCRIPT_DIR/extender.sh" | tee -a "$LOG_FILE"
else
log "No extender.sh script found in the script directory."
fi
2024-05-24 08:11:13 -06:00
fi
}
2024-05-24 08:34:29 -06:00
# Function to add additional users
add_users() {
log "Do you want to add additional users? (yes/no)"
read -r ADD_USERS
while [[ $ADD_USERS == "yes" ]]; do
log "Enter the username:"
read -r USERNAME
log "Enter the password:"
read -r -s PASSWORD
useradd -m "$USERNAME"
echo "$USERNAME:$PASSWORD" | chpasswd
log "Do you want to grant sudo access to $USERNAME? (yes/no)"
read -r GRANT_SUDO
if [[ $GRANT_SUDO == "yes" ]]; then
usermod -aG sudo "$USERNAME"
log "$USERNAME has been granted sudo access."
fi
log "Do you want to add a public key for $USERNAME? (yes/no)"
read -r ADD_PUBLIC_KEY
if [[ $ADD_PUBLIC_KEY == "yes" ]]; then
log "Please enter the public key:"
read -r PUBLIC_KEY
su - "$USERNAME" -c "mkdir -p ~/.ssh && echo '$PUBLIC_KEY' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh"
log "Public key added for $USERNAME."
fi
log "Do you want to add another user? (yes/no)"
read -r ADD_USERS
done
}
2024-05-24 08:11:13 -06:00
# Main script execution
detect_os
2024-05-24 08:29:43 -06:00
log "Detected OS: $OS, Package Manager: $PKG_MANAGER"
log "Is this correct? (yes/no)"
2024-05-24 08:11:13 -06:00
read -r CONFIRM
if [[ $CONFIRM != "yes" ]]; then
2024-05-24 08:29:43 -06:00
log "Please enter the correct OS and package manager:"
2024-05-24 08:11:13 -06:00
read -r OS PKG_MANAGER
2024-05-24 08:29:43 -06:00
if [[ $PKG_MANAGER != "apt" && $PKG_MANAGER != "yum" ]]; then
log "Please enter the command to update the package list (e.g., 'apt update' or 'yum update'):"
read -r UPDATE_CMD
log "Please enter the command to install a package (e.g., 'apt install -y' or 'yum install -y'):"
read -r INSTALL_CMD
fi
2024-05-24 08:11:13 -06:00
fi
2024-05-24 08:29:43 -06:00
log "Do you want to secure SSH? (yes/no)"
2024-05-24 08:11:13 -06:00
read -r SECURE_SSH
if [[ $SECURE_SSH == "yes" ]]; then
secure_ssh
fi
2024-05-24 08:29:43 -06:00
log "Do you want to set up a firewall? (yes/no)"
2024-05-24 08:11:13 -06:00
read -r SETUP_FIREWALL
if [[ $SETUP_FIREWALL == "yes" ]]; then
setup_firewall
fi
2024-05-24 08:29:43 -06:00
log "Do you want to perform a system update? (yes/no)"
2024-05-24 08:11:13 -06:00
read -r UPDATE_SYSTEM
if [[ $UPDATE_SYSTEM == "yes" ]]; then
system_update
fi
2024-05-24 08:29:43 -06:00
log "Setup complete. Summary of actions performed:"
[[ $SECURE_SSH == "yes" ]] && log "- SSH secured on port $SSH_PORT"
[[ $SETUP_FIREWALL == "yes" ]] && log "- Firewall configured with SSH port $SSH_PORT"
[[ $UPDATE_SYSTEM == "yes" ]] && log "- System updated"
# Run additional setup if requested
run_additional_setup
2024-05-24 08:34:29 -06:00
# Add additional users if requested
add_users