Created
May 16, 2024 15:21
-
-
Save det-peralta/43b2d91ebe16b2ec380eff9db50e1ffb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Global variables | |
SSH_CONFIG="/etc/ssh/sshd_config" | |
SSH_CONFIG_D="/etc/ssh/sshd_config.d/60-cloudimg-settings.conf" | |
# Function to update system repositories | |
update_system() { | |
sudo apt update && sudo apt upgrade -y | |
} | |
# Function to install SSH server | |
install_ssh_server() { | |
sudo apt install openssh-server -y | |
} | |
# Function to enable SSH server | |
enable_ssh_server() { | |
sudo systemctl enable ssh | |
} | |
# Function to start SSH server | |
start_ssh_server() { | |
sudo systemctl start ssh | |
} | |
# Function to enable and start SSH server | |
enable_and_start_ssh() { | |
sudo systemctl enable --now ssh | |
} | |
# Function to verify SSH server status | |
verify_ssh_status() { | |
sudo systemctl status ssh | |
} | |
# Function to enable UFW firewall | |
enable_ufw_firewall() { | |
sudo ufw enable | |
} | |
# Function to allow SSH traffic through firewall | |
allow_ssh_through_firewall() { | |
sudo ufw allow ssh | |
} | |
# Function to check UFW status | |
check_ufw_status() { | |
sudo ufw status | |
} | |
# Function to configure SSH for root login and password authentication | |
configure_ssh() { | |
sudo sed -i -e 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' \ | |
-e 's/^PasswordAuthentication.*/PasswordAuthentication yes/' $SSH_CONFIG | |
sudo rm -f $SSH_CONFIG_D | |
sudo systemctl restart sshd | |
} | |
# Main function | |
main() { | |
update_system | |
install_ssh_server | |
enable_and_start_ssh | |
# verify_ssh_status | |
enable_ufw_firewall | |
allow_ssh_through_firewall | |
check_ufw_status | |
configure_ssh | |
} | |
# Execute main function | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment