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


11

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

Permission denied (publickey......).

是否有一个标志告诉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.....).

我想要:

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

解决方案必须在客户端(例如ssh命令的某些标志或使用管道),我无法触摸sshd_config或任何其他系统配置文件。因为-通常-我构建了访问LAN中服务器的第三方软件(因此我无法生成密钥或配置系统文件),所以密码保存在DB中(因此无需再次尝试)。并且在我的代码中,如果我能够假设自己只有一次尝试ssh/ scp将简化相关代码。


1
如答案所示,您可以对此进行修改,但是为什么要这样做呢?人们经常输入错字,默认设置是允许这种脆弱性。一个更好的问题可能是“我怎么锁定发生故障的用户ñ登录尝试?” 特别是如果您要防止恶意登录。
— msw

我的原因是复杂而漫长的。我可以说的是,如果密码第一次不正确-下次它将继续不正确。
— 2013年

2
您想执行最基本的系统管理功能(身份验证),但不能触摸系统配置文件吗?你不走运 如果你关心你的解释长期和复杂的原因,也许我们也许能够看到X代替Ÿ你已经在你的介绍XY问题
— 城市生活垃圾

1
我可以看到这甚至是一个问题的唯一方法是,如果你正在做密码身份验证非交互,在你使用做任何工具的情况下是应该要么设施一个失败的密码后,中止或者,如果不,那么该实用程序应成为此问题的主题。
— Sammitch 2013年

1
我还注意到ssh注意到对公钥auth的尝试失败,您是否考虑过复制公钥并完全避免这种情况?
— Sammitch 2013年

Answers:


14

在sshd config手册页中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.

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

/etc/init.d/ssh restart 

要么

service ssh restart

在客户端,可以使用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.

因此,编辑~/.ssh/config文件并添加:

 Host <name_or_ip_of_host|*>
     NumberOfPasswordPrompts 1

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

  ssh -o NumberOfPasswordPrompts=1 user@hostname 

请参阅问题编辑。我希望使用某些标志/管道,而不希望更改sshd_config
— Nir 2013年

我已经编辑了给出客户端选项的答案。
— Drav Sloan

0

查看中的MaxAuthTries sshd_config。默认值为6,因此请记住,在选择值时,您可能先尝试pubkey auth,然后再尝试进行密码auth。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.