您的位置:首页 > 其它

git使用ssh密钥

2015-06-24 10:11 363 查看
git使用https协议,每次pull, push都要输入密码,相当的烦。
使用git协议,然后使用ssh密钥。这样可以省去每次都输密码。

大概需要三个步骤:
一、本地生成密钥对;
二、设置github上的公钥;
三、修改git的remote url为git协议。

一、生成密钥对。
=============
大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。首先先确认一下是否已经有一个公钥了。SSH 公钥默认储存在账户的主目录下的 
~/.ssh
 目录。进去看看:
<pre style="white-space: pre-wrap; word-wrap: break-word;"><code><span class="pln">$ cd </span><span class="pun" style="color: rgb(102, 102, 0);">~/.</span><span class="pln">ssh </span>
<span class="pln">$ ls</span>
<span class="pln">authorized_keys2  id_dsa       known_hosts config            id_dsa</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">pub</span></code>


关键是看有没有用 
something
 和 
something.pub
 来命名的一对文件,这个 
something
 通常就是 
id_dsa
 或 
id_rsa
。有 
.pub
后缀的文件就是公钥,另一个文件则是密钥。假如没有这些文件,或者干脆连 
.ssh
 目录都没有,可以用 
ssh-keygen
 来创建。该程序在
Linux/Mac 系统上由 SSH 包提供,而在 Windows 上则包含在 MSysGit 包里:
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">$</span><span class="pln"> ssh</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">keygen </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">t rsa </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">C </span><span class="str" style="color: rgb(0, 136, 0);">"</span><em><span class="str" style="color: rgb(0, 136, 0);">your_email@youremail.com</span></em><span class="str" style="color: rgb(0, 136, 0);">"</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);"># Creates a new ssh key using the provided email</span><span class="com" style="color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);"># </span><span class="com" style="color: rgb(136, 0, 0);">Generating public/private rsa key pair.</span><span class="com" style="color: rgb(136, 0, 0);"> </span>
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);"># </span><span class="com" style="color: rgb(136, 0, 0
4000
);">Enter file in which to save the key (/home/</span><em><span class="com" style="color: rgb(136, 0, 0);">you</span></em><span class="com" style="color: rgb(136, 0, 0);">/.ssh/id_rsa):</span></p>
直接Enter就行。然后,会提示你输入密码,如下(建议输一个,安全一点,当然不输也行):
<pre style="white-space: pre-wrap; word-wrap: break-word;"><span class="typ" style="color: rgb(102, 0, 102);">Enter</span><span class="pln"> passphrase </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">empty </span><span class="kwd" style="color: rgb(0, 0, 136);">for</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">no</span><span class="pln"> passphrase</span><span class="pun" style="color: rgb(102, 102, 0);">):</span><span class="pln"> </span><em><span class="pun" style="color: rgb(102, 102, 0);">[</span><span class="typ" style="color: rgb(102, 0, 102);">Type</span><span class="pln"> a passphrase</span><span class="pun" style="color: rgb(102, 102, 0);">]</span></em><span class="pln"> </span>
<span class="com" style="color: rgb(136, 0, 0);"># </span><span class="com" style="color: rgb(136, 0, 0);">Enter same passphrase again: </span><em><span class="com" style="color: rgb(136, 0, 0);">[Type passphrase again]</span></em>


完了之后,大概是这样。
<pre style="white-space: pre-wrap; word-wrap: break-word;"><span class="typ" style="color: rgb(102, 0, 102);">Your</span><span class="pln"> identification has been saved </span><span class="kwd" style="color: rgb(0, 0, 136);">in</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">home</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><em>you</em><span class="pun" style="color: rgb(102, 102, 0);">/.</span><span class="pln">ssh</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">id_rsa</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln"> </span>
<span class="com" style="color: rgb(136, 0, 0);"># </span><span class="com" style="color: rgb(136, 0, 0);">Your public key has been saved in /home/</span><em><span class="com" style="color: rgb(136, 0, 0);">you</span></em><span class="com" style="color: rgb(136, 0, 0);">/.ssh/id_rsa.pub.</span><span class="com" style="color: rgb(136, 0, 0);"> </span>
<span class="com" style="color: rgb(136, 0, 0);"># </span><span class="com" style="color: rgb(136, 0, 0);">The key fingerprint is:</span><span class="com" style="color: rgb(136, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);"># </span><span class="com" style="color: rgb(136, 0, 0);">01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db </span><em><span class="com" style="color: rgb(136, 0, 0);">your_email@youremail.com</span></em>


这样。你本地生成密钥对的工作就做好了。

二、添加公钥到你的github帐户
========================
1、查看你生成的公钥:大概如下:
<pre style="white-space: pre-wrap; word-wrap: break-word;"><code><span class="pln">$ cat </span><span class="pun" style="color: rgb(102, 102, 0);">~</span><span class="str" style="color: rgb(0, 136, 0);">/.ssh/</span><span class="pln">id_rsa</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">pub  </span>
<span class="pln">ssh</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">BWDSU </span><span class="typ" style="color: rgb(102, 0, 102);">GPl</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln">nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlE</span>
<span class="typ" style="color: rgb(102, 0, 102);">LEVf4h9lFX5QVkbPppSwg0cda3</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Pbv7kOdJ</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="typ" style="color: rgb(102, 0, 102);">MTyBlWXFCR</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="typ" style="color: rgb(102, 0, 102);">HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="lit" style="color: rgb(0, 102, 102);">7XA</span><span class="pln"> t3FaoJoAsncM1Q9x5</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="lit" style="color: rgb(0, 102, 102);">3V</span>
<span class="lit" style="color: rgb(0, 102, 102);">0Ww68</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="typ" style="color: rgb(102, 0, 102);">Pb0rwert</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="typ" style="color: rgb(102, 0, 102);">En</span><span class="pln"> mZ</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln">AW4OZPnTPI89ZPmVMLuayrD2cE86Z</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">il8b</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln">gw3r3</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="lit" style="color: rgb(0, 102, 102);">1nKatmIkjn2so1d01QraTlMqVSsbx</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">NrRFi9wrf</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln">M7Q</span><span class="pun" style="color: rgb(102, 102, 0);">==</span><span class="pln"> schacon@agadorlaptop</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">local</span></code>


2、登陆你的github帐户。然后 Account Settings -> 左栏点击 SSH Keys -> 点击 Add SSH key
3、然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,你随便填一个都行。
4、完了,点击 Add key。

这样,就OK了。然后,验证下这个key是不是正常工作。
<pre style="white-space: pre-wrap; word-wrap: break-word;"><span class="pln">$ ssh </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">T git@github</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">com</span>
<span class="com" style="color: rgb(136, 0, 0);"># Attempts to ssh to github</span>


如果,看到:
<pre style="white-space: pre-wrap; word-wrap: break-word;"><span class="typ" style="color: rgb(102, 0, 102);">Hi</span><span class="pln"> </span><em>username</em><span class="pun" style="color: rgb(102, 102, 0);">!</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">You</span><span class="str" style="color: rgb(0, 136, 0);">'ve successfully authenticated, but GitHub does not</span><span class="str" style="color: rgb(0, 136, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);"># </span><span class="str" style="color: rgb(0, 136, 0);">provide shell access.</span>


就表示你的设置已经成功了。

三、修改你本地的ssh remote url. 不用https协议,改用git 协议
================================================
可以用git remote -v 查看你当前的remote url
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">$ git remote </span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">v</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">origin https</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="com" style="color: rgb(136, 0, 0);">//github.com/someaccount/someproject.git (fetch)</span>
<span class="pln">origin https</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="com" style="color: rgb(136, 0, 0);">//github.com/someaccount/someproject.git (push)</span></p>


可以看到是使用https协议进行访问的。

你可以使用浏览器登陆你的github,在上面可以看到你的ssh协议相应的url。类似如下:
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">git@github</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">com
9847
</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">someaccount</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">someproject</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">git</span>
</p>


这时,你可以使用 git remote set-url 来调整你的url。
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln">git remote </span><span class="kwd" style="color: rgb(0, 0, 136);">set</span><span class="pun" style="color: rgb(102, 102, 0);">-</span><span class="pln">url origin git@github</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">com</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln">someaccount</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln">someproject</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">git</span>
</p>


完了之后,你便可以再用 git remote -v 查看一下。

OK。

至此,OK。

你可以用git fetch, git pull , git push, 现在进行远程操作,应该就不需要输入密码那么烦了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: