目录

配置ssh方式连接git

配置ssh方式连接git,每次配置都会忘记操作步骤,这里记录下。

1 关于 ssh

SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。[2]使用 SSH 协议可以连接远程服务器和服务并向它们验证。连接远程仓库时无需输入密码而且能实现对 github 的流畅访问。除此之外我还用 ssh 协议成功在 Windows Terminal 中连接上了自己的远程服务器。

2 检查现有的 ssh 密钥

Windows下检查: 桌面左下角windows徽标鼠标右键后展开应用和功能–>点击进入后找到【可选功能】—>查找【openssh】出现openssh客户端即表示自带的openssh可以使用;

Windows 打开 GitBash,Linux 下打开终端,输入:

1
$ ls -al ~/.ssh

如果你看到以下输出,那么说明你已经有 ssh 密钥(以 .pub 结尾的文件):

1
2
3
4
5
6
7
8
$ ls -al ~/.ssh |
total 26 |
drwxr-xr-x 1 197121    0 Mar 31 22:29 ./ |
drwxr-xr-x 1 197121    0 Apr 16 23:36 ../ |
-rw-r--r-- 1 197121 2610 Mar 31 22:27 id_rsa |
-rw-r--r-- 1 197121  573 Mar 31 22:27 id_rsa.pub |
-rw-r--r-- 1 197121  831 Apr  1 12:08 known_hosts |
-rw-r--r-- 1 197121   92 Mar 31 22:26 known_hosts.old |

如果你不想用原来的密钥或者没有密钥的话也不用着急,看下一步如何生成新的 ssh 密钥。

3 创建新的 ssh 密钥三部曲

3、1 生成新的 ssh 密钥

在 Windows 的 GitBash 或 Linux 终端输入:

1
2
$ ssh-keygen -t rsa -b 4096 -C "51482508@qq.com"
# 你可以把引号里的替换为你自己的邮箱,不换的话问题不大

然后终端会让你输入一些东西,可以不用输入直接一路回车,看到:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
SE@DESKTOP-8024PR5 MINGW64 ~/.ssh

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/SE/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/SE/.ssh/id_rsa
Your public key has been saved in /c/Users/SE/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:GLo38kvQhXjfnH1pTdjCQEwnzqr27nWPNOdG5SPfur4 51482508@qq.com
The key's randomart image is:
+---[RSA 4096]----+
|           += .  |
|     . .   o.= o |
|    . + .   o + o|
|     + = o +   =.|
|    o o S = . +.o|
|     o   .   + .o|
|    o + o   . *.+|
|     = o . . o Bo|
|      o. o+  .E=o|
+----[SHA256]-----+

代表密钥生成成功。

3、2 将SSH密钥添加到ssh-agent

3、2.1 确保 ssh 正在运行

终端输入:

1
$ ssh-agent -s

看到如下输出:

1
2
3
SSH_AUTH_SOCK=/tmp/ssh-WyTlOzJrybcW/agent.720; export SSH_AUTH_SOCK;
SSH_AGENT_PID=721; export SSH_AGENT_PID;
echo Agent pid 721;

代表 ssh 正常运行。

3.3.3 添加 ssh 到账户

执行以下两条命令:

1
2
3
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa 
# 这里如果文件名被改过要写你自己定义的文件名

看到输出:

1
dentity added: id_rsa (your_email@example.com)

添加成功!

4 将密钥添加到 github 账户

4、1 复制你的公钥

在终端使用:

1
$ cat ~/.ssh/id_rsa.pub

看到:

1
ssh-rsa AAAAB3NzaC1yc0EAAAADAQABAAACAQCtnLpzoVOiv2ABq+n/cexRh1aAyqpjFvSDx20GEG96DW8Q4PEbBcn6Mb/E3F3wTjI+2Hv5Gw3aoQ6JN8McqqPaeqzj82QKIjQFwah4RQNISUn39au7MpWfWpolQOv1YiJvq+GKaVfK2LBaq5hHx6Y6UpV1aJWics91SIWp3wg0MBMC0apSMwtCbbeNqb+4KpV1C5Sq9+qIeEEMnDS/SBKHh8tuRoGvUa5i39LTNLY3UM4Hqml78UnlzpOaobuFE4BgtWkYNQoDCkt9/6xKyh3JB+yKIDystfhWFBNUHF3Of6Zkfi95zG9ra/CzyPyTgRgyYtuje+uhlviIUMyg66c5crAgG8SVSqYuYhBrSVB72S02SIi2g6q2k8BAspnV3ZqO+KzC+KpYj5mYboQP6X2SyTbZB9w6f7TKYHoNb9jIF0xXNJOStf5gjs7YGf+PGrDcPuzuakmOKLCh2PEA+Y60VVpqHgK/H0EPUu0a/H4h1gsDIry5Xps4Pl/sGvgZ1JkdBTlcV45v3T4o9BvLa7cpf/IF0+NsVhENBg5JLJ2tFLFnhq60bP9aTqEiAeK4iFY4ee1aADR/szb5/FZg7YQvRezhAkuH0nTardb89FwDV4AnS3QObTBLDZqemRnRlW9SVhlpz68pvV7ht71jInnLwvD0/5zbnpdTIRmFKTp/Ew== 51482508@qq.com

全给复制下来!

4、2 将公钥添加到 github 账户

然后去 gihub:

  • 右上角下拉面板选择 Settings
  • 左侧选择 SSH and GPG keys
  • 点击 New SSH key
  • 随便起一个 title;
  • 把公钥粘贴到下面。

可能会输入密码,添加完成!

5 测试 ssh 连接

在终端中输入:

1
$ ssh -T git@github.com

可能看到如下警告:

1
2
3
4
5
6
7
$ ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi Chaggai7! You've successfully authenticated, but GitHub does not provide shell access.

输入 yes:

1
2
3
4
5
6
7
$ ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi Chaggai7! You've successfully authenticated, but GitHub does not provide shell access.

如果 xxxxxx 是你的 github 用户名,说明成功。

6 配置 git 使用 ssh 密钥登录

首先将你的仓库 clone 下来到一个位置。然后进入你的仓库。

6.1 检查当前的 remote url

在仓库下输入:

1
$ git remote -v

看到输出:

1
2
origin  https://xxxxx.git (fetch)
origin  https://xxxxx.git (push)

说明当前使用的还是 https 协议,如果以 git 开头表示 git 协议。

github把master默认分支改为了main, 为了适应它这种政治正确的变化, 我们也要做相应调整, 把本地git的master改成main.

其实所谓的把master改成main, 只不过是修改git的配置文件而已, 毕竟不管任何应用程序, 能灵活改动的地方必然就是配置文件.

前置提示

  1. 请使用2.28版本以后的git
  2. 默认分支和主分支我这里是一个意思, 毕竟在没创建项目前叫默认分支, 但创建项目后一般都会用默认分支作为主分支

把默认分支改为main

windows中git的配置文件在C:\Users\<用户名>

经过上面的修改, 当我们使用git init初始化某个项目的时候, 默认就会使用main做为主分支

除了手动修改配置文件外, 也可以使用git命令, 效果和手动修改没区别

1
git config --global init.defaultBranch main

以上方法只是让以后创建的项目默认分支为main, 但对于已经创建的项目则无能为力, 所以我们还需要对已存在的项目逐个进行修改.

修改已创建项目的主分支为main

  1. 切换到主分支master
  2. 使用git branch -M main命令, 把当前master分支改名为main, 其中-M的意思是移动或者重命名当前分支 此时你可能困在master和main的区别上,仔细看你的gitbash是不是main,有可能是master,如下: ~~~ SE@DESKTOP-88 MINGW64 /f/share/git (master) ~~~~ github在2020/10/1宣布上的所有新库都将用中性词「main」命名,取代原来的「master」,如果我们通过git push -u grigin master 方法上传仓库,在github仓库中就会出现一个master的分支。

现在都2023年了。这时候当然需要调整回main:

1
git config --global init.defaultBranch main

6.2 修改 remote url 为 git 协议

上 github 仓库,点 Code,选择 SSH,复制链接:如下

git@github.com:MaartenBaert/ssr.git 注意不是 https://github.com/MaartenBaert/ssr.git 在终端输入:

1
$ git remote set-url origin git@github.com:xxxxx.git

再检查 git 协议:

1
$ git remote -v

出现:

1
2
origin  git@xxxxx.git (fetch)
origin  git@xxxxx.git (push)

successed!

接下来可以愉快的 push&pull 了。 如果你真的坑在master和main的问题上按照上面如法炮制即可。最后说一下这个邮箱是任意手写的别介意啊!你可以使用你自己的。

7 附录

开始配置 1、下载并且安装git 2、第一次配置SSH (1)打开Git Bash,若是首次安装使用git,先配置用户名称和邮箱

1
2
3
4
5
6
7
#git config --global user.name "姓名"
#git config --global user.email "邮箱地址"

git config --global user.name "wangb072"
git config --global user.email "wangb072@chinatelecom.cn"


如果不是首次使用git,那么可以先查看自己的用户名以及邮箱:

1
2
3
git config user.name
git config user.email

(2)生成密钥

1
2
3
4
5
6
7
#ssh-keygen -t rsa -C "你的邮箱地址"

#ssh-keygen -t rsa -C "wangb072@chinatelecom.cn"


ssh-keygen -m PEM -t rsa -C wangb072@chinatelecom.cn -b 4096
    

生成密钥前

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
dragon@LAPTOP-UB9FPVOU MINGW64 /
$  ls -al ~/.ssh
total 13
drwxr-xr-x 1 dragon 197121   0 Oct 23 15:38 ./
drwxr-xr-x 1 dragon 197121   0 Nov 15 19:12 ../
-rw-r--r-- 1 dragon 197121 356 Nov  6 10:06 known_hosts

dragon@LAPTOP-UB9FPVOU MINGW64 /


生成密钥后

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
dragon@LAPTOP-UB9FPVOU MINGW64 /
$ ssh-keygen -t rsa -C "wangb072@chinatelecom.cn"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/dragon/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/dragon/.ssh/id_rsa
Your public key has been saved in /c/Users/dragon/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:1rZkqeSa5bVCJVQM4kz7xBkXRy+8xUkJe68P31t61vw wangb072@chinatelecom.cn
The key's randomart image is:
+---[RSA 3072]----+
|      o o++o+... |
|     + +.+.o =.. |
|      +.+   + *  |
|       o.... = . |
|        So* .   .|
|       +.= .   . |
|       .+ o   o.o|
|       =.. .   =B|
|      o ...   .+E|
+----[SHA256]-----+

dragon@LAPTOP-UB9FPVOU MINGW64 /
$

dragon@LAPTOP-UB9FPVOU MINGW64 /
$ ls -al ~/.ssh
total 18
drwxr-xr-x 1 dragon 197121    0 Nov 20 09:04 ./
drwxr-xr-x 1 dragon 197121    0 Nov 20 09:03 ../
-rw-r--r-- 1 dragon 197121 2610 Nov 20 09:04 id_rsa
-rw-r--r-- 1 dragon 197121  578 Nov 20 09:04 id_rsa.pub
-rw-r--r-- 1 dragon 197121  356 Nov  6 10:06 known_hosts




打开id_rsa.pub,并且复制里面的密钥

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cat ~/.ssh/id_rsa.pub


dragon@LAPTOP-UB9FPVOU MINGW64 /
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDiBgZlQ/Q5MGHyf1nv1s82SS+yWLHa6YzIc/jkH441iHywOqIpTkynT2VcHL+9ut27W2soArOKzy2fNOu3cmQbPR4Rf+40lAczZZ7dDmeZA4Dyg1Rhzbyovnb2yUY6jtASBn3RDMvC1QAOcfoYb+8HiHFX88I+hab7RuuSVgcgFhOAV4KIGIhyIBi/KL5ar9Y+0L7LgIoQ2IP8FaqcJd58up2tQdidST6UtIu9pLxgd2e1H+SaTR7M1a30nHQeOCpi5dBp59V0oPvJs7b6sKLPufWFANzJlokBP4iSqiX27OFQx9ZcBSBpLbVNXsn6pEfEni66R31pi/kRBePP6oIsaX2FpQcvEHqQe3RxyNmGe1lvCJ25CeSOhIOljSyKMUFulKJ2CmKNrQyufD/dizHYrkclBJGccIk+WJlRbHc8YZPXp6HfXWhIDF5iLw6Zzy2gnYJvJpWHFWsULDMg6lVYhPncRHQK92W5Vjg0Bk0Fjy8RfFyZi5WEWZJUUwufSRU= wangb072@chinatelecom.cn

dragon@LAPTOP-UB9FPVOU MINGW64 /
$


8 问题报错

git clone 项目失败

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dragon@LAPTOP-UB9FPVOU MINGW64 /d/GoProject
$ git clone ssh://wangb072@code.srdcloud.cn:29418/CTDICaaS/ctdi-caas-kubesphere && scp -p -P 29418 wangb072@code.srdcloud.cn:hooks/commit-msg ctdi-caas-kubesphere/.git/hooks/
Cloning into 'ctdi-caas-kubesphere'...
Unable to negotiate with 10.158.231.11 port 29418: no matching host key type found. Their offer: ssh-rsa,ssh-dss
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

dragon@LAPTOP-UB9FPVOU MINGW64 /d/GoProject

添加修改~/.ssh/Config,如果原来已有~/.ssh/Config,请备份

1
2
3
#touch ~/.ssh/Config

vi ~/.ssh/Config

填写如下内容

1
2
3
4
Host *

HostkeyAlgorithms +ssh-dss,ssh-rsa
PubkeyAcceptedKeyTypes +ssh-dss,ssh-rsa
1
2
3
4
5
6
7
$ cat ~/.ssh/Config

Host *

HostkeyAlgorithms +ssh-dss,ssh-rsa
PubkeyAcceptedKeyTypes +ssh-dss,ssh-rsa

1
2
3
4
5
Host *

KexAlgorithms +diffie-hellman-group1-sha1
HostkeyAlgorithms +ssh-dss,ssh-rsa
PubkeyAcceptedKeyTypes +ssh-dss,ssh-rsa

在windows的gitbash中,git add时出现如下告警信息

1
LF will be replaced by CRLF the next time Git touches it

Windows组合使用了CRLF(0x0D 0x0A, \r\n),无疑是符合标准语义的做法。

尽管这不是一个Bug或错误,但还是可以通过如下方式对Git进行配置,以避免在每次提交代码时显示:

1
2
3
4
5
6
7
8
9
# Linux/macOS系统下在提交代码时自动将CRLF转换为LF
git config --global core.autocrlf input

# Windows系统下在提交代码时自动将LF转换为CRLF
git config --global core.autocrlf true


# Windows系统下在提交检出均不转换
git config --global core.autocrlf false

实际执行如下: 提交时转换为LF,检出时不转换

1
2
# Linux/macOS系统下在提交代码时自动将CRLF转换为LF
git config --global core.autocrlf input
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
dragon@LAPTOP-UB9FPVOU MINGW64 /d/GoProject/ctdi-caas-kubesphere (master)
$ git add -A
warning: in the working copy of '.github/release-drafter.yml', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of '.github/workflows/release-drafter.yml', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-1.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-2.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-3.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-4.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-5.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-6.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-7.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-matrix-8.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-1.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-2.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-3.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-4.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-5.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-6.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-7.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'pkg/simple/client/monitoring/metricsserver/testdata/metrics-vector-8.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/MakeNowJust/heredoc/README.md', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/go-logfmt/logfmt/README.md', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/gosimple/slug/.gitignore', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/gosimple/slug/README.md', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/hashicorp/hcl/.gitignore', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/hashicorp/hcl/Makefile', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/oliveagle/jsonpath/.travis.yml', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/oliveagle/jsonpath/readme.md', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'vendor/github.com/pelletier/go-toml/example-crlf.toml', CRLF will be replaced by LF the next time Git touches it


9 参考

https://www.cnblogs.com/lyndonlu/articles/16919707.html

https://blog.csdn.net/xinlingncut/article/details/130811590