11#! /bin/bash
22
3+ # Server Configuration Script
4+ # This script provides a menu-driven interface to perform various server configuration tasks.
5+ # It allows users to install essential apps, set up NGINX and PHP, configure NVM, enable passwordless sudo,
6+ # set up SSH key-based authentication and configure static IP address.
7+ 8+ # Author: Decaded (https://github.com/decaded)
9+ 310# Function to display a menu and get user's choice
411show_menu () {
512 clear
@@ -11,13 +18,13 @@ show_menu() {
1118 echo " 3) Install Node Version Manager (NVM)"
1219 echo " 4) Enable Passwordless sudo access"
1320 echo " 5) Set up SSH key-based authentication"
21+ echo " 6) Configure Static IP Address"
22+ echo
23+ 1424 if [ -f " /etc/ssh/sshd_config_decoscript.backup" ]; then
15- echo " 6) Restore SSH Configuration"
16- else
17- echo " 6) Restore SSH Configuration (Not available)"
25+ echo " 9) Restore SSH Configuration"
1826 fi
19- echo " 7) Configure Static IP Address"
20- echo
27+ 2128 echo " 0) Exit"
2229 echo
2330 read -rp " Enter your choice: " choice
@@ -27,12 +34,13 @@ show_menu() {
2734 3) install_nvm ;;
2835 4) enable_passwordless_sudo " $USER " ;;
2936 5) setup_ssh_key_authentication ;;
30- 7 ) configure_static_ip ;;
31- 6 )
37+ 6 ) configure_static_ip ;;
38+ 9 )
3239 if [ -f " /etc/ssh/sshd_config_decoscript.backup" ]; then
3340 restore_ssh_config
3441 else
35- echo " SSH configuration backup is not available for restoration."
42+ echo " Invalid choice. Please select a valid option."
43+ show_menu
3644 fi
3745 ;;
3846 0)
@@ -74,77 +82,86 @@ install_essential_apps() {
7482 if ! [ -x " $( command -v dialog) " ]; then
7583 echo " Dialog is not installed. Installing dialog..."
7684 sudo apt update && sudo apt install dialog -y
77- fi
7885
79- while true ; do
80- # Define the dialog menu options
81- options=(" 1" " htop - Interactive process viewer" off
82- " 2" " screen - Terminal multiplexer" off
83- " 3" " nload - Network traffic monitor" off
84- " 4" " nano - Text editor" off
85- " 5" " firewalld - Firewall management" off
86- " 6" " fail2ban - Intrusion prevention system" off
87- " 7" " unattended-upgrades - Automatic updates" off
88- " 8" " git - Version control system" off)
89- 90- # Display the dialog menu and store the user's choices
91- choices=$( dialog --clear --title " Essential Apps Installer" --checklist " Choose which apps to install:" 0 0 0 " ${options[@]} " 2>&1 > /dev/tty)
92- 93- # Check if the user canceled or made no selection
86+ # Check if the installation was successful
9487 if [ $? -ne 0 ]; then
95- clear
96- echo " Canceled. Returning to the main menu."
88+ echo " Error: Failed to install dialog. Exiting."
9789 return
9890 fi
91+ fi
9992
100- # Process user choices and install selected apps
101- selected_apps=" "
102- 103- for choice in $choices ; do
104- case $choice in
105- 1) selected_apps+=" htop" ;;
106- 2) selected_apps+=" screen" ;;
107- 3) selected_apps+=" nload" ;;
108- 4) selected_apps+=" nano" ;;
109- 5) selected_apps+=" firewalld" ;;
110- 6) selected_apps+=" fail2ban" ;;
111- 7) selected_apps+=" unattended-upgrades" ;;
112- 8) selected_apps+=" git" ;;
113- esac
114- done
115- 116- echo " Installing selected apps: $selected_apps "
117- sudo apt update && sudo apt install $selected_apps -y
93+ # Define the dialog menu options
94+ app_options=(" 1" " htop - Interactive process viewer" off
95+ " 2" " screen - Terminal multiplexer" off
96+ " 3" " nload - Network traffic monitor" off
97+ " 4" " nano - Text editor" off
98+ " 5" " firewalld - Firewall management" off
99+ " 6" " fail2ban - Intrusion prevention system" off
100+ " 7" " unattended-upgrades - Automatic updates" off
101+ " 8" " git - Version control system" off)
118102
119- # Check if firewalld was selected
120- if [[ " $selected_apps " == * " firewalld" * ]]; then
121- configure_firewall
122- fi
103+ # Display the dialog menu and store the user's choices
104+ choices=$( dialog --clear --title " Essential Apps Installer" --checklist " Choose which apps to install:" 0 0 0 " ${app_options[@]} " 2>&1 > /dev/tty)
123105
124- # Check if Fail2ban was selected
125- if [[ " $selected_apps " == * " fail2ban" * ]]; then
126- configure_fail2ban
127- fi
106+ # Check if the user canceled or made no selection
107+ if [ $? -ne 0 ]; then
108+ clear
109+ echo " Canceled. Returning to the main menu."
110+ return
111+ fi
128112
129- # Check if unattended-upgrades was selected
130- if [[ " $selected_apps " == * " unattended-upgrades" * ]]; then
131- sudo dpkg-reconfigure -plow unattended-upgrades
132- fi
113+ # Process user choices and install selected apps
114+ selected_applications=" "
115+ 116+ for choice in $choices ; do
117+ case $choice in
118+ 1) selected_applications+=" htop" ;;
119+ 2) selected_applications+=" screen" ;;
120+ 3) selected_applications+=" nload" ;;
121+ 4) selected_applications+=" nano" ;;
122+ 5) selected_applications+=" firewalld" ;;
123+ 6) selected_applications+=" fail2ban" ;;
124+ 7) selected_applications+=" unattended-upgrades" ;;
125+ 8) selected_applications+=" git" ;;
126+ esac
127+ done
133128
134- # Check if Git was selected and is installed
135- if [[ " $selected_apps " == * " git" * ]] && ! [ -x " $( command -v git) " ]; then
136- echo " Git is not installed. Installing Git..."
137- sudo apt install git -y
138- fi
129+ if [ -z " $selected_applications " ]; then
130+ echo " No apps selected. Returning to the main menu."
131+ return
132+ fi
139133
140- # Configure Git only if it was selected
141- if [[ " $selected_apps " == * " git" * ]]; then
142- configure_git
143- fi
134+ echo " Installing selected apps: $selected_applications "
135+ sudo apt update && sudo apt install $selected_applications -y
144136
145- echo " Installation complete."
137+ # Check if there was an error during installation
138+ if [ $? -ne 0 ]; then
139+ echo " Error: Failed to install some or all of the selected apps. Please check your internet connection and try again."
146140 return
147- done
141+ fi
142+ 143+ # Check if firewalld was selected
144+ if [[ " $selected_applications " == * " firewalld" * ]]; then
145+ configure_firewall
146+ fi
147+ 148+ # Check if Fail2ban was selected
149+ if [[ " $selected_applications " == * " fail2ban" * ]]; then
150+ configure_fail2ban
151+ fi
152+ 153+ # Check if unattended-upgrades was selected
154+ if [[ " $selected_applications " == * " unattended-upgrades" * ]]; then
155+ sudo dpkg-reconfigure -plow unattended-upgrades
156+ fi
157+ 158+ # Configure Git only if it was selected
159+ if [[ " $selected_applications " == * " git" * ]]; then
160+ configure_git
161+ fi
162+ 163+ echo " Installation complete."
164+ return
148165}
149166
150167# Function to configure the firewall with checks
@@ -231,17 +248,17 @@ setup_ssh_key_authentication() {
231248 echo " #######################################################"
232249
233250 # Read the user-provided public key and save it to a variable
234- IFS= read -r user_public_key
251+ IFS= read -r ssh_public_key
235252
236253 # Create the ~/.ssh directory if it doesn't exist
237254 mkdir -p " $HOME /.ssh"
238255
239256 authorized_keys_file=" $HOME /.ssh/authorized_keys"
240257
241258 # Check if the authorized_keys file exists and the key is not already present
242- if [ -f " $authorized_keys_file " ] && ! grep -q " $user_public_key " " $authorized_keys_file " ; then
259+ if [ -f " $authorized_keys_file " ] && ! grep -q " $ssh_public_key " " $authorized_keys_file " ; then
243260 # Save the public key to the authorized_keys file
244- echo " $user_public_key " >> " $authorized_keys_file "
261+ echo " $ssh_public_key " >> " $authorized_keys_file "
245262 if [ $? -ne 0 ]; then
246263 echo " Error: Failed to save the public key to authorized_keys file."
247264 exit 1
@@ -250,7 +267,7 @@ setup_ssh_key_authentication() {
250267 echo " Public key added to authorized_keys."
251268 elif [ ! -f " $authorized_keys_file " ]; then
252269 echo " Creating authorized_keys file..."
253- echo " $user_public_key " > " $authorized_keys_file "
270+ echo " $ssh_public_key " > " $authorized_keys_file "
254271 if [ $? -ne 0 ]; then
255272 echo " Error: Failed to create authorized_keys file."
256273 exit 1
@@ -422,18 +439,14 @@ restore_ssh_config() {
422439 fi
423440}
424441
425- # Function to walidate ports
442+ # Function to validate if a given input is a valid port number
426443validate_port () {
427444 local port=" 1ドル "
428- if ! [[ " $port " =~ ^[0-9]+$ ]]; then
429- echo " Error: Invalid port number. Please enter a valid numeric port."
430- return 1
431- fi
432- 433- if [[ " $port " -lt 1 || " $port " -gt 65535 ]]; then
434- echo " Error: Port number should be between 1 and 65535."
435- return 1
445+ if ! [[ " $port " =~ ^[0-9]+$ ]] || (( port < 1 || port > 65535 )) ; then
446+ echo " Error: Invalid port number. Please enter a valid numeric port between 1 and 65535."
447+ return 1 # Invalid port
436448 fi
449+ return 0 # Valid port
437450}
438451
439452# Function to configure Git
@@ -517,13 +530,13 @@ configure_fail2ban() {
517530 sudo apt install fail2ban -y
518531 ;;
519532 2)
520- read -rp " Enter the URL of the user custom configuration: " custom_config_url
533+ read -rp " Enter the URL of the user custom configuration: " fail2ban_custom_config_url
521534
522535 # Check if the URL is valid and accessible
523- if wget --spider " $custom_config_url " 2> /dev/null; then
536+ if wget --spider " $fail2ban_custom_config_url " 2> /dev/null; then
524537 # Install Fail2ban if not already installed
525538 sudo apt install fail2ban -y
526- sudo wget -O /etc/fail2ban/jail.local " $custom_config_url "
539+ sudo wget -O /etc/fail2ban/jail.local " $fail2ban_custom_config_url "
527540 echo " User custom Fail2ban configuration applied."
528541 else
529542 echo " Warning: Invalid URL or unable to reach the URL. Using the default configuration."
@@ -544,15 +557,10 @@ configure_static_ip() {
544557 clear
545558 echo " Configuring a static IP address using Netplan."
546559
547- # Check if Netplan is installed, and if not, prompt the user to install it
548- if ! command -v netplan & > /dev/null; then
549- read -rp " Netplan is not installed. Do you want to install it? (Y/n): " install_netplan
550- if [[ " $install_netplan " =~ ^[Yy]$ ]]; then
551- sudo apt install netplan -y
552- else
553- echo " Netplan is required to configure the static IP address using this script. Exiting."
554- exit 1
555- fi
560+ # Check if Netplan is installed, and if not, install it
561+ if ! [ -x " $( command -v netplan) " ]; then
562+ echo " Netplan is not installed. Installing..."
563+ sudo apt update && sudo apt install netplan -y
556564 fi
557565
558566 # Prompt installation of ifconfig
@@ -572,11 +580,11 @@ configure_static_ip() {
572580 echo " $device_info "
573581
574582 # Prompt the user to enter the desired network device
575- read -rp " Enter the network device name (e.g., enp5s0): " selected_device
583+ read -rp " Enter the network device name (e.g., enp5s0): " network_device
576584
577585 # Check if the selected device exists in the device information
578- if ! echo " $device_info " | grep -q " $selected_device :" ; then
579- echo " Error: The selected network device '$selected_device ' does not exist. Please enter a valid device name."
586+ if ! echo " $device_info " | grep -q " $network_device :" ; then
587+ echo " Error: The selected network device '$network_device ' does not exist. Please enter a valid device name."
580588 return
581589 fi
582590
@@ -598,7 +606,7 @@ configure_static_ip() {
598606network:
599607 version: 2
600608 ethernets:
601- $selected_device :
609+ $network_device :
602610 addresses: [$static_ip_address /$net_mask ]
603611 gateway4: $gateway
604612 nameservers:
608616 # Apply the Netplan configuration
609617 sudo netplan apply
610618
611- echo " Static IP address configuration completed for $selected_device ."
619+ echo " Static IP address configuration completed for $network_device ."
612620}
613621
614622# Main script
0 commit comments