235 lines
7.1 KiB
Bash
Executable File
235 lines
7.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
#
|
|
# SPDX-FileCopyrightText: 2023 PeppemrintOS Team (peppermintosteam@proton.me)
|
|
|
|
# Function to generate SSH keys
|
|
generate_ssh_keys() {
|
|
dialog --yesno "Do you want to generate SSH keys?" 10 60
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
SSH_DIR=~/.ssh
|
|
mkdir -p $SSH_DIR
|
|
SSH_KEY_FILE=$SSH_DIR/id_rsa
|
|
ssh-keygen -t rsa -b 4096 -f $SSH_KEY_FILE -N ""
|
|
|
|
dialog --msgbox "SSH keys generated in $SSH_KEY_FILE." 10 60
|
|
}
|
|
|
|
# Function to add SSH key to authorized_keys
|
|
add_ssh_key() {
|
|
KEY_FILE=$(dialog --inputbox "Enter the path to the SSH public key file:" 10 60 3>&1 1>&2 2>&3 3>&-)
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
if [ -z "$KEY_FILE" ]; then
|
|
dialog --msgbox "Please enter a valid path to the SSH public key file." 10 60
|
|
else
|
|
mkdir -p ~/.ssh
|
|
cat "$KEY_FILE" >> ~/.ssh/authorized_keys
|
|
dialog --msgbox "SSH key added to authorized_keys." 10 60
|
|
fi
|
|
}
|
|
|
|
# Function to configure advanced SSH options
|
|
configure_advanced_ssh() {
|
|
while true; do
|
|
ADV_CHOICE=$(dialog --clear --backtitle "SSH Advanced Configuration" \
|
|
--title "SSH Advanced Menu" \
|
|
--menu "Choose an option:" 20 60 10 \
|
|
1 "Edit SSH Config File" \
|
|
2 "Restart SSH Service" \
|
|
3 "Check SSH Service Status" \
|
|
4 "Install OpenSSH Server" \
|
|
5 "Uninstall OpenSSH Server" \
|
|
6 "View SSH Logs" \
|
|
7 "Backup SSH Config" \
|
|
8 "Restore SSH Config" \
|
|
9 "Change SSH Port" \
|
|
10 "Enable/Disable Password Authentication" \
|
|
11 "List SSH Connections" \
|
|
12 "Back to SSH Menu" \
|
|
3>&1 1>&2 2>&3 3>&-)
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
break
|
|
fi
|
|
|
|
case $ADV_CHOICE in
|
|
1) ${EDITOR:-nano} /etc/ssh/sshd_config ;;
|
|
2) systemctl restart ssh ;;
|
|
3) systemctl status ssh ;;
|
|
4) install_openssh_server ;;
|
|
5) uninstall_openssh_server ;;
|
|
6) view_ssh_logs ;;
|
|
7) backup_ssh_config ;;
|
|
8) restore_ssh_config ;;
|
|
9) change_ssh_port ;;
|
|
10) toggle_password_authentication ;;
|
|
11) list_ssh_connections ;;
|
|
12) break ;;
|
|
*) dialog --msgbox "Invalid option. Please choose a valid option." 10 60 ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# Function to install OpenSSH Server
|
|
install_openssh_server() {
|
|
dialog --yesno "Do you want to install OpenSSH Server?" 10 60
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
apt update
|
|
apt install -y openssh-server
|
|
systemctl start ssh
|
|
systemctl enable ssh
|
|
dialog --msgbox "OpenSSH Server installed and enabled." 10 60
|
|
}
|
|
|
|
# Function to uninstall OpenSSH Server
|
|
uninstall_openssh_server() {
|
|
dialog --yesno "Do you want to uninstall OpenSSH Server?" 10 60
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
systemctl stop ssh
|
|
systemctl disable ssh
|
|
apt remove -y openssh-server
|
|
dialog --msgbox "OpenSSH Server uninstalled." 10 60
|
|
}
|
|
|
|
# Function to view SSH logs
|
|
view_ssh_logs() {
|
|
dialog --msgbox "$(journalctl -u ssh --no-pager)" 30 80
|
|
}
|
|
|
|
# Function to backup SSH config
|
|
backup_ssh_config() {
|
|
BACKUP_DIR="/var/backups"
|
|
BACKUP_FILE="sshd_config_$(date +"%Y%m%d_%H%M%S").bak"
|
|
cp /etc/ssh/sshd_config $BACKUP_DIR/$BACKUP_FILE
|
|
dialog --msgbox "SSH config backed up to $BACKUP_DIR/$BACKUP_FILE." 10 60
|
|
}
|
|
|
|
# Function to restore SSH config
|
|
restore_ssh_config() {
|
|
BACKUP_FILE=$(dialog --inputbox "Enter the full path of the SSH config backup file:" 10 60 3>&1 1>&2 2>&3 3>&-)
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
if [ -z "$BACKUP_FILE" ]; then
|
|
dialog --msgbox "Please enter a valid path to the SSH config backup file." 10 60
|
|
elif [ ! -f "$BACKUP_FILE" ]; then
|
|
dialog --msgbox "Backup file not found." 10 60
|
|
else
|
|
cp "$BACKUP_FILE" /etc/ssh/sshd_config
|
|
systemctl restart ssh
|
|
dialog --msgbox "SSH config restored from $BACKUP_FILE." 10 60
|
|
fi
|
|
}
|
|
|
|
# Function to change SSH port
|
|
change_ssh_port() {
|
|
NEW_PORT=$(dialog --inputbox "Enter the new SSH port:" 10 60 3>&1 1>&2 2>&3 3>&-)
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
if [[ $NEW_PORT =~ ^[0-9]+$ ]]; then
|
|
sed -i "s/#Port 22/Port $NEW_PORT/" /etc/ssh/sshd_config
|
|
systemctl restart ssh
|
|
dialog --msgbox "SSH port changed to $NEW_PORT." 10 60
|
|
else
|
|
dialog --msgbox "Invalid port number." 10 60
|
|
fi
|
|
}
|
|
|
|
# Function to enable/disable password authentication in SSH
|
|
toggle_password_authentication() {
|
|
STATUS=$(dialog --menu "Choose an option:" 10 60 2 \
|
|
1 "Enable Password Authentication" \
|
|
2 "Disable Password Authentication" \
|
|
3>&1 1>&2 2>&3 3>&-)
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
return 1
|
|
fi
|
|
|
|
case $STATUS in
|
|
1) sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config ;;
|
|
2) sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config ;;
|
|
esac
|
|
|
|
systemctl restart ssh
|
|
dialog --msgbox "Password authentication $(echo $STATUS | tr '[:lower:]' '[:upper:]')." 10 60
|
|
}
|
|
|
|
# Function to list SSH connections
|
|
list_ssh_connections() {
|
|
netstat -tnpa | grep 'ESTABLISHED.*sshd'
|
|
dialog --msgbox "List of SSH connections displayed." 10 60
|
|
}
|
|
|
|
# Function to check if OpenSSH is installed
|
|
check_ssh_installation() {
|
|
dpkg -l openssh-server >/dev/null 2>&1
|
|
if [ $? -eq 0 ]; then
|
|
dialog --msgbox "OpenSSH is installed." 10 60
|
|
else
|
|
dialog --msgbox "OpenSSH is not installed." 10 60
|
|
fi
|
|
}
|
|
|
|
# Function to configure SSH options
|
|
configure_ssh() {
|
|
while true; do
|
|
SSH_CHOICE=$(dialog --clear --backtitle "SSH Configuration" \
|
|
--title "SSH Menu" \
|
|
--menu "Choose an option:" 20 60 10 \
|
|
1 "Generate SSH Keys" \
|
|
2 "Add SSH Key to authorized_keys" \
|
|
3 "Advanced Configuration" \
|
|
4 "Check SSH Installation" \
|
|
5 "Back to Main Menu" \
|
|
3>&1 1>&2 2>&3 3>&-)
|
|
|
|
# Check if user canceled
|
|
if [ $? -ne 0 ]; then
|
|
break
|
|
fi
|
|
|
|
case $SSH_CHOICE in
|
|
1) generate_ssh_keys ;;
|
|
2) add_ssh_key ;;
|
|
3) configure_advanced_ssh ;;
|
|
4) check_ssh_installation ;;
|
|
5) break ;;
|
|
*) dialog --msgbox "Invalid option. Please choose a valid option." 10 60 ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# Execute the main function to configure SSH
|
|
configure_ssh
|