背景: 某些项目本地 PC 无法满足计算资源,一般团队会购置一个公共的服务器作为开发机
下面以人员 Sean、公共开发机器 ubuntu@192.168.31.59 为例说明一下如何协同使用, 确保每个人在自己的工作目录使用的是自己的账户进行 Git 拉取、推送、创建 CI Jobs 等流程
连接开发机
本地在 ~/.ssh/config 添加
Host ubuntu
HostName 192.168.31.59
User ubuntu
Port 22
IdentityFile C:/Users/sean2/.ssh/id_ed25519_work
IdentitiesOnly yes
其中 IdentityFile 替换为自己的 ssh 私钥
要避免每次登录输密码,将自己的公钥拷贝至服务器:
ssh-copy-id -i C:/Users/sean2/.ssh/id_ed25519_work.pub ubuntu这样,第一次登录输入密码后,后续直接运行 ssh ubuntu 就可以登录该服务器了。
目录安排
每个人在服务器上创建自己的工作目录,如 ~/sean_ws
Git 配置
Warning
请避免配置全局 git user.name, git user.email 等
为确保每个人拉取、推送、创建 CI Jobs 等流程都是使用自己的账户,请在服务器上创建自己的 shh key,以 Sean 为例:
- 创建专属 ssh key:
ssh-keygen -t ed25519 -C "sean@manifoldai.cn" -f ~/.ssh/id_ed25519_sean
然后把创建的公钥添加到个人 GitLab 账户。
- 添加个人 git config 配置文件:
# ~/.gitconfig-sean
[user]
name = Sean
email = sean@manifoldai.cn
[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_sean -o IdentitiesOnly=yes
- 更新全局 git config 添加:
# ~/.gitconfig
[includeIf "gitdir/i:~/sean_ws/**"]
path = ~/.gitconfig-sean
表示在 ~/sean_ws 目录下 git 配置文件使用的是 ~/.gitconfig-sean
更多参考 多 ssh key 配置