it-swarm.cn

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

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

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

[email protected]:~$ rm -rf .ssh/*
[email protected]:~$ 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: 
[email protected]:~$ ls .ssh/
id_rsa  id_rsa.pub
[email protected]:~$ 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.
[email protected]'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.

[email protected]:~$ ssh-agent $Shell
[email protected]:~$ ssh-add -L
The agent has no identities.
[email protected]:~$ ssh-add 
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
[email protected]:~$ ssh-add -L
ssh-rsa ...MY KEY HERE

[email protected]:~$ ssh-copy-id -i localhost 
[email protected]'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.

[email protected]:~$ ssh localhost echo 'testing'
[email protected]'s password: 

[email protected]:~$ 

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

EDIT2:

添加一些关于sshd的信息

[email protected]:~$ 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

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
[email protected]'s password: 
52
canesin

我做了以下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 
128
user2402070

发现了这个问题。

使用debuging运行服务器:

$sshd -Dd

我发现它无法读取auth_key

$chmod 750 $HOME

固定它。

16
canesin

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

chmod og-wx ~/.ssh/authorized_keys
13
shipr

执行以下步骤

ssh-keygen -t rsa -C "[email protected]"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.

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

# start the ssh-agent in the background
eval "$(ssh-agent -s)"
# Agent pid 59566
ssh-add 

将〜/ .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 

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

drwx------.   2 swati swati    4096 May  5 14:56 .ssh
5
swatisinghi

两个简单的步骤:

ssh-keygen -t rsa <Press enter for each line>
ssh-copy-id localhost

输入密码即可完成。

4
Rohit Patwa

如果你接受了一个问题,你可以接受的答案

    Agent admitted failure to sign using the key.

你需要

    ssh-add
1
J.stack

正确而安全的方法是复制密钥,如此处所述。

在其他情况下,sshpass可以很方便。

sshpass -p raspberry ssh [email protected]

请记住,这根本不安全。尽管在安全的环境中使用它并不是一个好主意,但它对于编写脚本,自动化测试非常有用......

这可以结合起来

ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no [email protected]

避免阻止脚本发生的确认问题。

同样,只在不同机器共享IP并且安全性不重要的开发系统中使用它。

https://ownyourbits.com/2017/02/22/easy-passwordless-ssh-with-sshh/

1
nachoparker

即使遵循了所有建议,我也遇到了同样的问题,但发现问题出在gnome-keyring干扰上。

解:

  1. 去搜索,查找“启动应用程序”
  2. 如果看到“SSH密钥代理”,请取消选中该框
  3. 重新启动计算机并连接到localhost。
0
Ananth Joshi

在Centos 7上

1 create rsa key
2 vim /etc/ssh/ssh_config
3
#   IdentityFile ~/.ssh/identity
uncoment this line > IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa

注意*我在复制密钥和其他一些答案之前就这样做了。但我很确定这就是你要做的所有事情,但如果没有,我会将rsa密钥附加到authorized_keys并运行

ssh-copy-id为用户名@ localhost

0
Don Davis

我用这种方式解决了ssh登录问题。

我在服务器端生成密钥对,然后将私钥scp回到我的Windows 10计算机,现在我可以无需密码登录。

以前我使用了我的10台笔记本电脑生成的密钥对,根本没有运气。

0
LazyRay