init.sh
· 13 KiB · Bash
原始檔案
#!/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 "Ensure Server is Ubuntu 22.04" # Accept 22.04.1, 22.04.2, etc
osVersion=$(sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "lsb_release -r | awk '{print \$2}'")
if [ "$osVersion" != "22.04" ]; then
print_error "Server is not Ubuntu 22.04. Please use Ubuntu 22.04"
exit 1
fi
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"
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..."
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo adduser $desiredUsername --gecos 'First Last,RoomNumber,WorkPhone,HomePhone' --disabled-password"
judge "User $desiredUsername created"
print_ok "Setting password for user $desiredUsername"
userPassword=$(uuidgen)
sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo $desiredUsername:$userPassword | sudo chpasswd"
judge "Password set for user $desiredUsername"
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 $userPassword"
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"
# 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 sshd"
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 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/3643fb2cafeb42379d362e33e5f2313a/download/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 install -y linux-generic-hwe-22.04"
judge "Latest kernel installed"
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"
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"
print_layout
}
print_layout()
{
print_ok "OS information"
sudo lsb_release -a
print_ok "OS install date"
stat -c %w /
print_ok "Secure Boot status"
sudo mokutil --sb-state
print_ok "Root file system"
sudo df -Th /
print_ok "Boot mode"
if [ -d /sys/firmware/efi ]; then echo "Boot mode: UEFI"; else echo "Boot mode: Legacy"; fi
print_ok "USB information"
sudo lsusb
print_ok "Disk layout"
sudo lsblk
print_ok "All disks information"
sudo fdisk -l
prprint_okint "Disk usage"
sudo df -Th
print_ok "Memory information"
sudo free -h
print_ok "Network information"
sudo ip link show
print_ok "Firewall status"
sudo ufw status
print_ok "Network location"
curl https://ipinfo.io
}
# 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 "Ensure Server is Ubuntu 22.04" # Accept 22.04.1, 22.04.2, etc |
130 | osVersion=$(sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "lsb_release -r | awk '{print \$2}'") |
131 | if [ "$osVersion" != "22.04" ]; then |
132 | print_error "Server is not Ubuntu 22.04. Please use Ubuntu 22.04" |
133 | exit 1 |
134 | fi |
135 | |
136 | print_ok "Changing hostname for $serverName to $desiredHostname" |
137 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo hostnamectl set-hostname $desiredHostname" |
138 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sleep 3" |
139 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo reboot" |
140 | print_ok "Hostname changed to $desiredHostname" |
141 | print_warn "Server is rebooting..." |
142 | |
143 | wait_server_till_can_ssh $userName $password $serverName |
144 | |
145 | print_ok "Creating a new user..." |
146 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo adduser $desiredUsername --gecos 'First Last,RoomNumber,WorkPhone,HomePhone' --disabled-password" |
147 | judge "User $desiredUsername created" |
148 | |
149 | print_ok "Setting password for user $desiredUsername" |
150 | userPassword=$(uuidgen) |
151 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo $desiredUsername:$userPassword | sudo chpasswd" |
152 | judge "Password set for user $desiredUsername" |
153 | |
154 | print_ok "Adding user $desiredUsername to sudo group" |
155 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo usermod -aG sudo $desiredUsername" |
156 | judge "User $desiredUsername created with password $userPassword" |
157 | |
158 | print_ok "Allowing user $desiredUsername to run sudo without password" |
159 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo mkdir -p /etc/sudoers.d" |
160 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "sudo touch /etc/sudoers.d/$desiredUsername" |
161 | sshpass -p $password ssh -o StrictHostKeyChecking=no $userName@$serverName "echo '$desiredUsername ALL=(ALL) NOPASSWD:ALL' | sudo tee -a /etc/sudoers.d/$desiredUsername" |
162 | judge "User $desiredUsername can run sudo without password" |
163 | |
164 | # If ~/ssh/id_rsa.pub does not exist, create it |
165 | if [ ! -f ~/.ssh/id_rsa.pub ]; then |
166 | print_warn "Creating ssh keys on local machine" |
167 | ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa |
168 | fi |
169 | |
170 | print_ok "Copying ssh keys with ssh-copy-id" |
171 | sshpass -p $userPassword ssh-copy-id -i ~/.ssh/id_rsa.pub $desiredUsername@$serverName |
172 | print_ok "SSH keys copied" |
173 | |
174 | wait_server_till_can_ssh $desiredUsername $userPassword $serverName |
175 | |
176 | print_ok "Disabling root login, password login and enabling ssh key login" |
177 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config" |
178 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config" |
179 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/PubkeyAuthentication no/PubkeyAuthentication yes/g' /etc/ssh/sshd_config" |
180 | # Uncomment those lines if they are commented |
181 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PermitRootLogin no/PermitRootLogin no/g' /etc/ssh/sshd_config" |
182 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PasswordAuthentication no/PasswordAuthentication no/g' /etc/ssh/sshd_config" |
183 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config" |
184 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemctl restart sshd" |
185 | judge "Disable root login, password login and enabled ssh key login" |
186 | |
187 | print_ok "Server is ready for $desiredUsername to login. Deleting other users..." |
188 | 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") |
189 | for otherUser in $otherUsers; do |
190 | print_warn "Deleting user $otherUser..." |
191 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo deluser --remove-home $otherUser" |
192 | done |
193 | |
194 | print_ok "Resetting machine-id" |
195 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm /etc/machine-id" |
196 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm /var/lib/dbus/machine-id" |
197 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemd-machine-id-setup" |
198 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo cp /etc/machine-id /var/lib/dbus/machine-id" |
199 | judge "Machine-id reset" |
200 | |
201 | print_ok "Enabling ufw firewall" |
202 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt-get install -y ufw" |
203 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo ufw allow OpenSSH" |
204 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "echo 'y' | sudo ufw enable" |
205 | judge "Ufw firewall enabled" |
206 | |
207 | print_ok "Enabling BBR if not enabled" |
208 | 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" |
209 | 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" |
210 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo sysctl -p" |
211 | judge "BBR enabled" |
212 | |
213 | print_ok "Selecting best mirror" |
214 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "curl -s https://gist.aiursoft.cn/anduin/3643fb2cafeb42379d362e33e5f2313a/download/HEAD/mirror.sh | bash" |
215 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt update" |
216 | judge "Best mirror selected" |
217 | |
218 | print_ok "Installing latest kernel..." |
219 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt install -y linux-generic-hwe-22.04" |
220 | judge "Latest kernel installed" |
221 | |
222 | print_ok "Installing updates" |
223 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt update" |
224 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt upgrade -y" |
225 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt autoremove -y" |
226 | judge "Updates installed" |
227 | |
228 | print_ok "Rebooting server..." |
229 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sleep 3" |
230 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo reboot" |
231 | print_warn "Server is rebooting..." |
232 | |
233 | wait_server_till_can_ssh $desiredUsername $userPassword $serverName |
234 | |
235 | print_ok "Set CPU to performance mode" |
236 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt install -y linux-tools-common linux-tools-$(uname -r)" |
237 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo cpupower frequency-info" |
238 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo cpupower frequency-set -g performance" || true |
239 | judge "CPU set to performance mode" |
240 | |
241 | print_ok "Set timezone to GMT" |
242 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo timedatectl set-timezone GMT" |
243 | judge "Timezone set to GMT" |
244 | |
245 | print_ok "Removing snap..." |
246 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo systemctl disable --now snapd" |
247 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt purge -y snapd" |
248 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo rm -rf /snap /var/snap /var/lib/snapd /var/cache/snapd /usr/lib/snapd ~/snap" |
249 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "cat << EOF | sudo tee -a /etc/apt/preferences.d/no-snap.pref |
250 | Package: snapd |
251 | Pin: release a=* |
252 | Pin-Priority: -10 |
253 | EOF" |
254 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo chown root:root /etc/apt/preferences.d/no-snap.pref" |
255 | judge "Snap removed" |
256 | |
257 | print_ok "Autoremoving apt packages" |
258 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt autoremove -y --purge" |
259 | judge "Apt packages autoremoved" |
260 | |
261 | print_ok "Benchmarking server..." |
262 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sudo apt install -y sysbench" |
263 | ssh -o StrictHostKeyChecking=no $desiredUsername@$serverName "sysbench cpu --threads=\$(nproc) run" |
264 | judge "Server benchmarked" |
265 | |
266 | print_ok "Server is ready for use" |
267 | print_ok "ssh $desiredUsername@$serverName" |
268 | |
269 | print_layout |
270 | } |
271 | |
272 | print_layout() |
273 | { |
274 | print_ok "OS information" |
275 | sudo lsb_release -a |
276 | print_ok "OS install date" |
277 | stat -c %w / |
278 | print_ok "Secure Boot status" |
279 | sudo mokutil --sb-state |
280 | print_ok "Root file system" |
281 | sudo df -Th / |
282 | print_ok "Boot mode" |
283 | if [ -d /sys/firmware/efi ]; then echo "Boot mode: UEFI"; else echo "Boot mode: Legacy"; fi |
284 | print_ok "USB information" |
285 | sudo lsusb |
286 | print_ok "Disk layout" |
287 | sudo lsblk |
288 | print_ok "All disks information" |
289 | sudo fdisk -l |
290 | prprint_okint "Disk usage" |
291 | sudo df -Th |
292 | print_ok "Memory information" |
293 | sudo free -h |
294 | print_ok "Network information" |
295 | sudo ip link show |
296 | print_ok "Firewall status" |
297 | sudo ufw status |
298 | print_ok "Network location" |
299 | curl https://ipinfo.io |
300 | } |
301 | |
302 | # To use this function: |
303 | # Arg1: username |
304 | # Arg2: password |
305 | # Arg3: servername |
306 | # Arg4: Desired hostname |
307 | # Arg5: Desired username |
308 | prepare_server "$@" |