本文最后更新于 2025年11月19日 晚上
自动更新安装安全补丁
1 2 3 4
| sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure unattended-upgrades
|
配置时区和NTP服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| # 设置时区 sudo timedatectl set-timezone Asia/Shanghai # 查看 date
# 查看同步状态 # System clock synchronized # NTP service timedatectl
# 启用systemd-timesyncd,并打开NTP(若未开启同步) # 安装 sudo apt install systemd-timesyncd -y #启用 sudo systemctl enable systemd-timesyncd sudo systemctl start systemd-timesyncd # 打开NTP sudo timedatectl set-ntp true # 查看同步状态,应变成 # System clock synchronized: yes # NTP service: active timedatectl
|
修改ssh端口号
1.修改配置文件
1
| sudo nano /etc/ssh/sshd_config
|
找到#Port 22这一行,取消注释并修改为其他端口号
2.重启ssh生效
保持ssh连接
1.编辑配置文件
1
| nano /etc/ssh/sshd_config
|
2.添加配置
1 2
| ClientAliveInterval 60 ClientAliveCountMax 3
|
ClientAliveInterval:指定了服务器端向客户端请求的时间间隔,默认是 0 ,不发送。60 表示每分钟发送一次,然后客户端响应,这样就保持长连接了。
ClientAliveCountMax:使用默认值 3 即可,ClientAliveCountMax 表示服务器发出请求后,客户端没有响应的次数达到一定值,就自动断开。正常情况下,客户端不会不响应。
3.重启sshd
SSH开启密钥登录
1.生成密钥对
1
| ssh-keygen -t rsa -b 4096
|
一路enter跳过即可,会在/root/.ssh下生成公钥id_rsa.pub和私钥id_rsa (公钥是用于服务器端,私钥是用于客户端) 然后将私钥id_rsa下载至电脑上(登录时会用到)
2.将公钥添加到 authorized_keys
1 2 3
| cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh
|
3.修改配置文件
1 2 3 4 5 6 7
| sudo nano /etc/ssh/sshd_config
PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no UsePAM yes
|
3.重启ssh生效
1
| sudo systemctl restart ssh
|
开启BBR
第一步:检查内核版本
首先,使用 uname 命令检查你当前的内核版本,需要的内核版本号大于或等于 4.9
第二步:修改 sysctl 配置
编辑 /etc/sysctl.conf 文件:
1
| sudo nano /etc/sysctl.conf
|
在文件的末尾添加以下两行配置:
1 2
| net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr
|
第三步:应用新配置
保存并关闭文件后,运行以下命令来使配置立即生效:
第四步:验证 BBR 是否成功开启
最后,你可以通过以下命令来验证 BBR 是否已经成功启用:
1 2 3 4
| sysctl net.ipv4.tcp_congestion_control
lsmod | grep bbr
|
开启Swap
一、查看当前 Swap 状态
二、创建 Swap 文件
假设你想创建一个 2G 的 Swap(推荐:内存 1G → Swap 2G):
1 2 3 4
| sudo fallocate -l 2G /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
|
三、设置权限(安全必须)
1
| sudo chmod 600 /swapfile
|
四、格式化为 Swap
输出类似:Setting up swapspace version 1, size = 2 GiB
五、启用 Swap
1 2 3 4
| sudo swapon /swapfile
swapon --show free -h
|
六、开机自动挂载
1 2 3 4
| sudo nano /etc/fstab
/swapfile none swap sw 0 0
|
七、(可选)调整 Swap 优先级与倾向
默认系统优先使用内存,可稍微调低 Swap 使用频率:
1 2 3 4 5 6 7 8 9 10 11 12
|
sudo sysctl vm.swappiness=80
echo "vm.swappiness=80" | sudo tee -a /etc/sysctl.conf
|
八、(可选)查看内存使用情况
配置fail2ban和SSH封禁
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
| apt install fail2ban -y
touch /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2295
filter = sshd
backend = systemd
maxretry = 3 findtime = 10m
bantime = 6h
banaction = iptables-multiport banaction_allports = iptables-allports
|
安装docker和docker compose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| sudo apt remove docker docker-engine docker.io containerd runc -y
sudo apt update sudo apt install -y ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker version docker-compose version
|
更新软件包
1
| sudo apt update -y && sudo apt upgrade -y
|