基于Debian操作系统。

性能

性能优化

开启BBR

Linux内核须为 4.9 以上。

编辑 /etc/sysctl.conf 文件,加入以下内容:

1
2
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
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
fs.file-max = 6815744
net.ipv4.tcp_no_metrics_save=1
net.ipv4.tcp_ecn=0
net.ipv4.tcp_frto=0
net.ipv4.tcp_mtu_probing=0
net.ipv4.tcp_rfc1337=0
net.ipv4.tcp_sack=1
net.ipv4.tcp_fack=1
net.ipv4.tcp_window_scaling=1
net.ipv4.tcp_adv_win_scale=1
net.ipv4.tcp_moderate_rcvbuf=1
net.core.rmem_max=33554432
net.core.wmem_max=33554432
net.ipv4.tcp_rmem=4096 87380 33554432
net.ipv4.tcp_wmem=4096 16384 33554432
net.ipv4.udp_rmem_min=8192
net.ipv4.udp_wmem_min=8192
net.ipv4.ip_forward=1
net.ipv4.conf.all.route_localnet=1
net.ipv4.conf.all.forwarding=1
net.ipv4.conf.default.forwarding=1
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
net.ipv6.conf.all.forwarding=1
net.ipv6.conf.default.forwarding=1

使修改生效,执行命令:sudo sysctl -p

性能测试

1
2
3
wget -qO- yabs.sh | bash
# 融合怪
bash <(wget -qO- bash.spiritlhl.net/ecs)

一键 DD 脚本

为了获取更为“纯净”的操作系统,使用 DD 方式重装系统。

1
2
3
4
5
6
7
8
9
10
wget --no-check-certificate -qO InstallNET.sh 'https://raw.githubusercontent.com/leitbogioro/Tools/master/Linux_reinstall/InstallNET.sh' \
&& chmod a+x InstallNET.sh \
&& bash InstallNET.sh -debian 12\
-mirror "https://deb.debian.org/debian" \
-port "56723" \
-pwd 'Rand0m_pAss!!' \
-timezone 'Europe/Berlin' \
-swap '1024' \
--bbr \
--fail2ban "1"

系统安全

网络

禁止 Ping

编辑 /etc/sysctl.conf 文件,加入以下内容:

1
net.ipv4.icmp_echo_ignore_all=1

使修改生效,执行命令:sudo sysctl -p

SSH 连接

  1. 创建新的无 sudo 权限用户
1
2
3
4
# 如果未安装 zsh,可以省略 `-s /bin/zsh` 部分
useradd -m -s /bin/zsh randomuser
# 设置密码
passwd randomuser
  1. 本地电脑生成密钥对(已有可忽略)
1
2
# 如无特殊设置,一路回车
ssh-keygen
  1. 添加公钥到新用户 .ssh 目录
1
2
3
4
5
6
7
8
9
10
# 切换登录到 randomuser
su - randomuser

# 写入公钥
mkdir ~/.ssh && vim ~/.ssh/authorized_keys

# 修改文件权限
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

exit # 退出 randomuser 用户,返回 root 用户/拥有 sudo 权限用户
1
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 randomuser@remote-server-ip
  1. 修改 sshd 配置文件

编辑 /etc/ssh/sshd_config 文件,修改或添加如下相应内容:

1
2
3
4
5
6
7
8
9
10
11
# ssh 连接端口
Port 55555

# 禁止 root 用户登录
PermitRootLogin no

# 不允许密码登录
PasswordAuthentication no

# 只允许 randomuser 用户登录
AllowUsers randomuser

重启 sshd 服务:systemctl restart sshd

Fail2Ban 禁止暴力破解

  • 修改SSH端口(可选):

    1
    2
    sed -i "s/#Port 22/Port 55555/g" /etc/ssh/sshd_config
    systemctl restart sshd.service
  • 更新防火墙规则(可选):

    1
    2
    3
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -A INPUT -p tcp --dport 22 -j DROP
    iptables -A INPUT -p tcp --dport 55555 -j ACCEPT

    防火墙规则本地持久化

    1
    2
    3
    4
    5
    iptables-save > /etc/iptables.up.rules
    touch /etc/network/if-pre-up.d/iptables
    chmod +x /etc/network/if-pre-up.d/iptables
    echo '#!/bin/sh' >> /etc/network/if-pre-up.d/iptables
    echo '/sbin/iptables-restore < /etc/iptables.up.rules' >> /etc/network/if-pre-up.d/iptables
  • 安装Fail2Ban

    1
    2
    3
    4
    5
    6
    7
    8
    apt install fail2ban -y
    service fail2ban status

    // 查看状态
    cat /etc/fail2ban/jail.conf | less
    cat /etc/fail2ban/jail.d/defaults-debian.conf
    fail2ban-client status
    fail2ban-client status sshd
  • 使用本地配置文件,新增文件 /etc/fail2ban/jail.d/jail-debian.local,加入如下内容(日志文件如果不存在须手动创建,否则重启 fail2ban 报错):

1
2
3
4
5
6
7
8
9
10
11
[sshd]
enabled = true
port = 55555
maxretry = 3
bantime = 6000
filter = sshd
logpath = /var/log/auth.log
# 也可以不指定 `logpath`,
#而修改文件 `/etc/fail2ban/jail.d/defaults-debian.conf`, 在顶部添加:
[DEFAULT]
backend = systemd
  • 重启Fail2Ban:service fail2ban restart
  • 查看 SSH 登录失败记录:lastb
  • 手动禁止恶意 IP:fail2ban-client set sshd banip <IP>
  • 清空 lastb 记录:echo > /var/log/btmp

其他

开启 WARP (纯 IPv6 附加 CloudFlare IPv4)

1
wget -N https://gitlab.com/fscarmen/warp/-/raw/main/menu.sh && bash menu.sh 4

禁用 IPv6

编辑 /etc/sysctl.conf 文件,加入以下内容:

1
2
3
4
net.ipv6.conf.all.disable_ipv6 = 1 # 禁用全部网卡的IPv6

# 只针对指定网卡禁用 IPv6
# net.ipv6.conf.enp0s3.disable_ipv6 = 1

修改生效,执行命令:sudo sysctl -p

添加 SWAP 分区/文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fallocate -l 1G /swapfile # 创建文件
chmod 600 /swapfile # 修改文件权限
ls -lh /swapfile # 验证
mkswap /swapfile # 标记文件为 swap
swapon /swapfile # 启用 swap

# 持久化 swap
cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

# 查看当前 swapness 值(值越大,表示使用 swap 的可能性越大)
cat /proc/sys/vm/swappiness
# 调整 swapness 值
echo "vm.swappiness=15" >> /etc/sysctl.conf
echo "vm.vfs_cache_pressure = 70" >> /etc/sysctl.conf
sysctl -p

额外软件安装

1
apt update && apt upgrade -y && apt install -y git wget curl vim zsh build-essential gnupg unzip zip && apt autoremove -y
  • 可选:
    • zsh:一个功能强大的 Unix shell,类似于 Bash(Debian 默认的 shell)。它被设计成一个交互式登录 shell 和一个用于 shell 脚本的命令解释器。
    • build-essential:元软件包(meta-package),它本身不包含任何实际的软件,但它会依赖一系列在编译软件时通常会用到的基本工具。
    • gnupg:一个完整且免费的 OpenPGP 标准(RFC 4880)实现。它主要用于加密和签名数据及通信。
    • ouch:支持多种格式(7zrar(只支持解压)、tarzip…)的压缩、解压缩命令行工具。