Git使用ssh key

时间:2023-11-25 22:08:26

生成ssh key步骤

这里以配置github的ssh key为例:

1. 配置git用户名和邮箱

git config user.name
"用户名"

git config user.email
"邮箱"

在config后加上 --global 即可全局设置用户名和邮箱。

2. 生成ssh key

ssh-keygen
-t rsa -C
"邮箱"

然后根据提示连续回车即可在~/.ssh目录下得到id_rsa和id_rsa.pub两个文件,id_rsa.pub文件里存放的就是我们要使用的key。

3. 上传key到github

clip < ~/.ssh/id_rsa.pub

  1. 复制key到剪贴板
  2. 登录github
  3. 点击右上方的Accounting settings图标
  4. 选择 SSH key
  5. 点击 Add SSH key

4. 测试是否配置成功

ssh -T git@github.com

如果配置成功,则会显示:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

解决本地多个ssh key问题

有的时候,不仅github使用ssh key,工作项目或者其他云平台可能也需要使用ssh key来认证,如果每次都覆盖了原来的id_rsa文件,那么之前的认证就会失效。这个问题我们可以通过在~/.ssh目录下增加config文件来解决。

下面以配置搜狐云平台的ssh key为例。

1. 第一步依然是配置git用户名和邮箱

git config user.name
"用户名"

git config user.email
"邮箱"

2. 生成ssh key时同时指定保存的文件名

ssh-keygen
-t rsa -f ~/.ssh/id_rsa.sohu -C
"email"

上面的id_rsa.sohu就是我们指定的文件名,这时~/.ssh目录下会多出id_rsa.sohu和id_rsa.sohu.pub两个文件,id_rsa.sohu.pub里保存的就是我们要使用的key。

3. 新增并配置config文件

添加config文件

如果config文件不存在,先添加;存在则直接修改

touch ~/.ssh/config

在config文件里添加如下内容(User表示你的用户名)

Host *.cloudscape.sohu.com

IdentityFile ~/.ssh/id_rsa.sohu

User test

4. 上传key到云平台后台(省略)

5. 测试ssh key是否配置成功

ssh -T git@git.cloudscape.sohu.com

成功的话会显示:

Welcome to GitLab, username!

至此,本地便成功配置多个ssh key。日后如需添加,则安装上述配置生成key,并修改config文件即可。

端口

端口可能会被防火墙屏蔽,可以设置SSH使用HTTPS的403端口。

测试HTTPS端口是否可用

2
3

$ ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

编辑SSH配置文件 ~/.ssh/config 如下:

2
3

测试是否配置成功

2
3

$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

 

Caching your GitHub password in Git

If you're cloning GitHub repositories using HTTPS, you can use a credential helper to tell Git to remember your GitHub username and password every time it talks to GitHub.

If you clone GitHub repositories using SSH, then you authenticate using SSH keys instead of a username and password. For help setting up an SSH connection, see Generating an SSH Key.

Tip: You need Git 1.7.10 or newer to use the credential helper.

The credential helper is included with GitHub Desktop. The app also provides a Git shell so you won't ever need to install and configure Git manually. For more information, see "Getting Started with GitHub Desktop."

If you prefer working with the command line, you can also install a native Git shell, such as msysgit. With msysgit, running the following in the command line will store your credentials:

git config --global credential.helper wincred

 

一些错误

disconnected no supported authentication methods available(server sent: publickey,keyboard interae)

安装Git客户端后,进行PULL时报如下错误

disconnected no supported authentication methods available(server sent: publickey,keyboard interactive)解决方案

 
 

因为TortoiseGit和Git的冲突
我们需要把TortoiseGit设置改正如下。

1.找到TortoiseGit -> Settings -> Network

2.将SSH client指向~\Git\bin\ssh.exe(Git安装路径下)(Git2.7版本下在C:\Program Files\Git\usr\bin)

然后便可正确push和pull

Git使用ssh key