1. 運行 git-bash.exe 進入命令行
2. 判斷是否已存在本地公鑰:
cat ~/.ssh/id_rsa.pub
如果看到一長串以 ssh-rsa 或 ssh-dsa 開頭的字符串,可以跳過 ssh-keygen 步驟
3. 生成 ssh key
ssh-keygen -t rsa -C "自定義標識符"
生成代碼會有兩個步驟,提示設置密碼(默認沒有密碼)、pub文件名稱及保持路徑,按Enter直接跳過步驟使用默認值。需要注意的是,如果自定義了文件名/路徑,需要在 SSH 客戶端做配置(步驟5開始介紹)。
用以下的命令或你生成的公鑰:
cat ~/.ssh/id_rsa.pub(或者你自定義的目錄/文件名)
4. 復制公鑰到阿里雲個人設置中的 SSH/My SSH Keys下,不同系統的復制命令如下:
Windows:
clip < ~/.ssh/id_rsa.pub(或者你自定義的目錄/文件名)
Mac:
pbcopy < ~/.ssh/id_rsa.pub
GUN/Linux(requires xclip):
xclip -sel clip < ~/.ssh/id_rsa.pub
5. 如果使用非默認位置/文件名存放 ssh key,必須配置好 SSH 客戶端以找到你的私鑰取鏈接 code 服務器,通常實在 ~/.ssh/config 配置
1 # git bash下執行,創建並修改config文件
2 touch ~/.ssh/config
在 config 添加如下配置:
1 # Our company's internal GitLab server 2 Host my-git.company.com3 RSAAuthentication yes4 IdentityFile ~/.ssh/id_rsa(或者是你自定義的目錄&文件名)
6. 驗證是否成功
ssh -T git@github.com
7. Git 下的一些配置
a. 生成多個 ssh key
重復執行步驟3,生成多個 ssh key,並自定義名稱/保存路徑即可
b. 管理多個 ssh key
運行 ssh-agent 命令添加私鑰:
1 ssh-add ~/.ssh/id_rsa_gitlab 2 ssh-add ~/.ssh/id_rsa_github
如果執行上述命令出現 Could not open a connection to your authentication agent,解決方法如下:
1 # 殺死 ssh-agent 線程 2 ps aux|grep ssh 3 kill -9 線程號 4 5 # 進入用戶名目錄下的 .ssh 目錄,打開 git bash 執行如下命令: 6 exec ssh-agent bash 7 eval ssh-agent -s 8 9 # 再執行 ssh-add 命令 10 ssh-add ./id_rsa_gitlab 11 ssh-add ./id_rsa_github
c. 創建並修改 config 配置文件
1 # gitlab 2 Host gitool.glanway.com 3 HostName gitool.glanway.com 4 PreferredAuthentications publickey 5 IdentityFile ~/.ssh/id_rsa_gitlab 6 User your-username 7 8 # github 9 Host github.com 10 HostName github.com 11 PreferredAuthentications publickey 12 IdentityFile ~/.ssh/id_rsa_github 13 User your-username
參考文章:
https://code.aliyun.com/help/ssh/README 阿里雲幫助文檔
https://www.cnblogs.com/mingyue5826/p/11141324.html Git安裝及配置SSH Key