|
|
|
# SSH密钥[](#ssh "Permalink")
|
|
|
|
|
|
|
|
Git 是一个分布式版本控制系统,这意味着您可以在本地工作. 此外,您还可以将更改共享或"推送"到其他服务器. CODE CHINA 支持使用 SSH 密钥在 Git 及其服务器之间进行安全通信.
|
|
|
|
Git 是一个分布式版本控制系统,这意味着您可以在本地工作. 此外,您还可以将更改共享或"推送"到其他服务器. GitCode 支持使用 SSH 密钥在 Git 及其服务器之间进行安全通信.
|
|
|
|
|
|
|
|
SSH 协议提供的这种安全机制,并允许您向 CODE CHINA 远程服务器进行身份验证,而无需每次都提供用户名或密码,我们推荐您使用这种方式来作为您推拉代码的凭据。
|
|
|
|
SSH 协议提供的这种安全机制,并允许您向 GitCode 远程服务器进行身份验证,而无需每次都提供用户名或密码,我们推荐您使用这种方式来作为您推拉代码的凭据。
|
|
|
|
|
|
|
|
通过阅读本文档可以帮助您配置安全的 SSH 密钥,这些密钥可用于帮助保护与 CODE CHINA 代码仓库的安全连接。
|
|
|
|
通过阅读本文档可以帮助您配置安全的 SSH 密钥,这些密钥可用于帮助保护与 GitCode 代码仓库的安全连接。
|
|
|
|
|
|
|
|
* 如果您需要有关创建 SSH 密钥的信息,请从我们的[ SSH 密钥选项](#options-for-ssh-keys)开始
|
|
|
|
* 如果您有专用于 CODE CHINA 帐户的 SSH 密钥,则可能对[使用非默认 SSH 密钥对路径](#working-with-non-default-ssh-key-pair-paths)感兴趣
|
|
|
|
* 如果您已经有了 SSH 密钥对,则可以转到[将 SSH 密钥添加到 CODE CHINA 帐户的方法](#adding-an-ssh-key-to-your-account)
|
|
|
|
* 如果您有专用于 GitCode 帐户的 SSH 密钥,则可能对[使用非默认 SSH 密钥对路径](#working-with-non-default-ssh-key-pair-paths)感兴趣
|
|
|
|
* 如果您已经有了 SSH 密钥对,则可以转到[将 SSH 密钥添加到 GitCode 帐户的方法](#adding-an-ssh-key-to-your-account)
|
|
|
|
|
|
|
|
## 环境要求[](#requirements "Permalink")
|
|
|
|
|
|
|
|
为了支持 SSH,CODE CHINA 需要安装 OpenSSH 客户端,该客户端已预装在 GNU / Linux 和 macOS 上,但未预先安装在 Windows 上。
|
|
|
|
为了支持 SSH,GitCode 需要安装 OpenSSH 客户端,该客户端已预装在 GNU / Linux 和 macOS 上,但未预先安装在 Windows 上。
|
|
|
|
|
|
|
|
我们要求您使用 SSH 6.5 或更高版本,因为它不包括现在不安全的 MD5 签名。您可以通过下命令查看系统上安装的 SSH 版本:
|
|
|
|
|
| ... | ... | @@ -22,7 +22,7 @@ ssh -V |
|
|
|
|
|
|
|
## SSH 密钥选项[](#options-for-ssh-keys "Permalink")
|
|
|
|
|
|
|
|
CODE CHINA 支持使用 RSA,DSA,ECDSA 和 ED25519 密钥。
|
|
|
|
GitCode 支持使用 RSA,DSA,ECDSA 和 ED25519 密钥。
|
|
|
|
|
|
|
|
**提示:**现有文档表明 ED25519 更安全,如果使用 RSA 密钥,则美国国家科学技术研究院[出版物 800-57 第 3 部分(PDF)](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57Pt3r1.pdf)建议密钥大小至少为 2048 位。
|
|
|
|
|
| ... | ... | @@ -30,7 +30,7 @@ CODE CHINA 支持使用 RSA,DSA,ECDSA 和 ED25519 密钥。 |
|
|
|
|
|
|
|
## 查看现有的 SSH 密钥[](#review-existing-ssh-keys "Permalink")
|
|
|
|
|
|
|
|
如果您已有 SSH 密钥,则可以使用它们来与 CODE CHINA 的代码库进行连接。 默认情况下,Linux 和 macOS 系统上的 SSH 密钥存储在用户的主目录中的`.ssh/`子目录中,下表包括每种 SSH 密钥算法的默认文件名:
|
|
|
|
如果您已有 SSH 密钥,则可以使用它们来与 GitCode 的代码库进行连接。 默认情况下,Linux 和 macOS 系统上的 SSH 密钥存储在用户的主目录中的`.ssh/`子目录中,下表包括每种 SSH 密钥算法的默认文件名:
|
|
|
|
|
|
|
|
| 算法 | 公钥 | 私钥 |
|
|
|
|
| --- | --- | --- |
|
| ... | ... | @@ -128,7 +128,7 @@ ssh-keygen -p -f /path/to/ssh_key |
|
|
|
|
|
|
|
如果您的 OpenSSH 版本介于 6.5 至 7.8(含)之间,请使用`-o`选项运行`ssh-keygen` ,以更安全的 OpenSSH 格式保存您的 SSH 私钥。
|
|
|
|
|
|
|
|
如果您已经具有可用于 CODE CHINA 的 RSA SSH 密钥,请考虑对其进行升级以使用更安全的密码加密格式。 您可以使用以下命令进行操作:
|
|
|
|
如果您已经具有可用于 GitCode 的 RSA SSH 密钥,请考虑对其进行升级以使用更安全的密码加密格式。 您可以使用以下命令进行操作:
|
|
|
|
|
|
|
|
```
|
|
|
|
ssh-keygen -o -f ~/.ssh/id_rsa
|
| ... | ... | @@ -142,9 +142,9 @@ ssh-keygen -o -t rsa -b 4096 -C "email@example.com" |
|
|
|
|
|
|
|
**注意:**ED25519 已将密钥加密为更安全的 OpenSSH 格式。
|
|
|
|
|
|
|
|
## 在您的 CODE CHINA 账号中添加 SSH 密钥[](#adding-an-ssh-key-to-your-account "Permalink")
|
|
|
|
## 在您的 GitCode 账号中添加 SSH 密钥[](#adding-an-ssh-key-to-your-account "Permalink")
|
|
|
|
|
|
|
|
现在,您可以将创建好的 SSH 密钥复制到您的 CODE CHINA 帐户。您可以参考以下步骤:
|
|
|
|
现在,您可以将创建好的 SSH 密钥复制到您的 GitCode 帐户。您可以参考以下步骤:
|
|
|
|
|
|
|
|
1. 从以文本格式保存 SSH 密钥的位置复制您的**公共** SSH 密钥,以下命令可以将 ED25519 的信息保存到指定操作系统的剪贴板中:
|
|
|
|
|
| ... | ... | @@ -168,7 +168,7 @@ ssh-keygen -o -t rsa -b 4096 -C "email@example.com" |
|
|
|
|
|
|
|
如果您使用的是 RSA 密钥,相应地替换即可。
|
|
|
|
|
|
|
|
2. 打开`https://codechina.csdn.net`并登录
|
|
|
|
2. 打开`https://gitcode.net`并登录
|
|
|
|
3. 选择右上角的头像,然后单击**设置**
|
|
|
|
4. 单击 **SSH 密钥**.
|
|
|
|
5. 将复制的公共密钥粘贴到**密钥**文本框中.
|
| ... | ... | @@ -185,29 +185,29 @@ ssh-keygen -o -t rsa -b 4096 -C "email@example.com" |
|
|
|
要测试是否正确添加了 SSH 密钥,可以在终端中运行以下命令:
|
|
|
|
|
|
|
|
```
|
|
|
|
ssh -T git@codechina.csdn.net
|
|
|
|
ssh -T git@gitcode.net
|
|
|
|
```
|
|
|
|
|
|
|
|
在您第一次通过 SSH 方式连接到 CODE CHINA的时候,将会询问您是否信任将要连接的 CODE CHINA host地址。当确认 `yes` 后,会将 CODE CHINA 作为已知主机添加到受信任的 hosts 地址中:
|
|
|
|
在您第一次通过 SSH 方式连接到 GitCode的时候,将会询问您是否信任将要连接的 GitCode host地址。当确认 `yes` 后,会将 GitCode 作为已知主机添加到受信任的 hosts 地址中:
|
|
|
|
|
|
|
|
```
|
|
|
|
The authenticity of host 'codechina.csdn.net (121.36.6.22)' can't be established.
|
|
|
|
The authenticity of host 'gitcode.net (121.36.6.22)' can't be established.
|
|
|
|
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
|
|
|
|
Are you sure you want to continue connecting (yes/no)? yes
|
|
|
|
Warning: Permanently added 'codechina.csdn.net' (ECDSA) to the list of known hosts.
|
|
|
|
Warning: Permanently added 'gitcode.net' (ECDSA) to the list of known hosts.
|
|
|
|
```
|
|
|
|
|
|
|
|
一旦添加到已知主机列表中,将不再要求您再次验证 CODE CHINA 主机的真实性。 再次运行以上命令时,您将只收到*欢迎使用 CODE CHINA 的`@username` !* 信息。
|
|
|
|
一旦添加到已知主机列表中,将不再要求您再次验证 GitCode 主机的真实性。 再次运行以上命令时,您将只收到*欢迎使用 GitCode 的`@username` !* 信息。
|
|
|
|
|
|
|
|
如果未出现欢迎消息,则可以通过使用以下命令在详细模式下运行`ssh`来解决问题:
|
|
|
|
|
|
|
|
```
|
|
|
|
ssh -Tv git@codechina.csdn.net
|
|
|
|
ssh -Tv git@gitcode.net
|
|
|
|
```
|
|
|
|
|
|
|
|
## 使用非默认路径的 SSH 密钥[](#working-with-non-default-ssh-key-pair-paths "Permalink")
|
|
|
|
|
|
|
|
如果您为 SSH 密钥对使用了非默认文件路径,请配置 SSH 客户端以指向 CODE CHINA 私有 SSH 密钥。
|
|
|
|
如果您为 SSH 密钥对使用了非默认文件路径,请配置 SSH 客户端以指向 GitCode 私有 SSH 密钥。
|
|
|
|
|
|
|
|
可以运行以下命令进行配置:
|
|
|
|
|
| ... | ... | @@ -216,13 +216,13 @@ eval $(ssh-agent -s) |
|
|
|
ssh-add <path to private SSH key>
|
|
|
|
```
|
|
|
|
|
|
|
|
以上设置将会保存到`~/.ssh/config`文件中。以下是两个专用于 CODE CHINA 的 SSH 密钥示例:
|
|
|
|
以上设置将会保存到`~/.ssh/config`文件中。以下是两个专用于 GitCode 的 SSH 密钥示例:
|
|
|
|
|
|
|
|
```
|
|
|
|
# CODE CHINA
|
|
|
|
Host codechina.csdn.net
|
|
|
|
# GitCode
|
|
|
|
Host gitcode.net
|
|
|
|
Preferredauthentications publickey
|
|
|
|
IdentityFile ~/.ssh/codechina_rsa
|
|
|
|
IdentityFile ~/.ssh/gitcode_rsa
|
|
|
|
|
|
|
|
# Github instance
|
|
|
|
Host github.com
|
| ... | ... | @@ -230,7 +230,7 @@ ssh-add <path to private SSH key> |
|
|
|
IdentityFile ~/.ssh/example_github_rsa
|
|
|
|
```
|
|
|
|
|
|
|
|
公共 SSH 密钥对于 CODE CHINA 必须是唯一的,因为它们将绑定到您的账号中。 SSH 密钥是通过 SSH 推送代码时唯一的标识符,这是为什么它需要唯一地映射到单个用户的原因。
|
|
|
|
公共 SSH 密钥对于 GitCode 必须是唯一的,因为它们将绑定到您的账号中。 SSH 密钥是通过 SSH 推送代码时唯一的标识符,这是为什么它需要唯一地映射到单个用户的原因。
|
|
|
|
|
|
|
|
## 为项目设置 SSH 密钥[](#per-repository-ssh-keys "Permalink")
|
|
|
|
|
| ... | ... | @@ -244,36 +244,36 @@ git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-file |
|
|
|
|
|
|
|
## 多账号设置[](#multiple-accounts-on-a-single-instance "Permalink")
|
|
|
|
|
|
|
|
[为项目设置 SSH 密钥](#per-repository-ssh-keys)方法还适用于在 CODE CHINA 中使用多个账号的情况。
|
|
|
|
[为项目设置 SSH 密钥](#per-repository-ssh-keys)方法还适用于在 GitCode 中使用多个账号的情况。
|
|
|
|
|
|
|
|
此外,您也可以直接在`~.ssh/config`为主机分配别名。 如果在`.ssh/config`中的`Host`块之外设置了`IdentityFile` ,则 SSH 和作为扩展的 Git 将无法登录. 这是 SSH 组装`IdentityFile`条目的方式,因此不能通过将`IdentitiesOnly`设置为`yes`来更改. `IdentityFile`条目应指向 SSH 密钥对的私钥。
|
|
|
|
|
|
|
|
**注意:**私钥和公钥应仅由用户读取,通过运行以下`chmod 0400 ~/.ssh/<example_ssh_key>`在 Linux 和 macOS 上完成此操作: `chmod 0400 ~/.ssh/<example_ssh_key>`和`chmod 0400 ~/.ssh/<example_sh_key.pub>` 。
|
|
|
|
|
|
|
|
```
|
|
|
|
# User1 Account Identity Host <user_1.codechina.csdn.net>
|
|
|
|
Hostname codechina.csdn.net
|
|
|
|
# User1 Account Identity Host <user_1.gitcode.net>
|
|
|
|
Hostname gitcode.net
|
|
|
|
PreferredAuthentications publickey
|
|
|
|
IdentityFile ~/.ssh/<example_ssh_key1>
|
|
|
|
|
|
|
|
# User2 Account Identity Host <user_2.codechina.csdn.net>
|
|
|
|
Hostname codechina.csdn.net
|
|
|
|
# User2 Account Identity Host <user_2.gitcode.net>
|
|
|
|
Hostname gitcode.net
|
|
|
|
PreferredAuthentications publickey
|
|
|
|
IdentityFile ~/.ssh/<example_ssh_key2>
|
|
|
|
```
|
|
|
|
|
|
|
|
**注意:**为提高效率和透明度,示例`Host`别名定义为`user_1.codechina.csdn.net`和`user_2.codechina.csdn.net` 。 高级配置难以维护,使用这种别名在使用其他工具(如`git remote`子命令)时会更容易理解。 SSH 可以将任何字符串理解为`Host`别名,因此`Tanuki1`和`Tanuki2`尽管提供了很少的上下文指向它们,也可以使用.
|
|
|
|
**注意:**为提高效率和透明度,示例`Host`别名定义为`user_1.gitcode.net`和`user_2.gitcode.net` 。 高级配置难以维护,使用这种别名在使用其他工具(如`git remote`子命令)时会更容易理解。 SSH 可以将任何字符串理解为`Host`别名,因此`Tanuki1`和`Tanuki2`尽管提供了很少的上下文指向它们,也可以使用.
|
|
|
|
|
|
|
|
克隆`CODE CHINA`代码仓库通常如下所示:
|
|
|
|
克隆`GitCode`代码仓库通常如下所示:
|
|
|
|
|
|
|
|
```
|
|
|
|
git clone git@gcodechina.csdn.net:repo-org/repo.git
|
|
|
|
git clone git@ggitcode.net:repo-org/repo.git
|
|
|
|
```
|
|
|
|
|
|
|
|
要为`user_1`克隆它,请将`codechina.csdn.net`替换为 SSH 别名`user_1.codechina.csdn.net` :
|
|
|
|
要为`user_1`克隆它,请将`gitcode.net`替换为 SSH 别名`user_1.gitcode.net` :
|
|
|
|
|
|
|
|
```
|
|
|
|
git clone git@<user_1.codechina.csdn.net>:repo-org/repo.git
|
|
|
|
git clone git@<user_1.gitcode.net>:repo-org/repo.git
|
|
|
|
```
|
|
|
|
|
|
|
|
使用`git remote`命令可以修复以前克隆的代码仓库。
|
| ... | ... | @@ -281,7 +281,7 @@ git clone git@<user_1.codechina.csdn.net>:repo-org/repo.git |
|
|
|
以下的示例假定远程代码仓库被别名为`origin`:
|
|
|
|
|
|
|
|
```
|
|
|
|
git remote set-url origin git@<user_1.codechina.csdn.net>:repo-org/repo.git
|
|
|
|
git remote set-url origin git@<user_1.gitcode.net>:repo-org/repo.git
|
|
|
|
```
|
|
|
|
|
|
|
|
### Eclipse[](#eclipse "Permalink")
|
| ... | ... | @@ -301,8 +301,8 @@ git remote set-url origin git@<user_1.codechina.csdn.net>:repo-org/repo.git |
|
|
|
|
|
|
|
## 故障排除[](#troubleshooting "Permalink")
|
|
|
|
|
|
|
|
如果在 Git Clone 上,系统会提示您输入密码,例如`git@codechina.csdn.net's password:`,则表明您的 SSH 设置有问题。
|
|
|
|
如果在 Git Clone 上,系统会提示您输入密码,例如`git@gitcode.net's password:`,则表明您的 SSH 设置有问题。
|
|
|
|
|
|
|
|
* 确保您正确地生成了 SSH 密钥,并将公共 SSH 密钥添加到了您 CODE CHINA 账号的 SSH 密钥中
|
|
|
|
* 确保您正确地生成了 SSH 密钥,并将公共 SSH 密钥添加到了您 GitCode 账号的 SSH 密钥中
|
|
|
|
* 尝试使用`ssh-agent`手动注册您的私有 SSH 密钥,如本文档前面所述
|
|
|
|
* 尝试通过运行`ssh -Tv git@codechina.csdn.net`调试连接 |
|
|
\ No newline at end of file |
|
|
|
* 尝试通过运行`ssh -Tv git@gitcode.net`调试连接 |
|
|
\ No newline at end of file |