git使用,同时使用ssh连接git和github

起因

想要做一个自己的代码托管所,使用ssh连接github是出现“ssh: connect to host github.com port 22: Connection timed out”。

正文开始

第一天在网上查到写方法,但是都没成功所以把~/.ssh目录都删除了,正好咱们从头开始:

  • 用户信息
   $ git config --global user.name "用户名"
   $ git config --global user.email 邮箱
  • 生成密匙
   $ ssh-keygen -t rsa -C "邮箱"
   //这里我们的git和github可能使用不同的邮箱,我们来生成不同的密匙
   $ ssh-keygen -t rsa -C "邮箱1"	全部回车
   $ ssh-keygen -t rsa -C "邮箱2"	需要修改密匙文件的文件名如图

git使用,同时使用ssh连接git和github_第1张图片
以上会生成如下结果:在这里插入图片描述
know_hosts文件之后会自己生成。

  • 配置ssh key:
    这里复制id_rsa.pub复制到git,id_rsa_outlook.pub复制到github。
    步骤:头像->settings->ssh key

  • 配置config文件(本文关键
    config文件在~/.ssh目录下,没有就新建一个
    直接粘过来:

Host git.***.com   #git地址
   User zzh@***.com	#邮箱
   Hostname git.***.com	#地址
   IdentityFile ~/.ssh/id_rsa	#git配置的公匙路径
   Port 22	#端口号
Host github.com
   User ***@***.com	#邮箱
   Hostname ssh.github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_outlook	#github配置的公匙路径
   Port 443	#端口
  • 验证
    正文结束,可以直接通过ssh克隆个项目试试,或者通过ssh -T进行验证:
    #github的地址 ssh -T [email protected]
    #出现如下内容,表示成功链接github,***为你的github账户的用户名
    Hi ***! You’ve successfully authenticated, but GitHub does not provide shell access.
    #公司的git地址
    ssh -T git@git.***.com
    #出现如下内容,表示成功链接github,***为公司git账户的用户名
    Hi ***! You’ve successfully authenticated, but GitHub does not provide shell access.

结束语

弄了一天半,效率有点低。做货罗列一下过程中遇到的异样,方便一些直接检索异常信息的同学:

 - ssh: connect to host github.com port 22: Connection timed out
 - bash: get: command not found
 - ssh: Could not resolve hostname fenjiread.com: Name or service not known
 - ssh_exchange_identification: Connection closed by remote host
 - ssh: connect to host github.com port 443: Connection timed out

这些错误是之前走的弯路,望大家畅通无阻。

你可能感兴趣的:(git)