ssh 免登录配置和多账号配置

免登录配置

在ssh终端中输入

ssh-keygen -t  rsa

执行后 一路回车(3次)

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/XXX/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/XXX/.ssh/id_rsa.
Your public key has been saved in /Users/XXX/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qw8xn3RgwjcPYcVjcAbbIYusAC2sDrBlHAY7B2IykmA [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|XBE.    *==      |
|OO=  o o O+.     |
|==o   = O..     |
|+o . . + =       |
|o   . o S o      |
| .     = +       |
|      . +        |
|       o         |
|      ...        |
+----[SHA256]-----+

然后登录你GitHub账号 依次点击"Setting" -> "SSH Keys"->"New SSH key"
将id_rsa.pub中的内容copy到 key那一栏 title是个简单的说明

最后测试是否成功!!

ssh -T [email protected]

会出现

The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

yes 回车

Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi XXXX! You've successfully authenticated, but GitHub does not provide shell access.

那么你就成功的完成了免登录配置

多账号登录

如果你有好几个git账号那么你要下面操作了

在ssh终端中输入

ssh-keygen -t  rsa -C "你的github账号" -f ~/.ssh/秘钥文件名称(例如: github_1_rsa)

执行后 一路回车(2次 ,因为你已经设置了名称)

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/XXX/.ssh/github_1_rsa.
Your public key has been saved in /Users/XXX/.ssh/github_1_rsa.pub.
The key fingerprint is:
SHA256:dZFCgwLaaDxVW7W+v4UfXeyB8ysKr5GRg9f+yqcf1vX [email protected](你的github账号)
The key's randomart image is:
+---[RSA 2048]----+
|    oo. .++ ..   |
| . =  .o. .o.   |
|  * . ..  o..    |
| . .    .oo.  .. |
|       .S=.. o .o|
|        . =. .=oo|
|         +...oo+o|
|          =.o=..o|
|         ..*O+o.E|
+----[SHA256]-----+

然后和上面操作一样将 github_1_rsa.pub 的内容加入 GitHub的ssh管理器中

给ssh 创建配置文件

vim ~/.shh/config (默认是没有该文件的)

## 配置写入
Host github.com(自定义名称) 
    HostName github.com  
    PreferredAuthentications publickey  
    IdentityFile ~/.ssh/id_rsa  
  
Host my.github.com  
    HostName github.com  
    PreferredAuthentications publickey  
    IdentityFile ~/.ssh/github_1_rsa  

使用:

// 原来写法
git clone [email protected]: github用户名/testgit.git
// 现在写法
git clone [email protected]: my的github的用户名/testgit.git

github提示Permission denied (publickey)

极大多数情况是由于github账号没有设置ssh公钥信息所致。 前往 GitHub 网站的"account settings"

如果你在新建秘钥的时候使用了自定义的名称,比如github_rsa,你需要再配置一个config文件

cd ~/.ssh
vi config

Host github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/github_rsa

你可能感兴趣的:(ssh 免登录配置和多账号配置)