1.檢查是否已經(jīng)存在SSH keys
打開(kāi)終端,輸入命令
$ ls -al ~/.ssh
如果已存在,則結(jié)果會(huì)列出目錄文件列表,則進(jìn)入第3步。
. .. id_rsa id_rsa.pub
如果不存在,則結(jié)果如下,則進(jìn)入第2步。
# ls: /Users/hony/.ssh: No such file or directory
2.生成SSH Keys
在終端中輸入以下命令,其中郵箱填寫(xiě)你的郵箱地址
$ ssh-keygen -t rsa -b 4096 -C "*your_email@example.com*"
# Generating public/private rsa key pair.
在接下來(lái)的步驟中,按回車鍵:
Enter file in which to save the key (/Users/hony/.ssh/id_rsa):
再次回車
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
結(jié)果看到這個(gè)圖形,就可以了
+---[RSA 4096]----+
| o . |
|.o + o + |
|o o + o * . |
|.. o.o + = |
|.o.o .+ S |
| + =+ X . |
|.o oEoO B . |
|o.o o+ B o |
|..o+. + . |
+----[SHA256]-----+
3.添加 SSH key 到 ssh-agent
1.確保ssh-agent是可用的
$ eval "$(ssh-agent -s)"
# Agent pid 2806
2.添加SSH key 到 ssh-agent
# id_rsa是SSH key對(duì)應(yīng)的文件的名字,如果要使用一個(gè)已經(jīng)存在的key添加到ssh-agent的話,替換id_rsa名字即可
$ ssh-add ~/.ssh/id_rsa
# Identity added: ...
4.把 SSH key 添加到遠(yuǎn)程倉(cāng)庫(kù)的賬號(hào)上
1.復(fù)制 SSH key 到粘貼板
$ pbcopy < ~/.ssh/id_rsa.pub
2.登錄github或bitbucket等遠(yuǎn)程倉(cāng)庫(kù)添加SSH key即可。