Debian/PVE/Ubuntu/FnOS 配置 SSH 服务🔐

SSH(Secure Shell)是一种加密的网络传输协议,用于在不安全的网络中提供安全的远程登录和其他安全网络服务。本指南将详细介绍在 Debian 12 上配置和优化 SSH 服务的完整流程。
📋 目录导航
📖 简介
SSH(Secure Shell)是一种加密的网络传输协议,用于在不安全的网络中提供安全的远程登录和其他安全网络服务。本指南将详细介绍在 Debian 12 上配置和优化 SSH 服务的完整流程。
🛠️ 一、SSH 服务安装与基本配置
1. 📦 安装 SSH 服务器并启用服务
1 2 3 4 5 6 7 8 9 10 11
| sudo apt-get update && sudo apt-get install openssh-server -y
sudo systemctl start ssh
sudo systemctl enable ssh
sudo systemctl status ssh
|
2. 📝 查看 SSH 配置文件
1 2 3 4 5
| cat /etc/ssh/sshd_config
less /etc/ssh/sshd_config
|
3. 💾 备份原始配置文件
1 2
| sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup.$(date +%Y%m%d)
|
⚙️ 二、SSH 服务优化配置
1. ⚡ 使用 sed 命令修改 SSH 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo sed -i.bak \ -e 's/^#Port 22/Port 22/' \ -e 's/^#PermitRootLogin.*/PermitRootLogin yes/' \ -e 's/^#GSSAPIAuthentication.*/GSSAPIAuthentication no/' \ -e 's/^#UseDNS.*/UseDNS no/' \ -e 's/^#Compression.*/Compression yes/' \ -e 's/^#TCPKeepAlive.*/TCPKeepAlive yes/' \ -e 's/^#ClientAliveInterval.*/ClientAliveInterval 10/' \ -e 's/^#ClientAliveCountMax.*/ClientAliveCountMax 999/' \ /etc/ssh/sshd_config
sudo systemctl restart ssh
|
2. 📋 配置说明
配置项 |
原值 |
修改后值 |
说明 |
Port |
#Port 22 |
Port 22 |
SSH 服务端口 |
PermitRootLogin |
#PermitRootLogin prohibit-password |
PermitRootLogin yes |
允许 root 用户登录 |
GSSAPIAuthentication |
#GSSAPIAuthentication no |
GSSAPIAuthentication no |
禁用 GSSAPI 认证 |
UseDNS |
#UseDNS no |
UseDNS no |
禁用 DNS 反向解析 |
ClientAliveInterval |
#ClientAliveInterval 0 |
ClientAliveInterval 10 |
客户端活跃检查间隔(秒) |
ClientAliveCountMax |
#ClientAliveCountMax 3 |
ClientAliveCountMax 999 |
客户端活跃检查最大次数 |
TCPKeepAlive |
#TCPKeepAlive yes |
TCPKeepAlive yes |
启用 TCP 保活机制,检测连接状态 |
Compression |
#Compression delayed |
Compression yes |
启用数据压缩,节省带宽但增加 CPU 开销 |
3. 查看 SSH 修改
1
| grep -E 'Port 22|PermitRootLogin|GSSAPIAuthentication|UseDNS|Compression|TCPKeepAlive|ClientAliveInterval|ClientAliveCountMax|PubkeyAuthentication|AuthorizedKeysFile' /etc/ssh/sshd_config
|
三 、客户端连接 SSH
1. 查看服务器 IP 地址
1
| hostname -I | awk '{print $1}'
|
输出示例:
root@debian13 ~ # hostname -I | awk ‘{print $1}’
10.10.10.246
2. 客户端连接
1
| ssh -p 22 root@10.10.10.246
|
🔒 四、高级安全配置
1. 🛡️ 增强 SSH 安全性
1 2
| sudo nano /etc/ssh/sshd_config
|
在配置文件中添加或修改以下选项:
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
| Port 2222
MaxAuthTries 3
LoginGraceTime 60
MaxSessions 10
PasswordAuthentication no PubkeyAuthentication yes
AllowUsers admin root
AllowGroups ssh-users
ClientAliveInterval 300 ClientAliveCountMax 2
|
2. 🔑 创建 SSH 密钥对
1 2 3 4 5 6 7
| ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id -p 22 user@your_server_ip
|
🔥 五、防火墙配置
1. 🛡️ 配置 UFW 防火墙
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo apt install ufw -y
sudo ufw allow ssh
sudo ufw allow 2222/tcp
sudo ufw enable
sudo ufw status verbose
|
2. 🔧 使用 iptables 配置防火墙
1 2 3 4 5 6 7 8 9
| sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 2222 -j ACCEPT
sudo apt install iptables-persistent -y sudo netfilter-persistent save
|
🔍 六、故障排除与监控
1. 📊 检查 SSH 服务状态
1 2 3 4 5 6 7 8 9 10 11
| sudo systemctl status ssh
sudo journalctl -u ssh -f
last
sudo grep "Failed password" /var/log/auth.log
|
2. 🧪 测试 SSH 连接
1 2 3 4 5 6 7 8
| ssh -v user@your_server_ip
ssh -p 2222 user@your_server_ip
ssh -i ~/.ssh/your_private_key user@your_server_ip
|
🚀 七、性能优化
1. ⚡ 优化 SSH 连接速度
1 2 3 4 5 6 7 8
| sudo nano /etc/ssh/sshd_config
UseDNS no GSSAPIAuthentication no Compression yes TCPKeepAlive yes
|
2. 🔄 启用 SSH 连接复用
1 2 3 4 5
| echo -e "Host *\n ControlMaster auto\n ControlPath ~/.ssh/sockets/%r@%h-%p\n ControlPersist 600" >> ~/.ssh/config
mkdir -p ~/.ssh/sockets
|
💾 八、备份与恢复
1. 📦 备份 SSH 配置
1 2 3 4 5 6 7 8
| sudo mkdir -p /backup/ssh
sudo tar -czf /backup/ssh/ssh_backup_$(date +%Y%m%d).tar.gz /etc/ssh
sudo tar -czf /backup/ssh/authorized_keys_backup_$(date +%Y%m%d).tar.gz /home/*/.ssh/authorized_keys /root/.ssh/authorized_keys 2>/dev/null || true
|
2. 🔄 恢复 SSH 配置
1 2 3 4 5 6 7 8 9 10 11
| sudo systemctl stop ssh
sudo tar -xzf /backup/ssh/ssh_backup_$(date +%Y%m%d).tar.gz -C /
sudo tar -xzf /backup/ssh/authorized_keys_backup_$(date +%Y%m%d).tar.gz -C /
sudo systemctl start ssh
|
🔎 九、安全审计脚本
1. 📜 创建 SSH 安全审计脚本
1 2
| sudo nano /usr/local/bin/ssh_audit.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
| #!/bin/bash
echo "=== SSH 安全审计 ===" echo "审计时间: $(date)" echo "=========================="
echo "SSH 服务状态:" systemctl status ssh | grep "Active:"
echo -e "\n开放端口:" netstat -tulnp | grep ssh
echo -e "\n最近登录记录:" last | head -10
echo -e "\n失败登录尝试:" grep "Failed password" /var/log/auth.log | tail -5
echo -e "\n配置文件权限:" ls -la /etc/ssh/sshd_config
echo -e "\nRoot 登录设置:" grep "PermitRootLogin" /etc/ssh/sshd_config
echo -e "\n审计完成!"
|
2. ⚡ 设置脚本权限并执行
1 2 3 4 5 6 7 8
| sudo chmod +x /usr/local/bin/ssh_audit.sh
sudo /usr/local/bin/ssh_audit.sh
echo "0 0 * * 0 root /usr/local/bin/ssh_audit.sh >> /var/log/ssh_audit.log" | sudo tee -a /etc/crontab
|
🐛 十、常见问题解决
1. 🔍 SSH 连接问题排查
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sudo systemctl status ssh
sudo ufw status
sudo netstat -tulnp | grep :22
sudo sshd -t
sudo journalctl -u ssh -n 50 --no-pager
|
2. ⏰ 连接超时问题
1 2
| echo -e "Host *\n ServerAliveInterval 60\n ServerAliveCountMax 5" >> ~/.ssh/config
|
📋 十一、最佳实践总结
- 🔄 更改默认端口:将 SSH 端口从 22 改为其他端口,减少自动化攻击
- 🚫 禁用 root 登录:使用普通用户登录后再切换至 root
- 🔑 使用密钥认证:禁用密码认证,只允许密钥认证
- 🌐 限制访问 IP:使用防火墙限制可访问 SSH 的 IP 范围
- 🔄 定期更新:保持系统和 SSH 软件包更新到最新版本
- 📊 监控日志:定期检查 SSH 日志,监控异常登录尝试
- 🛡️ 使用 Fail2Ban:安装 Fail2Ban 防止暴力破解攻击
- 💾 定期备份:定期备份 SSH 配置和授权密钥
通过以上配置,您可以在 Debian 12 上搭建一个安全、高效的 SSH 服务,实现安全的远程访问!🔐 记得定期审计和维护您的 SSH 配置哦!😊
Debian/PVE/Ubuntu/FnOS 配置 SSH 服务 🔐