熱門文章

2017年2月10日 星期五

免密碼 SSH Key 登入 ssh server

每次使用ssh 登入vps 都需要輸入那個又臭又長的密碼
後來找到使用公私鑰的方式來達成免密碼登入ssh


首先建立SSH Key

$ ssh-keygen -t rsa

➜  .ssh git:(master) ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/***/.ssh/id_rsa): 
/Users/***/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/QJpioneer/.ssh/id_rsa.
Your public key has been saved in /Users/QJpioneer/.ssh/id_rsa.pub.


步驟是 enter enter enter ,但因為我原本就有了所以多一個 enter y enter enter

按下enter預設會在你的家目錄底下的 .ssh 產生 id_rsa 與 id_rsa.pub
因為我原本就有ssh key 所以多一個是否覆蓋的選項(因為忘記密碼...)
怕忘記密碼所以就不設passphrase了


Mac 上可以安裝一個方便的程式來幫你設定好遠端的ssh

brew install ssh-copy-id

➜  .ssh git:(master) ssh-copy-id root@****.com
/usr/local/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/****/.ssh/id_rsa.pub"
/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/local/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

root@****.com's password: 

在這邊輸入一次的ssh server的密碼後之後就不用再輸入了,好方便啊


Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'root@qjpioneer.com'"
and check to make sure that only the key(s) you wanted were added.



#1組key增加
#現在請輸入 "ssh 'root@qjpioneer.com'" 登入server !!!


參考 http://ephrain.pixnet.net/blog/post/58483708-%5Blinux%5D-產生-ssh-key-來免密碼登入-ssh-server

PS. 如果你跟我一樣以前有ssh key 卻忘記密碼可以偷過這個找回來:https://help.github.com/articles/recovering-your-ssh-key-passphrase/



沒有留言:

張貼留言