生成公鑰和私鑰
ssh-keygen
等同於ssh-keygen -t rsa
運行上面的命令後,系統會出現一系列提示,可以一路回車,例如:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/glowd/.ssh/id_rsa): Enter
特別說明,要不要對私鑰設置口令(passphrase),如果擔心私鑰的安全,可以設置一個。沒有特殊需求直接Enter,爲空
運行結束以後, 默認在 ~/.ssh目錄生成兩個文件:
id_rsa :私鑰
id_rsa.pub :公鑰
2.導入公鑰到認證文件,更改權限
導入本機
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
導入要免密碼登錄的服務器
首先將公鑰複製到遠端服務器,用戶爲glowd
scp {-P port} ~/.ssh/id_rsa.pub glowd@host:~
在服務器上
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
在服務器上更改權限(必須)
chmod 755 ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
以上執行之後,有些機器應該可以直接訪問了,可以測試一下
ssh {remote ip}
3.常見問題及解決方案
生成密鑰並上傳至遠程主機後,任然不可用
打開服務器的 /etc/ssh/sshd_config 這個文件,取消註釋。
AuthorizedKeysFile .ssh/authorized_keys
重啓服務器的ssh服務。
RHEL/CentOS系統
$ service sshd restart
ubuntu系統
$ service ssh restart
debian系統
$ /etc/init.d/ssh restart
執行ssh-copy-id 命令
ssh-copy-id glowd@remote ip
如果不是默認端口22,是9001
ssh-copy-id -p 9001 glowd@remote ip
ssh連接遠程主機時,出現 sign_and_send_pubkey: signing failed: agent refused operation 錯誤,並且還是需要輸入密碼
表示ssh-agent 已經在運行了,但是找不到附加的任何keys,就是說你生成的key,沒有附加到ssh-agent上,需要附加一下,執行
查看hosts中是否配置了git服務器的地址,那樣的話,會一直讓你輸入密碼
ssh-add
當然你也可以查看附加了哪些key
ssh-add -l
結果就可以了
ssh glowd@{remote ip}
或者,使用9001端口
ssh -p 9001 glowd@{remote ip}
如果顯示下面的錯誤
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for [git.bbdservice.net]:51668 has changed,
and the key for the corresponding IP address [10.10.80.26]:51668
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
6e:92:c8:d6:f2:45:ae:ff:5f:1d:21:8b:01:52:8a:82.
Please contact your system administrator.
Add correct host key in /home/glowd/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/glowd/.ssh/known_hosts:1
remove with: ssh-keygen -f “/home/glowd/.ssh/known_hosts” -R [git.bbdservice.net]:51668
ECDSA host key for [git.bbdservice.net]:51668 has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.
說明你連接的那一行訪問,驗證已經改變,/home/glowd/.ssh/known_hosts:1,刪除那一行,或者直接把整個known_hosts文件刪除,重新連接一次就好了
4.前面搞定了免密碼登錄,想想也是很激動了。但是,還要寫ip,還要寫用戶名,是不是又有點不爽啦。搞定用戶名和端口號
找到 ~/.ssh/config 文件,如果木有的話就自個兒建一個吧,內容如下:
Host {remote ip}
User {username}
Port {port}
現在連接就很簡單了
ssh {remote ip}
5.我現在已經可以ssh 到某臺機器,並且不用輸入用戶名,密碼了。但是我現在要使用github,或者github,怎麼辦了。
看網上有些人說,不同的賬號,要生成不同的公匙,私匙,也是有解決方案的
假設github,我的賬號是 [email protected]
$ ssh-keygen -t rsa -C “[email protected]”
Generating public/private rsa key pair.
Enter file in which to save the key (/home/glowd/.ssh/id_rsa): {輸入你想要保存密匙的文件名 假設id_rsa_github}
Enter passphrase (empty for no passphrase):{enter}
Enter same passphrase again:{enter}
Your identification has been saved in /home/glowd/.ssh/id_rsa_github.
Your public key has been saved in /home/glowd/.ssh/id_rsa_github.pub.
現在我們去/home/glowd/.ssh/目錄下,發現多了兩個文件id_rsa_github和id_rsa_github.pub
但是此時如果你把id_rsa_github.pub的內容,拷貝到github的ssh key裏面去,然後連接github
ssh -T [email protected]
會發現結果是
Permission denied (publickey).
那是因爲現在ssh還不能識別這個密匙-key
想要識別這個key也很簡單
ssh-add /home/glowd/.ssh/id_rsa_github
如果你想查看現在有那些key可以識別
ssh-add -l
現在有你的key了,執行
ssh -T [email protected]
會顯示你成功通過驗證了並且登錄,但是GitHub沒有提供shell訪問,意思不能使用登錄後的命令行操作github
Hi Glowdable! You’ve successfully authenticated, but GitHub does not provide shell access.
現在好了,我有兩個key,一個ssh 服務器,一個github。但是我們公司也有gitlab,那麼我還要再生成一個key,用gitlab的賬號嗎?
當然如果你願意是沒有問題,而且網上很多同學也是這樣說的,但是這是沒有道理的。
首先,我們生成的key,並不和賬號綁定,雖然我們看到,生成key時好像使用了賬號,其實那不是賬號,那只是comment-註釋。就是說,無論你寫什麼,都不會對生成的key產生影響
ssh-keygen -C “[email protected]”
那麼,我們看到最後生成的key裏面,是包含了這個comment的。這是事實,不過在驗證時,[email protected]後面的這部分是沒有作用的,你可以任意修改。
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZhsxICWPITRpvOTosjTmuo2osYAIet2DdjBZ/qoLcU6TZtrojIt17I48fYnm4eq9LI1jF7kG+ZwiuAuChRVl2VEOhGsgkk3xG10lQEpJt2RHSxaBfaVwu0BpwJ0bCaFIq/W73WxeWE2AzuG7/V+QjwtvBBQMFKrZ3nbgifgsndrT8KdE/pCGxuxgB5Xx5Y7mdAwxhHzseB9Yl+gCgstjMDe1FjTkRibQ9uQZKSoTKLxQZ+NZC7Ylniv21/CO105XwM0CF4wGlei0FE9bn7cs/1SNxM1Aae67eYADkfSvQIkRS6yvtJ4YxrZ9Xsdw7smy8kJ1wBqhDFMz1FMZp5oDh [email protected]
結論:密鑰不和任何comment綁定,任何地方使用都是一樣的,嚴格來說,如果沒有特殊需求,一個就夠了。系統默認在校驗的時候就會讀取ssh-add -l中key所對應的密鑰文件