git 使用ssh密鑰
使用命令 git remote -v 查看你當前的 remote url
root@zengyue:/home/yuanGit# git remote -v
root@zengyue:/home/yuanGit# git remote -v origin https://github.com/seventeen71/yuan (fetch) #1 origin https://github.com/seventeen71/yuan (push) #2 origin [email protected]:seventeen71/yuan.git (push) #3
#1、#2處的地址都是以https開頭的,表示https傳輸,意味著你每次都要輸入討厭的username、password
作者本人的已經設置好了ssh傳輸,在#3處:地址是git開頭則表示是git協議
提示:完全設置好push模式的話應該是這樣
root@zengyue:/home/yuanGit# git remote -v
origin https://github.com/seventeen71/yuan (fetch)
origin [email protected]:seventeen71/yuan.git (push) #4
#4處這樣當你git push origin master時就不需要輸入討厭的密碼了,下面講如何去設置
在 ~/.ssh目錄下查看是否存在如下兩個文件:~宿主目錄一般為 /home/user(作者是zengyue) /home/zengyue
root@zengyue:~/.ssh# ls
id_rsa id_rsa.pub
如果不存在id_rsa id_rsa.pub 兩個文件的話:
二、生成SSH密鑰 2.1 ~/.ssh目錄無密鑰對從這兒開始2.1.1 使用命令: ssh-keygen 來創建
root@zengyue:~# ssh-keygen -t rsa -C "[email protected]"
郵件地址為你註冊github的地址
Creates a new ssh key using the provided email # Generating public/private rsa key pair.
Enter file in which to save the key (/home/zengyue/.ssh/id_rsa):
這兒讓你確認密鑰目錄,直接Enter回車就好:
Enter same passphrase again: [Type passphrase again]
接著讓你輸入及確認密碼,然後就生成了密鑰對。
出現下面提示說明OK了
Your public key has been saved in /home/zengyue/.ssh/id_rsa.pub.
The key fingerprint is: # ............ [email protected]
現在你可以在你的 ~/.ssh 目錄看是否有id_rsa id_rsa.pub 兩個文件
2.2.1 查看復制本地公鑰:cat id_rsa.pub 或者 vim id_rsa.pub 打開直接copy
記得一點都不能省略從 ssh-rsa 開始 [email protected]結束
root@zengyue:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbCLmkVonQAFjAgQVY7vH86NIsgw1kDV3dDYRXdmWoYSZ3gY4OEgzCqm8GWXRr+wNFUsSwhMmUabtEfRGGKlP/z2TBMzXFkcJ18zLAWXtgWWUH73tVfxXg+sdFpKhY0ppDYdU9Amk8ljVsge8+pKHVluk0ReBNtFsbp1w/seyrfUqsjigsnQHWeSPBns3CD7hs++7BXW5Mv4SH+ap30N+2tFBlqSuzitwLJCFed9isSFq2UQ4wiX25rs+eYpSloMY9kClMVVYuXN860EKCyF5V1ThS/TOcMoxDhGclDBoItS/oeTDGwopSTrsm5CFyUJFAd3qDYJ+yVy5fycVYfb6x [email protected]
2.2.2 登陸你的github帳戶。點擊你的頭像,然後
Settings -> 左欄點擊 SSH and GPG keys -> 點擊 New SSH key
2.2.3 將復制的公鑰粘貼到key文本域,title隨便起名,再點擊Add key
2.2.4 然後回到linux下確認:命令 ssh -T [email protected]
root@zengyue:~/.ssh# ssh -T [email protected]
Hi xxx! You‘ve successfully authenticated, but GitHub does not # provide shell access. 看到這個你就成功了。
2.2.5 調整remote url
命令 git remote -v 查看,詳細見文章開頭
root@zengyue:/home/yuanGit# git remote -v
origin https://github.com/seventeen71/yuan (fetch)
origin https://github.com/seventeen71/yuan (push)
如果你的是這樣,意思就是現在只有https傳輸,還需要輸入username /password
輸入命令:
git remote set-url origin [email protected]:[你的賬戶]/[你的github倉庫].git
如下圖進入你的倉庫,點擊1處,然後出現在2處的就是git協議地址了,
復制它拼接到命令git remote set-url origin 後面
git remote set-url origin [email protected]:someone/repository.git
你還可以去倉庫的 .git 目錄下修改conf文件
前面我們說過了,每一次提交git都會最後來這個文件找各種信息,下圖的 13 到15行就是,去你的github倉庫復制過來配置到這兒
11
12 [remote "origin"]
13 url = [email protected]:s--enten--/--an.git
14 fetch = +refs/heads/*:refs/remotes/origin/*
15 pushurl = [email protected]:s--enten--/--an.git.git
16 [branch "master"]
17 remote = origin
18 merge = refs/heads/master
當然你還可以使用git remote --help
git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <url>
git remote set-url --push origin <你的github倉庫url>
這樣去設置解決也是可以噠!
當然如果你遇到如下問題:去我下篇博客找吧!
執行git clone [email protected]:accountName/repository.git命令時不出錯,
運行git push時出錯,提示如下
Permission denied(publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
git 使用ssh密鑰