11#! /bin/bash
22#
3- # Hardens sshd by modifying '/etc/ssh/sshd_config'
3+ # Hardens sshd by modifying '/etc/ssh/sshd_config'.
44#
5- # Note: This configures sshd_config to the recommendations of the security
6- # auditing tool knonw as Lynis (https://github.com/CISOfy/lynis)
5+ # Note: This configures sshd_config to the recommendations of the security auditing tool
6+ # knonw as Lynis (https://github.com/CISOfy/lynis).
77#
88# Version: v1.0.3
99# License: MIT License
10- # Copyright (c) 2020-2021 Hunter T.
10+ # Copyright (c) 2020-2021 Hunter T.
1111#
12- # ###############################################################################
12+ # #######################################################################################
1313# ### [ Variables ]
1414
1515
@@ -21,125 +21,125 @@ nc=$'\033[0m'
2121
2222
2323# #### End of [ Variables ]
24- # ###############################################################################
24+ # #######################################################################################
2525# ### [ Prepping ]
2626
2727
28- # Checks to see if this script was executed with root privilege
28+ # # Check if the script was executed with root privilege.
2929if [[ $EUID != 0 ]]; then
30- echo " ${red} Please run this script as or with root privilege${nc} " >&2
30+ echo " ${red} Please run this script as or with root privilege$nc " >&2
3131 echo -e " \nExiting..."
3232 exit 1
3333fi
3434
35- # Confirms that sshd_config exists
35+ # # Confirm that ' sshd_config' exists.
3636if [[ ! -f $config_file ]]; then
3737 echo " ${red} 'sshd_config' doesn't exist" >&2
38- echo " ${cyan} sshd-server may not be installed${nc} "
38+ echo " ${cyan} sshd-server may not be installed$nc "
3939 echo -e " \nExiting..."
4040 exit 1
4141fi
4242
4343
4444# ### End of [ Prepping ]
45- # ###############################################################################
45+ # #######################################################################################
4646# ### [ Main ]
4747
4848
4949read -rp " We will now harden sshd. Press [Enter] to continue."
5050
51- # Only backs up the original sshd_config
51+ # # Backup 'sshd_config' if 'sshd_config.bak' doesn't already exist.
5252if [[ ! -f $config_file_bak ]]; then
5353 echo " Backing up original 'sshd_config'..."
5454 cp $config_file $config_file_bak || {
5555 echo " ${red} Failed to back up sshd_config" >&2
5656 echo " ${cyan} Please create a backup of the original 'sshd_config'" \
57- " before continuing${nc} "
57+ " before continuing$nc "
5858 exit 1
5959 }
6060fi
6161
6262echo " Setting LogLevel VERBOSE..."
63- sed -i ' s/\(#\)\?LogLevel\(.*\)\?/LogLevel VERBOSE/g' " $config_file " ||
64- echo " ${red} Failed to set LogLevel VERBOSE${nc} "
63+ sed -i ' s/\(#\)\?LogLevel\(.*\)\?/LogLevel VERBOSE/g' " $config_file " \
64+ || echo " ${red} Failed to set LogLevel VERBOSE$nc "
6565
6666echo " Setting LoginGraceTime 30..."
67- sed -i ' s/\(#\)\?LoginGraceTime\(.*\)\?/LoginGraceTime 30/g' " $config_file " ||
68- echo " ${red} Failed to set LoginGraceTime 30${nc} "
67+ sed -i ' s/\(#\)\?LoginGraceTime\(.*\)\?/LoginGraceTime 30/g' " $config_file " \
68+ || echo " ${red} Failed to set LoginGraceTime 30$nc "
6969
7070echo " Setting PermitRootLogin no..."
71- sed -i ' s/\(#\)\?PermitRootLogin\(.*\)\?/PermitRootLogin no/g' " $config_file " ||
72- echo " ${red} Failed to set PermitRootLogin no${nc} "
71+ sed -i ' s/\(#\)\?PermitRootLogin\(.*\)\?/PermitRootLogin no/g' " $config_file " \
72+ || echo " ${red} Failed to set PermitRootLogin no$nc "
7373
7474echo " Setting MaxAuthTries 3..."
75- sed -i ' s/\(#\)\?MaxAuthTries\(.*\)\?/MaxAuthTries 3/g' " $config_file " ||
76- echo " ${red} Failed to set MaxAuthTries 3${nc} "
75+ sed -i ' s/\(#\)\?MaxAuthTries\(.*\)\?/MaxAuthTries 3/g' " $config_file " \
76+ || echo " ${red} Failed to set MaxAuthTries 3$nc "
7777
7878echo " Setting MaxSessions 2..."
79- sed -i ' s/\(#\)\?MaxSessions\(.*\)\?/MaxSessions 2/g' " $config_file " ||
80- echo " ${red} Failed to set MaxSessions 2${nc} "
79+ sed -i ' s/\(#\)\?MaxSessions\(.*\)\?/MaxSessions 2/g' " $config_file " \
80+ || echo " ${red} Failed to set MaxSessions 2$nc "
8181
8282echo " Setting PubkeyAuthentication yes..."
83- sed -i ' s/\(#\)\?PubkeyAuthentication\(.*\)\?/PubkeyAuthentication yes/g' " $config_file " ||
84- echo " ${red} Failed to set PubkeyAuthentication yes${nc} "
83+ sed -i ' s/\(#\)\?PubkeyAuthentication\(.*\)\?/PubkeyAuthentication yes/g' " $config_file " \
84+ || echo " ${red} Failed to set PubkeyAuthentication yes$nc "
8585
8686# Uncomment only if an ssh key has been set
8787# echo "Setting PasswordAuthentication no..."
88- # sed -i 's/\(#\)\?PasswordAuthentication\(.*\)\?/PasswordAuthentication no/g' "$config_file" ||
89- # echo "${red}Failed to set PasswordAuthentication no${nc} "
88+ # sed -i 's/\(#\)\?PasswordAuthentication\(.*\)\?/PasswordAuthentication no/g' "$config_file" \
89+ # || echo "${red}Failed to set PasswordAuthentication no$nc "
9090
9191echo " Setting PermitEmptyPasswords no..."
92- sed -i ' s/\(#\)\?PermitEmptyPasswords\(.*\)\?/PermitEmptyPasswords no/g' " $config_file " ||
93- echo " ${red} Failed to set PermitEmptyPasswords no${nc} "
92+ sed -i ' s/\(#\)\?PermitEmptyPasswords\(.*\)\?/PermitEmptyPasswords no/g' " $config_file " \
93+ || echo " ${red} Failed to set PermitEmptyPasswords no$nc "
9494
9595echo " Setting ChallengeResponseAuthentication no..."
9696sed -i ' s/\(#\)\?ChallengeResponseAuthentication\(.*\)\?/ChallengeResponseAuthentication no/g' \
97- " $config_file " || echo " ${red} Failed to set ChallengeResponseAuthentication no${nc} "
97+ " $config_file " \ || echo " ${red} Failed to set ChallengeResponseAuthentication no$nc "
9898
9999echo " Setting UsePAM yes..."
100- sed -i ' s/\(#\)\?UsePAM\(.*\)\?/UsePAM yes/g' " $config_file " ||
101- echo " ${red} Failed to set UsePAM yes${nc} "
100+ sed -i ' s/\(#\)\?UsePAM\(.*\)\?/UsePAM yes/g' " $config_file " \
101+ || echo " ${red} Failed to set UsePAM yes$nc "
102102
103103echo " Setting AllowAgentForwarding no..."
104- sed -i ' s/\(#\)\?AllowAgentForwarding\(.*\)\?/AllowAgentForwarding no/g' " $config_file " ||
105- echo " ${red} Failed to set AllowAgentForwarding no${nc} "
104+ sed -i ' s/\(#\)\?AllowAgentForwarding\(.*\)\?/AllowAgentForwarding no/g' " $config_file " \
105+ || echo " ${red} Failed to set AllowAgentForwarding no$nc "
106106
107107echo " Setting AllowTcpForwarding no..."
108- sed -i ' s/\(#\)\?AllowTcpForwarding\(.*\)\?/AllowTcpForwarding no/g' " $config_file " ||
109- echo " ${red} Failed to set AllowTcpForwarding no${nc} "
108+ sed -i ' s/\(#\)\?AllowTcpForwarding\(.*\)\?/AllowTcpForwarding no/g' " $config_file " \
109+ || echo " ${red} Failed to set AllowTcpForwarding no$nc "
110110
111111echo " Setting X11Forwarding no..."
112- sed -i ' s/\(#\)\?X11Forwarding\(.*\)\?/X11Forwarding no/g' " $config_file " ||
113- echo " ${red} Failed to set X11Forwarding no${nc} "
112+ sed -i ' s/\(#\)\?X11Forwarding\(.*\)\?/X11Forwarding no/g' " $config_file " \
113+ || echo " ${red} Failed to set X11Forwarding no$nc "
114114
115115echo " Setting PrintMotd no..."
116- sed -i ' s/\(#\)\?PrintMotd\(.*\)\?/PrintMotd no/g' " $config_file " ||
117- echo " ${red} Failed to set PrintMotd no${nc} "
116+ sed -i ' s/\(#\)\?PrintMotd\(.*\)\?/PrintMotd no/g' " $config_file " \
117+ || echo " ${red} Failed to set PrintMotd no$nc "
118118
119119echo " Setting TCPKeepAlive no..."
120- sed -i ' s/\(#\)\?TCPKeepAlive\(.*\)\?/TCPKeepAlive no/g' " $config_file " ||
121- echo " ${red} Failed to set TCPKeepAlive no${nc} "
120+ sed -i ' s/\(#\)\?TCPKeepAlive\(.*\)\?/TCPKeepAlive no/g' " $config_file " \
121+ || echo " ${red} Failed to set TCPKeepAlive no$nc "
122122
123123echo " Setting Compression no..."
124- sed -i ' s/\(#\)\?Compression\(.*\)\?/Compression no/g' " $config_file " ||
125- echo " ${red} Failed to set Compression no${nc} "
124+ sed -i ' s/\(#\)\?Compression\(.*\)\?/Compression no/g' " $config_file " \
125+ || echo " ${red} Failed to set Compression no$nc "
126126
127127echo " Setting ClientAliveInterval 300..."
128- sed -i ' s/\(#\)\?ClientAliveInterval\(.*\)\?/ClientAliveInterval 300/g' " $config_file " ||
129- echo " ${red} Failed to set ClientAliveInterval 30${nc} "
128+ sed -i ' s/\(#\)\?ClientAliveInterval\(.*\)\?/ClientAliveInterval 300/g' " $config_file " \
129+ || echo " ${red} Failed to set ClientAliveInterval 30$nc "
130130
131131echo " Setting ClientAliveCountMax 2..."
132- sed -i ' s/\(#\)\?ClientAliveCountMax\(.*\)\?/ClientAliveCountMax 2/g' " $config_file " ||
133- echo " ${red} Failed to set ClientAliveCountMax 2${nc} "
132+ sed -i ' s/\(#\)\?ClientAliveCountMax\(.*\)\?/ClientAliveCountMax 2/g' " $config_file " \
133+ || echo " ${red} Failed to set ClientAliveCountMax 2$nc "
134134
135135echo -e " \nRestarting sshd..."
136136systemctl restart sshd
137137
138138echo -e " \nDone"
139- echo -e " ${cyan} NOTE: It is highly recommended to manually:\n1) Change" \
140- " sshd default port (22) to something else\n2) Add 'AllowUsers [your" \
141- " username]' to the bottom of 'sshd_config'${nc} "
139+ echo -e " ${cyan} NOTE: It is highly recommended to manually:\n1) Change sshd default " \
140+ " port (22) to something else\n2) Add 'AllowUsers [your username]' to the bottom " \
141+ " of 'sshd_config'$nc "
142142
143143
144144# ### End of [ Main ]
145- # ###############################################################################
145+ # #######################################################################################
0 commit comments