ssh 密钥登录在 Ubuntu 和 CentOS 7 中都不起作用
Posted
技术标签:
【中文标题】ssh 密钥登录在 Ubuntu 和 CentOS 7 中都不起作用【英文标题】:ssh key login not working neither in Ubuntu nor CentOS 7 【发布时间】:2018-10-13 02:03:47 【问题描述】:我已经阅读并观看了很多教程,但我无法使用它。
在 CentOS 7 中我做了以下工作:
我创建了 /.ssh 文件夹,权限为 700,我是所有者
$ mkdir .ssh
$ chmod 700 .ssh/
然后我 cd 到它
$ cd .ssh
我开始生成密钥
$ ssh-keygen
我选择了密码
然后我在.ssh/
文件夹中创建了authorized_keys 文件
$ nano authorized_keys
我把 id_rsa.pub 的内容贴在里面了。
然后我继续编辑 sshd_config
$ sudo nano /etc/ssh/sshd_config
并更改了以下值
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# 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'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
我保存并退出。我重启了sshd
$ sudo systemctl restart sshd.service
然后我将id_rsa
和id_rsa.pub
文件下载到我的本地台式计算机。
我多次尝试使用 Windows 中的cmder software 通过 SSH 连接,如下所示:
λ ssh -i C:\Users\MyUsername\path\to\id_rsa.pub username@serverAddress
服务器确实要求我输入密码,但它总是错误的!它不起作用,它传递给默认用户名密码登录
:(
为什么密钥密码不起作用?我在设置中缺少什么???
我在 Ubuntu 中尝试过同样的方法,但它也无法正常工作。
【问题讨论】:
您的问题属于Super User。这是off-topic here。 Okkeii。我该如何移动这个问题?$ sudo mv thisquestion@*** thatquestion@superuser
你不能,一旦你接受并赞成一个答案。
【参考方案1】:
ssh -i C:\Users\MyUsername\path\to\id_rsa.pub username@serverAddress
你给 ssh 命令id_rsa.pub
吗?您必须提供密钥id_rsa
。
【讨论】:
成功了。还有一个问题,如果我使用不同名称的 ssh 密钥可以吗?例如,mySSHkey
和 mySSHkey.pub
?还是我必须进行一些额外的设置?
我认为从安全的角度来看,根据情况使用不同的SSH密钥是一个很好的做法。在 Linux 和 Mac 中,我们可以使用~/.ssh/config
来配置通过主机名使用哪个用户名和密钥。我不了解 Windows。以上是关于ssh 密钥登录在 Ubuntu 和 CentOS 7 中都不起作用的主要内容,如果未能解决你的问题,请参考以下文章