如何在 ssh 命令中禁用重试密码

Nir*_*Nir 12 password ssh scp

我希望 ssh 命令只允许一次输入密码的机会,如果第一次密码错误,ssh 将返回

Permission denied (publickey......).
Run Code Online (Sandbox Code Playgroud)

是否有一个标志告诉 ssh 只请求一次密码?

代替:

[nir@dhcppc4 ~]$ ssh pokemon@192.168.1.103
pokemon@192.168.1.103's password: 
Permission denied, please try again.
pokemon@192.168.1.103's password: 
Permission denied, please try again.
pokemon@192.168.1.103's password: 
Permission denied (publickey.....).
Run Code Online (Sandbox Code Playgroud)

我想要:

[nir@dhcppc4 ~]$ ssh pokemon@192.168.1.103
pokemon@192.168.1.103's password: 
Permission denied (publickey.....).
Run Code Online (Sandbox Code Playgroud)

解决方案必须在客户端(例如 ssh 命令的某些标志或使用管道),我不能 touchsshd_config或任何其他系统配置文件。因为 - 通常 - 我构建了访问 LAN 中服务器的 3rd 方软件(因此我无法生成密钥或配置系统文件),密码保存在数据库中(因此不需要第二次尝试)。在我的代码中,如果我能够假设我只有一次尝试ssh/scp它将简化相关代码。

Dra*_*oan 16

在 sshd 配置手册页中man 5 sshd_config

 MaxAuthTries
     Specifies the maximum number of authentication attempts permitted
     per connection.  Once the number of failures reaches half this
     value, additional failures are logged.  The default is 6.
Run Code Online (Sandbox Code Playgroud)

因此,设置MaxAuthTries 2将是您需要的设置。sshd之后需要重新启动(必须以 root 身份运行):

/etc/init.d/ssh restart 
Run Code Online (Sandbox Code Playgroud)

或者

service ssh restart
Run Code Online (Sandbox Code Playgroud)

在客户端,这可以使用 ssh 设置进行设置(查看man 5 ssh_config您可以应用的设置):

 NumberOfPasswordPrompts
         Specifies the number of password prompts before giving up.  The
         argument to this keyword must be an integer.  The default is 3.
Run Code Online (Sandbox Code Playgroud)

所以编辑你的~/.ssh/config文件并添加:

 Host <name_or_ip_of_host|*>
     NumberOfPasswordPrompts 1
Run Code Online (Sandbox Code Playgroud)

<name_or_ip_of_host|*>规范IP或者您正在使用的命令行上的主机名,或*为所有主机的连接尝试。您还可以在命令行上指定它而无需编辑您的/.ssh/config文件:

  ssh -o NumberOfPasswordPrompts=1 user@hostname 
Run Code Online (Sandbox Code Playgroud)