Github 配置SSH keys教程

来源:互联网 发布:淘宝客怎么做网站推广 编辑:程序博客网 时间:2024/04/28 06:32

   不知道什么原因,CSDN博客保存的时候会丢失图片和格式,请可以戳这里到我有道云笔记分享

   

   今天特地把Github上的教程Github Generating SSH keys翻译成中文(并非全文翻译),因为自己的水平有限,有错误还请指出。
   SSH key 是为了确认你是项目的管理者或拥有者,然后可以不使用密码使用git。当然使用github的时候要用SSH链接,用https的话就无所谓了。  https可以clone任何人的project,但SSH只能clone属于你的,你必须是这个项目的拥有者。 另外SSH在push的时候不需要输入用户名和密码,方便了用户的使用,前提是你先得在github上添加你的SSH key。  
   因为CSDN Code项目也是使用Git,而且和Github差不多,因此CSDN Code也可以参考此教程。

第一步:检查SSH keys

首先,需要检查你的电脑上是否存在SSH keys,打开你的电脑终端,输入以下命令:

ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist

如果你已经有了SSH key,你会看到以下的默认文件。


  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub
然后你就可以直接跳过第二步直接进入第三步

第二步:创建一个新SSH key


要生成新的SSH密钥,打开终端输入以下命令,并确输入你的E-mail。默认设置是首选,因此当系统会提示“请输入要保存的重要文件:”你,只需按Enter键继续就可以了。


ssh-keygen -t rsa -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. # Enter file in which to save the key (/home/you/.ssh/id_rsa):

接下来要求你输入密码,这个密码是使用SSH链接 push的时候用的,可以选择设,如果不想设 按Enter就可以了

然后如果输出下面内容,表示key创建成功
# Your identification has been saved in /home/you/.ssh/id_rsa. # Your public key has been saved in /home/you/.ssh/id_rsa.pub. # The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

接下来把你刚刚创建的key添加到ssh-agent,执行以下两条命令。

# start the ssh-agent in the background eval "$(ssh-agent -s)" # Agent pid 59566 ssh-add ~/.ssh/id_rsa

第三步:把ssh key添加到github上

运行如下命令,拷贝key到剪切板. key的文件名是 id_dsa.pubid_ecdsa.pub 或者 id_ed25519.pub. 或者直接用gedit等编辑器打开这些文件复制即可。

sudo apt-get install xclip # Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`) xclip -sel clip < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard

然后打开你github设置页面,里面有个SSH keys  如图

然后点击Add SSH key  把刚刚复制的粘贴进去就可以了。

第四步:测试

为了确保一切工作正常,可以测试一下是否可以正常链接到github上。测试时,可能会让你输入密码,

打开你的终端,然后输入:

ssh -T git@github.com # Attempts to ssh to github

你可能会看到这样的错误信息:

... Agent admitted failure to sign using the key. debug1: No more authentication methods to try. Permission denied (publickey).

可参阅帮助文章。

您可能会看到这样的警告:

# The authenticity of host 'github.com (207.97.227.239)' can't be established. # RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. # Are you sure you want to continue connecting (yes/no)?

不用担心,这是正常的。直接输入“yes”。

# Hi username! You've successfully authenticated, but GitHub does not # provide shell access.

如果该用户名是你的,你已经成功设置SSH密钥!

如果仍然无法链接到github,你可以参考这些文章。

如果你从HTTPS切换到SSH,你现在需要更新远程存储库的URL。欲了解更多信息,请参见更改远程的URL。


1 0
原创粉丝点击