惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
C
Check Point Blog
有赞技术团队
有赞技术团队
H
Help Net Security
V
Vulnerabilities – Threatpost
N
News | PayPal Newsroom
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 最新话题
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
爱范儿
爱范儿
I
InfoQ
V
Visual Studio Blog
O
OpenAI News
Google DeepMind News
Google DeepMind News
S
Security Affairs
T
Troy Hunt's Blog
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
N
Netflix TechBlog - Medium
Latest news
Latest news
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Webroot Blog
Webroot Blog
S
Schneier on Security
MongoDB | Blog
MongoDB | Blog
T
Tor Project blog
V2EX - 技术
V2EX - 技术
Security Latest
Security Latest
Cloudbric
Cloudbric
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
S
Securelist
C
Cisco Blogs
博客园_首页
TaoSecurity Blog
TaoSecurity Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threat Research - Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts

博客园 - .net's

在Kubernetes中探索Pod多容器模式 特性:定义共享行为 译:Zed 解码:为什么不直接嵌入 Neovim? 9大关于生产力的错误认知 如何用费曼技巧快速学习任何东西 如何高效使用 Todoist — 完整指南 译:Building a Text Editor in the Times of AI 在AI时代构建文本编辑器 Combine and Conquer in Vim aa babel new Parse import.meta.url Installing Yarn on WSL wsl 2安装 vim备忘录 -- 可视模式 vim 备忘录 -- 插入模式 Windows Terminal PowerShell智能提示 安装配置Oh My Posh windows 10安装和配置NeoVim 0.8.2 Search and replace
SSH 服务器与客户端配置最佳实践
.net's · 2025-01-21 · via 博客园 - .net's

SSH 服务器与客户端配置最佳实践

目录

  1. SSH 服务端设置

    • 安装 SSH 服务器
    • 配置 SSH 服务器(最佳实践)
    • 启用密钥认证
    • 防火墙配置
    • 重启 SSH 服务
  2. SSH 客户端设置

    • 生成 SSH 密钥对(推荐 Ed25519)
    • 上传公钥到服务器
    • 配置 SSH 客户端(简化连接)
  3. 连接测试

    • 使用密钥连接
    • 验证配置
  4. 高级安全配置

    • 更改默认端口
    • 禁用密码登录
    • 使用 Fail2Ban 防止暴力破解
    • 限制用户访问
  5. 常见问题排查

    • 连接失败
    • 权限问题
    • 防火墙问题

1. SSH 服务端设置

1.1 安装 SSH 服务器

Ubuntu/Debian 系统

sudo apt update
sudo apt install openssh-server

CentOS/RHEL 系统

sudo yum install openssh-server

Fedora 系统

sudo dnf install openssh-server

1.2 配置 SSH 服务器(最佳实践)

编辑 SSH 配置文件 /etc/ssh/sshd_config

sudo nano /etc/ssh/sshd_config

推荐配置(最佳实践)

Port 2222                          # 更改默认端口,避免使用 22
PermitRootLogin no                 # 禁用 root 登录
PubkeyAuthentication yes           # 启用公钥认证
PasswordAuthentication no          # 禁用密码认证
AuthorizedKeysFile .ssh/authorized_keys # 指定公钥文件路径
ClientAliveInterval 300            # 设置空闲超时为 300 秒
ClientAliveCountMax 2              # 最多发送 2 次保活信号
AllowUsers username1 username2     # 仅允许特定用户登录

1.3 启用密钥认证

确保 ~/.ssh/authorized_keys 文件存在并包含客户端的公钥:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

1.4 防火墙配置

允许 SSH 流量通过防火墙。

Ubuntu/Debian 系统

sudo ufw allow 2222/tcp
sudo ufw enable

CentOS/RHEL/Fedora 系统

sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload

1.5 重启 SSH 服务

Ubuntu/Debian 系统

sudo systemctl restart ssh

CentOS/RHEL/Fedora 系统

sudo systemctl restart sshd

2. SSH 客户端设置

2.1 生成 SSH 密钥对(推荐 Ed25519)

在客户端生成 SSH 密钥对(推荐使用 Ed25519 算法):

ssh-keygen -t ed25519 -C "your_email@example.com"

按提示选择保存路径和设置密码(可选)。


2.2 上传公钥到服务器

使用 ssh-copy-id 将公钥上传到服务器:

ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip -p 2222

如果 ssh-copy-id 不可用,可以手动将公钥内容添加到服务器的 ~/.ssh/authorized_keys 文件中。


2.3 配置 SSH 客户端(简化连接)

编辑客户端的 SSH 配置文件 ~/.ssh/config

nano ~/.ssh/config

添加以下内容:

Host myserver
    HostName server_ip
    Port 2222
    User username
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 60         # 每 60 秒发送保活信号
    StrictHostKeyChecking no       # 首次连接时自动接受主机密钥(仅用于测试环境)

3. 连接测试

3.1 使用密钥连接

ssh myserver

3.2 验证配置

  • 确保连接成功。
  • 检查日志文件 /var/log/auth.log(Ubuntu/Debian)或 /var/log/secure(CentOS/RHEL)以确认认证方式为公钥认证。

4. 高级安全配置

4.1 更改默认端口

  • 避免使用默认的 22 端口,改为一个高位端口(如 2222)。
  • 更新防火墙规则以允许新端口。

4.2 禁用密码登录

  • 确保 /etc/ssh/sshd_configPasswordAuthentication 设置为 no

4.3 使用 Fail2Ban 防止暴力破解

  • 安装 Fail2Ban:
    sudo apt install fail2ban
    
  • 配置 Fail2Ban 监控 SSH 日志:
    sudo nano /etc/fail2ban/jail.local
    
    添加以下内容:
    [sshd]
    enabled = true
    port = 2222
    filter = sshd
    logpath = /var/log/auth.log
    maxretry = 3
    bantime = 3600
    

4.4 限制用户访问

  • /etc/ssh/sshd_config 中使用 AllowUsersAllowGroups 限制访问:
    AllowUsers username1 username2
    AllowGroups sshusers
    

5. 常见问题排查

5.1 连接失败

  • 检查服务器 IP 和端口是否正确。
  • 确保 SSH 服务正在运行:
    sudo systemctl status sshd
    

5.2 权限问题

  • 确保 ~/.ssh 目录权限为 700
    chmod 700 ~/.ssh
    
  • 确保 ~/.ssh/authorized_keys 文件权限为 600
    chmod 600 ~/.ssh/authorized_keys
    

5.3 防火墙问题

  • 检查防火墙是否允许 SSH 端口:
    sudo ufw status
    
    sudo firewall-cmd --list-ports
    

总结

通过以上最佳实践,您可以:

  1. 提高 SSH 连接的安全性。
  2. 简化客户端连接配置。
  3. 防止暴力破解和未授权访问。