This repository was archived by the owner on Apr 7, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -64,6 +64,7 @@ function sidebarGuide(): DefaultTheme.Sidebar {
6464 text : '附录' ,
6565 items : [
6666 { text : '提问的艺术' , link : '/appendix/how-to-ask' } ,
67+ { text : '使用 SSH 连接服务器' , link : '/appendix/ssh-server' } ,
6768 ]
6869 } ,
6970 {
Original file line number Diff line number Diff line change 1+ # 使用 SSH 连接服务器
2+
3+ 本文档将帮助使用 macOS 系统的同学完成 SSH 密钥对的生成与配置,并连接到本课程提供的服务器环境进行实验。
4+
5+ ## 1. SSH 简介
6+
7+ SSH(Secure Shell)是一种加密的网络传输协议,可以在不安全的网络中为网络服务提供安全的传输环境。通过 SSH,您可以安全地连接到远程服务器并执行命令。
8+
9+ > [ !tip]
10+ >
11+ > ** 为什么需要 SSH 密钥?**
12+ >
13+ > - ** 安全性** :相比密码登录,SSH 密钥更加安全
14+ > - ** 便利性** :配置完成后无需每次输入密码
15+ > - ** 自动化** :支持脚本自动化操作
16+
17+ 如果你好奇密钥是如何工作的,可以参考[ 这篇文章] ( https://www.ruanyifeng.com/blog/2011/08/what_is_a_digital_signature.html ) 。
18+
19+ ## 2. 生成 SSH 密钥对
20+
21+ ### 检查现有密钥
22+
23+ 在生成新密钥之前,先检查是否已经存在 SSH 密钥:
24+
25+ ``` bash
26+ # 在终端输入以下命令
27+ ls -la ~ /.ssh
28+ ```
29+
30+ 如果目录不存在或为空,说明您还没有 SSH 密钥,可以继续下面的步骤。
31+
32+ > [ !tip]
33+ >
34+ > 如果您有 ` id_rsa ` 和 ` id_rsa.pub ` 文件,说明您已经有 SSH 密钥对了。可以移步到配置服务器连接。
35+
36+ ### 生成新的密钥对
37+
38+ 在终端中输入以下命令生成 SSH 密钥对:
39+
40+ ``` bash
41+ # 在终端输入,替换自己的邮箱地址
42+ ssh-keygen -t rsa -b 4096 -C " your_email@example.com"
43+ ```
44+
45+ 执行上述命令后,系统会提示您进行一系列配置:
46+
47+ ``` bash
48+ # 连按三个回车即可
49+ Enter file in which to save the key (/Users/yourusername/.ssh/id_rsa):
50+ Enter passphrase (empty for no passphrase):
51+ Enter same passphrase again:
52+ ```
53+
54+ ### 验证密钥生成
55+
56+ 密钥生成完成后,您可以查看生成的文件:
57+
58+ ``` bash
59+ ls -la ~ /.ssh
60+ ```
61+
62+ 您应该能看到以下文件:
63+
64+ - ` id_rsa ` :私钥文件(请妥善保管,不要泄露)
65+ - ` id_rsa.pub ` :公钥文件(可以安全地分享给服务器)
66+
67+ 查看公钥内容:
68+
69+ ``` bash
70+ cat ~ /.ssh/id_rsa.pub
71+ ```
72+
73+ 观察到输出为:` ssh-rsa xxxxxxxxxxxxxxx = xxxxx@xxxx.xx ` 即正确
74+
75+ ## 3. 使用 VS Code 的 Remote 插件进行远程开发
76+
77+ ### 安装 VS Code 插件
78+
79+ ![ remote_explorer] ( remote_explorer.png )
80+
81+ ### 配置文件
82+
83+ - 在左侧找到形如电脑的图标
84+ - 点击SSH旁边的齿轮
85+ - 进入默认配置文件
86+
87+ ![ ssh_config] ( ssh_config2.png )
88+
89+ ### 连接服务器
90+
91+ - 点击SSH旁边的加号,新建远程
92+ - 在图中所示方框中输入以下命令连接到服务器:
93+
94+ ``` bash
95+ ssh root@服务器地址 -p 端口号
96+ ```
97+
98+ ![ ssh_server] ( ssh_server2.png )
99+
100+ 首次连接时,系统会显示服务器的指纹信息并询问是否信任:
101+
102+ ``` bash
103+ The authenticity of host ' xxxxxxx' can' t be established.
104+ ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
105+ Are you sure you want to continue connecting (yes/no/[fingerprint])?
106+ ```
107+
108+ 输入 `yes` 并按回车键继续。
109+
110+ 如果配置正确,您应该能够成功登录到服务器而无需输入密码。
Original file line number Diff line number Diff line change @@ -190,17 +190,17 @@ Git 的常用操作可以通过 [Git 学习网站](https://learngitbranching.js.
190190
191191### SSH
192192
193- 在 ICS 课程中没有直接使用 SSH 连接服务器的内容,但你可能需要 [ 使用 SSH 连接 GitHub ] ( https://docs.github.com/cn/authentication/connecting-to-github-with- ssh/ ) 。SSH 的完整文档可以通过 man ssh 查阅 。
193+ 本课程中,使用 Mac 的同学需要使用 SSH 连接服务器,请参考 [ 这个文档 ] ( /appendix/ ssh-server ) 。
194194
195- 如果你从未使用过 SSH,你可能需要为自己创建一个密钥对,参考指令: ` ssh-keygen -t ed25519 -C "your_email@example.com" ` 。密钥对的默认保存位置为 ` ~/.ssh/ ` ,其中的 ` id_rsa ` 为私钥, ` id_rsa.pub ` 为公钥。如果你好奇密钥是如何工作的,可以参考 [ 这篇文章 ] ( https://www.ruanyifeng .com/blog/2011/08/what_is_a_digital_signature.html ) 。
195+ 你可能还需要 [ 使用 SSH 连接 GitHub ] ( https://docs.github .com/cn/authentication/connecting-to-github-with-ssh/ ) 。SSH 的完整文档可以通过 man ssh 查阅 。
196196
197197### Vim
198198
199199Vim 的基本用法为运行 ` vim something.txt ` 打开文件,然后按 ` i ` 键编辑文件,此时可以直接打字,完成后按 ` ESC ` 键,输入 ` :wq ` 保存并退出,或输入 ` :q! ` 不保存直接退出。
200200
201201由于 Vim 有一些学习难度,大家当然可以直接选择开箱即用的 VS Code ,但是如果你习惯了 Vim 的操作,用起来就非常爽快。
202202
203- Vim 的用法非常丰富,如有兴趣可以通过 Vim 自带的教程程序 ` vimtutor ` 学习(在安装 vim 之后直接在命令行输入这个即可),也可以通过 [ Vim 学习网站] ( https://vim-adventures.com/ ) 学习,或者参考[ 这个保姆级入门视频] ( https://www.bilibili.com/video/BV13t4y1t7Wg ) 。
203+ Vim 的用法非常丰富,如有兴趣可以通过 Vim 自带的教程程序 ` vimtutor ` 学习(在安装 Vim 之后直接在命令行输入这个即可),也可以通过 [ Vim 学习网站] ( https://vim-adventures.com/ ) 学习,或者参考[ 这个保姆级入门视频] ( https://www.bilibili.com/video/BV13t4y1t7Wg ) 。
204204
205205> Copilot 官方提供了一个 [ Vim 插件] ( https://github.com/github/copilot.vim ) ,可以让你在 Vim 中使用 Copilot。
206206
You can’t perform that action at this time.
0 commit comments