> For the complete documentation index, see [llms.txt](https://rodneycheung.gitbook.io/handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rodneycheung.gitbook.io/handbook/2.-huan-jing-pian/kai-fa-huan-jing-pei-zhi/tong-yong/git_add_pub_key.md).

# Git服务器添加SSH Key

## 一、检查本机是否存在SSH key

输入以下命令：

```bash
ls -al ~/.ssh
```

如果没有类似`id_rsa.pub`、`id_ecdsa.pub`和`id_ed25519.pub`这样的文件，说明就需要生成新的SSH key。

## 二、生成SSH key

1. `ssh-keygen -t rsa -b 4096 -C "your_email@example.com"`
2. 接着会提示输入key保存位置和输入安全密码短语，直接回车使用默认值即可

## 三、启动ssh-agent

1. `eval "$(ssh-agent -s)"`
2. 将生成的密钥加入到ssh中:`ssh-add ~/.ssh/id_rsa`

## 四、将SSH key添加到github

1. 输出SSH key内容并拷贝

   ```
    cat ~/.ssh/id_rsa.pub
   ```
2. 粘贴到github账户的`Setting->SSH and GPG keys->New SSH key`中。

## 五、troubleshooting

1. `Unable to negotiate with xxx.xxx.xxx.xxx port 22: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1`

   &#x20;在\~/.ssh/config中加入以下内容:

   ```
    Host *
    KexAlgorithms +diffie-hellman-group1-sha1
   ```
