6.5. 使用智能卡验证配置 SSH 访问
SSH 连接需要身份验证。您可以使用密码或证书。按照以下流程,使用保存在智能卡中的证书启用验证。
有关使用 authselect
配置智能卡的详情,请参考使用 authselect 配置智能卡。
先决条件
- 该智能卡包含您的证书和私钥。
- 该卡插入读卡器并连接到计算机。
pcscd
服务正在您的本地计算机上运行。详情请查看安装用于管理和使用智能卡的工具。
流程
在使用智能卡验证的用户主目录中为 SSH 密钥创建新目录:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mkdir /home/<example_user>/.ssh
# mkdir /home/<example_user>/.ssh
使用
opensc
库运行ssh-keygen -D
命令以使用智能卡中的私钥检索现有公钥对,并将其添加到用户的 SSH 密钥目录的authorized_keys
列表中,以便通过智能卡验证启用 SSH 访问。Copy to Clipboard Copied! Toggle word wrap Toggle overflow ssh-keygen -D /usr/lib64/pkcs11/opensc-pkcs11.so >> ~<example_user>/.ssh/authorized_keys
# ssh-keygen -D /usr/lib64/pkcs11/opensc-pkcs11.so >> ~<example_user>/.ssh/authorized_keys
SSH 需要访问
/.ssh
目录和authorized_keys
文件的适当配置。要设置或更改访问权限,请输入:Copy to Clipboard Copied! Toggle word wrap Toggle overflow chown -R <example_user:example_user> ~<example_user>/.ssh/ chmod 700 ~<example_user>/.ssh/ chmod 600 ~<example_user>/.ssh/authorized_keys
# chown -R <example_user:example_user> ~<example_user>/.ssh/ # chmod 700 ~<example_user>/.ssh/ # chmod 600 ~<example_user>/.ssh/authorized_keys
验证
显示密钥:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow cat ~<example_user>/.ssh/authorized_keys
# cat ~<example_user>/.ssh/authorized_keys
终端会显示密钥。
您可以使用以下命令验证 SSH 访问:
ssh -I /usr/lib64/opensc-pkcs11.so -l <example_user> localhost hostname
# ssh -I /usr/lib64/opensc-pkcs11.so -l <example_user> localhost hostname
如果配置成功,会提示您输入智能卡 PIN。
这个配置现在可以在本地运行。现在,您可以复制公钥并将其分发到您要使用 SSH 的所有服务器中的 authorized_keys
文件。