-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.sh
62 lines (48 loc) · 1.12 KB
/
2.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
#!/bin/bash
# 修改SSH默认端口
NEW_SSH_PORT=10022
# 备份原始的sshd_config文件
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 修改端口
sudo sed -i "s/^Port .*/Port $NEW_SSH_PORT/" /etc/ssh/sshd_config
# 重启SSH服务
sudo systemctl restart ssh
# 更新包列表
sudo apt update
# 安装Fail2Ban
sudo apt install -y fail2ban
# 切换到Fail2Ban配置目录
cd /etc/fail2ban
# 备份配置文件
sudo cp fail2ban.conf fail2ban.local
sudo cp jail.conf jail.local.bak
# 添加[DEFAULT]配置
sudo bash -c 'cat << EOF >> jail.local
#DEFAULT配置开始
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime = 1h
findtime = 1m
maxretry = 3
banaction = iptables-multiport
action = %(action_mwl)s
#DEFAULT配置结束
EOF'
# 添加[sshd]服务配置,确保使用新的端口号
sudo bash -c 'cat << EOF >> jail.local
#sshd服务配置开始
[sshd]
enabled = true
filter = sshd
port = $NEW_SSH_PORT
maxretry = 3
findtime = 60
bantime = -1
action = %(action_mwl)s
#sshd服务配置结束
EOF'
# 重启Fail2Ban服务以应用更改
sudo systemctl restart fail2ban
# 检查Fail2Ban状态
sleep 10
sudo fail2ban-client status sshd