Skip to content

ssh remote workplace #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions 05-tasks/5.2.1-ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ root@<your-workspace-name>:~#
> - 工作台启动的时候会挂载当时已经有的公钥, 之后添加删除都不会影响已经启动的工作台
> - 如果不使用默认密钥, 登录时需要指定私钥路径: `ssh [email protected] -p <port> -i </path/to/your/private/key>`

## SSH Tips

### 使用配置文件
## SSH 配置

可以通过本地 SSH 配置文件 (默认在 ~/.ssh/config, 没有则新建) 简化登录过程, 并配置不同服务使用不同密钥:

Expand All @@ -74,11 +72,71 @@ Host * # 参考默认配置

之后即可执行 `ssh workspace` 快速登录.

### PyCharm 远程调试
## SSH 远程开发调试

PyCharm 专业版可以通过 SSH 使用服务端的 Python 环境进行远程调试, 避免开发与运行环境不一致, 且可以直接调试 GPU
代码. 请参阅 PyCharm 用户文档[配置 远程 Python 解释器](https://www.jetbrains.com/help/pycharm/configuring-remote-interpreters-via-ssh.html)并[部署本地代码至服务端](https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html).
通过 SSH 连接服务器后, 即可通过多种方法使用本地开发工具开发远程服务器上的项目.

> **[warning] Warning:**
>
> 当前 PyCharm 远程调试只能进行 Debug, 不能 Run
### SSHFS

使用 SSHFS 可以将远程服务器上的文件系统挂载至本地, 即可使用任意本地开发工具编辑远程服务器上的项目. Mac 用户可按以下步骤快速配置 SSHFS:

1. 前往 https://osxfuse.github.io/ 下载安装 FUSE for macOS 和 SSFFS

2. 创建一个新目录用以挂载远程项目:
```sh
$ mkdir -p ~/remote-workspace
```

3. 挂载远程目录:
```sh
$ sudo sshfs -o allow_other,defer_permissions \
-p <port> -F <path/to/ssh/config> \
root@workspace:<remote/path> <local/mount/point>
```
其中, `<port>` 为工作台 SSH 端口; `<path/to/ssh/config>` 为按上一章节骤配置的 SSH 配置文件, 如 `~/.ssh/config`, 如未配置该文件无需此参数; `<remote/path>` 为欲挂载的远程文件目录, 如 `/workspace/mnt/group/<your-group>/<your-name>/`; `<local/mount/point>` 为上一步创建的空目录

4. 使用任意编辑器打开上述目录, 进行开发
```sh
$ code <local/mount/point> # 以 VSCode 为例
```

5. 卸载远程目录:
```sh
$ sudo umount <local/mount/point>
```

SSHFS 详细配置方法请参阅[文档](https://www.digitalocean.com/community/tutorials/how-to-use-sshfs-to-mount-remote-file-systems-over-ssh).

### PyCharm

使用 PyCharm 专业版 (>=2018.3) 可以通过 SSH 将本地项目同步至服务器端, 并使用服务端的 Python 环境进行远程调试, 可以避免开发与运行环境不一致的问题, 还能直接使用 GPU 进行调试开发. 详细方法请参阅 PyCharm 用户文档[配置远程 Python 解释器](https://www.jetbrains.com/help/pycharm/configuring-remote-interpreters-via-ssh.html), 并[部署本地代码至服务端](https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html).

### VSCode

Visual Studio Code 用户可以安装 [Remote Workspace](https://marketplace.visualstudio.com/items?itemName=mkloubert.vscode-remote-workspace) 插件, 通过 SFTP 访问远程项目文件, 使用本地 VSCode 进行开发. 大致步骤如下, 详情参考插件主页.

1. 本地新建文件 `sftp_params.json`, 内容如下:
```json
{
"key": "<ssh-private-key-name>"
}
```
其中, `<ssh-private-key-name>` 为 ssh 私钥名称, 此私钥需位于 `~/.ssh/` 目录下.

2. 本地新建文件 `remote.code-workspace`, 内容如下:
```json
{
"folders": [
{
"uri": "sftp://[email protected]:<port><remote/path>?params=<path/to/sftp_params.json>",
"name": "<local-directory>"
}
]
}
```
其中, `<port>` 为工作台 ssh 端口; `<remote/path>` 为待挂载远程目录; `<path/to/sftp_params.json>` 为第一步所创建文件的完整目录; `<local-directory>` 为挂载至本地的目录名称.

3. 用 VSCode 打开 `remote.code-workspace`:
```sh
$ code remote.code-workspace
```