2011-09-16 86 views
46

編輯:正是完成了什麼如何SSH密碼到本地沒有密碼?

我需要沒有密碼的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:Ading從$導致的ssh -vv本地主機

$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: 

回答

99

我以下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 
+1

工程就像一個魅力!謝謝 –

+1

我可以不使用步驟3無密碼的SSH?我什麼時候需要做第3步? –

+2

@Rich:這只是爲了防止[@shipr在他的回答下面提到](http://stackoverflow.com/a/10744443/98528)。如果你的'〜/ .ssh/authorized_keys'已經存在並具有正確的權限,則不需要(3),但也沒有任何損害。 – akavel

13

已經發現了問題。

運行與調試運行的服務器:

$sshd -Dd 

我發現這是不能夠讀取AUTH_KEY

$chmod 750 $HOME 

固定它。

+3

爲什麼你們不使用符號chmod模式?這不再是90年代了,是嗎? –

+5

因爲符號模式很混亂。我不知道什麼750將是我的頭頂。就像'u = rwx,g = rx,o ='不管它是什麼,它都不會比750更容易!順便說一句,755燙髮也很好。 –

+0

非常有用。我使用了'/ usr/sbin/sshd -ddddD',並得到了以下消息'Authentication refused:對於目錄/ home/...的所有權或模式不合適。' – Leonardo

10

另外可能回答:authorized_keys文件可能存在並且可讀。但是如果它是羣組或世界可寫的,它仍然會提示輸入密碼。這個問題的答案的問題是

chmod og-wx ~/.ssh/authorized_keys 
+0

比我目前閱讀的任何內容都更好。 – OcuS

2

執行以下步驟

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. 

使用默認的文件和空密碼(只需在接下來的2個步驟中按回車鍵)

# 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 
1

兩個簡單的步驟:

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

輸入密碼,你就大功告成了。

1

這樣做的正確和安全的方法是複製鑰匙,就像這裏所說的。

在其他情況下,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/

0

爲接受的答案呢, 如果你encount你需要

ssh-add 
0

我面臨着同樣的問題,甚至以下的所有建議後

Agent admitted failure to sign using the key. 

問題,但發現問題在於gnome-keyring干擾。

解決方案:

  1. 開始搜索,尋找「啓動應用程序」
  2. 如果你看到「SSH密鑰代理」,取消勾選
  3. 重新啓動計算機,並連接到本地主機。
+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/17994750) – kometen