【征文202540】【教程】✅Linux服务器荒岛求生记:一次搞定从SSH登录、禁用Root、密钥认证,到防火墙、Fail2ban防爆、自动更新

本文仅在idcflare发布,禁止任何形式未经授权的转载

同时希望大家积极点赞回复喽:partying_face:

好久不写教程了,今天来随便写点什么,看看能不能拿个纪念徽章之类的
内容可能比较简单(但有点杂),会的人可以不看
如果有地方讲错了记得提醒一下

1. 前言

本教程适用于大部分云服务器/物理机,得是 Linux 系统(Ubuntu/Debian/CentOS 等等,他们与 linux 是包含关系)
这里解答一下为什么大部分服务器是 linux 系统
linux 开源免费,同时系统占用较小,最小安装可以低至 100mb 左右(同时完整安装最低 3g)
而 Windows Server 在某些情况下系统占用高,而且可能还会有许可费用

2. 让我们开始吧!

你的服务器必须要有公网 ip,同时得是 linux 系统
现在,
打开终端:
Windows:PowerShell 或 “Windows Terminal”
macOS/Linux:自带 Terminal
如果你想在手机上耍酷,可以装个termux(Release v0.118.3 · termux/termux-app · GitHub
)
让我们进行登录(第一次):
ssh root@服务器IP
之后按照提示输入密码(提示:密码输入无回显)
成功后会显示下面类似的内容

2.1 创建普通用户(部分情况下可以不做)

[!info] 为什么要做?
root 权限太大,有时候操作不当就会导致 game over,所以我们最好创建一个普通用户

创建用户(这里举例用户名 neo,你可以换成别的喽)

Ubuntu/Debian 系命令
adduser neo
# 过程中会让你设密码,照做。其他信息一路回车即可。
CentOS/RHEL/Alma/Rocky 系命令
adduser neo
passwd neo

把新用户neo加入 sudo 管理员组

Ubuntu/Debian 命令
usermod -aG sudo neo
CentOS/RHEL/Alma/Rocky 命令
usermod -aG wheel neo

3. 整个密钥

[!warning]
可以有效防止黑客爆破,当然,如果你的云服务商支持直接配置,你可以直接在你云服务厂商的配置面板搞一下,下面是命令方法

生成密钥命令
ssh-keygen -t ed25519 -C "你的备注"
# 一路回车即可,生成的公钥通常在 ~/.ssh/id_ed25519.pub
查看公钥命令
cat ~/.ssh/id_ed25519.pub

在服务器上,以 root 身份执行:

[!tip] 确保密钥登录可用后再进行下一步操作
先测试密钥登录是否正常,然后再禁用密码登录

配置服务器密钥命令
su - neo
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# 要把刚才打印出的整行公钥粘贴进下面命令的 >> 后面
echo "ssh-ed25519 AAAA...你的公钥..." >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

然后重新测一下可不可以用密钥登录(务必)示范(内容是瞎打的)

测试密钥登录命令
cat > ~/neo_key << 'EOF'
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACD7B8GjEnjG6JjoKMRM+Yicgufh0aYoe9zOltL1r6mutwAAAJgJfTl9CX05
fQAAAAtzc2gtZW818TUxOQAAACD7B8GjEnjG6JjoKMRM+Yicgufh0aYoe9zOltL1r6mutw
AAAEA6TsA3LumhA5QgHYsMnMdQ/BmkyTfzEAfH1IDzg04pv717waMSeMbomOgoxEz5iJyC
5+HRpih73M6W0vWvqa63AAAAFG5lb0BSYWluWXVuLUdUWUwwNWRxAQ==
-----END OPENSSH PRIVATE KEY-----
EOF
chmod 600 ~/neo_key
ssh -i ~/neo_key [email protected]

4. 禁root,禁密码(确保密钥可用)

[!danger]
以下操作用 sudo 权限执行(用新用户登录后做),执行前请确保密钥登录可用

配置 SSH 命令
# 备份 SSH 配置
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

# 这三条把关键项改成更安全的值
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sudo sshd -t
sudo systemctl reload sshd

5. 更新系统(可以防止漏洞)

[!info]
定期更新系统可以修复已知漏洞,提高服务器安全性

Ubuntu/Debian 更新命令
sudo apt update
sudo apt -y full-upgrade
# 如果提示有内核更新,重启:
sudo reboot
CentOS/RHEL/Alma/Rocky 更新命令
sudo dnf -y update || sudo yum -y update
sudo reboot

6. 防火墙限制

[!warning]
之前就有云服务器厂商默认开启全部端口…到现在都没改…所以最好做一下这一步
吃瓜,乐子云服务器厂商,可以猜猜这是哪家


端口全开?

Ubuntu/Debian(使用 UFW)

UFW 配置命令
sudo apt -y install ufw

# 允许 SSH(如果你改了端口,改成你的端口)
sudo ufw allow 22/tcp

# 常用的 Web 端口(按需放行)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# 开启防火墙并查看状态
sudo ufw enable
sudo ufw status numbered

CentOS/RHEL/Alma/Rocky(使用 firewalld)

firewalld 配置命令
sudo dnf -y install firewalld || sudo yum -y install firewalld
sudo systemctl enable --now firewalld

# 允许 SSH(改端口就换成你的)
sudo firewall-cmd --permanent --add-service=ssh

# 按需放行 Web 端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload
sudo firewall-cmd --list-all

配置 Swap(让内存不够时不至于"pass away")

[!tip]
如果你的小小服务器内存 ≤ 4G,建议加个 1~2G 的 swap;内存很大可以不配喽

Swap 配置命令
# 创建 2G 交换文件(按需调整大小)
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# 开机自动挂载
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# 检查
free -h

7. 装点常用工具喽

[!info]
当然不用的话可以不装

Ubuntu/Debian 工具安装
sudo apt -y install htop iotop iftop curl wget git vim net-tools unzip ncdu fail2ban
CentOS/RHEL/Alma/Rocky 工具安装
sudo dnf -y install htop iotop iftop curl wget git vim net-tools unzip ncdu fail2ban || \
sudo yum -y install htop iotop iftop curl wget git vim net-tools unzip ncdu fail2ban
工具 用途
htop 看 CPU/内存
ncdu 找占空间的大文件
fail2ban 自动封禁暴力破解 IP

8. 放爆破

[!todo]
启用 Fail2ban(自动挡的防爆破)
自动识别多次失败登录并封 IP

Fail2ban 配置命令
# 启用并开机自启
sudo systemctl enable --now fail2ban

# 建议创建本地配置覆盖
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# 至少确保 sshd 监狱是启用的(默认常启)
sudo systemctl restart fail2ban

# 查看状态
sudo fail2ban-client status
sudo fail2ban-client status sshd

9. 自动更新

[!success]
安全补丁自动打,不怕漏洞

Ubuntu/Debian 自动更新配置
sudo apt -y install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
CentOS/RHEL/Alma/Rocky 自动更新配置
sudo dnf -y install dnf-automatic || sudo yum -y install yum-cron
# dnf-automatic 的常见开启方式
sudo sed -i 's/apply_updates = .*/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer

10. 整个docker?(用来部署应用)

[!question]
Docker 是一个开源的容器化平台,可以让应用程序在任何环境中一致地运行,让你更快的部署项目

Ubuntu/Debian(简单法):

Docker 安装(Ubuntu/Debian)
sudo apt -y install docker.io
sudo systemctl enable --now docker
sudo usermod -aG docker $USER
# 重新登录终端后,验证:
docker run hello-world

CentOS/RHEL/Alma/Rocky:

Docker 安装(CentOS/RHEL/Alma/Rocky)
sudo dnf -y install docker || sudo yum -y install docker
sudo systemctl enable --now docker
sudo usermod -aG docker $USER

13. 公网端口测试

[!example]
安装 Nginx 并测试 web 服务是否正常运行,务必做一下

Ubuntu/Debian:

Nginx 安装和测试(Ubuntu/Debian)
sudo apt -y install nginx
sudo systemctl enable --now nginx
curl -I http://127.0.0.1

CentOS/RHEL/Alma/Rocky:

Nginx 安装和测试(CentOS/RHEL/Alma/Rocky)
sudo dnf -y install nginx || sudo yum -y install nginx
sudo systemctl enable --now nginx
curl -I http://127.0.0.1

访问 http://服务器IP

之后可以就好了

117 个赞

好文,感谢佬的教程,涨知识

3 个赞

感谢大佬分享精彩的教程

3 个赞

感谢教程

2 个赞

收藏学习

2 个赞

今天刚买了一个服务器,按照佬友教程基本做了一遍~感谢佬友分享捏~

2 个赞

我选择一般的小鸡纯密钥root,顺便ufw控端口,一般也够了。真生产服务器,当然也是个人玩的,直接服务zerotrust穿透出来再套其它cdn,全端口包括22关闭,ssh走zerotrust或者frp进堡垒机

4 个赞

感谢佬的教程,之前一直没有开启unattended-upgrades每次都手动ssh进去升级 :joy:
我想问一下有没有啥适合用来做监控的方案,这样就不用ssh上去也能看到异常了。目前在用node-exporter,Prometheus,grafana,但是不知道有没有更好的方案:thinking:

3 个赞

感谢大佬分享

3 个赞

实用,谢了

2 个赞

非常感谢大佬的经常分享,必须赞一个

2 个赞

感谢分享

2 个赞

轻量化用探针就能干这活

2 个赞

点赞收藏,感谢佬友的分享。

2 个赞

知识点太密集了。存着慢慢看

2 个赞

感谢教程

2 个赞

每天都学新知识,感谢分享

2 个赞

感谢分享

3 个赞

感谢大佬教程

2 个赞

满满干货啊 :smiling_face_with_three_hearts:

3 个赞