SSH 证书认证是一种比传统公钥认证更安全的方案。本文介绍 SSH 证书的工作原理和配置方法。
传统公钥认证的问题#
传统的 SSH 公钥认证需要在每台服务器上添加用户的公钥:
1
2
|
# 用户将公钥发送到服务器
ssh-copy-id user@server
|
这种方式在大规模环境中存在以下问题:
- 密钥管理困难:新增或撤销用户需要手动更新所有服务器
- 无时效性:公钥永久有效,离职后难以撤销
- 无法追踪:无法知道谁在什么时候登录过
- 扩展性差:成百上千台服务器时维护成本极高
SSH 证书认证的原理#
SSH 证书认证引入**证书颁发机构(CA)**的概念:
- CA 密钥:管理员创建一个 CA 私钥,用于签发证书
- 用户证书:CA 使用私钥对用户公钥进行签名,生成证书
- 服务器信任 CA:服务器只需配置信任 CA 公钥,无需逐个添加用户公钥
- 证书验证:用户登录时提交证书,服务器通过 CA 公钥验证证书有效性
证书包含的信息#
- 用户公钥
- 用户 ID(Key ID)
- 有效期(起始时间 + 结束时间)
- 允许的登录用户名(Principals)
- CA 签名
创建 SSH CA#
首先创建 CA 密钥对。建议使用 ED25519 算法:
1
2
3
4
5
|
# 创建用户证书CA
ssh-keygen -t ed25519 -f ~/.ssh/user_ca -C "SSH User CA"
# 创建主机证书CA(可选,用于验证服务器身份)
ssh-keygen -t ed25519 -f ~/.ssh/host_ca -C "SSH Host CA"
|
生成的 user_ca 是 CA 私钥,user_ca.pub 是 CA 公钥。
注意:CA 私钥必须妥善保管,建议离线存储或使用硬件密钥。
签发用户证书#
基本用法#
1
2
3
4
5
6
|
# 先生成用户密钥对
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "user@example.com"
# 使用 CA 签发用户证书
ssh-keygen -s ~/.ssh/user_ca -I "user@example.com" \
-n ubuntu -V +52w ~/.ssh/id_ed25519.pub
|
参数说明:
-s ~/.ssh/user_ca:指定 CA 私钥
-I "user@example.com":证书 Key ID(用于日志追踪)
-n ubuntu:允许登录的用户名(Principal)
-V +52w:证书有效期(52 周)
生成的证书文件为 id_ed25519-cert.pub。
签发多用户证书#
1
2
3
|
# 签发允许多个用户名的证书
ssh-keygen -s ~/.ssh/user_ca -I "admin@example.com" \
-n ubuntu,root,deploy -V +26w ~/.ssh/id_ed25519.pub
|
签发主机证书#
1
2
3
|
# 为服务器签发主机证书
ssh-keygen -s ~/.ssh/host_ca -I "server1.example.com" \
-h ~/.ssh/ssh_host_ed25519_key.pub
|
配置服务器#
配置服务器信任 CA#
在服务器的 /etc/ssh/ 目录下:
1
2
3
4
5
|
# 将 CA 公钥添加到信任列表
cat ~/.ssh/user_ca.pub >> /etc/ssh/trusted-user-ca-keys
# 可选:添加主机CA公钥(用于验证服务器身份)
cat ~/.ssh/host_ca.pub >> /etc/ssh/known_hosts
|
修改 SSH 配置#
编辑 /etc/ssh/sshd_config:
1
2
3
4
5
6
7
8
|
# 启用证书认证
TrustedUserCAKeys /etc/ssh/trusted-user-ca-keys
# 可选:禁用传统公钥认证(提高安全性)
# PubkeyAuthentication no
# 可选:启用主机证书验证
# HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub
|
重启 SSH 服务:
客户端配置#
配置证书#
在客户端的 ~/.ssh/config 中指定证书:
1
2
3
|
Host *
IdentityFile ~/.ssh/id_ed25519
CertificateFile ~/.ssh/id_ed25519-cert.pub
|
验证配置#
在输出中看到 Offering public key: ED25519-CERT 即表示正在使用证书认证。
证书管理#
查看证书信息#
1
|
ssh-keygen -L -f ~/.ssh/id_ed25519-cert.pub
|
输出示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/root/.ssh/id_ed25519-cert.pub:
Type: ssh-ed25519-cert-v01@openssh.com user certificate
Public key: ED25519-CERT SHA256:xxxxx
Signing CA: ED25519 SHA256:xxxxx
Key ID: "user@example.com"
Serial: 0
Valid: from 2026-04-29T00:00:00 to 2027-04-28T00:00:00
Principals:
ubuntu
Critical Options: (none)
Extensions:
permit-X11-forwarding
permit-agent-forwarding
permit-port-forwarding
permit-pty
permit-user-rc
|
撤销证书#
如果用户离职或证书泄露,需要撤销证书。有两种方式:
方式一:使用撤销列表(推荐)
1
2
3
4
5
6
7
8
9
10
11
|
# 创建撤销列表
touch /etc/ssh/revoked_keys
chmod 644 /etc/ssh/revoked_keys
# 在 sshd_config 中启用
echo "RevokedKeys /etc/ssh/revoked_keys" >> /etc/ssh/sshd_config
# 撤销证书(添加公钥而非证书)
echo "ssh-ed25519 AAAAxxxx user@compromised" >> /etc/ssh/revoked_keys
systemctl restart sshd
|
方式二:设置证书过期时间
通过设置短有效期来简化撤销:
1
2
3
|
# 签发短有效期证书
ssh-keygen -s ~/.ssh/user_ca -I "temp@example.com" \
-n ubuntu -V +24h ~/.ssh/id_ed25519.pub
|
续期证书#
证书过期前重新签发即可:
1
2
|
ssh-keygen -s ~/.ssh/user_ca -I "user@example.com" \
-n ubuntu -V +52w ~/.ssh/id_ed25519.pub
|
安全最佳实践#
- 分离 CA:用户 CA 和主机 CA 分离管理
- 离线 CA:CA 私钥离线存储,仅在签发证书时使用
- 合理有效期:根据安全需求设置证书有效期
- 日志审计:记录证书登录事件,便于审计
- 最小权限:仅授予必要的 Principals
- 备份 CA:妥善备份 CA 密钥,防止丢失
SSH 证书认证通过引入 CA 机制,解决了大规模环境中的密钥管理难题:
| 特性 |
传统公钥 |
证书认证 |
| 密钥分发 |
每台服务器 |
只需配置 CA |
| 时效性 |
永久有效 |
可设置过期 |
| 撤销 |
手动删除 |
撤销列表 |
| 追踪 |
困难 |
Key ID 可追溯 |
| 扩展性 |
差 |
好 |
通过本文的配置方法,可以在企业中实现集中化的 SSH 访问控制,提升安全性和管理效率。
参考来源#