Skip to content

Commit 3c57b32

Browse files
authored
Merge pull request #15 from Kotokowa/main
modified: docs/appendix/ssh-server.md with ways for creating multiple choices of ssh key.
2 parents d500ecc + d4bbaa7 commit 3c57b32

File tree

1 file changed

+100
-9
lines changed

1 file changed

+100
-9
lines changed

docs/appendix/ssh-server.md

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ SSH(Secure Shell)是一种加密的网络传输协议,可以在不安全
1818

1919
## 2. 生成 SSH 密钥对
2020

21-
下文将引导大家创建自己的 Ed25519 算法加密的 SSH 密钥对。不过,如果你已经有了 RSA 算法加密的密钥对,不用担心,它也能正常工作。
21+
下面介绍常用的 RSA 算法加密和 Ed25519 算法加密两种 SSH 密钥对,分别命名为 `id_rsa`, `id_rsa.pub``id_ed25519`, `id_ed25519.pub` 文件。当今 SSH 协议规范通常推荐使用 Ed25519 算法加密,不过现阶段我们不需要区分这两种加密具体区别。如果你很好奇这两种加密方式的优劣性,可以参阅[这篇文章](https://nikk.is-a.dev/blog/ed25119_n_rsa/)
2222

2323
### 检查现有密钥
2424

25-
在生成新密钥之前,先检查是否已经存在 SSH 密钥:
25+
在生成新密钥之前,务必先检查是否已经存在 SSH 密钥:
2626

2727
```bash
2828
ls ~/.ssh
@@ -34,14 +34,18 @@ ls ~/.ssh
3434
>
3535
>如果你有 `id_rsa``id_rsa.pub` 文件(由 RSA 算法加密),或 `id_ed25519``id_ed25519.pub` 文件(由 Ed25519 算法加密),说明你已经有 SSH 密钥对了。可以移步到[向他人分享你的公钥](#向他人分享你的公钥)
3636
37-
### 生成新的密钥对
37+
### 生成新的 Ed25519 密钥对
3838

3939
在终端中输入以下命令生成 SSH 密钥对:
4040

4141
```bash
4242
ssh-keygen
4343
```
4444

45+
> [!tip]
46+
>
47+
>在有些网站上,他们会宣称使用上面的方式生成了 RSA 密钥对,这是基于[比较老的 SSH 协议规范](https://www.rfc-editor.org/rfc/rfc4252)。自 [OpenSSH 9.5](https://www.openssh.com/releasenotes.html#9.5)起,使用上述方法生成的密钥对默认是 Ed25519 加密的。
48+
4549
执行上述命令后,系统会提示你进行一系列配置。除非你有配置 passphrase 的需求并且知道自己在做什么,否则连按三个回车即可。
4650

4751
```bash
@@ -60,28 +64,115 @@ ls ~/.ssh
6064

6165
你应该能看到以下文件:
6266

63-
- `id_ed25519`:私钥文件(请妥善保管,不要泄露)
67+
- `id_ed25519`:私钥文件(**请妥善保管,不要泄露**
6468
- `id_ed25519.pub`:公钥文件(可以安全地分享给服务器)
6569

6670
私钥和公钥统称为一组“密钥”或“密钥对”。
6771

6872
### 向他人分享你的公钥
6973

70-
如果你使用 RSA 算法加密的密钥,在终端中运行下面的命令:
74+
在终端中运行下面的命令:
75+
76+
```bash
77+
cat ~/.ssh/id_ed25519.pub
78+
```
79+
80+
观察到输出为 `ssh-ed25519 xxxxxxxxxxxxxxx xxxxx@xxxx`,这就是你的公钥,可以复制并分享给他人。
81+
82+
### 生成新的 RSA 密钥对
83+
84+
在终端中输入以下命令生成 SSH 密钥对:
85+
86+
```bash
87+
ssh-keygen -t rsa
88+
```
89+
90+
执行上述命令后,系统会提示你进行一系列配置。除非你有配置 passphrase 的需求并且知道自己在做什么,否则连按三个回车即可。
91+
92+
```bash
93+
Generating public/private rsa key pair.
94+
Enter file in which to save the key (/home/kano/.ssh/id_rsa):
95+
Enter passphrase (empty for no passphrase):
96+
Enter same passphrase again:
97+
```
98+
99+
### 验证密钥生成
100+
101+
密钥生成完成后,你可以查看生成的文件:
102+
103+
```bash
104+
ls ~/.ssh
105+
```
106+
107+
你应该能看到以下文件:
108+
109+
- `id_rsa`:私钥文件(**请妥善保管,不要泄露**
110+
- `id_rsa.pub`:公钥文件(可以安全地分享给服务器)
111+
112+
私钥和公钥统称为一组“密钥”或“密钥对”。
113+
114+
### 向他人分享你的公钥
115+
116+
在终端中运行下面的命令:
71117

72118
```bash
73119
cat ~/.ssh/id_rsa.pub
74120
```
75121

76-
如果你使用 Ed25519 算法加密的密钥,在终端中运行下面的命令:
122+
观察到输出为 `ssh-rsa xxxxxxxxxxxxxxx xxxxx@xxxx` ,这就是你的公钥,可以复制并分享给他人。
123+
124+
## 3. 重置密钥对
125+
126+
注意,本模块内容仅作为了解,供忘记了密码的同学参考以重置 SSH 密钥对。其他同学不需要执行。另外,如果在此处重置了 SSH 密钥对,你还需要在 GitHub 中重新上传新的 SSH 密钥对:
127+
128+
### 检查现有密钥
129+
130+
在删除密钥之前,务必先检查是否已经存在 SSH 密钥:
77131

78132
```bash
79-
cat ~/.ssh/id_ed25519.pub
133+
ls ~/.ssh
134+
```
135+
136+
如果目录不存在或为空,说明你还没有 SSH 密钥,忽略这个模块吧~
137+
138+
### 删除密钥对
139+
140+
***使用 bash 命令删除文件需要非常小心,我们强烈建议你直接复制以下代码指令**
141+
142+
下面两种删除密钥对方式中只要选择一种操作即可:
143+
144+
- 重置 SSH (这将同时删除已存储的所有服务器信息和受信的公钥,不推荐)
145+
146+
```bash
147+
rm -rf ~/.ssh/*
148+
```
149+
150+
- 删除特定的 SSH 密钥对,使用
151+
152+
```bash
153+
ls ~/.ssh
154+
```
155+
156+
此时终端会显示 `~/.ssh` 目录中存储的文件,例如:
157+
158+
```bash
159+
id_rsa id_rsa.pub id_ed25519 id_ed25519.pub known_hosts known_hosts.old authorized_keys
160+
```
161+
162+
使用 `rm` 操作删除指定的密钥对,例如,删除 Ed25519 加密的密钥对:
163+
164+
```bash
165+
rm ~/.ssh/id_ed25519
166+
rm ~/.ssh/id_ed25519.pub
80167
```
81168

82-
观察到输出为 `ssh-rsa xxxxxxxxxxxxxxx xxxxx@xxxx``ssh-ed25519 xxxxxxxxxxxxxxx xxxxx@xxxx`,这就是你的公钥,可以复制并分享给他人。
169+
如果要删除 RSA 加密的密钥对,将上面的 `ed25519` 改为 `rsa` 即可。
170+
171+
### 生成 SSH 密钥对
172+
173+
参见[上文](#2-生成-ssh-密钥对)
83174

84-
## 3. 使用 VS Code 的 Remote 插件进行远程开发
175+
## 4. 使用 VS Code 的 Remote 插件进行远程开发
85176

86177
注意,下面的操作请在已经分配到服务器之后再执行。
87178

0 commit comments

Comments
 (0)