# Variables new_ssh_port=1000 # Replace with your desired SSH port new_user="xxx"# Replace with your desired username public_key="xxxxx"# Replace with your public SSH key
# Ensure the script is run as root if [ "$(id -u)" != "0" ]; then echo"This script must be run as root" 1>&2 exit 1 fi
# Check if openssh-server is installed if ! dpkg -l | grep -q openssh-server; then echo"Installing OpenSSH Server..." apt-get update apt-get install -y openssh-server fi
# Enable SSH service systemctl enable ssh.service
# Start SSH service systemctl start ssh.service
# Verify service status if systemctl is-active --quiet ssh.service; then echo"SSH service is running" else echo"SSH service failed to start" systemctl status ssh.service exit 1 fi
# 1. Configure SSH echo"Configuring SSH..."
# Remove cloud init rm /etc/ssh/sshd_config.d/*
# Replace Port configuration - matches both commented and uncommented sed -i -E 's/^#?Port[[:space:]]+[0-9]+/Port '"$new_ssh_port"'/' /etc/ssh/sshd_config
# Replace PasswordAuthentication - matches both commented and uncommented, any value sed -i -E 's/^#?PasswordAuthentication[[:space:]]+(yes|no)/PasswordAuthentication no/' /etc/ssh/sshd_config
# Replace PermitRootLogin - matches any existing value sed -i -E 's/^#?PermitRootLogin[[:space:]]+(yes|no|prohibit-password|without-password)/PermitRootLogin no/' /etc/ssh/sshd_config
# 2. Create new user with sudo privileges echo"Creating new user '$new_user' with sudo privileges..." adduser --gecos """$new_user" echo"$new_user ALL=(ALL) ALL" >> /etc/sudoers
# Add public key for new user echo"Adding public key for new user..." mkdir -p /home/$new_user/.ssh echo$public_key > /home/$new_user/.ssh/authorized_keys chown -R $new_user:$new_user /home/$new_user/.ssh chmod 700 /home/$new_user/.ssh chmod 600 /home/$new_user/.ssh/authorized_keys
# Check if jq is installed if ! command -v jq &> /dev/null then echo"jq could not be found, attempting to install..." sudo apt-get update -y sudo apt-get install -y jq else echo"jq is already installed" fi
# List all UFW rules with numbers and filter by port 8443 rules=$(ufw status numbered | grep '8443' | awk -F '[][]''{ print $2 }')
# Check if any rules were found if [ -z "$rules" ]; then echo"No UFW rules found for port 8443." else # Reverse the rules order to avoid disrupting the numbering for rule in $(echo"$rules" | tac); do echo"Deleting rule $rule for port 8443..." yes | ufw delete $rule done
echo"UFW rules deletion complete." # Show current status ufw status verbose
echo"\n\n----------- update to new ip ranges ---------\n\n" fi
# Fetch JSON data using curl json_data=$(curl --request GET \ --url https://api.cloudflare.com/client/v4/ips \ --header 'Authorization: Bearer YOUR_BEARER_TOKEN' \ --header 'Content-Type: application/json')
# Check if curl command was successful if [ $? -ne 0 ]; then echo"Failed to fetch data from API." exit 1 fi
# Extract ipv4_cidrs using jq and convert it into a Bash array readarray -t ip_ranges < <(echo"$json_data" | jq -r '.result.ipv4_cidrs[]')
# Add UFW rules for each IP range for ip in"${ip_ranges[@]}"; do echo"Allowing IP range $ip on port 8443" ufw allow from $ip to any port 8443 done