配置sshd使用CA签名证书登录_ssh证书登录_更新sshd服务端的通讯密钥
转载注明来源: 本文链接 来自osnosn的博客,写于 2022-11-06.
用CA签名证书登录
- 请参考【SSH 证书登录教程】,这个旧了。
- 参考:【Creating SSH CA Certificate Signing Keys】,这是基于openSSH-5.3p1
- 【SSH 证书登录教程】
- 【基于CA签名的用户公钥管理】,【基于CA签名的用户公钥管理】
- 【证书登录的流程】
- 【Linux 采用 SSH CA 登陆验证】
- putty-0.77 还不支持这种认证。putty-0.78 开始支持CA签名证书认证。
- winscp-5.21.5 还不支持这种认证。下一个版本可能会支持。
ssh登录过程中,基于签名(证书)的用户认证
-
基于 openSSH-8.0p1 , 测试成功。
-
创建 用于签发用户证书的密钥(用户CA)。管理员创建,私钥不传递。
ssh-keygen -t ed25519 -C user_ca@myCA -f user_ca
。
得到两个文件,user_ca
,user_ca.pub
。
只是密钥对,不是证书。因为没做自签名证书。 -
让sshd服务端信任"用户CA"的公钥。(方法一,用户级)
建议限制 principals。如果多个服务器信任同一个CA,只要每个服务器上的 principals 都不同。
就能做到,用同一个CA签发的用户证书,能登录其中一个服务器,而不能登录别的服务器。
在sshd服务器上,用户的~/.ssh/authorized_keys
中加入一行(用户CA的公钥)。此文件权限mode要求是0600。
cert-authority,principals="host02,allhost" ssh-ed25519 AAAAC.....xx user_ca@myCA
内容为 user_ca.pub 文件的内容。
如果用户证书包含 host02 或 allhost 才能登录(此种情况不判断用户名)。
每个想用证书登录的用户的~/.ssh/authorized_keys
都要加一行。- 不做限制,在sshd服务器上,用户的
~/.ssh/authorized_keys
中加入一行(用户CA的公钥)。
cert-authority ssh-ed25519 AAAAC.....xx user_ca@myCA
如果用户证书没有principals,可以登录
如果用户证书有principals,必须包含用户名 才能登录。
- 不做限制,在sshd服务器上,用户的
-
让sshd服务端信任"用户CA"的公钥。(方法二,全局系统级)
在sshd服务器上,在/etc/ssh/sshd_config
中加入一行:
TrustedUserCAKeys /etc/ssh/user_ca_list.pub
,
重启 sshd 服务生效,service sshd restart
。
这种配置,用户证书必须有principals,且包含用户名才能登录。
user_ca_list.pub
可以多行,CA公钥文件user_ca.pub
的内容占其中一行。文件权限mode可以是0600或0644。
例如:ssh-ed25519 AAAAC.....xx Comment
-
静态限制 principals
在sshd服务器上,创建 principals 目录mkdir /etc/ssh/principals
。此目录权限mode要求是0755。
然后在/etc/ssh/sshd_config
中再加入一行,重启 sshd 服务。
AuthorizedPrincipalsFile /etc/ssh/principals/%u
。
之后再次修改user_ca_list.pub
和/etc/ssh/principals/
中的文件,无需重启sshd即生效。AuthorizedPrincipalsFile
,只能配置一个路径,不支持多个路径。- 根据不同的用户名, 限制 principals, 比如
/etc/ssh/principals/root
文件:
一行一个 principal。文件权限mode必须是0644。## 注释:/etc/ssh/principals/root文件内容 principl_01 principl_02
- 用户证书包含任意一个 principal 才能登录。这种情况不匹配用户名,用户证书的principals包含"用户名"无用。
- 如果对应用户的文件不存在或为空,用户证书无论包含什么princpals,或者无principal,都不能登录。
-
动态限制 principals
在sshd服务器上,/etc/ssh/sshd_config
中再加入两行:
AuthorizedPrincipalsCommand /etc/ssh/princ.sh %u %U %s %i
,
AuthorizedPrincipalsCommandUser root
, (或指定一个别的用户身份)
重启 sshd 服务生效,service sshd restart
。#!/bin/sh # 例子 /etc/ssh/princ.sh 文件, 权限mode是 0755,owner,group 都是 root # 传入四个参数 $1=username,$2=user ID, $3=证书的serial num, $4=证书的key ID # 例子中, $3 $4 没有使用 # 因 OpenSSH 权限提升漏洞 (CVE-2021-41617), 此程序只能由管理员编写,不应该开放给用户修改。 host=myhost02 buf="$host-$1" buf2=$(echo "$buf" |/bin/shasum -) buf2=${buf2%% *-} echo "$buf2" if [ "$2" -eq 0 ]; then echo "$host-root" echo "allhost-root" else echo "$buf" echo "allhost-user" fi
-
AuthorizedPrincipalsCommandUser abc
指定程序以 abc 用户身份执行。
可以接受 "%u" 表示以登录的身份执行, 但不建议。 -
另一个例子,也可以通过webserver查询principals。
AuthorizedPrincipalsCommand /bin/curl -s http://localhost/sshprincipals?user=%u&serial=%s&id=%i
-
任何用户登录,使用的用户证书,签名的CA在
TrustedUserCAKeys
列表中,
sshd 都会先检查AuthorizedPrincipalsFile
中的 principals,
如果找不到,才会执行AuthorizedPrincipalsCommand
程序。
如果程序的输出中,也没有找到匹配的 principals,则认证失败。 -
任何用户登录,使用的用户证书,签名的CA不在
TrustedUserCAKeys
列表中,
sshd不使用AuthorizedPrincipalsFile
,AuthorizedPrincipalsCommand
中的principals。 -
用
-