生成ssh公有密钥而且注冊到Github Generate ssh rsa keys and register public key on Github

私有密钥和公有密钥是成对的两个文件,私有文件保存在自己的本机,公有密钥保存到还有一端的server,站点等。

github就是一种站点。 仅仅有保存了私有密钥的机器才干訪问远程的server等。

使用该键的优点是不用使用password,而是以密钥的方式验证用户。 要想使本机能訪问github。

有三个步骤: 1. 创建私有密钥和公有密钥 2. 将公有密钥放到github里。 3. 測试是否设置成功。 步骤1: 首先推断本机是否创建了公有密钥: $ls ~/.ssh 假设没有相似 id_rsa和id_rsa.pub这种文件,则表明没有创建。 生成的办法是: $ssh-keygen -t rsa -C "yourgitemail.com" 运行后,会填写保存两种密钥的目录。和passphrase。所有能够按enter。 然后运行ls来查看生成后的文件。 $ls ~/.ssh id_rsa和id_rsa.pub各自是私有密钥和公有密钥。 步骤2: 打开id_rsa.pub将里面的内容拷贝到剪贴板。

加入到Github的ssh kesy设定里。 https://github.com/settings/ssh 点击右上角 Add SSH key 输入title(e.g:mac)。然后将复制的公有密钥ctrl+v粘贴到key. 这样就完毕了上述的步骤1和2. 步骤3: 接下来进行測试。

$ssh -T git@github.com 假设显示: ... Are you sure you want to continue connecting (yes/no)? 输入yes。

然后就能够看到 Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access. 这样就能够通过ssh方式clone Github上的project而且进行pull和push了。 使用密钥方法仅仅对ssh方式有效。

比方clone的时候须要选择ssh,这样就能保证git remote url是ssh方式的url。

假设使用http clone。则每次push须要输入账号password。要避免每次输入账号password,能够在~/.netrc文件中明文写账号password,但这不是个好办法。 总结:假设在步骤3的ssh命令后或者输入yes后出现github Permission denied错误。

把下面两行命令加到~/.bash_profile里. //start the ssh-agent in the background eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa 再运行 $source ~/.bash_profile $ssh -T git@github.com


原文地址:https://www.cnblogs.com/claireyuancy/p/6946808.html