Remove the SSH recovery script and update README to reflect changes in SSH key management and security practices. Enhance user prompts for SSH key setup during user creation, including special handling for the user "sergio". Streamline logging and error handling in the setup process.
This commit is contained in:
609
setup.sh
609
setup.sh
@@ -18,8 +18,10 @@ CREATE_ADDITIONAL_USER=""
|
||||
ADDITIONAL_USER=""
|
||||
USE_UFW=""
|
||||
USE_FAIL2BAN=""
|
||||
DISABLE_SSH_PASSWORD=""
|
||||
SETUP_SSH_KEYS=""
|
||||
|
||||
# Track which users were created during this setup
|
||||
SYSADMIN_USER_CREATED=""
|
||||
ADDITIONAL_USER_CREATED=""
|
||||
|
||||
# Logging functions
|
||||
log() {
|
||||
@@ -51,25 +53,11 @@ check_debian() {
|
||||
|
||||
# Check for required commands
|
||||
check_commands() {
|
||||
# Check useradd (in /usr/sbin/)
|
||||
if ! command -v useradd >/dev/null 2>&1 && ! [ -x /usr/sbin/useradd ]; then
|
||||
error "useradd command not found. Please install the passwd package: apt install -y passwd"
|
||||
fi
|
||||
|
||||
# Check passwd (usually in /usr/bin/)
|
||||
if ! command -v passwd >/dev/null 2>&1 && ! [ -x /usr/bin/passwd ]; then
|
||||
error "passwd command not found. Please install the passwd package: apt install -y passwd"
|
||||
fi
|
||||
|
||||
# Check usermod (in /usr/sbin/)
|
||||
if ! command -v usermod >/dev/null 2>&1 && ! [ -x /usr/sbin/usermod ]; then
|
||||
warn "usermod command not found. Will attempt to use full path."
|
||||
fi
|
||||
|
||||
# Check sudo (usually in /usr/bin/)
|
||||
if ! command -v sudo >/dev/null 2>&1 && ! [ -x /usr/bin/sudo ]; then
|
||||
warn "sudo command not found. Will install it during system setup."
|
||||
fi
|
||||
for cmd in useradd passwd usermod; do
|
||||
if ! command -v $cmd >/dev/null 2>&1; then
|
||||
error "$cmd command not found. Please install required packages: apt install -y passwd"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Ask user about creating an additional user
|
||||
@@ -116,85 +104,28 @@ ask_additional_user() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Ask user about SSH security preferences
|
||||
ask_ssh_security() {
|
||||
echo ""
|
||||
echo -e "${BLUE}=== SSH Security Configuration ===${NC}"
|
||||
echo "This script can disable SSH password authentication for enhanced security."
|
||||
echo ""
|
||||
echo -e "${YELLOW}⚠️ WARNING: Disabling password authentication means you can ONLY log in with SSH keys!${NC}"
|
||||
echo "If you lose your SSH keys, you could be locked out of the server."
|
||||
echo ""
|
||||
echo -e "${YELLOW}Consider keeping password authentication if:${NC}"
|
||||
echo "• This is your first time setting up SSH keys"
|
||||
echo "• You don't have a reliable way to store SSH keys"
|
||||
echo "• You need emergency access options"
|
||||
echo ""
|
||||
read -p "Disable SSH password authentication? (y/N): " disable_password_choice
|
||||
|
||||
if [[ "$disable_password_choice" =~ ^[Yy]$ ]]; then
|
||||
DISABLE_SSH_PASSWORD="yes"
|
||||
log "SSH password authentication will be disabled"
|
||||
|
||||
echo ""
|
||||
echo -e "${BLUE}=== SSH Key Setup ===${NC}"
|
||||
echo "Since password authentication will be disabled, you'll need SSH keys to connect."
|
||||
echo "You need to add YOUR computer's SSH public key to the server."
|
||||
echo ""
|
||||
read -p "Add your SSH public key to authorized_keys for login? (Y/n): " setup_keys_choice
|
||||
|
||||
if [[ "$setup_keys_choice" =~ ^[Nn]$ ]]; then
|
||||
SETUP_SSH_KEYS="no"
|
||||
warn "SSH keys will NOT be set up automatically"
|
||||
warn "You MUST manually add your public key to authorized_keys files after setup"
|
||||
else
|
||||
SETUP_SSH_KEYS="yes"
|
||||
log "You will be prompted to paste your SSH public key"
|
||||
fi
|
||||
else
|
||||
DISABLE_SSH_PASSWORD="no"
|
||||
SETUP_SSH_KEYS="no"
|
||||
log "SSH password authentication will remain enabled"
|
||||
|
||||
echo ""
|
||||
read -p "Still want to add your SSH public key for convenient access? (Y/n): " add_keys_choice
|
||||
if [[ ! "$add_keys_choice" =~ ^[Nn]$ ]]; then
|
||||
SETUP_SSH_KEYS="yes"
|
||||
log "You will be prompted to paste your SSH public key"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Ask user about firewall preferences
|
||||
# Ask user about firewall and security preferences
|
||||
ask_firewall_preferences() {
|
||||
echo ""
|
||||
echo -e "${BLUE}=== Firewall Configuration ===${NC}"
|
||||
echo "This script can configure UFW (Uncomplicated Firewall) or you can manage iptables manually."
|
||||
echo "UFW (Uncomplicated Firewall) provides easy firewall management."
|
||||
echo ""
|
||||
echo -e "${YELLOW}UFW vs iptables:${NC}"
|
||||
echo "• UFW: Easy to use, good for basic setups"
|
||||
echo "• Manual iptables: More control, better for complex setups (VPN servers, etc.)"
|
||||
echo ""
|
||||
read -p "Would you like to install and configure UFW? (y/N): " ufw_choice
|
||||
read -p "Install and configure UFW? (y/N): " ufw_choice
|
||||
|
||||
if [[ "$ufw_choice" =~ ^[Yy]$ ]]; then
|
||||
USE_UFW="yes"
|
||||
log "UFW will be installed and configured"
|
||||
else
|
||||
USE_UFW="no"
|
||||
log "UFW will be skipped - you can configure iptables manually"
|
||||
log "UFW will be skipped"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo -e "${BLUE}=== Fail2ban Configuration ===${NC}"
|
||||
echo "Fail2ban protects against brute-force attacks by monitoring logs and banning IPs."
|
||||
echo "Fail2ban protects against brute-force attacks."
|
||||
echo ""
|
||||
echo -e "${YELLOW}Consider skipping fail2ban if:${NC}"
|
||||
echo "• You're running a VPN server (WireGuard, OpenVPN, etc.)"
|
||||
echo "• You have complex iptables rules"
|
||||
echo "• You prefer managing IP banning manually"
|
||||
echo ""
|
||||
read -p "Would you like to install and configure fail2ban? (Y/n): " fail2ban_choice
|
||||
read -p "Install and configure fail2ban? (Y/n): " fail2ban_choice
|
||||
|
||||
if [[ "$fail2ban_choice" =~ ^[Nn]$ ]]; then
|
||||
USE_FAIL2BAN="no"
|
||||
@@ -205,6 +136,144 @@ ask_firewall_preferences() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Ask user about SSH key setup for created users
|
||||
ask_ssh_key_setup() {
|
||||
# Check if any users were created
|
||||
local users_created=()
|
||||
|
||||
if [[ "$SYSADMIN_USER_CREATED" == "yes" ]]; then
|
||||
users_created+=("$SYSADMIN_USER")
|
||||
fi
|
||||
|
||||
if [[ "$ADDITIONAL_USER_CREATED" == "yes" ]]; then
|
||||
users_created+=("$ADDITIONAL_USER")
|
||||
fi
|
||||
|
||||
# Only prompt if users were actually created
|
||||
if [[ ${#users_created[@]} -eq 0 ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo -e "${BLUE}=== SSH Key Setup ===${NC}"
|
||||
echo "Users created during setup: ${users_created[*]}"
|
||||
echo "You can add your SSH public key for easier login."
|
||||
echo ""
|
||||
read -p "Add your SSH public key to created users? (Y/n): " add_key_choice
|
||||
|
||||
if [[ "$add_key_choice" =~ ^[Nn]$ ]]; then
|
||||
log "SSH key setup skipped"
|
||||
return
|
||||
fi
|
||||
|
||||
local user_public_key=""
|
||||
|
||||
# Special case for user "sergio" - offer pre-defined key
|
||||
if [[ " ${users_created[*]} " =~ " sergio " ]]; then
|
||||
echo ""
|
||||
echo "Detected user 'sergio' was created."
|
||||
echo "Use pre-configured SSH key for sergio? (Y/n)"
|
||||
echo "Key: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINBYyuGSa2wswiiObp2qj30MoiNRyFdBIBciFSbtrkZ8 mbpm1"
|
||||
echo ""
|
||||
read -p "Use this key? (Y/n): " use_sergio_key
|
||||
|
||||
if [[ ! "$use_sergio_key" =~ ^[Nn]$ ]]; then
|
||||
user_public_key="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINBYyuGSa2wswiiObp2qj30MoiNRyFdBIBciFSbtrkZ8 mbpm1"
|
||||
log "Using pre-configured key for sergio"
|
||||
fi
|
||||
fi
|
||||
|
||||
# If no pre-defined key was used, prompt for manual entry
|
||||
if [[ -z "$user_public_key" ]]; then
|
||||
echo ""
|
||||
echo "Please paste your SSH public key (starts with ssh-rsa, ssh-ed25519, etc.):"
|
||||
echo "You can find it with: cat ~/.ssh/id_ed25519.pub (or id_rsa.pub)"
|
||||
echo ""
|
||||
|
||||
while true; do
|
||||
read -r user_public_key
|
||||
|
||||
if [[ -z "$user_public_key" ]]; then
|
||||
echo -e "${RED}Public key cannot be empty. Please try again.${NC}"
|
||||
continue
|
||||
elif [[ "$user_public_key" =~ ^(ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2-) ]]; then
|
||||
break
|
||||
else
|
||||
echo -e "${RED}Invalid SSH public key format. Please ensure you copied the entire key.${NC}"
|
||||
continue
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Add key to all created users and generate SSH keys for them
|
||||
for username in "${users_created[@]}"; do
|
||||
setup_ssh_key_for_user "$username" "$user_public_key"
|
||||
generate_ssh_key_for_user "$username"
|
||||
done
|
||||
|
||||
echo ""
|
||||
log "SSH key added to: ${users_created[*]}"
|
||||
log "SSH keys generated for: ${users_created[*]}"
|
||||
}
|
||||
|
||||
setup_ssh_key_for_user() {
|
||||
local username="$1"
|
||||
local public_key="$2"
|
||||
local user_home="/home/$username"
|
||||
local ssh_dir="$user_home/.ssh"
|
||||
local authorized_keys="$ssh_dir/authorized_keys"
|
||||
|
||||
log "Setting up SSH key for user: $username"
|
||||
|
||||
# Create .ssh directory if it doesn't exist
|
||||
if [ ! -d "$ssh_dir" ]; then
|
||||
sudo -u "$username" mkdir -p "$ssh_dir"
|
||||
sudo -u "$username" chmod 700 "$ssh_dir"
|
||||
fi
|
||||
|
||||
# Check if key already exists
|
||||
if [ -f "$authorized_keys" ] && grep -Fxq "$public_key" "$authorized_keys"; then
|
||||
warn "SSH key already exists for $username"
|
||||
else
|
||||
echo "$public_key" | sudo -u "$username" tee -a "$authorized_keys" > /dev/null
|
||||
sudo -u "$username" chmod 600 "$authorized_keys"
|
||||
log "SSH key added for $username"
|
||||
fi
|
||||
}
|
||||
|
||||
generate_ssh_key_for_user() {
|
||||
local username="$1"
|
||||
local user_home="/home/$username"
|
||||
local ssh_dir="$user_home/.ssh"
|
||||
local private_key="$ssh_dir/id_ed25519"
|
||||
local public_key="$ssh_dir/id_ed25519.pub"
|
||||
|
||||
# Check if SSH key already exists
|
||||
if [[ -f "$private_key" ]]; then
|
||||
log "SSH key already exists for $username, skipping generation"
|
||||
return
|
||||
fi
|
||||
|
||||
log "Generating SSH key for user: $username"
|
||||
|
||||
# Create .ssh directory if it doesn't exist
|
||||
if [ ! -d "$ssh_dir" ]; then
|
||||
sudo -u "$username" mkdir -p "$ssh_dir"
|
||||
sudo -u "$username" chmod 700 "$ssh_dir"
|
||||
fi
|
||||
|
||||
# Generate SSH key without passphrase
|
||||
sudo -u "$username" ssh-keygen -t ed25519 -f "$private_key" -N "" -C "$username@$(hostname)"
|
||||
|
||||
# Set proper permissions
|
||||
sudo -u "$username" chmod 600 "$private_key"
|
||||
sudo -u "$username" chmod 644 "$public_key"
|
||||
|
||||
log "SSH key pair generated for $username"
|
||||
echo " Private key: $private_key"
|
||||
echo " Public key: $public_key"
|
||||
}
|
||||
|
||||
### === DEBIAN SYSTEM SETUP === ###
|
||||
setup_system() {
|
||||
log "Starting Debian 12 initial setup..."
|
||||
@@ -236,37 +305,21 @@ create_sysadmin_user() {
|
||||
|
||||
if id "$SYSADMIN_USER" &>/dev/null; then
|
||||
warn "User $SYSADMIN_USER already exists, skipping creation"
|
||||
SYSADMIN_USER_CREATED="no"
|
||||
else
|
||||
# Create user with home directory
|
||||
if command -v useradd >/dev/null 2>&1; then
|
||||
useradd -m -s /bin/bash "$SYSADMIN_USER"
|
||||
elif [ -x /usr/sbin/useradd ]; then
|
||||
/usr/sbin/useradd -m -s /bin/bash "$SYSADMIN_USER"
|
||||
else
|
||||
error "useradd command not found. Please install the passwd package."
|
||||
fi
|
||||
useradd -m -s /bin/bash "$SYSADMIN_USER"
|
||||
log "User $SYSADMIN_USER created successfully"
|
||||
SYSADMIN_USER_CREATED="yes"
|
||||
|
||||
# Set password for sysadmin user
|
||||
echo "Please set a password for user $SYSADMIN_USER:"
|
||||
if command -v passwd >/dev/null 2>&1; then
|
||||
passwd "$SYSADMIN_USER"
|
||||
elif [ -x /usr/bin/passwd ]; then
|
||||
/usr/bin/passwd "$SYSADMIN_USER"
|
||||
else
|
||||
error "passwd command not found. Please install the passwd package."
|
||||
fi
|
||||
passwd "$SYSADMIN_USER"
|
||||
fi
|
||||
|
||||
# Add sysadmin to sudo group
|
||||
log "Adding $SYSADMIN_USER to sudo group..."
|
||||
if command -v usermod >/dev/null 2>&1; then
|
||||
usermod -aG sudo "$SYSADMIN_USER"
|
||||
elif [ -x /usr/sbin/usermod ]; then
|
||||
/usr/sbin/usermod -aG sudo "$SYSADMIN_USER"
|
||||
else
|
||||
error "usermod command not found. Please install the passwd package."
|
||||
fi
|
||||
usermod -aG sudo "$SYSADMIN_USER"
|
||||
}
|
||||
|
||||
create_additional_user() {
|
||||
@@ -275,37 +328,21 @@ create_additional_user() {
|
||||
|
||||
if id "$ADDITIONAL_USER" &>/dev/null; then
|
||||
warn "User $ADDITIONAL_USER already exists, skipping creation"
|
||||
ADDITIONAL_USER_CREATED="no"
|
||||
else
|
||||
# Create user with home directory
|
||||
if command -v useradd >/dev/null 2>&1; then
|
||||
useradd -m -s /bin/bash "$ADDITIONAL_USER"
|
||||
elif [ -x /usr/sbin/useradd ]; then
|
||||
/usr/sbin/useradd -m -s /bin/bash "$ADDITIONAL_USER"
|
||||
else
|
||||
error "useradd command not found. Please install the passwd package."
|
||||
fi
|
||||
useradd -m -s /bin/bash "$ADDITIONAL_USER"
|
||||
log "User $ADDITIONAL_USER created successfully"
|
||||
ADDITIONAL_USER_CREATED="yes"
|
||||
|
||||
# Set password for additional user
|
||||
echo "Please set a password for user $ADDITIONAL_USER:"
|
||||
if command -v passwd >/dev/null 2>&1; then
|
||||
passwd "$ADDITIONAL_USER"
|
||||
elif [ -x /usr/bin/passwd ]; then
|
||||
/usr/bin/passwd "$ADDITIONAL_USER"
|
||||
else
|
||||
error "passwd command not found. Please install the passwd package."
|
||||
fi
|
||||
passwd "$ADDITIONAL_USER"
|
||||
fi
|
||||
|
||||
# Add additional user to sudo group
|
||||
log "Adding $ADDITIONAL_USER to sudo group..."
|
||||
if command -v usermod >/dev/null 2>&1; then
|
||||
usermod -aG sudo "$ADDITIONAL_USER"
|
||||
elif [ -x /usr/sbin/usermod ]; then
|
||||
/usr/sbin/usermod -aG sudo "$ADDITIONAL_USER"
|
||||
else
|
||||
error "usermod command not found. Please install the passwd package."
|
||||
fi
|
||||
usermod -aG sudo "$ADDITIONAL_USER"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -315,20 +352,11 @@ configure_security() {
|
||||
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
|
||||
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
|
||||
|
||||
# Configure SSH settings safely
|
||||
# Configure SSH settings
|
||||
log "Applying SSH security settings..."
|
||||
|
||||
# Backup original sshd_config
|
||||
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup.$(date +%Y%m%d_%H%M%S)
|
||||
|
||||
# Configure PasswordAuthentication
|
||||
if [[ "$DISABLE_SSH_PASSWORD" == "yes" ]]; then
|
||||
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
|
||||
log "SSH password authentication disabled"
|
||||
else
|
||||
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
|
||||
log "SSH password authentication enabled"
|
||||
fi
|
||||
# Configure basic SSH security - enable password auth by default for safety
|
||||
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
|
||||
|
||||
# Configure other SSH settings more safely
|
||||
sed -i 's/^#*PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config
|
||||
@@ -357,25 +385,17 @@ configure_security() {
|
||||
echo "AllowUsers $SYSADMIN_USER" >> /etc/ssh/sshd_config
|
||||
fi
|
||||
|
||||
# Test SSH configuration before proceeding
|
||||
# Test SSH configuration
|
||||
log "Testing SSH configuration..."
|
||||
if ! sshd -t; then
|
||||
error "SSH configuration is invalid! Restoring backup..."
|
||||
mv /etc/ssh/sshd_config.backup.$(date +%Y%m%d_%H%M%S) /etc/ssh/sshd_config
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "SSH configuration is valid"
|
||||
sshd -t || error "SSH configuration is invalid!"
|
||||
|
||||
# Remove/lock root password
|
||||
log "Locking root password..."
|
||||
passwd -l root
|
||||
|
||||
# Configure firewall based on user choice
|
||||
# Configure firewall if requested
|
||||
if [[ "$USE_UFW" == "yes" ]]; then
|
||||
configure_ufw
|
||||
else
|
||||
configure_basic_iptables
|
||||
fi
|
||||
|
||||
# Configure fail2ban if requested
|
||||
@@ -439,71 +459,21 @@ configure_ufw() {
|
||||
ufw status verbose
|
||||
}
|
||||
|
||||
configure_basic_iptables() {
|
||||
log "Setting up basic iptables rules..."
|
||||
|
||||
# Create a simple script for basic iptables rules
|
||||
cat > /etc/iptables-basic.sh << 'EOF'
|
||||
#!/bin/bash
|
||||
# Basic iptables rules for SSH, HTTP, HTTPS
|
||||
|
||||
# Flush existing rules
|
||||
iptables -F
|
||||
iptables -X
|
||||
iptables -t nat -F
|
||||
iptables -t nat -X
|
||||
iptables -t mangle -F
|
||||
iptables -t mangle -X
|
||||
|
||||
# Set default policies
|
||||
iptables -P INPUT DROP
|
||||
iptables -P FORWARD DROP
|
||||
iptables -P OUTPUT ACCEPT
|
||||
|
||||
# Allow loopback
|
||||
iptables -A INPUT -i lo -j ACCEPT
|
||||
iptables -A OUTPUT -o lo -j ACCEPT
|
||||
|
||||
# Allow established and related connections
|
||||
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
|
||||
|
||||
# Allow SSH (port 22)
|
||||
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
|
||||
|
||||
# Allow HTTP (port 80)
|
||||
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
|
||||
|
||||
# Allow HTTPS (port 443)
|
||||
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
|
||||
|
||||
# Save rules
|
||||
iptables-save > /etc/iptables/rules.v4
|
||||
EOF
|
||||
|
||||
chmod +x /etc/iptables-basic.sh
|
||||
|
||||
# Create iptables directory if it doesn't exist
|
||||
mkdir -p /etc/iptables
|
||||
|
||||
# Run the basic rules script
|
||||
/etc/iptables-basic.sh
|
||||
|
||||
# Install iptables-persistent to make rules persistent
|
||||
apt install -y iptables-persistent
|
||||
|
||||
log "Basic iptables rules applied and saved"
|
||||
warn "You can customize /etc/iptables-basic.sh and run it to update rules"
|
||||
warn "Current rules are saved in /etc/iptables/rules.v4"
|
||||
}
|
||||
|
||||
configure_fail2ban() {
|
||||
if [[ "$USE_FAIL2BAN" == "yes" ]]; then
|
||||
log "Configuring fail2ban..."
|
||||
cat > /etc/fail2ban/jail.local << EOF
|
||||
|
||||
# Ensure fail2ban directories exist
|
||||
mkdir -p /etc/fail2ban/jail.d
|
||||
|
||||
# Create configuration in jail.d instead of overwriting jail.local
|
||||
cat > /etc/fail2ban/jail.d/custom.conf << EOF
|
||||
[DEFAULT]
|
||||
bantime = 1h
|
||||
findtime = 10m
|
||||
maxretry = 3
|
||||
ignoreip = 127.0.0.1/8 ::1
|
||||
|
||||
[sshd]
|
||||
enabled = true
|
||||
@@ -514,128 +484,35 @@ maxretry = 3
|
||||
bantime = 1h
|
||||
EOF
|
||||
|
||||
# Start and enable fail2ban
|
||||
# Verify log file exists
|
||||
if [[ ! -f /var/log/auth.log ]]; then
|
||||
warn "Auth log file doesn't exist yet, creating it..."
|
||||
touch /var/log/auth.log
|
||||
chmod 640 /var/log/auth.log
|
||||
fi
|
||||
|
||||
# Start and enable fail2ban with error handling
|
||||
systemctl enable fail2ban
|
||||
systemctl start fail2ban
|
||||
|
||||
log "Fail2ban configured and started"
|
||||
else
|
||||
# Remove/disable fail2ban if user chose not to use it
|
||||
if systemctl is-active --quiet fail2ban; then
|
||||
log "Stopping and disabling fail2ban..."
|
||||
systemctl stop fail2ban
|
||||
systemctl disable fail2ban
|
||||
# Give a moment for systemd to process
|
||||
sleep 2
|
||||
|
||||
if systemctl start fail2ban; then
|
||||
# Verify it's actually running
|
||||
if systemctl is-active --quiet fail2ban; then
|
||||
log "Fail2ban configured and started successfully"
|
||||
# Show status briefly
|
||||
systemctl status fail2ban --no-pager --lines=3
|
||||
else
|
||||
warn "Fail2ban started but may have issues. Check: systemctl status fail2ban"
|
||||
fi
|
||||
else
|
||||
error "Failed to start fail2ban service"
|
||||
fi
|
||||
|
||||
if dpkg -l | grep -q fail2ban; then
|
||||
log "Removing fail2ban package..."
|
||||
apt remove --purge -y fail2ban
|
||||
apt autoremove -y
|
||||
fi
|
||||
|
||||
log "Fail2ban has been removed/disabled"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
### === SSH KEY SETUP === ###
|
||||
setup_user_ssh_keys() {
|
||||
if [[ "$SETUP_SSH_KEYS" == "no" ]]; then
|
||||
log "SSH key setup skipped"
|
||||
return
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo -e "${BLUE}=== SSH Public Key Setup ===${NC}"
|
||||
echo "To set up SSH key authentication, you need to provide your SSH public key."
|
||||
echo "You can find your public key on your computer by running:"
|
||||
echo -e "${YELLOW} cat ~/.ssh/id_rsa.pub${NC}"
|
||||
echo -e "${YELLOW} # or${NC}"
|
||||
echo -e "${YELLOW} cat ~/.ssh/id_ed25519.pub${NC}"
|
||||
echo ""
|
||||
echo "If you don't have an SSH key pair, generate one on your computer first:"
|
||||
echo -e "${YELLOW} ssh-keygen -t ed25519 -C \"your_email@example.com\"${NC}"
|
||||
echo ""
|
||||
|
||||
while true; do
|
||||
echo "Please paste your SSH public key (starts with ssh-rsa, ssh-ed25519, etc.):"
|
||||
read -r user_public_key
|
||||
|
||||
if [[ -z "$user_public_key" ]]; then
|
||||
echo -e "${RED}Public key cannot be empty. Please try again.${NC}"
|
||||
continue
|
||||
elif [[ "$user_public_key" =~ ^(ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2-) ]]; then
|
||||
break
|
||||
else
|
||||
echo -e "${RED}Invalid SSH public key format. Please ensure you copied the entire key.${NC}"
|
||||
continue
|
||||
fi
|
||||
done
|
||||
|
||||
# Add SSH key to sysadmin user
|
||||
setup_ssh_key_for_user "$SYSADMIN_USER" "$user_public_key"
|
||||
|
||||
# Add SSH key to additional user if created
|
||||
if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then
|
||||
setup_ssh_key_for_user "$ADDITIONAL_USER" "$user_public_key"
|
||||
fi
|
||||
}
|
||||
|
||||
setup_ssh_key_for_user() {
|
||||
local username="$1"
|
||||
local public_key="$2"
|
||||
local user_home="/home/$username"
|
||||
local ssh_dir="$user_home/.ssh"
|
||||
local authorized_keys="$ssh_dir/authorized_keys"
|
||||
|
||||
log "Setting up SSH key for user: $username"
|
||||
|
||||
# Create .ssh directory for user if it doesn't exist
|
||||
if [ ! -d "$ssh_dir" ]; then
|
||||
sudo -u "$username" mkdir -p "$ssh_dir"
|
||||
sudo -u "$username" chmod 700 "$ssh_dir"
|
||||
fi
|
||||
|
||||
# Add public key to authorized_keys
|
||||
log "Adding public key to authorized_keys for $username..."
|
||||
|
||||
# Check if key already exists in authorized_keys
|
||||
if [ -f "$authorized_keys" ] && grep -Fxq "$public_key" "$authorized_keys"; then
|
||||
warn "SSH key already exists in authorized_keys for $username"
|
||||
else
|
||||
echo "$public_key" | sudo -u "$username" tee -a "$authorized_keys" > /dev/null
|
||||
sudo -u "$username" chmod 600 "$authorized_keys"
|
||||
log "Public key added to authorized_keys for $username"
|
||||
fi
|
||||
}
|
||||
|
||||
show_ssh_setup_summary() {
|
||||
if [[ "$SETUP_SSH_KEYS" == "yes" ]]; then
|
||||
echo ""
|
||||
echo -e "${GREEN}✓ SSH key authentication has been set up for:${NC}"
|
||||
echo " - User: $SYSADMIN_USER"
|
||||
if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then
|
||||
echo " - User: $ADDITIONAL_USER"
|
||||
fi
|
||||
echo ""
|
||||
echo -e "${YELLOW}Your SSH public key has been added to authorized_keys files.${NC}"
|
||||
echo "You can now connect using your SSH private key."
|
||||
elif [[ "$DISABLE_SSH_PASSWORD" == "yes" ]]; then
|
||||
echo ""
|
||||
echo -e "${RED}⚠️ SSH password authentication is DISABLED${NC}"
|
||||
echo -e "${YELLOW}You MUST add your SSH public key manually:${NC}"
|
||||
echo ""
|
||||
echo "1. On your computer, get your public key:"
|
||||
echo " cat ~/.ssh/id_ed25519.pub"
|
||||
echo ""
|
||||
echo "2. Add it to the server's authorized_keys:"
|
||||
echo " echo 'YOUR_PUBLIC_KEY' >> /home/$SYSADMIN_USER/.ssh/authorized_keys"
|
||||
if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then
|
||||
echo " echo 'YOUR_PUBLIC_KEY' >> /home/$ADDITIONAL_USER/.ssh/authorized_keys"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
### === CLEANUP AND FINALIZATION === ###
|
||||
@@ -657,70 +534,44 @@ finalize_setup() {
|
||||
|
||||
log "SSH service restarted successfully"
|
||||
|
||||
# Download customization script for sysadmin user
|
||||
log "Downloading customization script..."
|
||||
if wget -O /home/$SYSADMIN_USER/costumize.sh "https://git.del-c.net/Del-c.net/debian-first-boot-setup/raw/branch/main/costumize.sh"; then
|
||||
chmod +x /home/$SYSADMIN_USER/costumize.sh
|
||||
chown $SYSADMIN_USER:$SYSADMIN_USER /home/$SYSADMIN_USER/costumize.sh
|
||||
log "Customization script downloaded to /home/$SYSADMIN_USER/costumize.sh"
|
||||
else
|
||||
warn "Failed to download customization script"
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
log "Cleaning up..."
|
||||
apt autoremove -y
|
||||
apt autoclean
|
||||
|
||||
# Final security check
|
||||
log "Final system status:"
|
||||
echo "================================"
|
||||
|
||||
if [[ "$USE_UFW" == "yes" ]]; then
|
||||
echo "UFW Status:"
|
||||
ufw status
|
||||
else
|
||||
echo "Iptables Status:"
|
||||
iptables -L -n | head -20
|
||||
fi
|
||||
|
||||
echo "================================"
|
||||
echo "SSH Configuration:"
|
||||
grep -E "PermitRootLogin|PasswordAuthentication|PubkeyAuthentication|AllowUsers" /etc/ssh/sshd_config
|
||||
echo "================================"
|
||||
|
||||
if [[ "$USE_FAIL2BAN" == "yes" ]]; then
|
||||
echo "Fail2ban Status:"
|
||||
systemctl status fail2ban --no-pager -l
|
||||
echo "================================"
|
||||
fi
|
||||
|
||||
echo "Sudo users:"
|
||||
grep -E "sudo|wheel" /etc/group
|
||||
echo "================================"
|
||||
|
||||
log "Setup completed successfully!"
|
||||
echo ""
|
||||
echo -e "${BLUE}=== IMPORTANT NOTES ===${NC}"
|
||||
echo -e "${YELLOW}1. Root SSH login is now DISABLED${NC}"
|
||||
echo -e "${YELLOW}2. Root password is LOCKED${NC}"
|
||||
echo -e "${YELLOW}3. SSH password authentication is DISABLED${NC}"
|
||||
echo -e "${YELLOW}4. Use '$SYSADMIN_USER' user with SSH key to connect${NC}"
|
||||
echo -e "${BLUE}=== SETUP SUMMARY ===${NC}"
|
||||
echo -e "${YELLOW}• Root SSH login: DISABLED${NC}"
|
||||
echo -e "${YELLOW}• Root password: LOCKED${NC}"
|
||||
echo -e "${YELLOW}• Main user: $SYSADMIN_USER (sudo access)${NC}"
|
||||
|
||||
if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then
|
||||
echo -e "${YELLOW}• Additional user: $ADDITIONAL_USER (sudo access)${NC}"
|
||||
fi
|
||||
|
||||
if [[ "$USE_UFW" == "yes" ]]; then
|
||||
echo -e "${YELLOW}5. UFW firewall is active (SSH, HTTP, HTTPS allowed)${NC}"
|
||||
else
|
||||
echo -e "${YELLOW}5. Basic iptables rules are active (SSH, HTTP, HTTPS allowed)${NC}"
|
||||
echo -e "${YELLOW} You can customize /etc/iptables-basic.sh for additional rules${NC}"
|
||||
echo -e "${YELLOW}• UFW firewall: ENABLED (SSH, HTTP, HTTPS)${NC}"
|
||||
fi
|
||||
|
||||
local note_number=6
|
||||
if [[ "$USE_FAIL2BAN" == "yes" ]]; then
|
||||
echo -e "${YELLOW}$note_number. Fail2ban is protecting SSH${NC}"
|
||||
((note_number++))
|
||||
else
|
||||
echo -e "${YELLOW}$note_number. Consider implementing IP banning manually if needed${NC}"
|
||||
((note_number++))
|
||||
echo -e "${YELLOW}• Fail2ban: ENABLED (SSH protection)${NC}"
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}$note_number. Automatic security updates are enabled${NC}"
|
||||
((note_number++))
|
||||
echo -e "${YELLOW}$note_number. SSH keys generated for both $SYSADMIN_USER and root users${NC}"
|
||||
|
||||
echo -e "${YELLOW}• Automatic security updates: ENABLED${NC}"
|
||||
echo ""
|
||||
echo -e "${RED}REBOOT RECOMMENDED${NC}"
|
||||
echo ""
|
||||
echo "To connect: ssh $SYSADMIN_USER@$(hostname -I | awk '{print $1}')"
|
||||
echo -e "${GREEN}Connect with: ssh $SYSADMIN_USER@$(hostname -I | awk '{print $1}')${NC}"
|
||||
echo -e "${YELLOW}Reboot recommended${NC}"
|
||||
}
|
||||
|
||||
### === MAIN === ###
|
||||
@@ -731,11 +582,11 @@ main() {
|
||||
check_commands
|
||||
|
||||
echo -e "${BLUE}=== Debian 12 Initial Setup ===${NC}"
|
||||
echo "This script will:"
|
||||
echo "1. Set up a secure Debian 12 system"
|
||||
echo "2. Create a sysadmin user with SSH key authentication"
|
||||
echo "3. Optionally create an additional user account"
|
||||
echo "4. Generate SSH keys for all created users and root"
|
||||
echo "This script will set up a secure Debian 12 system with:"
|
||||
echo "• Sysadmin user with sudo access"
|
||||
echo "• Optional additional user"
|
||||
echo "• SSH security hardening"
|
||||
echo "• Optional UFW firewall and fail2ban"
|
||||
echo ""
|
||||
read -p "Continue? (y/N): " confirm
|
||||
|
||||
@@ -746,18 +597,18 @@ main() {
|
||||
|
||||
# Ask for user and security preferences
|
||||
ask_additional_user
|
||||
ask_ssh_security
|
||||
ask_firewall_preferences
|
||||
|
||||
# System setup
|
||||
setup_system
|
||||
create_sysadmin_user
|
||||
create_additional_user
|
||||
|
||||
# SSH key setup for created users
|
||||
ask_ssh_key_setup
|
||||
|
||||
configure_security
|
||||
|
||||
# SSH key setup
|
||||
setup_user_ssh_keys
|
||||
show_ssh_setup_summary
|
||||
|
||||
# Finalize
|
||||
finalize_setup
|
||||
|
||||
Reference in New Issue
Block a user