init.sh
· 12 KiB · Bash
Raw
#!/bin/bash
#==========================
# Set up the environment
#==========================
set -e # exit on error
set -o pipefail # exit on pipeline error
set -u # treat unset variable as error
#==========================
# Basic Information
#==========================
export LC_ALL=C
export LANG=en_US.UTF-8
export DEBIAN_FRONTEND=noninteractive
export SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
#==========================
# Color
#==========================
Green="\033[32m"
Red="\033[31m"
Yellow="\033[33m"
Blue="\033[36m"
Font="\033[0m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
OK="${Green}[ OK ]${Font}"
ERROR="${Red}[FAILED]${Font}"
WARNING="${Yellow}[ WARN ]${Font}"
#==========================
# Print Colorful Text
#==========================
function print_ok() {
echo -e "${OK} ${Blue} $1 ${Font}"
}
function print_error() {
echo -e "${ERROR} ${Red} $1 ${Font}"
}
function print_warn() {
echo -e "${WARNING} ${Yellow} $1 ${Font}"
}
#==========================
# Judge function
#==========================
function judge() {
if [[ 0 -eq $? ]]; then
print_ok "$1 succeeded"
sleep 0.2
else
print_error "$1 failed"
exit 1
fi
}
prepare_host()
{
print_ok "Update apt-get"
sudo apt-get update
judge "Update apt-get"
print_ok "Install sshpass"
sudo apt-get install -y sshpass
judge "Install sshpass"
}
wait_server_till_can_ssh()
{
userName=$1
password=$2
serverName=$3
print_ok "Waiting for server to be ready: ssh $userName@$serverName"
while true; do
set +e
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo 'Server is ready'"
if [ $? -eq 0 ]; then
break
fi
print_warn "Server is not ready yet. Retrying..."
sleep 5
done
print_ok "Server is ready to connect via ssh"
set -e
}
prepare_server()
{
userName=$1
if [ -z "$userName" ]; then
print_error "Please provide username. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'"
exit 1
fi
password=$2
if [ -z "$password" ]; then
print_error "Please provide password for user $userName. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'"
exit 1
fi
serverName=$3
if [ -z "$serverName" ]; then
print_error "Please provide server name. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'"
exit 1
fi
desiredHostname=$4
if [ -z "$desiredHostname" ]; then
echo "Please provide desired hostname. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'"
exit 1
fi
desiredUsername=$5
if [ -z "$desiredUsername" ]; then
print_error "Please provide desired username. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'"
exit 1
fi
prepare_host
ssh-keygen -f "/home/anduin/.ssh/known_hosts" -R $serverName
wait_server_till_can_ssh $userName $password $serverName
print_ok "Changing hostname for $serverName to $desiredHostname"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo hostnamectl set-hostname $desiredHostname"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sleep 3"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo reboot" || true
sleep 5
print_ok "Hostname changed to $desiredHostname"
print_warn "Server is rebooting..."
wait_server_till_can_ssh $userName $password $serverName
print_ok "Creating a new user..."
alreadyExist=$(sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "cat /etc/passwd | grep -w $desiredUsername | wc -l")
if [ $alreadyExist -gt 0 ]; then
print_ok "User $desiredUsername already exists."
else
print_ok "Creating user $desiredUsername"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo adduser $desiredUsername --gecos 'First Last,RoomNumber,WorkPhone,HomePhone' --disabled-password"
judge "User $desiredUsername created"
fi
print_ok "Adding user $desiredUsername to sudo group"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo usermod -aG sudo $desiredUsername"
judge "User $desiredUsername created with password"
print_ok "Allowing user $desiredUsername to run sudo without password"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo mkdir -p /etc/sudoers.d"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo touch /etc/sudoers.d/$desiredUsername"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo '$desiredUsername ALL=(ALL) NOPASSWD:ALL' | sudo tee -a /etc/sudoers.d/$desiredUsername"
judge "User $desiredUsername can run sudo without password"
userPassword=$(uuidgen)
print_ok "Setting password for user $desiredUsername to $userPassword"
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo $desiredUsername:$userPassword | sudo chpasswd"
judge "Password set for user $desiredUsername as $userPassword"
# If ~/ssh/id_rsa.pub does not exist, create it
if [ ! -f ~/.ssh/id_rsa.pub ]; then
print_warn "Creating ssh keys on local machine"
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
fi
print_ok "Copying ssh keys with ssh-copy-id"
sshpass -p $userPassword ssh-copy-id -i ~/.ssh/id_rsa.pub $desiredUsername@$serverName
print_ok "SSH keys copied"
wait_server_till_can_ssh $desiredUsername $userPassword $serverName
print_ok "Disabling root login, password login and enabling ssh key login"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PubkeyAuthentication no/PubkeyAuthentication yes/g' /etc/ssh/sshd_config"
# Uncomment those lines if they are commented
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PermitRootLogin no/PermitRootLogin no/g' /etc/ssh/sshd_config"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PasswordAuthentication no/PasswordAuthentication no/g' /etc/ssh/sshd_config"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemctl restart ssh*"
judge "Disable root login, password login and enabled ssh key login"
print_ok "Server is ready for $desiredUsername to login. Deleting other users..."
otherUsers=$(ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "cat /etc/passwd | grep -v nologin | grep -v false | grep -v root | grep -v sync | grep -v $desiredUsername | cut -d: -f1")
for otherUser in $otherUsers; do
print_warn "Deleting user $otherUser..."
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo pkill -u $otherUser" || true
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo deluser --remove-home $otherUser"
done
print_ok "Resetting machine-id"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm /etc/machine-id"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm /var/lib/dbus/machine-id"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemd-machine-id-setup"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo cp /etc/machine-id /var/lib/dbus/machine-id"
judge "Machine-id reset"
print_ok "Enabling ufw firewall"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt-get install -y ufw"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo ufw allow OpenSSH"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "echo 'y' | sudo ufw enable"
judge "Ufw firewall enabled"
print_ok "Enabling BBR if not enabled"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl net.ipv4.tcp_available_congestion_control | grep -q bbr || echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl net.ipv4.tcp_available_congestion_control | grep -q bbr || echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl -p"
judge "BBR enabled"
print_ok "Selecting best mirror"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "curl -s https://gist.aiursoft.cn/anduin/879917820a6c4b268fc12c21f1b3fe7a/raw/HEAD/mirror.sh | bash"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt update"
judge "Best mirror selected"
print_ok "Installing latest kernel..."
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt search linux-generic-hwe-* | awk -F'/' '/linux-generic-hwe-/ {print $1}' | sort | head -n 1 | xargs -r sudo apt install -y"
judge "Latest kernel installed"
print_ok "Rebooting server..."
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sleep 3"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo reboot" || true
sleep 5
print_warn "Server is rebooting..."
wait_server_till_can_ssh $desiredUsername $userPassword $serverName
print_ok "Installing updates"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt update"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt upgrade -y"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt autoremove -y"
judge "Updates installed"
print_ok "Rebooting server..."
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sleep 3"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo reboot" || true
sleep 5
print_warn "Server is rebooting..."
wait_server_till_can_ssh $desiredUsername $userPassword $serverName
print_ok "Set CPU to performance mode"
ssh -o StrictHostKeyChecking=no "$desiredUsername@$serverName" "sudo apt install -y linux-tools-common linux-tools-\$(uname -r)"
ssh -o StrictHostKeyChecking=no "$desiredUsername@$serverName" "sudo cpupower frequency-info"
ssh -o StrictHostKeyChecking=no "$desiredUsername@$serverName" "sudo cpupower frequency-set -g performance" || true
judge "CPU set to performance mode"
print_ok "Set timezone to GMT"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo timedatectl set-timezone GMT"
judge "Timezone set to GMT"
print_ok "Removing snap..."
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemctl disable --now snapd"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt purge -y snapd"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm -rf /snap /var/snap /var/lib/snapd /var/cache/snapd /usr/lib/snapd ~/snap"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "cat << EOF | sudo tee -a /etc/apt/preferences.d/no-snap.pref
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOF"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo chown root:root /etc/apt/preferences.d/no-snap.pref"
judge "Snap removed"
print_ok "Autoremoving apt packages"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt autoremove -y --purge"
judge "Apt packages autoremoved"
print_ok "Benchmarking server..."
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt install -y sysbench"
ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sysbench cpu --threads=\$(nproc) run"
judge "Server benchmarked"
print_ok "Server is ready for use"
print_ok "ssh $desiredUsername@$serverName"
}
# To use this function:
# Arg1: username
# Arg2: password
# Arg3: servername
# Arg4: Desired hostname
# Arg5: Desired username
prepare_server "$@"
1 | #!/bin/bash |
2 | |
3 | #========================== |
4 | # Set up the environment |
5 | #========================== |
6 | set -e # exit on error |
7 | set -o pipefail # exit on pipeline error |
8 | set -u # treat unset variable as error |
9 | |
10 | #========================== |
11 | # Basic Information |
12 | #========================== |
13 | export LC_ALL=C |
14 | export LANG=en_US.UTF-8 |
15 | export DEBIAN_FRONTEND=noninteractive |
16 | export SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" |
17 | |
18 | #========================== |
19 | # Color |
20 | #========================== |
21 | Green="\033[32m" |
22 | Red="\033[31m" |
23 | Yellow="\033[33m" |
24 | Blue="\033[36m" |
25 | Font="\033[0m" |
26 | GreenBG="\033[42;37m" |
27 | RedBG="\033[41;37m" |
28 | OK="${Green}[ OK ]${Font}" |
29 | ERROR="${Red}[FAILED]${Font}" |
30 | WARNING="${Yellow}[ WARN ]${Font}" |
31 | |
32 | #========================== |
33 | # Print Colorful Text |
34 | #========================== |
35 | function print_ok() { |
36 | echo -e "${OK} ${Blue} $1 ${Font}" |
37 | } |
38 | |
39 | function print_error() { |
40 | echo -e "${ERROR} ${Red} $1 ${Font}" |
41 | } |
42 | |
43 | function print_warn() { |
44 | echo -e "${WARNING} ${Yellow} $1 ${Font}" |
45 | } |
46 | |
47 | #========================== |
48 | # Judge function |
49 | #========================== |
50 | function judge() { |
51 | if [[ 0 -eq $? ]]; then |
52 | print_ok "$1 succeeded" |
53 | sleep 0.2 |
54 | else |
55 | print_error "$1 failed" |
56 | exit 1 |
57 | fi |
58 | } |
59 | |
60 | prepare_host() |
61 | { |
62 | print_ok "Update apt-get" |
63 | sudo apt-get update |
64 | judge "Update apt-get" |
65 | |
66 | print_ok "Install sshpass" |
67 | sudo apt-get install -y sshpass |
68 | judge "Install sshpass" |
69 | } |
70 | |
71 | wait_server_till_can_ssh() |
72 | { |
73 | userName=$1 |
74 | password=$2 |
75 | serverName=$3 |
76 | |
77 | print_ok "Waiting for server to be ready: ssh $userName@$serverName" |
78 | while true; do |
79 | set +e |
80 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo 'Server is ready'" |
81 | if [ $? -eq 0 ]; then |
82 | break |
83 | fi |
84 | print_warn "Server is not ready yet. Retrying..." |
85 | sleep 5 |
86 | done |
87 | |
88 | print_ok "Server is ready to connect via ssh" |
89 | set -e |
90 | } |
91 | |
92 | prepare_server() |
93 | { |
94 | userName=$1 |
95 | if [ -z "$userName" ]; then |
96 | print_error "Please provide username. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'" |
97 | exit 1 |
98 | fi |
99 | |
100 | password=$2 |
101 | if [ -z "$password" ]; then |
102 | print_error "Please provide password for user $userName. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'" |
103 | exit 1 |
104 | fi |
105 | |
106 | serverName=$3 |
107 | if [ -z "$serverName" ]; then |
108 | print_error "Please provide server name. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'" |
109 | exit 1 |
110 | fi |
111 | |
112 | desiredHostname=$4 |
113 | if [ -z "$desiredHostname" ]; then |
114 | echo "Please provide desired hostname. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'" |
115 | exit 1 |
116 | fi |
117 | |
118 | desiredUsername=$5 |
119 | if [ -z "$desiredUsername" ]; then |
120 | print_error "Please provide desired username. Usage: prepare_server.sh '<username>' '<password>' '<serverName>' '<desiredHostname>' '<desiredUsername>'" |
121 | exit 1 |
122 | fi |
123 | |
124 | prepare_host |
125 | ssh-keygen -f "/home/anduin/.ssh/known_hosts" -R $serverName |
126 | |
127 | wait_server_till_can_ssh $userName $password $serverName |
128 | |
129 | print_ok "Changing hostname for $serverName to $desiredHostname" |
130 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo hostnamectl set-hostname $desiredHostname" |
131 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sleep 3" |
132 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo reboot" || true |
133 | sleep 5 |
134 | print_ok "Hostname changed to $desiredHostname" |
135 | print_warn "Server is rebooting..." |
136 | |
137 | wait_server_till_can_ssh $userName $password $serverName |
138 | |
139 | print_ok "Creating a new user..." |
140 | alreadyExist=$(sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "cat /etc/passwd | grep -w $desiredUsername | wc -l") |
141 | if [ $alreadyExist -gt 0 ]; then |
142 | print_ok "User $desiredUsername already exists." |
143 | else |
144 | print_ok "Creating user $desiredUsername" |
145 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo adduser $desiredUsername --gecos 'First Last,RoomNumber,WorkPhone,HomePhone' --disabled-password" |
146 | judge "User $desiredUsername created" |
147 | fi |
148 | |
149 | print_ok "Adding user $desiredUsername to sudo group" |
150 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo usermod -aG sudo $desiredUsername" |
151 | judge "User $desiredUsername created with password" |
152 | |
153 | print_ok "Allowing user $desiredUsername to run sudo without password" |
154 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo mkdir -p /etc/sudoers.d" |
155 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo touch /etc/sudoers.d/$desiredUsername" |
156 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo '$desiredUsername ALL=(ALL) NOPASSWD:ALL' | sudo tee -a /etc/sudoers.d/$desiredUsername" |
157 | judge "User $desiredUsername can run sudo without password" |
158 | |
159 | userPassword=$(uuidgen) |
160 | print_ok "Setting password for user $desiredUsername to $userPassword" |
161 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo $desiredUsername:$userPassword | sudo chpasswd" |
162 | judge "Password set for user $desiredUsername as $userPassword" |
163 | |
164 | |
165 | # If ~/ssh/id_rsa.pub does not exist, create it |
166 | if [ ! -f ~/.ssh/id_rsa.pub ]; then |
167 | print_warn "Creating ssh keys on local machine" |
168 | ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa |
169 | fi |
170 | |
171 | print_ok "Copying ssh keys with ssh-copy-id" |
172 | sshpass -p $userPassword ssh-copy-id -i ~/.ssh/id_rsa.pub $desiredUsername@$serverName |
173 | print_ok "SSH keys copied" |
174 | |
175 | wait_server_till_can_ssh $desiredUsername $userPassword $serverName |
176 | |
177 | print_ok "Disabling root login, password login and enabling ssh key login" |
178 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config" |
179 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config" |
180 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PubkeyAuthentication no/PubkeyAuthentication yes/g' /etc/ssh/sshd_config" |
181 | # Uncomment those lines if they are commented |
182 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PermitRootLogin no/PermitRootLogin no/g' /etc/ssh/sshd_config" |
183 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PasswordAuthentication no/PasswordAuthentication no/g' /etc/ssh/sshd_config" |
184 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config" |
185 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemctl restart ssh*" |
186 | judge "Disable root login, password login and enabled ssh key login" |
187 | |
188 | print_ok "Server is ready for $desiredUsername to login. Deleting other users..." |
189 | otherUsers=$(ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "cat /etc/passwd | grep -v nologin | grep -v false | grep -v root | grep -v sync | grep -v $desiredUsername | cut -d: -f1") |
190 | for otherUser in $otherUsers; do |
191 | print_warn "Deleting user $otherUser..." |
192 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo pkill -u $otherUser" || true |
193 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo deluser --remove-home $otherUser" |
194 | done |
195 | |
196 | print_ok "Resetting machine-id" |
197 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm /etc/machine-id" |
198 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm /var/lib/dbus/machine-id" |
199 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemd-machine-id-setup" |
200 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo cp /etc/machine-id /var/lib/dbus/machine-id" |
201 | judge "Machine-id reset" |
202 | |
203 | print_ok "Enabling ufw firewall" |
204 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt-get install -y ufw" |
205 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo ufw allow OpenSSH" |
206 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "echo 'y' | sudo ufw enable" |
207 | judge "Ufw firewall enabled" |
208 | |
209 | print_ok "Enabling BBR if not enabled" |
210 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl net.ipv4.tcp_available_congestion_control | grep -q bbr || echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf" |
211 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl net.ipv4.tcp_available_congestion_control | grep -q bbr || echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf" |
212 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl -p" |
213 | judge "BBR enabled" |
214 | |
215 | print_ok "Selecting best mirror" |
216 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "curl -s https://gist.aiursoft.cn/anduin/879917820a6c4b268fc12c21f1b3fe7a/raw/HEAD/mirror.sh | bash" |
217 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt update" |
218 | judge "Best mirror selected" |
219 | |
220 | print_ok "Installing latest kernel..." |
221 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt search linux-generic-hwe-* | awk -F'/' '/linux-generic-hwe-/ {print $1}' | sort | head -n 1 | xargs -r sudo apt install -y" |
222 | judge "Latest kernel installed" |
223 | |
224 | print_ok "Rebooting server..." |
225 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sleep 3" |
226 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo reboot" || true |
227 | sleep 5 |
228 | print_warn "Server is rebooting..." |
229 | |
230 | wait_server_till_can_ssh $desiredUsername $userPassword $serverName |
231 | |
232 | print_ok "Installing updates" |
233 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt update" |
234 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt upgrade -y" |
235 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt autoremove -y" |
236 | judge "Updates installed" |
237 | |
238 | print_ok "Rebooting server..." |
239 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sleep 3" |
240 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo reboot" || true |
241 | sleep 5 |
242 | print_warn "Server is rebooting..." |
243 | |
244 | wait_server_till_can_ssh $desiredUsername $userPassword $serverName |
245 | |
246 | print_ok "Set CPU to performance mode" |
247 | ssh -o StrictHostKeyChecking=no "$desiredUsername@$serverName" "sudo apt install -y linux-tools-common linux-tools-\$(uname -r)" |
248 | ssh -o StrictHostKeyChecking=no "$desiredUsername@$serverName" "sudo cpupower frequency-info" |
249 | ssh -o StrictHostKeyChecking=no "$desiredUsername@$serverName" "sudo cpupower frequency-set -g performance" || true |
250 | judge "CPU set to performance mode" |
251 | |
252 | print_ok "Set timezone to GMT" |
253 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo timedatectl set-timezone GMT" |
254 | judge "Timezone set to GMT" |
255 | |
256 | print_ok "Removing snap..." |
257 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemctl disable --now snapd" |
258 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt purge -y snapd" |
259 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm -rf /snap /var/snap /var/lib/snapd /var/cache/snapd /usr/lib/snapd ~/snap" |
260 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "cat << EOF | sudo tee -a /etc/apt/preferences.d/no-snap.pref |
261 | Package: snapd |
262 | Pin: release a=* |
263 | Pin-Priority: -10 |
264 | EOF" |
265 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo chown root:root /etc/apt/preferences.d/no-snap.pref" |
266 | judge "Snap removed" |
267 | |
268 | print_ok "Autoremoving apt packages" |
269 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt autoremove -y --purge" |
270 | judge "Apt packages autoremoved" |
271 | |
272 | print_ok "Benchmarking server..." |
273 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt install -y sysbench" |
274 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sysbench cpu --threads=\$(nproc) run" |
275 | judge "Server benchmarked" |
276 | |
277 | print_ok "Server is ready for use" |
278 | print_ok "ssh $desiredUsername@$serverName" |
279 | } |
280 | |
281 | # To use this function: |
282 | # Arg1: username |
283 | # Arg2: password |
284 | # Arg3: servername |
285 | # Arg4: Desired hostname |
286 | # Arg5: Desired username |
287 | prepare_server "$@" |
mirror.sh
· 5.0 KiB · Bash
Raw
#!/usr/bin/env bash
# Step 1: Ensure required packages are installed
sudo apt update
sudo apt install -y curl apt-transport-https lsb-release
function switchSource() {
# Get current Ubuntu codename (e.g., jammy, focal, bionic)
codename=$(lsb_release -cs)
# Define a list of potential mirrors
mirrors=(
"https://archive.ubuntu.com/ubuntu/"
"https://mirror.aarnet.edu.au/pub/ubuntu/archive/" # Australia
"https://mirror.fsmg.org.nz/ubuntu/" # New Zealand
"https://mirrors.neterra.net/ubuntu/archive/" # Bulgaria
"https://mirror.csclub.uwaterloo.ca/ubuntu/" # Canada
"https://mirrors.dotsrc.org/ubuntu/" # Denmark
"https://mirrors.nic.funet.fi/ubuntu/" # Finland
"https://mirror.ubuntu.ikoula.com/" # France
"https://mirror.xtom.com.hk/ubuntu/" # Hong Kong
"https://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu/" # India
"https://ftp.udx.icscoe.jp/Linux/ubuntu/" # Japan
"https://ftp.kaist.ac.kr/ubuntu/" # Korea
"https://ubuntu.mirror.garr.it/ubuntu/" # Italy
"https://ftp.uni-stuttgart.de/ubuntu/" # Germany
"https://mirror.i3d.net/pub/ubuntu/" # Netherlands
"https://mirroronet.pl/pub/mirrors/ubuntu/" # Poland
"https://ubuntu.mobinhost.com/ubuntu/" # Iran
"http://sg.archive.ubuntu.com/ubuntu/" # Singapore
"http://ossmirror.mycloud.services/os/linux/ubuntu/" # Singapore
"https://mirror.enzu.com/ubuntu/" # United States
"http://jp.archive.ubuntu.com/ubuntu/" # Japan
"http://kr.archive.ubuntu.com/ubuntu/" # Korea
"http://us.archive.ubuntu.com/ubuntu/" # United States
"http://tw.archive.ubuntu.com/ubuntu/" # Taiwan
"https://mirror.twds.com.tw/ubuntu/" # Taiwan
"https://ubuntu.mirrors.uk2.net/ubuntu/" # United Kingdom
"http://mirrors.ustc.edu.cn/ubuntu/" # 中国科学技术大学
"http://ftp.sjtu.edu.cn/ubuntu/" # 上海交通大学
"http://mirrors.tuna.tsinghua.edu.cn/ubuntu/" # 清华大学
"http://mirrors.aliyun.com/ubuntu/" # 阿里云
"http://mirrors.163.com/ubuntu/" # 网易
"http://mirrors.cloud.tencent.com/ubuntu/" # 腾讯云
"http://mirror.aiursoft.cn/ubuntu/" # Aiursoft
"http://mirrors.anduinos.com/ubuntu/" # AnduinOS
"http://mirrors.huaweicloud.com/ubuntu/" # 华为云
"http://mirrors.zju.edu.cn/ubuntu/" # 浙江大学
"http://azure.archive.ubuntu.com/ubuntu/" # Azure
"https://mirrors.isu.net.sa/apt-mirror/" # Saudi Arabia
"https://mirror.team-host.ru/ubuntu/" # Russia
"https://labs.eif.urjc.es/mirror/ubuntu/" # Spain
"https://mirror.alastyr.com/ubuntu/ubuntu-archive/" # Turkey
"https://ftp.acc.umu.se/ubuntu/" # Sweden
"https://mirror.kku.ac.th/ubuntu/" # Thailand
"https://mirror.bizflycloud.vn/ubuntu/" # Vietnam
)
declare -A results
# Function to test speed of a single mirror
test_speed() {
url="$1"
# Attempt to do a quick GET and measure total time
response="$(curl -o /dev/null -s -w "%{http_code} %{time_total}\n" \
--connect-timeout 1 --max-time 2 "$url")"
http_code=$(echo "$response" | awk '{print $1}')
time_total=$(echo "$response" | awk '{print $2}')
# If HTTP code == 200, mark the measured time; otherwise use a large value
if [ "$http_code" -eq 200 ]; then
results["$url"]="$time_total"
else
echo "Failed to access $url (HTTP code: $http_code)"
results["$url"]="9999"
fi
}
echo "Testing all mirrors for Ubuntu '$codename'..."
for mirror in "${mirrors[@]}"; do
test_speed "$mirror"
done
# Sort mirrors by time_total
# Example of sorted_mirrors entry: "https://archive.ubuntu.com/ubuntu/ 0.034"
sorted_mirrors="$(
for url in "${!results[@]}"; do
echo "$url ${results[$url]}"
done | sort -k2 -n
)"
echo
echo "=== Sorted mirrors by response time (ascending) ==="
echo "$sorted_mirrors"
echo
# Pick the top (fastest) mirror from the sorted list
fastest_mirror="$(echo "$sorted_mirrors" | head -n 1 | awk '{print $1}')"
echo "Fastest mirror found: $fastest_mirror"
echo "Updating /etc/apt/sources.list..."
# Update /etc/apt/sources.list with the fastest mirror
sudo tee /etc/apt/sources.list >/dev/null <<EOF
deb $fastest_mirror $codename main restricted universe multiverse
deb $fastest_mirror $codename-updates main restricted universe multiverse
deb $fastest_mirror $codename-backports main restricted universe multiverse
deb $fastest_mirror $codename-security main restricted universe multiverse
EOF
# Final check
sudo apt update
echo "All done!"
}
# Call the main function
switchSource
1 | #!/usr/bin/env bash |
2 | # Step 1: Ensure required packages are installed |
3 | sudo apt update |
4 | sudo apt install -y curl apt-transport-https lsb-release |
5 | |
6 | function switchSource() { |
7 | # Get current Ubuntu codename (e.g., jammy, focal, bionic) |
8 | codename=$(lsb_release -cs) |
9 | |
10 | # Define a list of potential mirrors |
11 | mirrors=( |
12 | "https://archive.ubuntu.com/ubuntu/" |
13 | "https://mirror.aarnet.edu.au/pub/ubuntu/archive/" # Australia |
14 | "https://mirror.fsmg.org.nz/ubuntu/" # New Zealand |
15 | "https://mirrors.neterra.net/ubuntu/archive/" # Bulgaria |
16 | "https://mirror.csclub.uwaterloo.ca/ubuntu/" # Canada |
17 | "https://mirrors.dotsrc.org/ubuntu/" # Denmark |
18 | "https://mirrors.nic.funet.fi/ubuntu/" # Finland |
19 | "https://mirror.ubuntu.ikoula.com/" # France |
20 | "https://mirror.xtom.com.hk/ubuntu/" # Hong Kong |
21 | "https://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu/" # India |
22 | "https://ftp.udx.icscoe.jp/Linux/ubuntu/" # Japan |
23 | "https://ftp.kaist.ac.kr/ubuntu/" # Korea |
24 | "https://ubuntu.mirror.garr.it/ubuntu/" # Italy |
25 | "https://ftp.uni-stuttgart.de/ubuntu/" # Germany |
26 | "https://mirror.i3d.net/pub/ubuntu/" # Netherlands |
27 | "https://mirroronet.pl/pub/mirrors/ubuntu/" # Poland |
28 | "https://ubuntu.mobinhost.com/ubuntu/" # Iran |
29 | "http://sg.archive.ubuntu.com/ubuntu/" # Singapore |
30 | "http://ossmirror.mycloud.services/os/linux/ubuntu/" # Singapore |
31 | "https://mirror.enzu.com/ubuntu/" # United States |
32 | "http://jp.archive.ubuntu.com/ubuntu/" # Japan |
33 | "http://kr.archive.ubuntu.com/ubuntu/" # Korea |
34 | "http://us.archive.ubuntu.com/ubuntu/" # United States |
35 | "http://tw.archive.ubuntu.com/ubuntu/" # Taiwan |
36 | "https://mirror.twds.com.tw/ubuntu/" # Taiwan |
37 | "https://ubuntu.mirrors.uk2.net/ubuntu/" # United Kingdom |
38 | "http://mirrors.ustc.edu.cn/ubuntu/" # 中国科学技术大学 |
39 | "http://ftp.sjtu.edu.cn/ubuntu/" # 上海交通大学 |
40 | "http://mirrors.tuna.tsinghua.edu.cn/ubuntu/" # 清华大学 |
41 | "http://mirrors.aliyun.com/ubuntu/" # 阿里云 |
42 | "http://mirrors.163.com/ubuntu/" # 网易 |
43 | "http://mirrors.cloud.tencent.com/ubuntu/" # 腾讯云 |
44 | "http://mirror.aiursoft.cn/ubuntu/" # Aiursoft |
45 | "http://mirrors.anduinos.com/ubuntu/" # AnduinOS |
46 | "http://mirrors.huaweicloud.com/ubuntu/" # 华为云 |
47 | "http://mirrors.zju.edu.cn/ubuntu/" # 浙江大学 |
48 | "http://azure.archive.ubuntu.com/ubuntu/" # Azure |
49 | "https://mirrors.isu.net.sa/apt-mirror/" # Saudi Arabia |
50 | "https://mirror.team-host.ru/ubuntu/" # Russia |
51 | "https://labs.eif.urjc.es/mirror/ubuntu/" # Spain |
52 | "https://mirror.alastyr.com/ubuntu/ubuntu-archive/" # Turkey |
53 | "https://ftp.acc.umu.se/ubuntu/" # Sweden |
54 | "https://mirror.kku.ac.th/ubuntu/" # Thailand |
55 | "https://mirror.bizflycloud.vn/ubuntu/" # Vietnam |
56 | ) |
57 | |
58 | declare -A results |
59 | |
60 | # Function to test speed of a single mirror |
61 | test_speed() { |
62 | url="$1" |
63 | # Attempt to do a quick GET and measure total time |
64 | response="$(curl -o /dev/null -s -w "%{http_code} %{time_total}\n" \ |
65 | --connect-timeout 1 --max-time 2 "$url")" |
66 | |
67 | http_code=$(echo "$response" | awk '{print $1}') |
68 | time_total=$(echo "$response" | awk '{print $2}') |
69 | |
70 | # If HTTP code == 200, mark the measured time; otherwise use a large value |
71 | if [ "$http_code" -eq 200 ]; then |
72 | results["$url"]="$time_total" |
73 | else |
74 | echo "Failed to access $url (HTTP code: $http_code)" |
75 | results["$url"]="9999" |
76 | fi |
77 | } |
78 | |
79 | echo "Testing all mirrors for Ubuntu '$codename'..." |
80 | for mirror in "${mirrors[@]}"; do |
81 | test_speed "$mirror" |
82 | done |
83 | |
84 | # Sort mirrors by time_total |
85 | # Example of sorted_mirrors entry: "https://archive.ubuntu.com/ubuntu/ 0.034" |
86 | sorted_mirrors="$( |
87 | for url in "${!results[@]}"; do |
88 | echo "$url ${results[$url]}" |
89 | done | sort -k2 -n |
90 | )" |
91 | |
92 | echo |
93 | echo "=== Sorted mirrors by response time (ascending) ===" |
94 | echo "$sorted_mirrors" |
95 | echo |
96 | |
97 | # Pick the top (fastest) mirror from the sorted list |
98 | fastest_mirror="$(echo "$sorted_mirrors" | head -n 1 | awk '{print $1}')" |
99 | |
100 | echo "Fastest mirror found: $fastest_mirror" |
101 | echo "Updating /etc/apt/sources.list..." |
102 | |
103 | # Update /etc/apt/sources.list with the fastest mirror |
104 | sudo tee /etc/apt/sources.list >/dev/null <<EOF |
105 | deb $fastest_mirror $codename main restricted universe multiverse |
106 | deb $fastest_mirror $codename-updates main restricted universe multiverse |
107 | deb $fastest_mirror $codename-backports main restricted universe multiverse |
108 | deb $fastest_mirror $codename-security main restricted universe multiverse |
109 | EOF |
110 | |
111 | # Final check |
112 | sudo apt update |
113 | echo "All done!" |
114 | } |
115 | |
116 | # Call the main function |
117 | switchSource |