我们在工作中会以 ssh 的方式配置公司的 git 账号,但是平时也会使用 github 管理自己的项目。出于各种原因,自己的 github 项目无法使用公司的 git 账号,于是我们可以为自己的 github 创建一个新的 git 账号,这就需要生成新的 ssh 密钥。 下面总结了创建多个互相独立的 ssh 密钥的步骤(以公司的和自己 github 的为例)。
~/.ssh/
下创建两个文件夹,分别为 company/
和 github/
,前者存放公司的 ssh密钥,后者存自己github 的 ssh 密钥。下面的步骤以创建 github 密钥为例:ssh-keygen -t rsa -C "your_github_email@example.com"
Enter file in which to save the key
,输入文件路径 ~/.ssh/github/id_rsa_github
,最后就会在该路径下生成 id_rsa_github
和 id_rsa_github.pub
两个文件(若直接回车的话,会保存在~/.ssh/
文件夹中)。Enter passphrase (empty for no passphrase)
,直接回车即可。id_rsa_github.pub
中保存的就是 ssh 公钥。可以利用命令 pbcopy
将其复制到剪贴板,比如我的命令就是 pbcopy < ~/.ssh/github/id_rsa_github.pub
。当然你也可以找到该文件手动复制。~/.ssh/
文件夹中创建文件config
,添加如下内容:
# The git info for company Host git.XXX.com # git别名,写公司的git名字即可 HostName git.XXX.com # git名字,同样写公司的git名字 User git # 写 git 即可 IdentityFile ~/.ssh/company/id_rsa #私钥路径,若写错会连接失败 # The git info for github Host github.com # git别名,写github的git名字即可 HostName github.com # git名字,同样写github的git名字 User git # 写 git 即可 IdentityFile ~/.ssh/github/id_rsa_github #私钥路径,若写错会连接失败
注:配置文件中各参数含义请参见扩展部分ssh-add
将 IdentityFile 添加到 ssh-agent中,具体到本文中就是执行 ssh-add ~/.ssh/company/id_rsa
和 ssh-add ~/.ssh/github/id_rsa_github
。其实这个操作只是把专用密钥添加到 ssh-agent 的高速缓存中,因此略过该步骤也不会有影响。ssh -T git@github.com
,若出现
Hi xiaoxi666! You've successfully authenticated, but GitHub does not provide shell access.
这样的字段,即说明配置成功。公司的同理。本文描述了单机配置两个 git 平台ssh密钥的方法,多个账号同理。推荐在 ~/.ssh/
文件夹下为不同的平台建立不同的文件夹,方便管理。根据上述配置,我的 ~/.ssh/
文件夹下是这样子的:
├── company
│ ├── id_rsa
│ └── id_rsa.pub
├── config
├── github
│ ├── id_rsa_github
│ └── id_rsa_github.pub
~/.ssh/
文件夹下创建配置文件,其实也可以指定位置,具体可查阅 ssh帮助手册(man 1 ssh
)。
-F configfile Specifies an alternative per-user configuration file. If a configuration file is given on the command line, the system-wide configuration file (/etc/ssh/ssh_config) will be ignored. The default for the per-user configuration file is ~/.ssh/config.ssh -T banana
,以后 clone 项目时也可以用 banana 代替 github.com 。git@github.com
开头,@ 前面的 git 就是 github 对应服务器主机中的一个用户名,即上面设置的 User,@ 后面的 github.com 就是 HostName);git config --help
查看官方说明),你一定有过下面这种配置的经历:
git config --global user.name "user_name" git config --global user.email "user_name@example.com"
实际上,git 有三种配置选项,分别对应于 --system 、--global、--local,上面就是 --global选项。三种配置选项分别对应于三个配置文件:
--system -> /usr/local/git/etc/gitconfig --global -> ~/.gitconfig --local -> 你的项目仓库路径/.git/config
这三种配置文件的优先级由低到高为:--system、--global、--local,优先级高的配置会覆盖优先级低的配置。一般情况下,我们不会去设置 --system 选项,主要用的是 --global 和 --local。正如网上满天飞的教程中,我们一般会设置 --global,这样就不用在每个项目中设置一遍了;但是如果配置了多个 git 账号,需要在一些项目中使用不同的作者名字,就需要单独为该项目设置 --local。需要注意的是, --local 选项只能 cd 到当前项目路径下中执行,否则会报错 fatal: --local can only be used inside a git repository。
注一:如果 --system、--global、--local 都没有设置 user.email,将无法提交代码,因为无法识别代码作者。在设置了 user.email 的前提下,如果 --system、--global、--local 都没有设置 user.name,则会使用计算机登录用户名作为代码提交的作者名字。
注二:设置 --local 时记得同时设置 user.name 和 user.email,如果只设置 user.name,提交名字虽然正确了,但 user.email 还是用的 --global 中的 user.email,这会导致 github 仓库中显示的提交者却不会链接到你的账号:头像是灰色的八爪鱼,名字也没有链接。pbcopy
和 pbpaste
是两个非常好用的命令,可查阅相关资料了解。用户名@主机名 当前路径 $
或 主机名:当前路径 用户名$
的格式显示,其中主机名可以用命令 scutil –-set HostName new_hostname
设置。