前言

SSH(Secure Shell) 是一种加密的网络协议,用于安全地远程访问和管理计算机系统。它通过加密通信防止数据被窃听或篡改,广泛用于:

  • 远程登录服务器(如 ssh user@host
  • 安全传输文件(scp/sftp
  • Git 等版本控制的认证

在 GitHub 中配置 SSH 密钥的主要目的是 替代账号密码,实现更安全、便捷的身份认证,最主要的是免密码操作 Git 仓库

而同一台机器上同一个 ssh key 只能添加至一个 Github 账户,如果添加至多个就会报错:Key is already in use

现在我有多个 Github 账户,个人账户和公司账户,我希望都能配置 ssh。

配置

1、生成密钥

ls -al ~/.ssh 列出现有的公钥和私钥对

生成个人账户的ssh:

ssh-keygen -t rsa

对于保存密钥的位置,按回车键使用默认位置。一个私钥和公钥 ~/.ssh/id_rsa.pub 将在默认的 SSH 位置 ~/.ssh/ 创建。


生成工作账号的ssh:

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"
  1. ssh-keygen
    OpenSSH 自带的密钥生成工具,用于创建认证密钥(公钥和私钥)。
  2. -t rsa
    指定密钥类型为 ​​RSA​​(默认算法,兼容性最好)。其他可选类型有 ed25519ecdsa 等。
  3. -C "email@work_mail.com"
    在公钥末尾添加注释(通常是邮箱或标识符)。
    • 注释不会影响密钥功能,仅作为备注。
    • 例如生成的公钥文件会包含:ssh-rsa AAAAB3N... email@work_mail.com
  4. -f "id_rsa_work_user1"
    指定密钥文件的名称(默认路径是 ~/.ssh/)。
    • 私钥:id_rsa_work_user1
    • 公钥:id_rsa_work_user1.pub
    • 如果省略 -f,默认生成 id_rsaid_rsa.pub

创建好后在 ~/.ssh 目录下就有了两对不同的密钥:

~/.ssh/id_rsa
~/.ssh/id_rsa.pub
~/.ssh/id_rsa_work_user1
~/.ssh/id_rsa_work_user1.pub
  1. 私钥(Private Key):id_rsa、id_rsa_work_user1
    • 作用:用于身份认证的机密文件,相当于你的“密码”。
    • 用途:本地 SSH 客户端(如 Git、终端)用它来证明你的身份。
      当你执行 git pushssh user@host 时,系统会自动匹配对应的私钥。
    • 注意必须严格保密,不能泄露给任何人。
  2. 公钥(Public Key):id_rsa.pub、id_rsa_work_user1.pub
    • 作用:用于公开分发的文件,相当于“锁”,其他人可以用它验证你的身份。
    • 用途:需要将它添加到远程服务(如 GitHub、GitLab、服务器等)
  3. 必须添加公钥到 GitHub,私钥绝对不能上传或分享给任何人!

2、将公钥添加到 GitHub

分别将两个公钥添加至 GitHub 个人账户和公司账户。

  1. 复制公钥内容

    cat ~/.ssh/id_rsa_work_user1.pub
    

    或直接打开文件复制全部内容(包括 ssh-rsa 和邮箱注释)。

  2. 在 GitHub 中添加公钥

    • 进入 Settings → SSH and GPG Keys → New SSH Key

    • 粘贴公钥内容,标题可写备注(如 Work Laptop)。

3、配置 SSH 配置文件

默认情况下,SSH 只会自动使用 ~/.ssh/id_rsa。如果密钥文件名不同(如 id_rsa_work_user1),每次连接需通过 -i 指定密钥:

ssh -i ~/.ssh/id_rsa_work_user1 user@github.com

通过 config 文件可自动匹配密钥,无需额外输入。


1、创建 SSH 配置文件 ~/.ssh/config

$ cd ~/.ssh/
$ touch config

2、在 ~/.ssh/config 中复制下面配置:

# Personal account, - the default config
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa

# Work account-1
Host github.com-work_user1
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work_user1

关键配置项说明:

  • Host:自定义别名,用于简化命令(ssh github.com

    # 代替完整的 ssh -i ~/.ssh/id_rsa git@github.com
    ssh github.com
    

    注意:设置 remote origin 时需要与这里配置的别名保持一致

  • HostName:实际的主机地址,可配置公司服务器地址;可用 * 匹配多个主机(如:*.company.com

  • User:登录用户名,Git 服务通常为 git,服务器可能是你的公司用户名

  • IdentityFile:指定对应的私钥路径

  • IdentitiesOnly:强制只使用指定的密钥,避免 SSH 尝试其他密钥导致失败

  • Port:如果服务使用非默认端口(22),需手动指定

4、测试 SSH 连接

命令格式:

# 命令格式
ssh -T git@<主机地址>

示例:

  • GitHub:ssh -T git@github.com

    成功时会返回:

    Hi <你的用户名>! You've successfully authenticated, but GitHub does not provide shell access.
    
  • GitLab:ssh -T git@gitlab.com

    成功时会返回:

    Welcome to GitLab, @<你的用户名>!
    

这里我们配置了多密钥:

$ ssh -T git@github.com
$ ssh -T git@github.com-work_user1
  • 调试模式(查看详细错误)。添加 -v(verbose)参数显示详细连接过程,帮助定位问题:

    ssh -vT git@github.com
    

5、不同repo配置不同用户信息

不同 repo 我希望配置不同的 user 信息,比如个人账户 GitHub 中我希望隐藏邮箱,而公司账户中需要使用公司邮箱。

通常个人账户的用户信息是设置的全局配置:

$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"

查看全局配置:

$ git config --global --list

# or
$ git config --global user.name
$ git config --global user.email

而在公司的 repo 中设置局部配置(仅对当前仓库生效):

$ cd /path/to/your/repo
$ git config [--local] user.name "Your Work Name"
$ git config [--local] user.email "your.work@company.com"

查看局部配置:

git config --local --list

由于我们配置了多个 HostName 都是 github.com 的 ssh,最后对公司 repo 进行 git 操作之前需要修改 remote 地址:

git remote set-url origin git@github.com-work_user1:user/repo.git
# 个人账户
git@github.com:user/repo.git

# 公司账户
git@github.com-work_user1:user/repo.git

相关操作

git clone git@github.com-work_user1:user/repo.git

YOLO