Skip to content

Commit 28f269a

Browse files
author
Hou Junjie
committed
ssh remote workplace
1 parent f37eba9 commit 28f269a

File tree

1 file changed

+67
-9
lines changed

1 file changed

+67
-9
lines changed

05-tasks/5.2.1-ssh.md

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ root@<your-workspace-name>:~#
4949
> - 工作台启动的时候会挂载当时已经有的公钥, 之后添加删除都不会影响已经启动的工作台
5050
> - 如果不使用默认密钥, 登录时需要指定私钥路径: `ssh [email protected] -p <port> -i </path/to/your/private/key>`
5151
52-
## SSH Tips
53-
54-
### 使用配置文件
52+
## SSH 配置
5553
5654
可以通过本地 SSH 配置文件 (默认在 ~/.ssh/config, 没有则新建) 简化登录过程, 并配置不同服务使用不同密钥:
5755
@@ -74,11 +72,71 @@ Host * # 参考默认配置
7472
7573
之后即可执行 `ssh workspace` 快速登录.
7674
77-
### PyCharm 远程调试
75+
## SSH 远程开发调试
7876
79-
PyCharm 专业版可以通过 SSH 使用服务端的 Python 环境进行远程调试, 避免开发与运行环境不一致, 且可以直接调试 GPU
80-
代码. 请参阅 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).
77+
通过 SSH 连接服务器后, 即可通过多种方法使用本地开发工具开发远程服务器上的项目.
8178
82-
> **[warning] Warning:**
83-
>
84-
> 当前 PyCharm 远程调试只能进行 Debug, 不能 Run
79+
### SSHFS
80+
81+
使用 SSHFS 可以将远程服务器上的文件系统挂载至本地, 即可使用任意本地开发工具编辑远程服务器上的项目. Mac 用户可按以下步骤快速配置 SSHFS:
82+
83+
1. 前往 https://osxfuse.github.io/ 下载安装 FUSE for macOS 和 SSFFS
84+
85+
2. 创建一个新目录用以挂载远程项目:
86+
```sh
87+
$ mkdir -p ~/remote-workspace
88+
```
89+
90+
3. 挂载远程目录:
91+
```sh
92+
$ sudo sshfs -o allow_other,defer_permissions \
93+
-p <port> -F <path/to/ssh/config> \
94+
root@workspace:<remote/path> <local/mount/point>
95+
```
96+
其中, `<port>` 为工作台 SSH 端口; `<path/to/ssh/config>` 为按上一章节骤配置的 SSH 配置文件, 如 `~/.ssh/config`, 如未配置该文件无需此参数; `<remote/path>` 为欲挂载的远程文件目录, 如 `/workspace/mnt/group/<your-group>/<your-name>/`; `<local/mount/point>` 为上一步创建的空目录
97+
98+
4. 使用任意编辑器打开上述目录, 进行开发
99+
```sh
100+
$ code <local/mount/point> # 以 VSCode 为例
101+
```
102+
103+
5. 卸载远程目录:
104+
```sh
105+
$ sudo umount <local/mount/point>
106+
```
107+
108+
SSHFS 详细配置方法请参阅[文档](https://www.digitalocean.com/community/tutorials/how-to-use-sshfs-to-mount-remote-file-systems-over-ssh).
109+
110+
### PyCharm
111+
112+
使用 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).
113+
114+
### VSCode
115+
116+
Visual Studio Code 用户可以安装 [Remote Workspace](https://marketplace.visualstudio.com/items?itemName=mkloubert.vscode-remote-workspace) 插件, 通过 SFTP 访问远程项目文件, 使用本地 VSCode 进行开发. 大致步骤如下, 详情参考插件主页.
117+
118+
1. 本地新建文件 `sftp_params.json`, 内容如下:
119+
```json
120+
{
121+
"key": "<ssh-private-key-name>"
122+
}
123+
```
124+
其中, `<ssh-private-key-name>` 为 ssh 私钥名称, 此私钥需位于 `~/.ssh/` 目录下.
125+
126+
2. 本地新建文件 `remote.code-workspace`, 内容如下:
127+
```json
128+
{
129+
"folders": [
130+
{
131+
"uri": "sftp://[email protected]:<port><remote/path>?params=<path/to/sftp_params.json>",
132+
"name": "<local-directory>"
133+
}
134+
]
135+
}
136+
```
137+
其中, `<port>` 为工作台 ssh 端口; `<remote/plath>` 为待挂载远程目录; `<path/to/sftp_params.json>` 为第一步所创建文件的完整目录; `<local-directory>` 为挂载至本地的目录名称.
138+
139+
3. 用 VSCode 打开 `remote.code-workspace`:
140+
```sh
141+
$ code remote.code-workspace
142+
```

0 commit comments

Comments
 (0)