首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何强制SSH只允许具有密钥的用户登录?

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

Ask Ubuntu用户
提问于 2013-09-17 22:35:24
回答 4查看 304.8K关注 0票数 126

我试着遵循这里的说明:http://lani78.wordpress.com/2008/08/08/generate-a-ssh-key-and-disable-password-authentication-on-ubuntu-server/

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

这是我的sshd_config文件-我有遗漏什么吗?我已经试过重新启动SSH和计算机本身了。

代码语言:javascript
复制
# 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
EN

回答 4

Ask Ubuntu用户

发布于 2013-09-17 22:51:58

默认情况下,PasswordAuthentication设置为yes,因此在/etc/ssh/sshd_config中显式注释它并重新启动sshd没有任何效果。

您需要显式地将PasswordAuthentication no设置为只允许公钥身份验证。

代码语言:javascript
复制
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no

PubkeyAuthentication yes

注意(man sshd_config):PasswordAuthentication指定是否允许密码身份验证。默认情况是肯定的。

并重新启动sshd

  • 用于sysvinit service ssh restart
  • 用于systemd systemctl restart sshd.service

此外,最佳实践是使用以下指令(顺序) DenyUsers AllowUsers DenyGroups AllowGroups来实现更精细的SSH访问控制粒度和灵活性。->引用:sshd_配置5男子 -> Ubuntu手册页不再包含此内容,因为它吸收了openssh上游文档(但是FreeBSD、EL 7、8手册页仍然有它们)。

最后但并非最不重要

注意:请注意将UsePAM no设置为这样的方式:密码锁定用户帐户(这不同于禁用/过期的用户帐户->、man passwdman usermod)将无法登录,即使它们具有在~/.ssh/authorized_keys中配置的公钥身份验证。

票数 164
EN

Ask Ubuntu用户

发布于 2014-11-25 09:21:36

根据这个关于SSH键的wiki页面和这个回答,您需要在您的sshd_config中更改这两行:

代码语言:javascript
复制
PasswordAuthentication no
ChallengeResponseAuthentication no
票数 17
EN

Ask Ubuntu用户

发布于 2015-10-09 08:27:35

/etc/ssh/sshd_config中,下面的设置适用于我:

代码语言:javascript
复制
PasswordAuthentication no
UsePAM no

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

票数 5
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/346857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档