如何在没有密码的情况下ssh到localhost?

can*_*sin 56 passwords ssh networking openssh login

编辑:准确地完成所做的事情

我需要SSH本地主机没有密码,通常的方式(使用公钥)不起作用.

user@PC:~$ rm -rf .ssh/*
user@PC:~$ ssh-keygen -t rsa > /dev/null 
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
user@PC:~$ ls .ssh/
id_rsa  id_rsa.pub
user@PC:~$ ssh-copy-id -i localhost 
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is f7:87:b5:4e:31:a1:72:11:8e:5f:d2:61:bd:b3:40:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
user@localhost's password: 
Now try logging into the machine, with "ssh 'localhost'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

user@PC:~$ ssh-agent $SHELL
user@PC:~$ ssh-add -L
The agent has no identities.
user@PC:~$ ssh-add 
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
user@PC:~$ ssh-add -L
ssh-rsa ...MY KEY HERE

user@PC:~$ ssh-copy-id -i localhost 
user@localhost's password: 
Now try logging into the machine, with "ssh 'localhost'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

user@PC:~$ ssh localhost echo 'testing'
user@localhost's password: 

user@PC:~$ 
Run Code Online (Sandbox Code Playgroud)

所以你可以在最后一个命令中看到它仍在询问密码!我该如何解决这个问题?Ubuntu-10.04,OpenSSH_5.3p1

EDIT2:

添加一些关于sshd的信息

user@PC:~$ cat /etc/ssh/sshd_config | grep Authentication
# Authentication:
RSAAuthentication yes
PubkeyAuthentication yes
RhostsRSAAuthentication no
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
ChallengeResponseAuthentication no
# PasswordAuthentication yes
Run Code Online (Sandbox Code Playgroud)

EDIT3:来自$ ssh -vv localhost的结果

$ssh -vv localhost
...
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/user/.ssh/identity
debug1: Offering public key: /home/user/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/user/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
user@localhost's password: 
Run Code Online (Sandbox Code Playgroud)

小智 138

我做了以下3个步骤来创建密码少登录

1. ssh-keygen -t rsa
Press enter for each line 
2. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
3. chmod og-wx ~/.ssh/authorized_keys 
Run Code Online (Sandbox Code Playgroud)

  • 我可以不使用第3步而使用无密码ssh吗?我何时需要执行第3步? (2认同)
  • @Rich:这只是为了防止[@shipr在下面的回答中提及](http://stackoverflow.com/a/10744443/98528).如果您的`〜/ .ssh/authorized_keys`已经存在且具有正确的权限,则不需要(3.),但也没有任何损害. (2认同)
  • 除了将 `id_rsa.pub` 附加到 `authorized_keys`(在我的 *Mac* 上在此之前不存在)之外,我还将它附加到了 `known_hosts`。我仍然被要求提供*密码* (2认同)

can*_*sin 17

发现了这个问题.

使用debuging运行服务器:

$sshd -Dd
Run Code Online (Sandbox Code Playgroud)

我发现它无法读取auth_key

$chmod 750 $HOME
Run Code Online (Sandbox Code Playgroud)

固定它.

  • 因为符号模式令人困惑.我不知道750会不会是我的头脑.类似'u = rwx,g = rx,o ='无论是什么,它都不比750容易!顺便说一句,755烫发工作也很好. (5认同)
  • 你们为什么不使用符号chmod模式?它不再是90年了,是吗? (3认同)

shi*_*ipr 13

另一个可能的答案:authorized_keys文件可能存在且可读.但如果它是组或世界可写的,它仍然会提示输入密码.这个问题的答案是

chmod og-wx ~/.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)


swa*_*ghi 5

执行以下步骤

ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
Run Code Online (Sandbox Code Playgroud)

使用默认文件和空密码(只需在接下来的两个步骤中按Enter键)

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
# Agent pid 59566
ssh-add 
Run Code Online (Sandbox Code Playgroud)

将〜/ .ssh/id_rsa.pub的内容复制到〜/ .ssh/authorized_keys

确保以下是权限

 ls -l .ssh/
 total 20
-rw-r--r--. 1 swati swati  399 May  5 14:53 authorized_keys
-rw-r--r--. 1 swati swati  761 Jan 12 15:59 config
-rw-------. 1 swati swati 1671 Jan 12 15:44 id_rsa
-rw-r--r--. 1 swati swati  399 Jan 12 15:44 id_rsa.pub
-rw-r--r--. 1 swati swati  410 Jan 12 15:46 known_hosts 
Run Code Online (Sandbox Code Playgroud)

另外,请确保.ssh目录的权限.这也很重要

drwx------.   2 swati swati    4096 May  5 14:56 .ssh
Run Code Online (Sandbox Code Playgroud)


Roh*_*twa 5

两个简单步骤:

ssh-keygen -t rsa <Press enter for each line>
ssh-copy-id localhost
Run Code Online (Sandbox Code Playgroud)

输入密码,您就完成了。