Repository navigation
Expand file tree
/
Copy pathinit_no_clash.sh
More file actions
232 lines (201 loc) · 7.96 KB
/
Copy pathinit_no_clash.sh
File metadata and controls
232 lines (201 loc) · 7.96 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
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
#!/usr/bin/env bash
set -euo pipefail
# init_no_clash.sh
# 仅包含原 init.sh 的步骤:1 + 2 + 5(不含 clash/sing-box/snell/substore)
# 按要求:额外安装 sudo;写入 PS1 与 alias 到环境配置
if [[ "$(id -u)" -ne 0 ]]; then
echo "错误:请用 root 运行(sudo bash init_no_clash.sh)"
exit 1
fi
# SSH 端口:唯一配置点。可用环境变量覆盖,例如:
# SSH_PORT=22 bash init_no_clash.sh
# 下面的 sshd_config 与 fail2ban jail.local 都会引用这个变量,不会出现"改了 sshd 忘了改 fail2ban"的问题。
SSH_PORT="${SSH_PORT:-50000}"
BACKUP_DIR="/root/ssh_backup_$(date +%Y%m%d_%H%M%S)"
step1_install_packages() {
echo "===== 步骤1:安装基础软件(含 curl + sudo) ====="
local pkgs="sudo curl vim wget telnet iperf3 iproute2"
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y $pkgs
for p in $pkgs; do
dpkg -s "$p" >/dev/null 2>&1 || {
echo "错误:$p 安装失败"
return 1
}
done
echo "步骤1完成。"
}
step2_tools_script() {
echo "===== 步骤2:下载并执行 tools.sh(自动输入 2) ====="
local url="https://raw.githubusercontent.com/kakakakaka1/init/main/tools.sh"
local path="/root/tools.sh"
wget -q -O "$path" "$url"
chmod +x "$path"
printf "2\n" | "$path"
echo "步骤2完成。"
}
step_env_vars() {
echo "===== 环境变量与别名配置 ====="
# 全局生效
tee /etc/profile.d/custom_shell_env.sh >/dev/null <<'EOFENV'
PS1='\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\]'
alias ll='ls --color=auto -lAF'
alias ls='ls --color=auto'
EOFENV
chmod 644 /etc/profile.d/custom_shell_env.sh
# root 用户也追加一份(兼容某些环境不加载 /etc/profile.d)
if ! grep -q "alias ll='ls --color=auto -lAF'" /root/.bashrc 2>/dev/null; then
cat >> /root/.bashrc <<'EOFBASHRC'
PS1='\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\]'
alias ll='ls --color=auto -lAF'
alias ls='ls --color=auto'
EOFBASHRC
fi
echo "环境变量与别名已写入。"
}
step5_ssh_hardening() {
echo "===== 步骤5:SSH 安全加固 + Fail2ban ====="
mkdir -p "$BACKUP_DIR"
[[ -f /etc/ssh/sshd_config ]] && cp /etc/ssh/sshd_config "$BACKUP_DIR/sshd_config.backup"
[[ -f /etc/fail2ban/jail.local ]] && cp /etc/fail2ban/jail.local "$BACKUP_DIR/jail.local.backup"
DEBIAN_FRONTEND=noninteractive apt-get install -y fail2ban openssh-server
mkdir -p /root/.ssh
chmod 700 /root/.ssh
tee /root/.ssh/authorized_keys >/dev/null <<'EOFKEY'
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2CNY7JG7dO3JVB0sCIfKJTtJH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tee /etc/ssh/sshd_config >/dev/null <<SSHEOF
Include /etc/ssh/sshd_config.d/*.conf
Port ${SSH_PORT}
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTH
LogLevel VERBOSE
LoginGraceTime 60
PermitRootLogin prohibit-password
StrictModes yes
MaxAuthTries 3
MaxSessions 10
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
PermitEmptyPasswords no
KbdInteractiveAuthentication no
KerberosAuthentication no
GSSAPIAuthentication no
UsePAM yes
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts no
X11Forwarding no
PermitTTY yes
PrintMotd no
TCPKeepAlive yes
PermitUserEnvironment no
Compression delayed
ClientAliveInterval 300
ClientAliveCountMax 2
UseDNS no
PidFile /run/sshd.pid
MaxStartups 10:30:100
PermitTunnel no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
SSHEOF
# 覆盖 VPS 商家镜像可能自带的 /etc/ssh/sshd_config.d/01-permitrootlogin.conf
# (不少镜像会写 PermitRootLogin yes)。sshd 取"首个匹配值",且 Include 按文件名
# 字典序读取,所以必须用 00- 前缀才能排在 01- 之前,否则会被镜像的配置盖掉。
tee /etc/ssh/sshd_config.d/00-ssh-hardening.conf >/dev/null <<'SSHDROPIN'
# Generated by init_no_clash.sh - must sort BEFORE the vendor drop-ins
PermitRootLogin prohibit-password
PasswordAuthentication no
KbdInteractiveAuthentication no
SSHDROPIN
chmod 644 /etc/ssh/sshd_config.d/00-ssh-hardening.conf
tee /etc/fail2ban/filter.d/sshd-aggressive.conf >/dev/null <<'F2B1'
[INCLUDES]
before = common.conf
[Definition]
_daemon = sshd
failregex = ^.*sshd.*authentication failure.*rhost=<HOST>.*$
^.*sshd.*Failed password for .* from <HOST>.*$
^.*sshd.*Failed password for invalid user .* from <HOST>.*$
^.*sshd.*Invalid user .* from <HOST>.*$
^.*sshd.*Connection closed by <HOST> port.*\[preauth\]$
^.*sshd.*Did not receive identification string from <HOST>.*$
^.*sshd.*Bad protocol version identification .* from <HOST>.*$
^.*sshd.*error: kex_exchange_identification.*<HOST>.*$
^.*sshd.*Connection reset by <HOST> port.*$
^.*sshd.*Connection closed by authenticating user \S+ <HOST> port [0-9]+ \[preauth\]$
^.*sshd.*Connection reset by authenticating user \S+ <HOST> port [0-9]+ \[preauth\]$
ignoreregex =
F2B1
tee /etc/fail2ban/filter.d/port-scan.conf >/dev/null <<'F2B2'
[Definition]
failregex = ^.*sshd.*: error: kex_exchange_identification: Connection closed by remote host <HOST>.*$
^.*sshd.*: Did not receive identification string from <HOST>.*$
^.*sshd.*: Bad protocol version identification.*from <HOST>.*$
ignoreregex = ^.*127\.0\.0\.1.*$
^.*::1.*$
F2B2
tee /etc/fail2ban/jail.local >/dev/null <<JAIL
[DEFAULT]
bantime = 360000
findtime = 60000
maxretry = 3
backend = systemd
[sshd]
enabled = true
# 必须与 sshd_config 的 Port 保持一致,否则 fail2ban 的封禁规则会打在没人监听的端口上,
# 表现为"已封禁 N 个 IP"但实际一个都没挡住。这里引用同一个 SSH_PORT 变量避免漏改。
port = ${SSH_PORT}
filter = sshd-aggressive
backend = systemd
maxretry = 2
findtime = 315360000
bantime = -1
ignoreip = 127.0.0.1/8 ::1
[port-scan]
enabled = true
port = all
filter = port-scan
backend = systemd
maxretry = 2
findtime = 315360000
bantime = -1
ignoreip = 127.0.0.1/8 ::1
[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
bantime = 604800
findtime = 86400
maxretry = 2
ignoreip = 127.0.0.1/8 ::1
JAIL
sshd -t
systemctl enable fail2ban
systemctl restart fail2ban
echo "⚠️ SSH 将改为端口 ${SSH_PORT},并禁用密码登录。"
echo "⚠️ 请确认公钥可用:ssh -p ${SSH_PORT} root@<IP>"
if command -v at >/dev/null 2>&1; then
echo "systemctl restart ssh" | at now + 10 seconds 2>/dev/null || (sleep 10 && systemctl restart ssh) &
else
(sleep 10 && systemctl restart ssh) &
fi
echo "步骤5完成。备份目录:$BACKUP_DIR"
}
main() {
step1_install_packages
step2_tools_script
step_env_vars
step5_ssh_hardening
echo "==== 全部完成:init_no_clash.sh ===="
}
main "$@"