热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Linux下如何同时访问多个github帐号

Github.com提供了Git的版本库托管服务,对于开源项目提供300MB免费空间:laugh:,如果需要扩大空间或者需要私有的Git库访问,Github还提供收费服务。
Github.com 提供了 Git 的版本库托管服务,对于开源项目提供 300MB 免费空间 :laugh: ,如果需要扩大空间或者需要私有的 Git 库访问,Github 还提供收费服务。 群英汇 在Github上的官方版本库地址为:  http://github.com/ossxp-com/ 。但同时,我还有一些偏个人的数据,非公司协同开发的代码,要放到 Github 上去,于是又注册了一个空间: http://github.com/jiangxin/ 。 问题出现了:
  • Github 使用公钥进行认证,两个不同的 Github 帐号,需要配置不同的 SSH 公钥,公钥不能相同
  • 如果为不同 Github 帐号一个配置为  rsa 格式公钥,一个配置 dsa 格式公钥,但是认证的时候,总是以第一个公钥判断用户身份
怎么办呢?...

新项目无法 push 到新的 github 帐号

使用 Github 上的新帐号jiangxin,创建了一个新的空的版本库 freemind-mmx。在本地的 freemind-mmx 项目中为 github 配置为新的源,PUSH的时候失败:
$ git remote add github git@github.com:jiangxin/freemind-mmx.git
$ git push github debian
ERROR: Permission to jiangxin/freemind-mmx denied to ossxp-com.
fatal: The remote end hung up unexpectedly
如上所示,因为连接 github 上的 jiangxin/freemind-mmx.git 版本库时,认证首先使用的公钥是 ossxp-com 的公钥,导致认证失败。 想要在 Linux 中同时访问多个 Github 的多个帐号的关键是,如何让认证的时候选择不同的公钥。即:
  • 多个公钥在一个 Linux 帐号下共存的问题;
  • 连接不同服务器时,公钥选择的问题;

在 ~/.ssh 目录下创建多套公钥

缺省在 ~/.ssh 下已经有一些 ssh 公钥了:
$ ls ~/.ssh/
authorized_keys  id_dsa  id_dsa.pub  id_rsa  id_rsa.pub  known_hosts
其中 id_dsa 和 id_dsa.pub 是 DSA 格式的私钥和公钥,id_rsa 和 id_rsa.pub 是 RSA 格式的公钥(是 ossxp-com 帐号在 github 上认证使用的公钥) 创建目录 ~/.ssh/github1, 用于保存 ossxp-com 帐号在 github 上的公钥/私钥对
$ mkdir ~/.ssh/github1
$ cp ~/.ssh/id_rsa* ~/.ssh/github1/
创建目录 ~/.ssh/github2,用于保存 jiangxin 帐号在 github 上的公钥/私钥对
$ mkdir ~/.ssh/github2
$ ssh-keygen -t rsa -f ~/.ssh/github2/id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jiangxin/.ssh/github2/id_rsa.
Your public key has been saved in /home/jiangxin/.ssh/github2/id_rsa.pub.
The key fingerprint is:
0d:38:95:b2:d7:2b:1d:c8:cb:42:bc:94:bd:50:cb:f3 jiangxin@hp
The key's randomart image is:
+--[ RSA 2048]----+
|        ..       |
|      .oo        |
|     .oO.+       |
|      B.Ooo      |
|     o =S*.o     |
|      o = E      |
|       . .       |
|                 |
|                 |
+-----------------+

用 IdentityFile 指令设置不同公钥的优先级

在 ~/.ssh/config 文件中,可以使用 IdentityFile 指令,设置公钥认证所使用的文件。可以通过多条 IdentityFile 指令,依次尝试各个公钥。 那么对于使用 ossxp-com 帐号访问 github.com 的时候,修改 ~/.ssh/config 文件为:
IdentityFile ~/.ssh/github1/id_rsa
IdentityFile ~/.ssh/github2/id_rsa
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_dsa
那么对于使用 jiangxin 帐号访问 github.com 的时候,修改 ~/.ssh/config 文件为:
IdentityFile ~/.ssh/github2/id_rsa
IdentityFile ~/.ssh/github1/id_rsa
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_dsa
即每次连接不同的 Github 帐号修改不同公钥的认证优先级。但是这个方法还是不够智能。又查了一下 ssh_config 的 MAN 手册。找到了下面的方法。

设置虚拟域名,用 Host 指令设置不同的 Github 认证公钥

可以在 ssh 配置文件中,用 Host 指令设置为不同的主机设定各自独立的设置。使用这个配置,就可以针对在 Github 上不同的帐号,自动设置认证公钥,避免了每次手动修改 IdentityFile 指令的麻烦。 配置文件 ~/.ssh/config 的设置示例
Host ossxp.github.com
 User git
 Hostname github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/github1/id_rsa

Host jx.github.com
 User git
 Hostname github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/github2/id_rsa
原有 ossxp-com 的项目,重新设置 remote URL: $ git remote -v github  git@github.com:ossxp-com/redmine-ossxp-hacks.git (fetch) github  git@github.com:ossxp-com/redmine-ossxp-hacks.git (push) origin  git@bj.ossxp.com:ossxp/redmine-0.8.x.git (fetch) origin  git@bj.ossxp.com:ossxp/redmine-0.8.x.git (push) ossxp   git@ossxp.com:ossxp/redmine-0.8.x.git (fetch) ossxp   git@ossxp.com:ossxp/redmine-0.8.x.git (push) $ git config remote.github.url ossxp.github.com:ossxp-com/redmine-ossxp-hacks.git $ git remote -v github  ossxp.github.com:ossxp-com/redmine-ossxp-hacks.git (fetch) github  ossxp.github.com:ossxp-com/redmine-ossxp-hacks.git (push) origin  git@bj.ossxp.com:ossxp/redmine-0.8.x.git (fetch) origin  git@bj.ossxp.com:ossxp/redmine-0.8.x.git (push) ossxp   git@ossxp.com:ossxp/redmine-0.8.x.git (fetch) ossxp   git@ossxp.com:ossxp/redmine-0.8.x.git (push) 使用 jiangxin 帐号创建的 github 项目,使用 自定义连接 URL 提交:
$ git remote rm github
$ git remote add github jx.github.com:jiangxin/freemind-mmx.git
$ git push github master
Counting objects: 2761, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (833/833), done.
Writing objects: 100% (2761/2761), 31.61 MiB | 64 KiB/s, done.
Total 2761 (delta 2076), reused 2378 (delta 1907)
To git@github.com:jiangxin/freemind-mmx.git
* [new branch]      master -> master

$ git push github debian
Counting objects: 84, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (77/77), done.
Writing objects: 100% (82/82), 115.47 KiB, done.
Total 82 (delta 25), reused 0 (delta 0)
To git@github.com:jiangxin/freemind-mmx.git
* [new branch]      debian -> debian

$ tg remote --populate github
tg: Remote github can now follow TopGit topic branches.
tg: Populating local topic branches from remote 'github'...
tg: The remote 'github' is now the default source of topic branches.

$ tg -r github push --all
...
...

推荐阅读
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 大坑|左上角_pycharm连接服务器同步写代码(图文详细过程)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了pycharm连接服务器同步写代码(图文详细过程)相关的知识,希望对你有一定的参考价值。pycharm连接服务 ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
  • 如何监控 Linux 服务器状态?,分享
    Linux服务器我们天天打交道,特别是Linux工程师更是如此。为了保证服务器的安全与性能,我们经常需要监控服务器的一些状态,以保证工作能顺利开展。本文介绍的几个命令,不仅仅适用于 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  •     这里使用自己编译的hadoop-2.7.0版本部署在windows上,记得几年前,部署hadoop需要借助于cygwin,还需要开启ssh服务,最近发现,原来不需要借助cy ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了markdown[软件代理设置]相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 在单位的一台4cpu的服务器上部署了esxserver,挂载了6个虚拟机,目前运行正常。在安装部署过程中,得到了cnvz.net论坛精华区 ... [详细]
  • Linux一键安装web环境全攻略
    摘自阿里云服务器官网,此处一键安装包下载:点此下载安装须知1、此安装包可在阿里云所有Linux系统上部署安装,此安装包包含的软件及版本为& ... [详细]
  • centos php部署到nginx 404_NodeJS项目部署到阿里云ECS服务器全程详解
    本文转载自:http:www.kovli.com20170919ecs-deploy作者:Kovli本文详细介绍如何部署NodeJS项目到阿里云ECS上, ... [详细]
  • 阿里云服务器iis设置方法与上千种Linux桌面版本相比,Linux服务器只有可怜的十几种。但想要选对适合你的企业需要的仍然不是件容易的事情,选Linux服务器首先要 ... [详细]
  • 如何查看电脑系统版本_腾讯云服务器系统版本怎么看?Windows和Centos版本怎么选?...
    腾讯云服务器系统版本怎么看?想要知道自己的腾讯云服务器系统版本是哪个,可以登录云服务器后台管理系统查看,或者使用命令行查询,如果不会操作& ... [详细]
  • 主流操作系统简介
    主流操作系统简介子墨居士操作系统理论定义为管理计算机硬件资源,控制其他程序运行并为用户提供交互操作界面的系统软件的集合。操作系统是计算机系统的关键组成部分࿰ ... [详细]
author-avatar
L鸿玖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有