1
1
#! /bin/bash
2
2
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
+
3
10
# Function to display a menu and get user's choice
4
11
show_menu () {
5
12
clear
@@ -11,13 +18,13 @@ show_menu() {
11
18
echo " 3) Install Node Version Manager (NVM)"
12
19
echo " 4) Enable Passwordless sudo access"
13
20
echo " 5) Set up SSH key-based authentication"
21
+ echo " 6) Configure Static IP Address"
22
+ echo
23
+
14
24
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"
18
26
fi
19
- echo " 7) Configure Static IP Address"
20
- echo
27
+
21
28
echo " 0) Exit"
22
29
echo
23
30
read -rp " Enter your choice: " choice
@@ -27,12 +34,13 @@ show_menu() {
27
34
3) install_nvm ;;
28
35
4) enable_passwordless_sudo " $USER " ;;
29
36
5) setup_ssh_key_authentication ;;
30
- 7 ) configure_static_ip ;;
31
- 6 )
37
+ 6 ) configure_static_ip ;;
38
+ 9 )
32
39
if [ -f " /etc/ssh/sshd_config_decoscript.backup" ]; then
33
40
restore_ssh_config
34
41
else
35
- echo " SSH configuration backup is not available for restoration."
42
+ echo " Invalid choice. Please select a valid option."
43
+ show_menu
36
44
fi
37
45
;;
38
46
0)
@@ -74,77 +82,86 @@ install_essential_apps() {
74
82
if ! [ -x " $( command -v dialog) " ]; then
75
83
echo " Dialog is not installed. Installing dialog..."
76
84
sudo apt update && sudo apt install dialog -y
77
- fi
78
85
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
94
87
if [ $? -ne 0 ]; then
95
- clear
96
- echo " Canceled. Returning to the main menu."
88
+ echo " Error: Failed to install dialog. Exiting."
97
89
return
98
90
fi
91
+ fi
99
92
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)
118
102
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)
123
105
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
128
112
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
133
128
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
139
133
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
144
136
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."
146
140
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
148
165
}
149
166
150
167
# Function to configure the firewall with checks
@@ -231,17 +248,17 @@ setup_ssh_key_authentication() {
231
248
echo " #######################################################"
232
249
233
250
# 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
235
252
236
253
# Create the ~/.ssh directory if it doesn't exist
237
254
mkdir -p " $HOME /.ssh"
238
255
239
256
authorized_keys_file=" $HOME /.ssh/authorized_keys"
240
257
241
258
# 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
243
260
# 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 "
245
262
if [ $? -ne 0 ]; then
246
263
echo " Error: Failed to save the public key to authorized_keys file."
247
264
exit 1
@@ -250,7 +267,7 @@ setup_ssh_key_authentication() {
250
267
echo " Public key added to authorized_keys."
251
268
elif [ ! -f " $authorized_keys_file " ]; then
252
269
echo " Creating authorized_keys file..."
253
- echo " $user_public_key " > " $authorized_keys_file "
270
+ echo " $ssh_public_key " > " $authorized_keys_file "
254
271
if [ $? -ne 0 ]; then
255
272
echo " Error: Failed to create authorized_keys file."
256
273
exit 1
@@ -422,18 +439,14 @@ restore_ssh_config() {
422
439
fi
423
440
}
424
441
425
- # Function to walidate ports
442
+ # Function to validate if a given input is a valid port number
426
443
validate_port () {
427
444
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
436
448
fi
449
+ return 0 # Valid port
437
450
}
438
451
439
452
# Function to configure Git
@@ -517,13 +530,13 @@ configure_fail2ban() {
517
530
sudo apt install fail2ban -y
518
531
;;
519
532
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
521
534
522
535
# 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
524
537
# Install Fail2ban if not already installed
525
538
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 "
527
540
echo " User custom Fail2ban configuration applied."
528
541
else
529
542
echo " Warning: Invalid URL or unable to reach the URL. Using the default configuration."
@@ -544,15 +557,10 @@ configure_static_ip() {
544
557
clear
545
558
echo " Configuring a static IP address using Netplan."
546
559
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
556
564
fi
557
565
558
566
# Prompt installation of ifconfig
@@ -572,11 +580,11 @@ configure_static_ip() {
572
580
echo " $device_info "
573
581
574
582
# 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
576
584
577
585
# 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."
580
588
return
581
589
fi
582
590
@@ -598,7 +606,7 @@ configure_static_ip() {
598
606
network:
599
607
version: 2
600
608
ethernets:
601
- $selected_device :
609
+ $network_device :
602
610
addresses: [$static_ip_address /$net_mask ]
603
611
gateway4: $gateway
604
612
nameservers:
608
616
# Apply the Netplan configuration
609
617
sudo netplan apply
610
618
611
- echo " Static IP address configuration completed for $selected_device ."
619
+ echo " Static IP address configuration completed for $network_device ."
612
620
}
613
621
614
622
# Main script
0 commit comments