一柿赊、密鑰生成
前往ssh存儲(chǔ)路徑
mac:cd ~/.ssh
win:cd C:\Users\用戶(hù)名\.ssh
ssh-keygen -t rsa -C xx@xxxxx.com
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/mac/.ssh/id_ed25519): xxxx
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
說(shuō)明:一般git服務(wù)器登錄頁(yè)面后賬號(hào)設(shè)置中 SSH Keys 管理都會(huì)說(shuō)明如何添加導(dǎo)入
- -t 指密鑰的類(lèi)型趋急,gitee舉例ed25519蒋川,github舉例ed25519廉油,coding舉例rsa
- -C 指注釋 一般填寫(xiě)郵箱地址
- Enter File 提示你輸入要生成的密鑰文件名
- Enter passphrase 提示你要輸入的密鑰密碼 (不輸入默認(rèn)確認(rèn)默認(rèn)為空惠险,如果設(shè)置請(qǐng)記錄后續(xù)會(huì)用到)
- Enter same 提示確認(rèn)輸入密碼
二、多個(gè)密鑰管理
1抒线、舉例:創(chuàng)建分別為gitee班巩、coding、github嘶炭、bitbucket生成了四組密鑰對(duì)
mac@Mac-Pro .ssh % ls
bitbucket coding config gitee.pub github.pub
bitbucket.pub coding.pub gitee github
2抱慌、使用ssh-add 命令添加私鑰
ssh-add ~/.ssh/gitee
Identity added: /Users/mac/.ssh/gitee (xx@xxxxx.com)
ssh-add ~/.ssh/coding
Identity added: /Users/mac/.ssh/coding (xx@xxxxx.com)
ssh-add ~/.ssh/github
Identity added: /Users/mac/.ssh/github (xx@xxxxx.com)
ssh-add ~/.ssh/bitbucket
Identity added: /Users/mac/.ssh/bitbucket (xx@xxxxx.com)
說(shuō)明: 如果在Enter passphrase 過(guò)程中設(shè)置了密碼,此操作需要輸入當(dāng)時(shí)密碼
添加成功會(huì)有對(duì)應(yīng)提示:Identity added
3眨猎、檢查已添加的私鑰
ssh-add -l
mac@Mac-Pro .ssh % ssh-add -l
256 SHA256:a7qJ45YL+bLodKdOrxx2a28P9V2igRlPU xx@xxxxx.com (ED25519)
3072 SHA256:KHunn5oxHSxxxxU8YkikEsyxxX/VpOY xx@xxxxx.com (RSA)
3072 SHA256:vu+3aiCKGDcTgRNyCxxxxxEbwQ6r15tQWRGg xx@xxxxx.com (RSA)
3072 SHA256:sMzqOy2nf3SHxxxxxxxx1gqmuCx9qSMLz0mk xx@xxxxx.com (RSA)
4抑进、添加config文件
創(chuàng)建config文件
touch config
文件內(nèi)容:
# gitee
Host gitee
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee
User yaod
# github
Host github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github
User yaod
# coding
Host coding
HostName kitsudo-01.coding.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/coding
User yaod
# BitBucket
Host bucket
HostName 192.168.1.22
Port 7999
PreferredAuthentications publickey
IdentityFile ~/.ssh/bucket
User yaod
說(shuō)明
- Host example # 關(guān)鍵詞 區(qū)分用
- HostName example.com # 主機(jī)地址,如果是ip填寫(xiě)ip地址
- User root # 用戶(hù)名
- IdentityFile ~/.ssh/xxx # 認(rèn)證文件睡陪,私鑰
- PreferredAuthentications publickey # 配置值為publickey寺渗,優(yōu)先使用publickey身份驗(yàn)證
- Port 22 # 指定端口,如果ip加端口號(hào)兰迫,需要填寫(xiě)端口號(hào)
4信殊、在相應(yīng)服務(wù)端配置公鑰
對(duì)應(yīng)的xx.pub中copy公鑰數(shù)據(jù),粘貼到服務(wù)端Add key的位置汁果。
5涡拘、連通測(cè)試 舉例
ssh -T git@gitee.com
The authenticity of host 'gitee.com (212.64.63.215)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.
ssh -T git@e.coding.net
CODING 提示: Hello xxx, You've connected to coding.net via SSH. This is a Personal Key.
有些服務(wù)可能會(huì)提示host主機(jī)不受信任,可添加信任列表解決据德,例如上面的212.64.63.215 gitee.com
sudo vim /etc/hosts
212.64.63.215 gitee.com
帶端口號(hào)的連通測(cè)試
ssh -p 7999 -T git@192.168.1.22
出現(xiàn)成功字樣表明連通成功鳄乏,可使用ssh地址操作遠(yuǎn)程倉(cāng)庫(kù)
三、參考文章
Git高級(jí)之配置多個(gè)SSH key
Mac下ssh登錄測(cè)試報(bào)錯(cuò)“Could not resolve hostname xxx: nodename nor servname provided, or not known”的解決辦法