linux - 检查是否为 SSH 配置了 RSA

标签 linux shell encryption rsa

我已经使用用户 X 在我的服务器 A 和客户端 B 之间安装/配置了 RSA 加密。当我以用户 X 身份执行脚本时,我的脚本工作正常,但我也想考虑不同的场景
1. 如果用户 Y 执行它提示输入密码身份验证的脚本,而不是我希望脚本抛出错误并继续。
2. 如果客户端 C 没有配置服务器 A(rsa 加密),我想抛出一个不同的错误并继续。

当前脚本

ssh $server_name 'mcell -q -n "'"$i"'" ' || echo " Failed to SSH "

问题:
1. 我可以在 SSH 之前检查是否安装了 RSA 吗?
2. 我可以在调用 SSH 之前检查客户端安装了哪个用户 RSA 吗?

最佳答案

RSA(如 DSA 和 ECDSA)是内置于 SSH 中的非对称密码,因此无需“安装”。您的意思可能是您为用户 X 生成了一个 RSA key 对,并使用该 key 对对服务器 A 进行公钥身份验证。

SSH默认支持多种认证机制,公钥认证只是其中一种(参见man sshd, AUTHENTICATION一节):

Finally, the server and the client enter an authentication dialog. The client tries to authenticate itself using host-based authentication, public key authentication, challenge-response authentication, or password authentication.

可用的身份验证方法是按顺序尝试的,这意味着先尝试公钥身份验证,然后再尝试密码身份验证。

如果您使用用户 X 运行 ssh -v,您将获得如下调试输出:

...
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: <b>Authentications that can continue: publickey,password</b>
debug1: <b>Next authentication method: publickey</b>
debug1: Offering RSA public key: /home/X/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: Enabling compression at level 6.
debug1: <b>Authentication succeeded (publickey).</b>
Authenticated to servera ([192.168.23.42]:22).
...

而对于用户 Y,输出将如下所示:

...
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: <b>Authentications that can continue: publickey,password</b>
debug1: <b>Next authentication method: publickey</b>
debug1: Trying private key: /home/Y/.ssh/id_rsa
debug1: <b>Next authentication method: password</b>
Y@servera's password: 

用户Y没有私钥,所以认证方式失败,接下来尝试密码认证。为防止您需要将以下行添加到服务器的 sshd_config:

PasswordAuthentication no
UsePAM no

并重新启动 sshd。重启后,用户 Y 的登录尝试将失败,并出现“权限被拒绝”错误:

...
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: <b>Authentications that can continue: publickey</b>
debug1: <b>Next authentication method: publickey</b>
debug1: Trying private key: /home/Y/.ssh/id_rsa
debug1: <b>Authentications that can continue: publickey</b>
debug1: <b>No more authentication methods to try.</b>
<b>Permission denied (publickey).</b>

您还可以在客户端禁用密码验证,可以通过创建文件 /home/Y/.ssh/config 为特定用户禁用,也可以为 /中的所有用户禁用etc/ssh/ssh_config(不是 sshd_config):

Host *
  PasswordAuthentication no

如果您想将该限制限制在特定主机上,请将 * 替换为主机名。

关于linux - 检查是否为 SSH 配置了 RSA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27996201/

相关文章:

linux - 通过 ssh 连接并重试的 Shell 脚本

java - 命令行 Jasypt 客户端加密 'Operation not possible'

php - 如何使用 PHP 解密对称加密的 OpenPGP 消息?

sql-server-ce - SQL Server Compact数据库文件解密解锁

android - Android 5/L 上的 Valgrind 崩溃

linux - 从 grep 中排除 .svn 目录

linux - 如何防止root用户误删除重要文件

c - 过程信号掩码,阻塞信号集和阻塞信号之间的区别?

linux - Crontab 以不同方式执行脚本

bash - 在 bash 中的命令之前设置变量