最近手上一个项目须要使用到一台服务器做为专用的部署服务器,在实施过程当中遇到了一些问题,具体以下:javascript
1. 服务器的ssh默认端口和项目git仓库的ssh端口不一致 2. 部署须要使用项目提供的ssh key,不能使用服务器自己的默认ssh key
这些问题都被顺利解决了,这里特记录一下,防止遗忘。html
针对上述问题,下面主要从这三个点来记录解决方案。java
如何生成ssh keypython
如何使用特定ssh端口从git仓库拉取项目git
如何使用特定密钥文件从git仓库拉取项目github
系统默认的ssh key存放在以下目录:shell
[root@hostname ~]# cd ~/.ssh/ [root@hostname .ssh]# ls authorized_keys id_rsa id_rsa.pub known_hosts
咱们将新建.git目录,用来存放git相关部署key的公私钥。ruby
[root@hostname .ssh]# mkdir ~/.git[root@hostname .ssh]# ssh-keygen -t rsa -f ~/.git/pub_coding.id_rsa -C "sunsky.lau@gmail.com"Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): # 回车Enter same passphrase again: # 回车Your identification has been saved in /root/.git/pub_coding.id_rsa. Your public key has been saved in /root/.git/pub_coding.id_rsa.pub. The key fingerprint is:b1:30:9c:9c:24:78:54:1e:b1:bb:d9:65:3a:44:8c:3b sunsky.lau@gmail.com The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | oo.=. | | . .* B | | . @ + | | * o | | E S o | | * + | | o + | | . | | | +-----------------+ [root@hostname .ssh]# ls ~/.git/ pub_coding.id_rsa pub_coding.id_rsa.pub
经过上述操做,在~/.git
目录下生成了2个文件,其中pub_coding.id_rsa
为私钥,pub_coding.id_rsa.pub
为公钥。
咱们须要将公钥添加到相关版本控制软件的帐户下。服务器
这种状况通常会发生在咱们本地的ssh默认端口和git仓库的ssh端口不一致时。好比,咱们本地使用了57832
做为ssh默认端口,而git仓库使用了22
做为ssh默认端口。
这种状况,对使用https方式访问git仓库的用户是不会受到影响的,可是会致使使用ssh方式访问git仓库的用户拉取项目使用。markdown
针对这个问题,这里提供两种解决方法:
使用ssh://的方式拉取项目
[root@hostname .ssh]# git clone ssh://git@git.coding.net:端口号/用户名/项目名称.git
咱们能够在上面的命令中去指明对应的ssh的端口号。
使用ssh config配置来自定义端口
这种方式,咱们将放到管理多ssh key的段落中去作介绍。
这个问题,换句话说就是如何git如何使用多ssh key。针对这种多ssh key的管理,咱们目前主要经过定义ssh的客户端配置文件来实现。
咱们能够在ssh的客户端配置文件文件中定义服务器别名、服务器地址以及针对特定服务器使用的一些专用链接配置信息。
有关ssh的客户端配置文件,咱们能够经过man config
来获取相关的介绍,这里简单放一部分介绍。
NAME ssh_config - OpenSSH SSH client configuration files SYNOPSIS ~/.ssh/config /etc/ssh/ssh_config DESCRIPTION ssh(1) obtains configuration data from the following sources in the following order: 1. command-line options 2. user’s configuration file (~/.ssh/config) 3. system-wide configuration file (/etc/ssh/ssh_config)
从描述中,咱们能够看到,有关ssh的客户端配置文件有2个,分别是~/.ssh/config
和/etc/ssh/ssh_config
。他们一个是用户范围的配置,一个是系统范围的配置。
因为咱们的操做要限定在用户范围,所以要使用~/.ssh/config
文件。
须要注意的是,~/.ssh/config
文件默认不存在,须要用户本身建立。
样例文件:
[root@hostname ~]# touch ~/.ssh/config [root@hostname ~]# cat ~/.ssh/config # github key Host git-github Port 22 User git HostName git.github.com PreferredAuthentications publickey IdentityFile ~/.git/pub_github.id_rsa # coding key Host git-coding Port 22 User git HostName git.coding.net PreferredAuthentications publickey IdentityFile ~/.git/pub_coding.id_rsa
下面对上述配置文件中使用到的配置字段信息进行简单解释。
Host 它涵盖了下面一个段的配置,咱们能够经过他来替代将要链接的服务器地址。 这里可使用任意字段或通配符。 当ssh的时候若是服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被做为最终的服务器地址使用,而且将使用该Host字段下面配置的全部自定义配置来覆盖默认的`/etc/ssh/ssh_config`配置信息。 Port 自定义的端口 User 自定义的用户名 HostName 真正链接的服务器地址 PreferredAuthentications 指定优先使用哪一种方式验证,支持密码和秘钥验证方式 IdentityFile 指定本次链接使用的密钥文件
经过上面设置以后,咱们就可使用多ssh key来链接不一样的git仓库了
咱们可使用ssh
来进行链接验证测试。
[root@hostname .ssh]# ssh -T git@git-coding Hello 用户名 You've connected to Coding.net by SSH successfully! [root@hostname .ssh]# ssh -T git@git-github Hi 用户名! You've successfully authenticated, but GitHub does not provide shell access.
经过上述设置以后,咱们就能够经过不一样的Host来针对不一样的git仓库和git项目使用不一样的ssh key了。可是,这里还须要注意的是,一般状况下咱们从git仓库拉取的项目ssh访问地址,相似这种git@git仓库地址:用户名/项目名.git
。咱们必定要把这里的git仓库地址替换为咱们ssh config里面设定的Host。
范例:
[root@hostname .ssh]# git clone git@github.com:用户名/项目名.git 替换为以下 [root@hostname .ssh]# git clone git@pub_github:用户名/项目名.git
到这里就大功告成了!
参考连接:
多个ssh key的管理
git生成ssh key及本地解决多个ssh key的问题
ssh配置讲解大全
ssh_config 文件配置详解
ssh config配置更新