ssh - ssh 密钥登录在 Ubuntu 和 CentOS 7 中都不起作用
问题描述
我已经阅读并观看了很多教程,但我无法让它工作。
在 CentOS 7 中,我做了以下事情:
我创建了具有 700 权限的 /.ssh 文件夹,我作为所有者
$ 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 软件通过 SSH 连接,如下所示:
λ ssh -i C:\Users\MyUsername\path\to\id_rsa.pub username@serverAddress
服务器确实要求我输入密码,但它总是错误的!它不起作用,它传递给默认用户名密码登录
:(
为什么密钥密码不起作用?我在设置中缺少什么???
我在 Ubuntu 中尝试过同样的方法,但它也不起作用。
解决方案
ssh -i C:\Users\MyUsername\path\to\id_rsa.pub username@serverAddress
你给id_rsa.pub
ssh 命令吗?你必须给id_rsa
, 密钥。
推荐阅读
- android - 如何加密存储在我的 android 应用程序的 localstorage 中的 Cookie sqlite3 数据库?
- flutter - 如何在运行时根据 Flutter 中的 AppBar 内容动态改变 AppBar 的高度?
- redis - 构建 Redish 哈希存储的首选方式
- react-native - 如何在 React-Native 论文中保留 TextInput 的值?
- python - Pyglet 声音不会加载,“.load”给出一个 AttributeError 但它在前一天工作
- python - 将值添加到 Python 中的键字典
- postgresql - 比较 postgresql 中的时间戳返回小时:分钟不包括周末
- javascript - 旋转图标引导程序 3
- nested-lists - 如何按列表中的第二个元素对嵌套列表进行排序,然后输出整个列表中第二低的元素?
- c# - Unity C# 调用在以下方法或属性之间不明确:`System.Math.Round(double, int)' 和 `System.Math.Round(decimal, int)