forked from hawshemi/Linux-Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-optimizer.sh
275 lines (221 loc) · 5.94 KB
/
linux-optimizer.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
#!/bin/bash
clear
# Green, Yellow & Red Messages.
green_msg() {
tput setaf 2
echo "[*] ----- $1"
tput sgr0
}
yellow_msg() {
tput setaf 3
echo "[*] ----- $1"
tput sgr0
}
red_msg() {
tput setaf 1
echo "[*] ----- $1"
tput sgr0
}
# Paths
HOST_PATH="/etc/hosts"
DNS_PATH="/etc/resolv.conf"
# Intro
echo
green_msg '================================================================='
green_msg 'This script will automatically Optimize your Linux Server.'
green_msg 'Tested on: Ubuntu 20+, Debian 11+, CentOS stream 8+, AlmaLinux 8+, Fedora 37+'
green_msg 'Root access is required.'
green_msg 'Source is @ https://github.com/hawshemi/linux-optimizer'
green_msg '================================================================='
echo
# Root
check_if_running_as_root() {
# If you want to run as another user, please modify $EUID to be owned by this user
if [[ "$EUID" -ne '0' ]]; then
echo
red_msg 'Error: You must run this script as root!'
echo
sleep 0.5
exit 1
fi
}
# Check Root
check_if_running_as_root
sleep 0.5
# Install dependencies
install_dependencies_debian_based() {
echo
yellow_msg 'Installing Dependencies...'
echo
sleep 0.5
apt update -q
apt install -y wget curl sudo jq
echo
green_msg 'Dependencies Installed.'
echo
sleep 0.5
}
# Install dependencies
install_dependencies_rhel_based() {
echo
yellow_msg 'Installing Dependencies...'
echo
sleep 0.5
# dnf up -y
dnf install -y wget curl sudo jq
echo
green_msg 'Dependencies Installed.'
echo
sleep 0.5
}
# Fix Hosts file
fix_etc_hosts(){
echo
yellow_msg "Fixing Hosts file."
sleep 0.5
cp $HOST_PATH /etc/hosts.bak
yellow_msg "Default hosts file saved. Directory: /etc/hosts.bak"
sleep 0.5
if ! grep -q $(hostname) $HOST_PATH; then
echo "127.0.1.1 $(hostname)" | sudo tee -a $HOST_PATH > /dev/null
green_msg "Hosts Fixed."
echo
sleep 0.5
else
green_msg "Hosts OK. No changes made."
echo
sleep 0.5
fi
}
# Fix DNS
fix_dns(){
echo
yellow_msg "Fixing DNS Temporarily."
sleep 0.5
cp $DNS_PATH /etc/resolv.conf.bak
yellow_msg "Default resolv.conf file saved. Directory: /etc/resolv.conf.bak"
sleep 0.5
sed -i '/nameserver/d' $DNS_PATH
echo "nameserver 77.88.8.8" >> $DNS_PATH
echo "nameserver 1.1.1.1" >> $DNS_PATH
green_msg "DNS Fixed Temporarily."
echo
sleep 0.5
}
# Timezone
set_timezone() {
echo
yellow_msg 'Setting TimeZone based on VPS IP address...'
sleep 0.5
get_public_ip() {
local ip_sources=("https://ipv4.icanhazip.com" "https://api.ipify.org" "https://ipv4.ident.me/")
local ip
for source in "${ip_sources[@]}"; do
ip=$(curl -s "$source")
if [ -n "$ip" ]; then
echo "$ip"
return 0
fi
done
red_msg "Error: Failed to fetch public IP address from known sources. Moving on..."
return 1
}
public_ip=$(get_public_ip)
if [ $? -eq 0 ]; then
location_info=$(curl -s "http://ip-api.com/json/$public_ip")
timezone=$(echo "$location_info" | jq -r '.timezone')
sudo timedatectl set-timezone "$timezone"
green_msg "Timezone set to $timezone"
else
red_msg "Error: Failed to fetch public IP address from known sources. Moving on..."
fi
echo
sleep 0.5
}
# OS Detection
if [[ $(grep -oP '(?<=^NAME=").*(?=")' /etc/os-release) == "Ubuntu" ]]; then
OS="ubuntu"
echo
sleep 0.5
yellow_msg "OS: Ubuntu"
echo
sleep 0.5
elif [[ $(grep -oP '(?<=^NAME=").*(?=")' /etc/os-release) == "Debian GNU/Linux" ]]; then
OS="debian"
echo
sleep 0.5
yellow_msg "OS: Debian"
echo
sleep 0.5
elif [[ $(grep -oP '(?<=^NAME=").*(?=")' /etc/os-release) == "CentOS Stream" ]]; then
OS="centos"
echo
sleep 0.5
yellow_msg "OS: Centos Stream"
echo
sleep 0.5
elif [[ $(grep -oP '(?<=^NAME=").*(?=")' /etc/os-release) == "AlmaLinux" ]]; then
OS="almalinux"
echo
sleep 0.5
yellow_msg "OS: AlmaLinux"
echo
sleep 0.5
elif [[ $(grep -oP '(?<=^NAME=").*(?=")' /etc/os-release) == "Fedora Linux" ]]; then
OS="fedora"
echo
sleep 0.5
yellow_msg "OS: Fedora"
echo
sleep 0.5
else
echo
sleep 0.5
red_msg "Unknown OS, Create an issue here: https://github.com/hawshemi/Linux-Optimizer"
OS="unknown"
echo
sleep 2
fi
## Run
# Install dependencies
if [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
install_dependencies_debian_based
elif [[ "$OS" == "centos" || "$OS" == "fedora" || "$OS" == "almalinux" ]]; then
install_dependencies_rhel_based
fi
# Fix Hosts file
fix_etc_hosts
sleep 0.5
# Fix DNS
fix_dns
sleep 0.5
# Timezone
set_timezone
sleep 0.5
# Run Script based on Distros
case $OS in
ubuntu)
# Ubuntu
wget "https://raw.githubusercontent.com/dpteam/Linux-Optimizer/main/scripts/ubuntu-optimizer.sh" -q -O ubuntu-optimizer.sh && chmod +x ubuntu-optimizer.sh && bash ubuntu-optimizer.sh
;;
debian)
# Debian
wget "https://raw.githubusercontent.com/dpteam/Linux-Optimizer/main/scripts/debian-optimizer.sh" -q -O debian-optimizer.sh && chmod +x debian-optimizer.sh && bash debian-optimizer.sh
;;
centos)
# CentOS
wget "https://raw.githubusercontent.com/dpteam/Linux-Optimizer/main/scripts/centos-optimizer.sh" -q -O centos-optimizer.sh && chmod +x centos-optimizer.sh && bash centos-optimizer.sh
;;
almalinux)
# AlmaLinux
wget "https://raw.githubusercontent.com/dpteam/Linux-Optimizer/main/scripts/centos-optimizer.sh" -q -O almalinux-optimizer.sh && chmod +x almalinux-optimizer.sh && bash almalinux-optimizer.sh
;;
fedora)
# Fedora
wget "https://raw.githubusercontent.com/dpteam/Linux-Optimizer/main/scripts/fedora-optimizer.sh" -q -O fedora-optimizer.sh && chmod +x fedora-optimizer.sh && bash fedora-optimizer.sh
;;
unknown)
# Unknown
exit
;;
esac