准备您的机器
如果要设置新计算机,建议您先保护它。假设您运行的是 Ubuntu 22.04:
禁止密码登录SSH(仅允许密钥登录)
首先,确保您实际上是使用密钥而不是密码登录服务器,否则,这将锁定您。许多托管服务提供商支持上传公钥,并在新计算机上自动为您设置基于密钥的 root 登录。
编辑 /etc/ssh/sshd_config 并找到 PasswordAuthentication。确保它未注释并设置为 no。如果您进行了任何更改,请重新启动 sshd:
安装 fail2ban 以阻止重复的登录尝试
首先,安装 fail2ban:
编辑 /etc/fail2ban/jail.local 并放入以下内容:
最后,重新启动 fail2ban:
安装防火墙,仅允许 SSH、HTTP 和 HTTPS 端口
首先,安装 iptables-persistent。在安装过程中,它会询问您是否要保留当前规则 – decline。
编辑 /etc/iptables/rules.v4 并放入以下内容:
使用 iptables-persistent,该配置将在启动时加载。但是由于我们现在没有重启,因此我们需要第一次手动加载它:
如果您的服务器也可以通过 IPv6 访问,请编辑 /etc/iptables/rules.v6 并在其中添加以下内容:
iptables-persistent 将在机器启动时自动加载配置。但是由于我们现在不会立刻重启,我们需要第一次手动加载它:
禁止密码登录SSH(仅允许密钥登录)
首先,确保您实际上是使用密钥而不是密码登录服务器,否则,这将锁定您。许多托管服务提供商支持上传公钥,并在新计算机上自动为您设置基于密钥的 root 登录。
编辑 /etc/ssh/sshd_config 并找到 PasswordAuthentication。确保它未注释并设置为 no。如果您进行了任何更改,请重新启动 sshd:
XML:
systemctl restart ssh.service
更新系统软件包
XML:
apt update && apt upgrade -y
安装 fail2ban 以阻止重复的登录尝试
首先,安装 fail2ban:
Bash:
apt install fail2ban
编辑 /etc/fail2ban/jail.local 并放入以下内容:
Bash:
[DEFAULT]
destemail = [email protected]
sendername = Fail2Ban
[sshd]
enabled = true
port = 22
mode = aggressive
最后,重新启动 fail2ban:
Bash:
systemctl restart fail2ban
安装防火墙,仅允许 SSH、HTTP 和 HTTPS 端口
首先,安装 iptables-persistent。在安装过程中,它会询问您是否要保留当前规则 – decline。
代码:
apt install -y iptables-persistent
编辑 /etc/iptables/rules.v4 并放入以下内容:
代码:
*filter
# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
# Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# (optional) Allow HTTP/3 connections from anywhere.
-A INPUT -p udp --dport 443 -j ACCEPT
# Allow SSH connections
# The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# Allow destination unreachable messages, especially code 4 (fragmentation required) is required or PMTUD breaks
-A INPUT -p icmp -m icmp --icmp-type 3 -j ACCEPT
# Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
使用 iptables-persistent,该配置将在启动时加载。但是由于我们现在没有重启,因此我们需要第一次手动加载它:
代码:
iptables-restore < /etc/iptables/rules.v4
如果您的服务器也可以通过 IPv6 访问,请编辑 /etc/iptables/rules.v6 并在其中添加以下内容:
代码:
*filter
# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d ::1/128 -j REJECT
# Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# (optional) Allow HTTP/3 connections from anywhere.
-A INPUT -p udp --dport 443 -j ACCEPT
# Allow SSH connections
# The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmpv6 -j ACCEPT
# Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
iptables-persistent 将在机器启动时自动加载配置。但是由于我们现在不会立刻重启,我们需要第一次手动加载它:
代码:
iptables-restore < /etc/iptables/rules.v4