-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubuntu-v1.sh
343 lines (267 loc) · 8.63 KB
/
ubuntu-v1.sh
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/sh
# Declare Paths
SYS_PATH="/etc/sysctl.conf"
LIM_PATH="/etc/security/limits.conf"
PROF_PATH="/etc/profile"
SSH_PATH="/etc/ssh/sshd_config"
DNS_PATH="/etc/resolv.conf"
# Fix DNS
fix_dns() {
echo
echo "$(tput setaf 3)----- Optimizing System DNS Settings.$(tput sgr0)"
echo
sleep 1
sed -i '/nameserver/d' $DNS_PATH
echo 'nameserver 8.8.8.8' >> $DNS_PATH
echo 'nameserver 8.8.4.4' >> $DNS_PATH
echo
echo "$(tput setaf 2)----- System DNS Optimized.$(tput sgr0)"
echo
sleep 1
}
# Update & Upgrade & Remove & Clean
complete_update() {
echo
echo "$(tput setaf 3)----- Updating the System.$(tput sgr0)"
echo
sleep 1
sudo apt update
sudo apt -y upgrade
sleep 0.5
sudo apt -y dist-upgrade
sudo apt -y autoremove
sudo apt -y autoclean
sudo apt -y clean
echo
echo "$(tput setaf 2)----- System Updated Successfully.$(tput sgr0)"
echo
sleep 1
}
## Install useful packages
installations() {
echo
echo "$(tput setaf 3)----- Installing Useful Packeges.$(tput sgr0)"
echo
sleep 1
# Purge firewalld to install UFW.
sudo apt -y purge firewalld
# Install
sudo apt -y install software-properties-common build-essential apt-transport-https iptables iptables-persistent lsb-release ca-certificates ubuntu-keyring gnupg2 apt-utils cron bash-completion
sudo apt -y install curl git zip unzip ufw wget preload locales nano vim python3 python3-pip jq qrencode socat busybox net-tools haveged htop libssl-dev libsqlite3-dev dialog
sudo apt -y install binutils make automake autoconf libtool
sleep 0.5
echo
echo "$(tput setaf 2)----- Useful Packages Installed Succesfully.$(tput sgr0)"
echo
sleep 0.5
}
# Enable packages at server boot
enable_packages() {
sudo systemctl enable preload haveged cron
echo
echo "$(tput setaf 2)----- Packages Enabled Succesfully.$(tput sgr0)"
echo
sleep 0.5
}
## Swap Maker
swap_maker() {
echo
echo "$(tput setaf 3)----- Making SWAP Space.$(tput sgr0)"
echo
sleep 1
# 2 GB Swap Size
SWAP_SIZE=2G
# Default Swap Path
SWAP_PATH="/swapfile"
# Make Swap
sudo fallocate -l $SWAP_SIZE $SWAP_PATH # Allocate size
sudo chmod 600 $SWAP_PATH # Set proper permission
sudo mkswap $SWAP_PATH # Setup swap
sudo swapon $SWAP_PATH # Enable swap
echo "$SWAP_PATH none swap sw 0 0" >> /etc/fstab # Add to fstab
echo
echo $(tput setaf 2)----- SWAP Created Successfully.$(tput sgr0)
echo
sleep 0.5
}
# Remove Old SYSCTL Config to prevent duplicates.
remove_old_sysctl() {
sed -i '/fs.file-max/d' $SYS_PATH
sed -i '/fs.inotify.max_user_instances/d' $SYS_PATH
sed -i '/net.ipv4.tcp_syncookies/d' $SYS_PATH
sed -i '/net.ipv4.tcp_fin_timeout/d' $SYS_PATH
sed -i '/net.ipv4.tcp_tw_reuse/d' $SYS_PATH
sed -i '/net.ipv4.ip_local_port_range/d' $SYS_PATH
sed -i '/net.ipv4.tcp_max_syn_backlog/d' $SYS_PATH
sed -i '/net.ipv4.tcp_max_tw_buckets/d' $SYS_PATH
sed -i '/net.ipv4.route.gc_timeout/d' $SYS_PATH
sed -i '/net.ipv4.tcp_syn_retries/d' $SYS_PATH
sed -i '/net.ipv4.tcp_synack_retries/d' $SYS_PATH
sed -i '/net.core.somaxconn/d' $SYS_PATH
sed -i '/net.core.netdev_max_backlog/d' $SYS_PATH
sed -i '/net.ipv4.tcp_timestamps/d' $SYS_PATH
sed -i '/net.ipv4.tcp_max_orphans/d' $SYS_PATH
sed -i '/net.ipv4.ip_forward/d' $SYS_PATH
#IPv6
sed -i '/net.ipv6.conf.all.disable_ipv6/d' $SYS_PATH
sed -i '/net.ipv6.conf.default.disable_ipv6/d' $SYS_PATH
sed -i '/net.ipv6.conf.all.forwarding/d' $SYS_PATH
# System Limits.
sed -i '/soft/d' $LIM_PATH
sed -i '/hard/d' $LIM_PATH
# BBR
sed -i '/net.core.default_qdisc/d' $SYS_PATH
sed -i '/net.ipv4.tcp_congestion_control/d' $SYS_PATH
sed -i '/net.ipv4.tcp_ecn/d' $SYS_PATH
# uLimit
sed -i '/1000000/d' $PROF_PATH
#SWAP
sed -i '/vm.swappiness/d' $SYS_PATH
sed -i '/vm.vfs_cache_pressure/d' $SYS_PATH
}
## SYSCTL Optimization
sysctl_optimizations() {
echo
echo "$(tput setaf 3)----- Optimizing the Network.$(tput sgr0)"
echo
sleep 1
# Optimize Swap Settings
echo 'vm.swappiness=10' >> $SYS_PATH
echo 'vm.vfs_cache_pressure=50' >> $SYS_PATH
sleep 0.5
# Optimize Network Settings
echo 'fs.file-max = 1000000' >> $SYS_PATH
echo 'net.core.rmem_default = 1048576' >> $SYS_PATH
echo 'net.core.rmem_max = 2097152' >> $SYS_PATH
echo 'net.core.wmem_default = 1048576' >> $SYS_PATH
echo 'net.core.wmem_max = 2097152' >> $SYS_PATH
echo 'net.core.netdev_max_backlog = 16384' >> $SYS_PATH
echo 'net.core.somaxconn = 32768' >> $SYS_PATH
echo 'net.ipv4.tcp_fastopen = 3' >> $SYS_PATH
echo 'net.ipv4.tcp_mtu_probing = 1' >> $SYS_PATH
echo 'net.ipv4.tcp_retries2 = 8' >> $SYS_PATH
echo 'net.ipv4.tcp_slow_start_after_idle = 0' >> $SYS_PATH
echo 'net.ipv4.ip_forward = 1' | tee -a $SYS_PATH
echo 'tcp_keepalive_time=1800' >> $SYS_PATH
echo 'tcp_max_syn_backlog=1500' >> $SYS_PATH
echo 'net.ipv4.tcp_syn_retries =3' >> $SYS_PATH
echo 'net.ipv4.tcp_synack_retries=3' >> $SYS_PATH
echo 'tcp_max_syn_backlog=1500' >> $SYS_PATH
echo 'net.ipv4.tcp_max_orphans=4096' >> $SYS_PATH
echo 'net.ipv4.tcp_sack=0' >> $SYS_PATH
echo 'net.ipv6.conf.all.disable_ipv6 = 0' >> $SYS_PATH
echo 'net.ipv6.conf.default.disable_ipv6 = 0' >> $SYS_PATH
echo 'net.ipv6.conf.all.forwarding = 1' >> $SYS_PATH
# Use BBR
echo 'net.core.default_qdisc = fq' >> $SYS_PATH
echo 'net.ipv4.tcp_congestion_control = bbr' >> $SYS_PATH
sysctl -p
echo
echo $(tput setaf 2)----- Network is Optimized.$(tput sgr0)
echo
sleep 0.5
}
# Remove old SSH config to prevent duplicates.
remove_old_ssh_conf() {
# Make a backup of the original sshd_config file
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
echo
echo "$(tput setaf 2)----- Default SSH Config file Saved. Directory: /etc/ssh/sshd_config.bak$(tput sgr0)"
echo
sleep 1
# Disable DNS lookups for connecting clients
sed -i 's/#UseDNS yes/UseDNS no/' $SSH_PATH
# Enable compression for SSH connections
sed -i 's/#Compression no/Compression yes/' $SSH_PATH
# Remove less efficient encryption ciphers
sed -i 's/Ciphers .*/Ciphers aes256-ctr,chacha20-poly1305@openssh.com/' $SSH_PATH
# Remove these lines
sed -i '/MaxAuthTries/d' $SSH_PATH
sed -i '/MaxSessions/d' $SSH_PATH
sed -i '/TCPKeepAlive/d' $SSH_PATH
sed -i '/ClientAliveInterval/d' $SSH_PATH
sed -i '/ClientAliveCountMax/d' $SSH_PATH
sed -i '/AllowAgentForwarding/d' $SSH_PATH
sed -i '/PermitRootLogin/d' $SSH_PATH
sed -i '/AllowTcpForwarding/d' $SSH_PATH
sed -i '/GatewayPorts/d' $SSH_PATH
sed -i '/PermitTunnel/d' $SSH_PATH
}
## Update SSH config
update_sshd_conf() {
echo
echo "$(tput setaf 3)----- Optimizing SSH.$(tput sgr0)"
echo
sleep 1
# Enable TCP keep-alive messages
echo "TCPKeepAlive yes" | tee -a $SSH_PATH
# Configure client keep-alive messages
echo "ClientAliveInterval 3000" | tee -a $SSH_PATH
echo "ClientAliveCountMax 100" | tee -a $SSH_PATH
# Allow agent forwarding
echo "AllowAgentForwarding yes" | tee -a $SSH_PATH
#Permit Root Login
echo "PermitRootLogin yes" | tee -a $SSH_PATH
# Allow TCP forwarding
echo "AllowTcpForwarding yes" | tee -a $SSH_PATH
# Enable gateway ports
echo "GatewayPorts yes" | tee -a $SSH_PATH
# Enable tunneling
echo "PermitTunnel yes" | tee -a $SSH_PATH
# Restart the SSH service to apply the changes
service ssh restart
echo
echo $(tput setaf 2)----- SSH is Optimized.$(tput sgr0)
echo
}
# System Limits Optimizations
limits_optimizations() {
echo
echo "$(tput setaf 3)----- Optimizing System Limits.$(tput sgr0)"
echo
sleep 1
echo '* soft nproc 655350' >> $LIM_PATH
echo '* hard nproc 655350' >> $LIM_PATH
echo '* soft nofile 655350' >> $LIM_PATH
echo '* hard nofile 655350' >> $LIM_PATH
echo 'root soft nproc 655350' >> $LIM_PATH
echo 'root hard nproc 655350' >> $LIM_PATH
echo 'root soft nofile 655350' >> $LIM_PATH
echo 'root hard nofile 655350' >> $LIM_PATH
sudo sysctl -p
echo
echo $(tput setaf 2)----- System Limits Optimized.$(tput sgr0)
echo
sleep 0.5
}
# RUN BABY, RUN
fix_dns
sleep 0.5
complete_update
sleep 0.5
installations
sleep 0.5
enable_packages
sleep 0.5
swap_maker
sleep 0.5
remove_old_sysctl
sleep 0.5
sysctl_optimizations
sleep 0.5
remove_old_ssh_conf
sleep 0.5
update_sshd_conf
sleep 0.5
limits_optimizations
sleep 1
# Outro
echo
echo $(tput setaf 2)=========================$(tput sgr0)
echo "$(tput setaf 2)----- Done! Server is Optimized.$(tput sgr0)"
echo "$(tput setaf 3)----- Reboot in 5 seconds...$(tput sgr0)"
echo $(tput setaf 2)=========================$(tput sgr0)
sudo sleep 5 ; shutdown -r 0
echo
echo
echo