GitHub第一步--生成ssh keys

转自个人博客:http://siukwan.sinaapp.com/

亲自在Mac OS和cent OS中测试成功,命令均一样。

SSH keys可以无需密码进行github更新,每台电脑均需要生成唯一的SSH并添加到github个人资料的settings中。

Step 1: 检查SSH keys

首先我们需要检测系统是否已经存在SSH keys,可以使用终端输入下列的命令:

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

如果存在,可以看到如下的列表:

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub

如果想使用现有的SSH keys,可以忽略步骤2和步骤3,如果没有现成的SSH keys,我们接下来会通过终端来生成。

Step 2: 生成SSH key

  1. 输入下面的命令,其中需要填写自己的邮箱:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    # Creates a new ssh key, using the provided email as a label
    # Generating public/private rsa key pair.
    
  2. 使用默认设置即可,直接按回车:
    # Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
    
  3. 这个也可以直接按回车:
    # Enter passphrase (empty for no passphrase): [Type a passphrase]
    # Enter same passphrase again: [Type passphrase again]
  4. 最后我们会看到下面的信息:
    # Your identification has been saved in /Users/you/.ssh/id_rsa.
    # Your public key has been saved in /Users/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
    

Step 3: 把生成的key添加到ssh-agent

ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序,其实ssh-agent就是一个密钥管理器,原型ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以自动将验证申请交给ssh-agent来完成整个认证过程。

  1. 先检查ssh-agent是否在运行:
    # start the ssh-agent in the background
    eval "$(ssh-agent -s)"
    # Agent pid 59566
    
  2. 把SSH key添加ssh-agent:
    ssh-add ~/.ssh/id_rsa
    

注意:如果你采用现有的SSH keys,你需要把id_rsa(这是一个文件名)替换成现有的私钥文件名。

Step 4: 把SSH key添加到github账户

这个时候需要复制id_rsa.pub的内容,即SSH key,注意这个文件不一定是id_rsa.pub,有可能是id_dsa.pub,id_ecdsa.pub or id_ed25519,需要看自己的设置。

pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

 

详细步骤:

1.

  1. Settings icon in the user bar进入个人账户的Settings.

  1. 2.
  2. SSH keys选择SSH keys.

  3. 3.
  4. SSH Key button点击Add SSH key.

  5. 4.添加title,题目可为 "Personal MacBook Air",根据自己的情况添加。

  6. 5.
  7. The key field复制key.

  8. 6.
  9. The Add key button点击Add key.
  10. 最后会要求输入github的密码进行确认.

Step 5: 测试连接

在完成上述步骤后,可以测试连接,确保ssh正常工作。在尝试ssh连接时,github会提示认证。

  1. 打开终端并输入:
    ssh -T git@github.com
    # Attempts to ssh to GitHub
    
  2. 然后会看到下面的警告:
    # 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.
    
  3. 看到上面的信息后证明你已经成功ssh连接到github!

    If you receive a message about "access denied," you can read these instructions for diagnosing the issue.

    If you're switching from HTTPS to SSH, you'll now need to update your remote repository URLs. For more information, see Changing a remote's URL.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值