V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
git
Pro Git
Atlassian Git Tutorial
Pro Git 简体中文翻译
GitX
boizz
V2EX  ›  git

一台电脑配置多个 SSH KEY,多个用户身份

  •  
  •   boizz · 2017-02-23 20:49:31 +08:00 · 4758 次点击
    这是一个创建于 2590 天前的主题,其中的信息可能已经有所发展或是发生改变。

    SSH KEY

    生成 SSH KEY

    $ ssh-keygen -t rsa -C "[email protected]"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again: 
    

    [email protected] 改成自己的邮箱。

    密码输入成功后出现如下提示,表示创建成功:

    Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.
    Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.
    The key fingerprint is:
    fingerprint 值 [email protected]
    The key's randomart image is:
     +--[ RSA 2048]----+
     | .+ + |
     | =oO. |
     ...
    

    注解:这里第一次输入的是文件名,如果直接按回车则会自动生成私钥和公钥:id_rsaid_rsa.pub。后面跟着的是密码确认密码

    上方的命令执行多次则会生成多个 SSH KEY 。

    查看 SSH KEY

    $ cat ~/.ssh/id_rsa.pub
    ssh-rsa 公开密钥的内容 [email protected]
    

    如果创建时输入了文件名,上方的id_rsa替换成文件名。

    此处返回的这段内容可用于使用 SSH KEY 的网站,这里不作过多阐述。

    GIT CONFIG

    github 或者 gitlab 等网站都会要求验证身份。通常情况下配置一个全局信息就可以了,针对一些特殊情况,如果需要配置多个身份信息,可以为项目单独配置。

    配置全局信息

    $ git config --global user.name "Firstname Lastname"
    $ git config --global user.email "[email protected]"
    

    这个命令会在~/.gitconfig填入以下信息:

    [user]
      name = Firstname Lastname
      email = [email protected]
    

    如果需要修改信息,直接修改这个文件即可。

    配置单独信息

    $ cd your_project
    $ git config user.name "Firstname Lastname"
    $ git config user.email "[email protected]"
    

    这个命令会在项目目录下输出文件:/.git /.config

    这里设置的姓名和邮箱地址会用在 Git 的提交日志中。

    为不同网站应用各自的 SSH KEY

    ~/.ssh 目录下创建 config 文件:

    $ vim ~/.ssh/config
    

    输入以下信息:

    Host github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_a
    
    Host git.oschina.net
        HostName git.oschina.net
        User git
        IdentityFile ~/.ssh/id_rsa_b
    
    ...
    

    再把对应的公钥添加至对应的网站上面。

    注解:未加入配置文件的网站会自动应用id_rsa

    至此,多个 SSH KEY 就可以同时使用了。

    原文地址:一台电脑配置多个 SSH KEY ,多个用户身份
    文章作者:Heleth
    版权声明:自由转载-非商用-非衍生-保持署名(创意共享 3.0 许可证) 转载请注明出处

    2 条回复    2017-02-24 10:59:49 +08:00
    ryd994
        1
    ryd994  
       2017-02-24 02:27:08 +08:00 via Android
    呃………致远星战况如何?
    这种 rtfm 的事, Google 一下前三条找不到答案?

    @Livid 非作者全文转载
    msg7086
        2
    msg7086  
       2017-02-24 10:59:49 +08:00
    @ryd994 看了一眼,应该是作者本人。
    不过这文的确致远星了一点……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1094 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:54 · PVG 02:54 · LAX 11:54 · JFK 14:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.