同一台PC链接多个git服务器


同一台PC链接多个git服务器

1.~/.ssh/目录下创建config文件
2.生成gitlab的公钥
    ssh-keygen -t rsa -C "qdyxmas@qq.com" -f ~/.ssh/id_rsa_lab
3.生成github的公钥
    ssh-keygen -t rsa -C "qdyxmas@gmail.com" -f ~/.ssh/id_rsa_hub
4.编辑config文件,把gitlab和github的信息输入
    Host github.com
        User qdyxmas@gmail.com
        Hostname ssh.github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_hub
        Port 443
    Host gitlab.com
      Hostname altssh.gitlab.com
      User qdyxmas@qq.com 
      Port 443
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/id_rsa_lab
6.把公钥信息复制到gitlab(github)
     ~/.ssh/id_rsa_lab.pub
7.测试是否正常ssh -vT git@gitlab.com
    root@qdyxmas:~/unittest# ssh -vT git@gitlab.com
    OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
    debug1: Reading configuration data /root/.ssh/config
    debug1: /root/.ssh/config line 7: Applying options for gitlab.com
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug1: Connecting to altssh.gitlab.com [35.190.168.187] port 443.
    debug1: Connection established.
    debug1: permanently_set_uid: 0/0
    debug1: identity file /root/.ssh/id_rsa_lab type 1
    debug1: key_load_public: No such file or directory
    debug1: identity file /root/.ssh/id_rsa_lab-cert type -1
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
    debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
    debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.8 pat OpenSSH* compat 0x04000000
    debug1: Authenticating to altssh.gitlab.com:443 as 'git'
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    debug1: kex: algorithm: curve25519-sha256@libssh.org
    debug1: kex: host key algorithm: ecdsa-sha2-nistp256
    debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
    debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug1: Server host key: ecdsa-sha2-nistp256 SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw
    debug1: Host '[altssh.gitlab.com]:443' is known and matches the ECDSA host key.
    debug1: Found key in /root/.ssh/known_hosts:3
    debug1: rekey after 134217728 blocks
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug1: rekey after 134217728 blocks
    debug1: SSH2_MSG_NEWKEYS received
    debug1: SSH2_MSG_EXT_INFO received
    debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug1: Authentications that can continue: publickey
    debug1: Next authentication method: publickey
    debug1: Offering RSA public key: /root/.ssh/id_rsa_lab
    debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
    debug1: Authentication succeeded (publickey).
    Authenticated to altssh.gitlab.com ([35.190.168.187]:443).
    debug1: channel 0: new [client-session]
    debug1: Requesting no-more-sessions@openssh.com
    debug1: Entering interactive session.
    debug1: pledge: network
    debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
    debug1: Remote: Forced command.
    debug1: Remote: Port forwarding disabled.
    debug1: Remote: X11 forwarding disabled.
    debug1: Remote: Agent forwarding disabled.
    debug1: Remote: PTY allocation disabled.
    debug1: Remote: Forced command.
    debug1: Remote: Port forwarding disabled.
    debug1: Remote: X11 forwarding disabled.
    debug1: Remote: Agent forwarding disabled.
    debug1: Remote: PTY allocation disabled.
    debug1: Sending environment.
    debug1: Sending env LANG = en_US.UTF-8
    Welcome to GitLab, @qudeyong!
    debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
    debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
    debug1: channel 0: free: client-session, nchannels 1
    Transferred: sent 2672, received 3304 bytes, in 0.5 seconds
    Bytes per second: sent 5369.5, received 6639.6
    debug1: Exit status 0
8. jenkins上账号下载代码
    jenkins下的凭据=》全局凭据=》添加凭据
    选择SSH Username with private key
    username:填写凭据说明,比如gitlab_key
    私钥内容里面把~/.ssh/id_rsa_lab内容复制过来就OK