> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 通过 SSH 使用 Visual Studio Code 进行开发

本指南将引导你如何从本地计算机通过 SSH 使用 Visual Studio Code(VS Code)连接你的开发板(例如运行 Ubuntu 的 RB3 Gen-2)。

### 前提条件

* 开发板已安装最新的 Ubuntu 镜像:
  * [Qualcomm Dragonwing RB3 Gen 2 Vision Kit](/zh/devices/rb3-gen2-vision-kit)
  * [IQ-9075 EVK](/zh/devices/iq9075-evk/setup)
* 开发板已连接互联网。

### 步骤 1 — 启用 SSH / 查找 IP 地址

1. 使用串口连接设备,参见 [Qualcomm 文档:在设备上配置 Ubuntu](https://docs.qualcomm.com/bundle/publicresource/topics/80-90441-1/Use_Ubuntu_on_RB3_Gen2_3.html)(该文档面向 RB3,但同样适用于 Rubik Pi 3)。
2. 通过控制台登录,默认用户名为 `ubuntu`,默认密码也是 `ubuntu`。
3. 确保网络已设置好(以太网或 Wi-Fi)。
4. 查找开发板的 IP 地址。例如通过:

   ```
   sudo apt update && sudo apt install -y net-tools
   ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
   # 192.168.1.253
   ```

### 步骤 2 — 在 VS Code 中安装/启用 Remote ‑ SSH 扩展

在你的**本地**计算机上:

1. 打开 VS Code。
2. 进入 Extensions(扩展)。
3. 搜索 **Remote ‑ SSH**(由 Microsoft 提供)并安装。

### 步骤 3 — 在 VS Code 中将开发板添加为 SSH 主机

1. 在 VS Code 中打开**命令面板**(F1,macOS 上为 Command+Shift+P,Windows/Linux 上为 Ctrl+Shift+P)。

2. 运行 `Remote‑SSH: Add New SSH Host...`

3. 输入 SSH 连接字符串,例如:

   ```
   ubuntu@board-ip-address
   ```

   \*可选:\*如果你使用密钥,可以包含身份文件:

   ```
   ssh -i ~/.ssh/id_rsa_board user@board-ip-address
   ```

4. 选择要使用的 SSH 配置文件(通常是 `~/.ssh/config`)。VS Code 会为你的开发板插入一个条目。

   配置示例片段:

   ```
   Host my-board
       HostName 192.168.1.100
       User ubuntu
       IdentityFile ~/.ssh/id_rsa_board
   ```

### 步骤 4 — 通过 VS Code 连接到开发板

1. 在 VS Code 中,再次打开命令面板 → `Remote‑SSH: Connect to Host...`
2. 选择你的主机(例如 `ubuntu@board-ip-address` 或你在 SSH 配置中指定的名称)。
3. 如出现提示,选择远程主机的平台。
4. 稍等片刻后,VS Code 会自动将 **VS Code Server** 安装到开发板上。状态会显示在底部状态栏中。
5. 连接成功后,你可以使用 **File → Open Folder...** 浏览开发板上的文件,打开终端(这些终端将在开发板上运行),并根据需要在远程主机上安装扩展。

### 故障排除

* \*\*VS Code Server 占用过多 RAM:\*\*在远程主机上运行大量 VS Code 扩展可能消耗大量内存,导致内存不足(OOM)错误和随机网络断连
  * \*\*解决方案:\*\*在远程主机上禁用或有选择地启用扩展,以降低内存占用

* \*\*SSH 连接失败:\*\*开发板不可达 / IP 错误 / SSH 未运行
  * \*\*解决方案:\*\*检查网络,从终端运行 `ssh ubuntu@IP`,并[通过串口连接](https://docs.qualcomm.com/bundle/publicresource/topics/80-90441-1/Use_Ubuntu_on_RB3_Gen2_3.html)验证开发板的网络工具已更新且工作正常,例如

    ```
    sudo apt update && sudo apt install -y net-tools
    ifconfig
    ```

* \*\*主机平台检测不正确:\*\*VS Code 猜测错误(例如 ARM 与 x86)
  * \*\*解决方案:\*\*在 VS Code 设置中手动设置 `remote.SSH.remotePlatform`

### 可选:使用基于 SSH 密钥的身份验证

为避免输入密码:

1. 在本地计算机上,如果还没有 SSH 密钥对,先生成一个:

   ```
   ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_board
   ```

2. 将公钥添加到你的开发板:

   ```
   ssh-copy-id -i ~/.ssh/id_rsa_board.pub user@board-ip-address
   ```

3. 确认可以*无需*密码即可通过 SSH 登录。

4. 更新你的 VS Code SSH 配置以引用此密钥(如步骤 3 所述)。
