InitMate Code
This commit is contained in:
parent
0c8d56e954
commit
4cb04f2829
137
InitMate.sh
Normal file
137
InitMate.sh
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Check if the script is run as root
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo "Please run as root"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Function to detect the OS and package manager
|
||||||
|
detect_os() {
|
||||||
|
if [[ -f /etc/os-release ]]; then
|
||||||
|
source /etc/os-release
|
||||||
|
OS=$ID
|
||||||
|
else
|
||||||
|
echo "Unable to detect the operating system. Please enter it manually (e.g., ubuntu, debian, centos):"
|
||||||
|
read -r OS
|
||||||
|
fi
|
||||||
|
|
||||||
|
case $OS in
|
||||||
|
ubuntu|debian)
|
||||||
|
PKG_MANAGER="apt"
|
||||||
|
;;
|
||||||
|
centos|fedora)
|
||||||
|
PKG_MANAGER="yum"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unknown operating system. Please enter the package manager (e.g., apt, yum):"
|
||||||
|
read -r PKG_MANAGER
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to secure SSH
|
||||||
|
secure_ssh() {
|
||||||
|
echo "Securing SSH..."
|
||||||
|
SSH_PORT=$((RANDOM % 64512 + 1024))
|
||||||
|
echo "New SSH port: $SSH_PORT"
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
echo "Please enter your public key:"
|
||||||
|
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
|
||||||
|
echo "UFW is already installed. Opening SSH port $SSH_PORT."
|
||||||
|
ufw allow "$SSH_PORT"/tcp
|
||||||
|
elif command -v firewall-cmd &>/dev/null; then
|
||||||
|
echo "Firewalld is already installed. Opening SSH port $SSH_PORT."
|
||||||
|
firewall-cmd --permanent --add-port="$SSH_PORT"/tcp
|
||||||
|
firewall-cmd --reload
|
||||||
|
else
|
||||||
|
echo "No firewall detected. Do you want to install UFW? (yes/no)"
|
||||||
|
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
|
||||||
|
fi
|
||||||
|
ufw allow "$SSH_PORT"/tcp
|
||||||
|
ufw enable
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Do you want to open additional ports? (yes/no)"
|
||||||
|
read -r OPEN_PORTS
|
||||||
|
if [[ $OPEN_PORTS == "yes" ]]; then
|
||||||
|
echo "Please enter the ports to open (comma-separated, e.g., 80,443,8080):"
|
||||||
|
read -r ADDITIONAL_PORTS
|
||||||
|
IFS=',' read -r -a PORT_ARRAY <<< "$ADDITIONAL_PORTS"
|
||||||
|
for PORT in "${PORT_ARRAY[@]}"; do
|
||||||
|
if command -v ufw &>/dev/null; then
|
||||||
|
ufw allow "$PORT"/tcp
|
||||||
|
elif command -v firewall-cmd &>/dev/null; then
|
||||||
|
firewall-cmd --permanent --add-port="$PORT"/tcp
|
||||||
|
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
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main script execution
|
||||||
|
detect_os
|
||||||
|
echo "Detected OS: $OS, Package Manager: $PKG_MANAGER"
|
||||||
|
echo "Is this correct? (yes/no)"
|
||||||
|
read -r CONFIRM
|
||||||
|
if [[ $CONFIRM != "yes" ]]; then
|
||||||
|
echo "Please enter the correct OS and package manager:"
|
||||||
|
read -r OS PKG_MANAGER
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Do you want to secure SSH? (yes/no)"
|
||||||
|
read -r SECURE_SSH
|
||||||
|
if [[ $SECURE_SSH == "yes" ]]; then
|
||||||
|
secure_ssh
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Do you want to set up a firewall? (yes/no)"
|
||||||
|
read -r SETUP_FIREWALL
|
||||||
|
if [[ $SETUP_FIREWALL == "yes" ]]; then
|
||||||
|
setup_firewall
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Do you want to perform a system update? (yes/no)"
|
||||||
|
read -r UPDATE_SYSTEM
|
||||||
|
if [[ $UPDATE_SYSTEM == "yes" ]]; then
|
||||||
|
system_update
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Setup complete. Summary of actions performed:"
|
||||||
|
[[ $SECURE_SSH == "yes" ]] && echo "- SSH secured on port $SSH_PORT"
|
||||||
|
[[ $SETUP_FIREWALL == "yes" ]] && echo "- Firewall configured with SSH port $SSH_PORT"
|
||||||
|
[[ $UPDATE_SYSTEM == "yes" ]] && echo "- System updated"
|
Loading…
Reference in New Issue
Block a user