多 SSH Key 实现同一台服务器部署多 Git 仓库
2018-07-20 18:26
483 查看
本文以以下需求为背景,介绍详细的做法:
需在同一台服务器同时部署两个不同的 Github 仓库(对 Bitbucket 等 git 服务同样适用)
root 用户可在远程登录 SSH 后附上预期的 SSH Key 进行 git 命令操作
nginx 用户进程(如 php-fpm)可在进程内附上预期的 SSH Key 进行 git 命令操作
仓库 1:
仓库 2:
此处的 nginx 用户的主目录因操作系统不同而拥有不同的路径,经测试 CentOS 系的操作系统可能值为:
之其一,请读者执行
查看并以实际路径为准,替换本文中的对应命令的路径。如:
仓库 1:
仓库 2:
root 用户:
nginx 用户:
需在同一台服务器同时部署两个不同的 Github 仓库(对 Bitbucket 等 git 服务同样适用)
root 用户可在远程登录 SSH 后附上预期的 SSH Key 进行 git 命令操作
nginx 用户进程(如 php-fpm)可在进程内附上预期的 SSH Key 进行 git 命令操作
1. 生成多个 SSH Key
以 root 身份登录服务器,为 root 用户和 nginx 用户分别生成 SSH Key。仓库 1:
$ ssh-keygen -b 2048 -t rsa -f "~/.ssh/id_rsa_github_myrepo1" $ sudo -u nginx ssh-keygen -b 2048 -t rsa -C "nginx@localhost" -f "/var/lib/nginx/.ssh/id_rsa_github_myrepo1"
仓库 2:
$ ssh-keygen -b 2048 -t rsa -f "~/.ssh/id_rsa_github_myrepo2" $ sudo -u nginx ssh-keygen -b 2048 -t rsa -C "nginx@localhost" -f "/var/lib/nginx/.ssh/id_rsa_github_myrepo2"
此处的 nginx 用户的主目录因操作系统不同而拥有不同的路径,经测试 CentOS 系的操作系统可能值为:
"/var/lib/nginx" "/var/cache/nginx" "/usr/share/nginx"
之其一,请读者执行
sudo -u nginx ssh-keygen
查看并以实际路径为准,替换本文中的对应命令的路径。如:
[root@localhost]# sudo -u nginx ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/var/cache/nginx/.ssh/id_rsa): 此处的 nginx 用户的主目录为 "/var/cache/nginx"。
2. 获取 SSH Key 公钥
将生成好的 SSH Key 的公钥部分添加到两个 Github 仓库设置的 Deploy Keys(部署密钥)中。仓库 1:
$ cat "~/.ssh/id_rsa_github_myrepo1.pub" $ cat "/var/lib/nginx/.ssh/id_rsa_github_myrepo1.pub"
仓库 2:
$ cat "~/.ssh/id_rsa_github_myrepo2.pub" $ cat "/var/lib/nginx/.ssh/id_rsa_github_myrepo2.pub"
3. 将不同的 SSH Key 与「主机名」对应起来
编辑用户的 SSH 配置文件,并指定主机的详细自定义配置。root 用户:
$ nano "~/.ssh/config" Host github.com-myrepo1 HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_myrepo1 Host github.com-myrepo2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_myrepo2
nginx 用户:
$ nano "/var/lib/nginx/.ssh/config" Host github.com-myrepo1 HostName github.com User git IdentityFile /var/lib/nginx/.ssh/id_rsa_github_myrepo1 Host github.com-myrepo2 HostName github.com User git IdentityFile /var/lib/nginx/.ssh/id_rsa_github_myrepo2
4. 进行 git 命令操作
完成了上述步骤,读者现在可以在 root 或 nginx 用户登录或以其运行的进程中执行 git 命令,并且会自动附上指定的 SSH Key。例如:$ git clone git@github.com-myrepo1:tommy/myrepo1.git "/var/www/myrepo1" $ git clone git@github.com-myrepo2:tommy/myrepo2.git "/var/www/myrepo2"
PHP 运行于 nginx 用户的 php-fpm 进程时:
<? header( 'Content-type: text/text' ); exit( shell_exec( 'cd /var/www/myrepo1; git pull origin master 2>&1' ) ); exit( shell_exec( 'cd /var/www/myrepo2; git pull origin master 2>&1' ) );
额外补充
修改 git 仓库地址
git remote set-url <name> <newurl> git remote set-url origin git@github.com-myrepo1:tommy/myrepo1.git git remote set-url origin git@github.com-myrepo2:tommy/myrepo2.git
修改know_hosts
删掉,重新生成即可。rm -f ~/.ssh/know_hosts ssh -T git@github.com Hi Anonymous! You've successfully authenticated, but Github.com does not provide shell access.
修改config权限
Bad owner or permissions sudo chmod 600 ~/.ssh/config
相关文章推荐
- ubuntu14.04搭建git服务器、远端仓库、自动部署
- [置顶] 本地git仓库推送到服务器自建的git仓库实现目录文件同步教程[自整理]
- git hook远程仓库实现网站的自动部署
- GIT服务器实现web代码自动部署
- Git系列②之部署企业级开源仓库gitlab服务器
- 自己搭建Git服务器实现SSH以及远程仓库迁移
- git实现代码提交自动部署到相应的web服务器
- GIT服务器实现web代码自动部署
- git实现自动化部署,同时push到多个远程仓库
- 部署GIT 服务器仓库
- GIT服务器实现web代码自动部署
- GIT服务器实现web代码自动部署
- 如何利用单台服务器实现10万多个Git仓库托管
- 如何利用单台服务器实现10万多个Git仓库托管
- Jenkins-Docker-SpringBoot-java-git-gradle-持续集成与自动部署-实现跨服务器部署
- Git系列②之部署企业级开源仓库gitlab服务器
- centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
- 搭建内部开发git服务器并使用hook实现自动部署
- 从零开始部署Node.js服务至阿里云ECS服务器并实现外网IP访问
- Git在服务器上的部署与使用