如何强制SSH仅允许具有密钥的用户登录?


73

我尝试按照以下说明进行操作:http : //lani78.wordpress.com/2008/08/08/generate-a-ssh-key-and-disable-password-authentication-on-ubuntu-server/

只允许服务器上具有公共密钥的用户进行身份验证,但是我无法让SSH禁止仅使用用户名/密码登录。

这是我的sshd_config文件-我缺少任何内容吗?我已经尝试过重新启动SSH和计算机本身。

# Package generated configuration file
# See the sshd_config(5) manpage for details


# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes


# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768


# Logging
SyslogFacility AUTH
LogLevel INFO


# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes


RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile        %h/.ssh/authorized_keys


# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes


# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no


# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no


# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication no


# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes


# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes


X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no


#MaxStartups 10:30:60
#Banner /etc/issue.net


# Allow client to pass locale environment variables
AcceptEnv LANG LC_*


Subsystem sftp /usr/lib/openssh/sftp-server


# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no

1
仅供参考:实际上,重新启动sshd并不是必需的。/etc/inid.d/ssh reload命令应该足够了。
杜仲

不要忘记取消注释#AuthorizedKeysFile并将公用密钥复制到〜/ .ssh / authorized_keys(然后重新启动)。没有这个,它将无法正常工作。
ivanleoncz

如果在2016年不是真的,那么在2019年肯定是需要重新启动的情况。重新加载是不够的。
KDN

Answers:


98

即使您在中将其注释掉,默认情况下PasswordAuthentication也将其设置为yes/etc/ssh/sshd_config

您需要明确设置PasswordAuthentication no为仅允许公共密钥身份验证。

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no

注意(man sshd_config):PasswordAuthentication指定是否允许密码认证。默认值为是。

并重新启动sshd service ssh restart(预系统迁移)或systemctl restart sshd.service


6
我们也应该有UsePAM no
Konstantinos 2015年

@pidosaurus为什么?到底有什么好处呢?
jan-glx 2015年

1
@YAK我喜欢保持简单,我不想使用PAM。但是有人可以使用正确配置的PAM身份验证。我认为此链接很有启发性:arlimus.github.io/articles/usepam
Konstantinos

1
还可以考虑禁用ChallengeResponseAuthentication,请参见superuser.com/a/374234/2879
cic 2016年

13

根据这个有关SSH密钥的维基页面和此答案,您需要在您的以下两行中进行更改sshd_config

PasswordAuthentication no
ChallengeResponseAuthentication no

1
关于挑战响应的第二行有什么不同?
Ryan Burnette

1
“本身不提供“附加安全性。”术语“ ChallengeResponseAuthentication”只是一个OpenSSH配置关键字;它指的是SSH协议中的“键盘交互” userauth方法
pzkpfw

4

在中/etc/ssh/sshd_config,以下设置对我有用:

PasswordAuthentication no
UsePAM no

最后,重新启动sshd守护程序。


3

默认情况下,sshd_config文件中异常注释掉了您想要的行。

# Change to no to disable tunnelled clear text passwords
--->#PasswordAuthentication yes

要禁用密码,请将更改yesno删除注释

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

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.