<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Github &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/github/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 15 Feb 2021 02:43:15 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 在Github上添加SSH key</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8github%e4%b8%8a%e6%b7%bb%e5%8a%a0ssh-key/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8github%e4%b8%8a%e6%b7%bb%e5%8a%a0ssh-key/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 06 Oct 2015 05:06:40 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Github]]></category>
		<category><![CDATA[Permission denied]]></category>
		<category><![CDATA[ssh key]]></category>
		<category><![CDATA[添加]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8559</guid>

					<description><![CDATA[<p>
如果你在Github上有一个项目，为了能让这个项目可以在多台计算机上开发，需要把目标计算机的SSH key添加到你的Github账户中，否则你以SSH方式<span style="color:#0000ff;">git clone</span>时会提示无权限，例如你可以试试：</p>
<blockquote>
<p>
		git clone git@github.com:codelast/raspberry-pi.git</p>
</blockquote>
<p>会输出类似于这样的错误信息：</p>
<blockquote>
<p>
		Permission denied (publickey).</p>
</blockquote>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
所以下一步就要在Github中添加SSH key，让你可以在目标计算机上进行开发以及代码提交。<br />
其实Github上已经有完整的教程了，只不过我记性不好，每次都要去翻Github教程，所以特地写下来。</p>
<p><span style="background-color:#00ff00;">✔</span> 检查SSH key是否已经存在<br />
检查文件&#160;~/.ssh/id_rsa.pub 是否存在，如果存在，就不用生成一个新的SSH key了，直接跳到下面的第3步。<br />
如果该文件存在，但你不想使用它的话，那么就按下面的做法另外生成一个文件也可以(例如 ~/.ssh/id_rsa_github.pub)。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);">✔</span>&#160;生成一个新的SSH key<br />
如果第1步中的SSH key不存在，则需要生成：</p>
<blockquote>
<p>
		ssh-keygen -t rsa -b 4096 -C &#34;your_email@example.com&#34;</p>
</blockquote>
<p>其中，your_email@example.com要修改成你的邮箱地址。<br />
回车后输出如下：</p>
<blockquote>
<div>
		Generating public/private rsa key pair.</div></blockquote>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8github%e4%b8%8a%e6%b7%bb%e5%8a%a0ssh-key/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
如果你在Github上有一个项目，为了能让这个项目可以在多台计算机上开发，需要把目标计算机的SSH key添加到你的Github账户中，否则你以SSH方式<span style="color:#0000ff;">git clone</span>时会提示无权限，例如你可以试试：</p>
<blockquote>
<p>
		git clone git@github.com:codelast/raspberry-pi.git</p>
</blockquote>
<p>会输出类似于这样的错误信息：</p>
<blockquote>
<p>
		Permission denied (publickey).</p>
</blockquote>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
所以下一步就要在Github中添加SSH key，让你可以在目标计算机上进行开发以及代码提交。<br />
其实Github上已经有完整的教程了，只不过我记性不好，每次都要去翻Github教程，所以特地写下来。</p>
<p><span style="background-color:#00ff00;"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span> 检查SSH key是否已经存在<br />
检查文件&nbsp;~/.ssh/id_rsa.pub 是否存在，如果存在，就不用生成一个新的SSH key了，直接跳到下面的第3步。<br />
如果该文件存在，但你不想使用它的话，那么就按下面的做法另外生成一个文件也可以(例如 ~/.ssh/id_rsa_github.pub)。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;生成一个新的SSH key<br />
如果第1步中的SSH key不存在，则需要生成：</p>
<blockquote>
<p>
		ssh-keygen -t rsa -b 4096 -C &quot;your_email@example.com&quot;</p>
</blockquote>
<p>其中，your_email@example.com要修改成你的邮箱地址。<br />
回车后输出如下：</p>
<blockquote>
<div>
		Generating public/private rsa key pair.</div>
<div>
		Enter file in which to save the key (/home/xxx/.ssh/id_rsa):</div>
</blockquote>
<p>其中，xxx是你的用户名，直接回车，会将key保存到默认文件中。<br />
接着会输出：</p>
<blockquote>
<div>
		Enter passphrase (empty for no passphrase):&nbsp;</div>
<div>
		Enter same passphrase again:</div>
</blockquote>
<p>这两步是让你输入一个密码，以及确认密码，这个密码在你提交代码到Github时会用到。将这个密码设置成和另一台可以提交代码的计算机上的那个密码一致（要不然在N台计算机上就有N个密码，太麻烦了）。<br />
回车后就提示成功了：</p>
<blockquote>
<div>
		Your identification has been saved in /home/xxx/.ssh/id_rsa.</div>
<div>
		Your public key has been saved in /home/xxx/.ssh/id_rsa.pub.</div>
<div>
		The key fingerprint is:</div>
<div>
		01:0f:f4:3b:...:f0:a2:db your_email@example.com</div>
</blockquote>
<p>到这一步，你会发现&nbsp;~/.ssh/id_rsa.pub 文件已经生成了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;将SSH key添加到ssh-agent<br />
先确认ssh-agent处于启用状态：</p>
<blockquote>
<p>
		eval &quot;$(ssh-agent -s)&quot;</p>
</blockquote>
<p>输出类似于：</p>
<blockquote>
<p>
		Agent pid 32070</p>
</blockquote>
<p>然后将SSH key添加到ssh-agent：</p>
<blockquote>
<p>
		ssh-add ~/.ssh/id_rsa</p>
</blockquote>
<p>这时又会要你输入密码：</p>
<blockquote>
<p>
		Enter passphrase for /home/xxx/.ssh/id_rsa:</p>
</blockquote>
<p>再次输入前面第2步中设置的密码，回车，这一步就算完成了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;将SSH key添加到Github账户中<br />
将SSH key复制到内存：</p>
<blockquote>
<p>
		xclip -sel clip &lt; ~/.ssh/id_rsa.pub</p>
</blockquote>
<p>如果你的程序没有安装过xclip程序的话，需要先用 sudo apt-get install xclip 安装（对Ubuntu来说）。如果你不想装这个程序，直接 cat&nbsp;~/.ssh/id_rsa.pub 并复制出其中的内容也可以。<br />
然后，在Github个人设置的&ldquo;SSH keys&rdquo;界面中，点击&ldquo;Add SSH key&rdquo;，如下图所示：</p>
<p>在 <span style="color:#0000ff;">Title</span> 栏里填一个有意义的名字，用于标识你这个SSH key是用在哪里的，然后在 Key 那一栏里粘贴上刚用 xclip 命令复制出来的SSH key，最后点击&ldquo;Add key&rdquo;即可将此key添加到你的Github账户中了。</p>
<div style="text-align: center;">
	<img decoding="async" alt="Add an SSH key" src="http://www.codelast.com/wp-content/uploads/ckfinder/images/github_add_ssh_key.png" style="width: 504px; height: 457px;" /></div>
<p>
现在再试试本文一开始所说的git clone命令，你应该可以用SSH方式clone你有权限的项目了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;设置每次git远程操作时无需输入密码<br />
在进行git远程操作时，例如git pull，git push等命令，会要求你输入密码，为了免密码操作，可以将密码存储：</p>
<blockquote>
<p>
		git config --global credential.helper store</p>
</blockquote>
<p>此外，在 <span style="color:#0000ff;">~/.ssh/config</span>&nbsp;文件中还要做以下配置：</p>
<blockquote>
<div>
		# GitHub</div>
<div>
		Host github.com</div>
<div>
		HostName github.com</div>
<div>
		<span style="background-color:#ff99ff;">AddKeysToAgent yes</span></div>
<div>
		PreferredAuthentications publickey</div>
<div>
		IdentityFile ~/.ssh/id_rsa</div>
</blockquote>
<div>
	这一段配置，会在你git pull的时候自动读取，它的大概含义就是：对github.com域名，指定使用publickey验证，密钥认证使用的私钥文件是&nbsp;IdentityFile ~/.ssh/id_rsa（如果你像前面说的使用了别的文件名例如 id_rsa_github，那么这里也要做相应的修改）。<br />
	<strong><span style="color:#ee82ee;">注意</span></strong>：标红的那一行，我发现在MacOS上没有的话就无效。</div>
<p>
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8github%e4%b8%8a%e6%b7%bb%e5%8a%a0ssh-key/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
