-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_server.sh
More file actions
executable file
·80 lines (69 loc) · 2.28 KB
/
Copy pathsecure_server.sh
File metadata and controls
executable file
·80 lines (69 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
#
# This quickly secures a fresh ubuntu install
# Created script from: http://bit.ly/1XaKzBd
#######################################
# Uncomment, update, or append property:value pair in file
# Globals:
# None
# Arguments:
# prop - property to change
# val - new value for property
# file - filename to update
# Returns:
# None
#######################################
change_prop() {
prop="$1"
val="$2"
file="$3"
is_commented=$(egrep "^#*[[:space:]]*$prop$val[[:space:]]*$" "$file")
has_prop=$(egrep "^$prop\b" "$file")
if [[ $is_commented != "" ]]; then
sed -i.tmp "s/^#*[ ]*${prop}${val}.*$/${prop}${val}/im" "$file"
rm "$file.tmp"
elif [[ $has_prop != "" ]]; then
sed -i.tmp "s/^$prop\b.*$/${prop}${val}/im" "$file"
rm "$file.tmp"
else
echo "$prop$val" >> "$file"
fi
}
#######################################
# SSH
#######################################
echo "Disabling root login in ssh..."
if [ ! -f /etc/ssh/sshd_config ]; then
echo "sshd config file not found... exiting script."
exit 1
fi
change_prop PermitRootLogin " no" /etc/ssh/sshd_config
echo "Disabling password login to ssh..."
change_prop PasswordAuthentication " no" /etc/ssh/sshd_config
service ssh restart
echo "done"
#######################################
# /etc/sysctl.conf
#######################################
echo "Updating /etc/sysctl.conf..."
if [ ! -f /etc/sysctl.conf ]; then
echo "/etc/sysctl.conf not found, exiting."
exit 1
fi
change_prop "net.ipv4.conf.default.rp_filter" "=1" /etc/sysctl.conf
change_prop "net.ipv4.conf.all.rp_filter" "=1" /etc/sysctl.conf
change_prop "net.ipv4.conf.all.accept_redirects" " = 0" /etc/sysctl.conf
change_prop "net.ipv6.conf.all.accept_redirects" " = 0" /etc/sysctl.conf
change_prop "net.ipv4.conf.all.send_redirects" " = 0" /etc/sysctl.conf
change_prop "net.ipv4.conf.all.accept_source_route" " = 0" /etc/sysctl.conf
change_prop "net.ipv6.conf.all.accept_source_route" " = 0" /etc/sysctl.conf
echo "done."
#######################################
## FIREWALL
#######################################
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw enable
ufw status
exit 0