Git使用SSH配置多环境

准备

  1. git下载安装 https://blog.csdn.net/sanxd/article/details/82624127
  2. 配置环境变量
    $ where git
    D:\Program Files\Git\mingw64\bin\git.exe
    where git result
    系统变量Path中添加 D:\Program Files\Git\mingw64\bin;D:\Program Files\Git\mingw64\libexec\git-core
  3. Github访问慢?
    修改host https://www.cnblogs.com/liuchao888/p/11733996.html

SSH多环境配置

  1. 熟悉ssh
    gitlab ssh://git@IP:port/组名/项目名.git
    github git@github.com:用户名/项目名.git
  2. 新建密钥 完整步骤链接: https://www.cnblogs.com/popfisher/p/5731232.html

注意: 没有.ssh文件夹就新建,把生成的密钥对放进.ssh中

EDZ@WIN-4451K6L42EA MINGW64 ~
$ **ssh-keygen -t rsa -C 邮箱**   //这是输入的
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/EDZ/.ssh/id_rsa):   #直接回车密钥在 /用户名/.ssh/下,否则直接 /用户名/下   这是输入的
Created directory '/c/Users/EDZ/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/EDZ/.ssh/id_rsa
Your public key has been saved in /c/Users/EDZ/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:lr/H9b/BUwCNJgWc8bKuLF+z8EDcRedh14BEIuIWx6w 邮箱值
The key's randomart image is:
+---[RSA 3072]----+
|      ooo.+B*+=.o|
|     . +o.+++=.o.|
|      o.  .oo o  |
|     .E. o +   . |
|        S o     .|
|       o o    o .|
|        o =. . = |
|      .. * +o   +|
|       o+ +.   .+|
+----[SHA256]-----+

$** ssh-keygen -t rsa -C 邮箱**  //这是输入的
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/EDZ/.ssh/id_rsa): **id_rsa_github**   #/用户名/下   这是输入的
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github
Your public key has been saved in id_rsa_github.pub
The key fingerprint is:
SHA256:l/i05dz6rLzMkEB7PCHGZBVgnHNFzFr1TKOWhCuHYUw 邮箱值
The key's randomart image is:
+---[RSA 3072]----+
|       .BEo*+o...|
|       =++..= o+.|
|        *o+o.+  o|
|       o B.=.    |
|        S X .    |
|         * B .   |
|          = o .  |
|           = o   |
|            B+o  |
+----[SHA256]-----+ 
  1. 添加公钥到服务器上
  2. 针对不同的密钥对使用不同的设置,需要配置known_hosts文件
    使用命令将私钥添加到配置列表 known_hosts 中
$ssh-agent bash
$ssh-add ~/.ssh/id_rsa_github                   # id_rsa_github是私钥名  命令运行完毕后,会在.ssh下生成known_hosts 文件
  1. 配置不同环境下默认的登录用户.在用户目录下 C:\Users\EDZ.ssh下,没有config文本文件就新建。
    Host 域名别名,简短,使用别名后,ssh://git@IP:port/gdicbc/estage.git。gitlab.hrfax.cn直接用别名
    HostName         真实的域名地址 域名/github.com
    IdentityFile         id_rsa的地址
    PreferredAuthentications 配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等
    User             配置使用用户名
    config配置文件内容如下
# 配置github.com
Host github.com                 
    HostName github.com
    IdentityFile C:\\Users\\popfisher\\.ssh\\id_rsa_github
    PreferredAuthentications publickey
    User username1
# 配置git.oschina.net 
Host git.oschina.net 
    HostName git.oschina.net
    IdentityFile C:\\Users\\popfisher\\.ssh\\id_rsa_oschina
    PreferredAuthentications publickey
    User username2

测试git配置成功 -T:不显示终端,只显示连接成功信息 完整提示链接:https://www.cnblogs.com/hanguozhi/p/10878043.html
ssh -T git@github.com
在使用ssh -T时,gitlab提示connection timeout. 但ping域名是能ping通的。最后尝试 git clone服务器上的代码,就好啦,提示如下:
The authenticity of host '[IP]:port ([IP]:port)' can't be established.
RSA key fingerprint is SHA256:kVqyxRfS0owcnjaxxsTx55IYOUii9sZi1EywGUsPrfE.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
填yes后让你输入密码,就可以用了。


画外题
查看当前用户名和邮箱
git config
可以看到是没有全局的用户名和密码的。
当然,你可以设置,也可以不设置全局用户名和邮箱,命令如下:
git config --global user.name "yourname"
git config --global user.email "mygitlab@mali.cn"

posted @ 2020-09-22 21:50  wana  阅读(496)  评论(0编辑  收藏  举报