1515
1616config_file_bak=" /etc/ssh/sshd_config.bak"
1717config_file=" /etc/ssh/sshd_config"
18- green=$ '\033 [0;32m'
19- cyan=$ '\033 [0;36m'
20- red=$ '\033 [1;31m'
21- nc=$ '\033 [0m'
18+ green=" $( printf ' \033[0;32m' ) "
19+ cyan=" $( printf ' \033[0;36m' ) "
20+ red=" $( printf ' \033[1;31m' ) "
21+ nc=" $( printf ' \033[0m' ) "
2222
2323
2424# #### End of [ Variables ]
@@ -28,15 +28,15 @@ nc=$'\033[0m'
2828
2929# # Check if the script was executed with root privilege.
3030if [[ $EUID != 0 ]]; then
31- echo " ${red} Please run this script as or with root privilege$nc " >&2
31+ echo " ${red} Please run this script as or with root privilege${nc} " >&2
3232 echo -e " \nExiting..."
3333 exit 2
3434fi
3535
3636# # Confirm that 'sshd_config' exists.
3737if [[ ! -f $config_file ]]; then
3838 echo " ${red} 'sshd_config' doesn't exist" >&2
39- echo " ${cyan} openssh-server may not be installed$nc "
39+ echo " ${cyan} openssh-server may not be installed${nc} "
4040 echo -e " \nExiting..."
4141 exit 3
4242fi
@@ -60,7 +60,7 @@ if [[ -f $config_file_bak ]]; then
6060 rm $config_file_bak && cp $config_file $config_file_bak || {
6161 echo " ${red} Failed to back up sshd_config" >&2
6262 echo " ${cyan} Please create a backup of the original 'sshd_config'" \
63- " before continuing$nc "
63+ " before continuing${nc} "
6464 exit 1
6565 }
6666 ;;
7070 cp $config_file $config_file_bak || {
7171 echo " ${red} Failed to back up sshd_config" >&2
7272 echo " ${cyan} Please create a backup of the original 'sshd_config' before" \
73- " continuing$nc "
73+ " continuing${nc} "
7474 exit 1
7575 }
7676fi
@@ -80,143 +80,143 @@ if grep -Eq '^LogLevel VERBOSE$' "$config_file"; then
8080elif grep -Eq ' ^#?LogLevel(.*)?$' " $config_file " ; then
8181 echo " Setting 'LogLevel VERBOSE'..."
8282 sed -Ei ' s/^#?LogLevel(.*)?$/LogLevel VERBOSE/gm' " $config_file " \
83- || echo " ${red} Failed to set 'LogLevel VERBOSE'$nc "
83+ || echo " ${red} Failed to set 'LogLevel VERBOSE'${nc} "
8484fi
8585
8686if grep -Eq ' ^LoginGraceTime 30$' " $config_file " ; then
8787 echo " LoginGraceTime already set to '30'"
8888elif grep -Eq ' ^#?LoginGraceTime(.*)?$' " $config_file " ; then
8989 echo " Setting 'LoginGraceTime 30'..."
9090 sed -Ei ' s/^#?LoginGraceTime(.*)?$/LoginGraceTime 30/gm' " $config_file " \
91- || echo " ${red} Failed to set 'LoginGraceTime 30'$nc "
91+ || echo " ${red} Failed to set 'LoginGraceTime 30'${nc} "
9292fi
9393
9494if grep -Eq ' ^PermitRootLogin no$' " $config_file " ; then
9595 echo " PermitRootLogin already set to 'no'"
9696elif grep -Eq ' ^#?PermitRootLogin(.*)?$' " $config_file " ; then
9797 echo " Setting 'PermitRootLogin no'..."
9898 sed -Ei ' s/^#?PermitRootLogin(.*)?$/PermitRootLogin no/gm' " $config_file " \
99- || echo " ${red} Failed to set 'PermitRootLogin no'$nc "
99+ || echo " ${red} Failed to set 'PermitRootLogin no'${nc} "
100100fi
101101
102102if grep -Eq ' ^MaxAuthTries 3$' " $config_file " ; then
103103 echo " MaxAuthTries already set to '3'"
104104elif grep -Eq ' ^#?MaxAuthTries(.*)?$' " $config_file " ; then
105105 echo " Setting 'MaxAuthTries 3'..."
106106 sed -Ei ' s/^#?MaxAuthTries(.*)?$/MaxAuthTries 3/gm' " $config_file " \
107- || echo " ${red} Failed to set 'MaxAuthTries 3'$nc "
107+ || echo " ${red} Failed to set 'MaxAuthTries 3'${nc} "
108108fi
109109
110110if grep -Eq ' ^MaxSessions 2$' " $config_file " ; then
111111 echo " MaxSessions already set to '2'"
112112elif grep -Eq ' ^#?MaxSessions(.*)?$' " $config_file " ; then
113113 echo " Setting 'MaxSessions 2'..."
114114 sed -Ei ' s/^#?MaxSessions(.*)?$/MaxSessions 2/gm' " $config_file " \
115- || echo " ${red} Failed to set 'MaxSessions 2'$nc "
115+ || echo " ${red} Failed to set 'MaxSessions 2'${nc} "
116116fi
117117
118118if grep -Eq ' ^PubkeyAuthentication yes$' " $config_file " ; then
119119 echo " PubkeyAuthentication already set to 'yes'"
120120elif grep -Eq ' ^#?PubkeyAuthentication(.*)?$' " $config_file " ; then
121121 echo " Setting 'PubkeyAuthentication yes'..."
122122 sed -Ei ' s/^#?PubkeyAuthentication(.*)?$/PubkeyAuthentication yes/gm' " $config_file " \
123- || echo " ${red} Failed to set 'PubkeyAuthentication yes'$nc "
123+ || echo " ${red} Failed to set 'PubkeyAuthentication yes'${nc} "
124124fi
125125
126126if grep -Eq ' ^PermitEmptyPasswords no$' " $config_file " ; then
127127 echo " PermitEmptyPasswords already set to 'no'"
128128elif grep -Eq ' ^#?PermitEmptyPasswords(.*)?$' " $config_file " ; then
129129 echo " Setting 'PermitEmptyPasswords no'..."
130130 sed -Ei ' s/^#?PermitEmptyPasswords(.*)?$/PermitEmptyPasswords no/gm' " $config_file " \
131- || echo " ${red} Failed to set 'PermitEmptyPasswords no'$nc "
131+ || echo " ${red} Failed to set 'PermitEmptyPasswords no'${nc} "
132132fi
133133
134134if grep -Eq ' ^ChallengeResponseAuthentication no$' " $config_file " ; then
135135 echo " ChallengeResponseAuthentication already set to 'no'"
136136elif grep -Eq ' ^#?ChallengeResponseAuthentication(.*)?$' " $config_file " ; then
137137 echo " Setting 'ChallengeResponseAuthentication no'..."
138138 sed -Ei ' s/^#?ChallengeResponseAuthentication(.*)?$/ChallengeResponseAuthentication no/gm' " $config_file " \
139- || echo " ${red} Failed to set 'ChallengeResponseAuthentication no'$nc "
139+ || echo " ${red} Failed to set 'ChallengeResponseAuthentication no'${nc} "
140140fi
141141
142142if grep -Eq ' ^KbdInteractiveAuthentication no$' " $config_file " ; then
143143 echo " KbdInteractiveAuthentication already set to 'no'"
144144elif grep -Eq ' ^#?KbdInteractiveAuthentication(.*)?$' " $config_file " ; then
145145 echo " Setting 'KbdInteractiveAuthentication no'..."
146146 sed -Ei ' s/^#?KbdInteractiveAuthentication(.*)?$/KbdInteractiveAuthentication no/gm' " $config_file " \
147- || echo " ${red} Failed to set 'KbdInteractiveAuthentication no'$nc "
147+ || echo " ${red} Failed to set 'KbdInteractiveAuthentication no'${nc} "
148148fi
149149
150150if grep -Eq ' ^UsePAM yes$' " $config_file " ; then
151151 echo " UsePAM already set to 'yes'"
152152elif grep -Eq ' ^#?UsePAM(.*)?$' " $config_file " ; then
153153 echo " Setting 'UsePAM yes'..."
154154 sed -Ei ' s/^#?UsePAM(.*)?$/UsePAM yes/gm' " $config_file " \
155- || echo " ${red} Failed to set 'UsePAM yes'$nc "
155+ || echo " ${red} Failed to set 'UsePAM yes'${nc} "
156156fi
157157
158158if grep -Eq ' ^AllowAgentForwarding no$' " $config_file " ; then
159159 echo " AllowAgentForwarding already set to 'no'"
160160elif grep -Eq ' ^#?AllowAgentForwarding(.*)?$' " $config_file " ; then
161161 echo " Setting 'AllowAgentForwarding no'..."
162162 sed -Ei ' s/^#?AllowAgentForwarding(.*)?$/AllowAgentForwarding no/gm' " $config_file " \
163- || echo " ${red} Failed to set 'AllowAgentForwarding no'$nc "
163+ || echo " ${red} Failed to set 'AllowAgentForwarding no'${nc} "
164164fi
165165
166166if grep -Eq ' ^AllowTcpForwarding no$' " $config_file " ; then
167167 echo " AllowTcpForwarding already set to 'no'"
168168elif grep -Eq ' ^#?AllowTcpForwarding(.*)?$' " $config_file " ; then
169169 echo " Setting 'AllowTcpForwarding no'..."
170170 sed -Ei ' s/^#?AllowTcpForwarding(.*)?$/AllowTcpForwarding no/gm' " $config_file " \
171- || echo " ${red} Failed to set 'AllowTcpForwarding no'$nc "
171+ || echo " ${red} Failed to set 'AllowTcpForwarding no'${nc} "
172172fi
173173
174174if grep -Eq ' ^X11Forwarding no$' " $config_file " ; then
175175 echo " X11Forwarding already set to 'no'"
176176elif grep -Eq ' ^#?X11Forwarding(.*)?$' " $config_file " ; then
177177 echo " Setting 'X11Forwarding no'..."
178178 sed -Ei ' s/^#?X11Forwarding(.*)?$/X11Forwarding no/gm' " $config_file " \
179- || echo " ${red} Failed to set 'X11Forwarding no'$nc "
179+ || echo " ${red} Failed to set 'X11Forwarding no'${nc} "
180180fi
181181
182182if grep -Eq ' ^PrintMotd no$' " $config_file " ; then
183183 echo " PrintMotd already set to 'no'"
184184elif grep -Eq ' ^#?PrintMotd(.*)?$' " $config_file " ; then
185185 echo " Setting 'PrintMotd no'..."
186186 sed -Ei ' s/^#?PrintMotd(.*)?$/PrintMotd no/gm' " $config_file " \
187- || echo " ${red} Failed to set 'PrintMotd no'$nc "
187+ || echo " ${red} Failed to set 'PrintMotd no'${nc} "
188188fi
189189
190190if grep -Eq ' ^TCPKeepAlive no$' " $config_file " ; then
191191 echo " TCPKeepAlive already set to 'no'"
192192elif grep -Eq ' ^#?TCPKeepAlive(.*)?$' " $config_file " ; then
193193 echo " Setting 'TCPKeepAlive no'..."
194194 sed -Ei ' s/^#?TCPKeepAlive(.*)?$/TCPKeepAlive no/gm' " $config_file " \
195- || echo " ${red} Failed to set 'TCPKeepAlive no'$nc "
195+ || echo " ${red} Failed to set 'TCPKeepAlive no'${nc} "
196196fi
197197
198198if grep -Eq ' ^Compression no$' " $config_file " ; then
199199 echo " Compression already set to 'no'"
200200elif grep -Eq ' ^#?Compression(.*)?$' " $config_file " ; then
201201 echo " Setting 'Compression no'..."
202202 sed -Ei ' s/^#?Compression(.*)?$/Compression no/gm' " $config_file " \
203- || echo " ${red} Failed to set 'Compression no'$nc "
203+ || echo " ${red} Failed to set 'Compression no'${nc} "
204204fi
205205
206206if grep -Eq ' ^ClientAliveInterval 300$' " $config_file " ; then
207207 echo " ClientAliveInterval already set to '300'"
208208elif grep -Eq ' ^#?ClientAliveInterval(.*)?$' " $config_file " ; then
209209 echo " Setting 'ClientAliveInterval 300'..."
210210 sed -Ei ' s/^#?ClientAliveInterval(.*)?$/ClientAliveInterval 300/gm' " $config_file " \
211- || echo " ${red} Failed to set 'ClientAliveInterval 300'$nc "
211+ || echo " ${red} Failed to set 'ClientAliveInterval 300'${nc} "
212212fi
213213
214214if grep -Eq ' ^ClientAliveCountMax 2$' " $config_file " ; then
215215 echo " UseClientAliveCountMaxPAM already set to '2'"
216216elif grep -Eq ' ^#?ClientAliveCountMax(.*)?$' " $config_file " ; then
217217 echo " Setting 'ClientAliveCountMax 2'..."
218218 sed -Ei ' s/^#?ClientAliveCountMax(.*)?$/ClientAliveCountMax 2/gm' " $config_file " \
219- || echo " ${red} Failed to set 'ClientAliveCountMax 2'$nc "
219+ || echo " ${red} Failed to set 'ClientAliveCountMax 2'${nc} "
220220fi
221221
222222echo -e " \nRestarting sshd..."
@@ -226,7 +226,7 @@ echo -e "\n${green}Finished hardening sshd"
226226echo -e " ${cyan} It is highly recommended to manually:
2272271) Change the default sshd port (22)
2282282) Disable PasswordAuthentication in favor of PubkeyAuthentication
229- 3) Add 'AllowUsers [your username]' to the bottom of 'sshd_config'$nc "
229+ 3) Add 'AllowUsers [your username]' to the bottom of 'sshd_config'${nc} "
230230
231231
232232# ### End of [ Main ]
0 commit comments