跳到主要内容

Linux 远程控制

Linux 远程控制

直接使用命令行

SSH

安装

虽然完整版 Ubuntu 好像默认安装了 SSH 服务,但是还是有必要重新装一下:

$ sudo apt install openssh-server
关于apt-getaptaptitude的区别

知乎 - apt-getaptaptitude的区别在哪?:能用apt就用apt,搜索包用aptitude

安装好了,SSH 服务就自动开始跑了。

通过下面这条指令查看 SSH 服务是否正在运行:

$ systemctl status ssh

如果输出形如这样则说明 SSH 服务正常启动了:

shiercoding@shiercoding:~$ systemctl status ssh
 ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-02-11 11:38:15 CST; 1 week 0 days ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: ... (sshd)
      Tasks: 1 (limit: 18869)
     Memory: ...M
        CPU: ...ms
     CGroup: /system.slice/ssh.service
             └─1047 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
...

使用

装有 Linux 系统的设备开启 SSH 服务后,他就会成为一台服务器,我们可以通过其他设备去连接这台服务器。

连接之前首先要找出服务器的 IP(内网)地址:知乎 - 如何在 Ubuntu 中检查你的 IP 地址

然后在你的(可以是Windows设备上)打开命令行,输入:

$ ssh [目标主机用户名]@[目标主机IP地址]

比如,我在Windows上要链接用户名为shiercoding的SSH服务器,该服务器内网IP地址为10.0.0.114,则可以通过如下指令:

$ ssh shiercoding@10.0.0.114

一般就能连接上了,输入目标主机的密码,然后当成在目标主机里用Bash命令行即可。

Tmux

阮一峰的网络日志 - Tmux 使用教程:阮一峰YYDS!!!Tmux看这一篇就够了,只需要了解一些快捷键和创建、接入会话的方法即可。

安装建议使用这条命令:

$ sudo apt install tmux

OpenVSCode Server


VSCode

OpenVSCode Server 是一个把宇宙第一的编辑器 VSCode 搬到了浏览器里的项目。

VSCode 本身是基于一个叫 Electron 的项目开发的,Electron 本质上又是一个 Chrome 内核的类似浏览器的东西,所以移植是可能的。

项目地址:Github - openvscode-server

是的……这玩意暂时只支持 Linux 系统……不过不要紧,我们的目的就是在不太好编辑代码的 Linux 字符界面下写代码。

它具体的原理大概是在被远程操控的电脑上运行一个 VSCode 内核,这个内核会开启一个 Web Service,将被远程操控的电脑当成一台服务器。通过其他电脑访问这个服务器开启的端口即可登录 OpenVSCode Server,并在浏览器里码代码。

下载

Github - openvscode-server/releases

根据你的性格选择是用Pre-release还是Latest版本(一般Pre-release可能会有点 Bug,但是会相比前一个版本增加很多新功能,Latest是经过公测后的稳定版)。

然后根据被控制的电脑架构选择版本(一般为x64)。

下载完成后解压缩即可:知乎 - Ubuntu压缩及解压文件简介

准备

解压缩后文件目录树大致为这样:

.
├── bin
│ ├── helpers
│ │ └── browser.sh
│ ├── openvscode-server
│ └── remote-cli
│ └── openvscode-server
├── extensions
│ └──...
├── node
├── node_modules
│ └──...
├── out
│ └──...
├── package.json
├── product.json
└── resources
└── server
├── code-192.png
├── code-512.png
├── favicon.ico
└── manifest.json

上图标蓝的即为我们需要的可执行文件,其余文件都基本为依赖。

cdbin目录查看openvscode-server文件的用法

shiercoding@shiercoding:~/openvscode$ cd bin
shiercoding@shiercoding:~/openvscode/bin$ ./openvscode-server -h
OpenVSCode Server 1.74.3

Usage: openvscode-server [options]

Options
  --host <ip-address>            The host name or IP address the server should listen to. If not set, defaults to 'localhost'.
  --port <port | port range>     The port the server should listen to. If 0 is passed a random free port is picked. If a range in the format num-num is passed, a free port from the range
                                 (end inclusive) is selected.
  --socket-path <path>           The path to a socket file for the server to listen to.
  --connection-token <token>     A secret that must be included with all requests.
  --connection-token-file <path> Path to a file that contains the connection token.
  --without-connection-token     Run without a connection token. Only use this if the connection is secured by other means.
  --accept-server-license-terms  If set, the user accepts the server license terms and the server will be started without a user prompt.
  --server-data-dir              Specifies the directory that server data is kept in.
  --telemetry-level <level>      Sets the initial telemetry level. Valid levels are: 'off', 'crash', 'error' and 'all'. If not specified, the server will send telemetry until a client
                                 connects, it will then use the clients telemetry setting. Setting this to 'off' is equivalent to --disable-telemetry
  --user-data-dir <dir>          Specifies the directory that user data is kept in. Can be used to open multiple distinct instances of Code.
  -h --help                      Print usage.

Troubleshooting
  --log <level> Log level to use. Default is 'info'. Allowed values are 'critical', 'error', 'warn', 'info', 'debug', 'trace', 'off'. You can also configure the log level of an extension
                by passing extension id and log level in the following format: '${publisher}.${name}:${logLevel}'. For example: 'vscode.csharp:trace'. Can receive one or more such
                entries.
  -v --version  Print version.

Extensions Management
  --extensions-dir <dir>              Set the root path for extensions.
  --install-extension <ext-id | path> Installs or updates an extension. The argument is either an extension id or a path to a VSIX. The identifier of an extension is
                                      '${publisher}.${name}'. Use '--force' argument to update to latest version. To install a specific version provide '@${version}'. For example:
                                      'vscode.csharp@1.2.3'.
  --uninstall-extension <ext-id>      Uninstalls an extension.
  --list-extensions                   List the installed extensions.
  --show-versions                     Show versions of installed extensions, when using --list-extensions.
  --category <category>               Filters installed extensions by provided category, when using --list-extensions.
  --pre-release                       Installs the pre-release version of the extension, when using --install-extension
  --start-server                      Start the server when installing or uninstalling extensions. To be used in combination with 'install-extension', 'install-builtin-extension' and
                                      'uninstall-extension'.

在使用前我推荐先安装几个拓展(OpenVSCode Server 提供的拓展不全,需要用命令安装,其它常用的拓展可以直接在拓展商店里搜索到):

$ ./openvscode-server --install-extension formulahendry.code-runner  # Code Runner

使用

如果直接运行:

$ ./openvscode-server

你会得到类似这样的输出:

shiercoding@shiercoding:~/openvscode/bin$ ./openvscode-server
Server bound to 127.0.0.1:3000 (IPv4)
Extension host agent listening on 3000

[09:59:15]

Web UI available at http://localhost:3000/?tkn=11451419-5760-4479-8568-114514191981
[09:59:15] Extension host agent started.
...

然后你只能在目标主机上的浏览器访问 OpenVSCode Server,这显然不是我们想要的,所以要开启私服:

$ ./openvscode-server --host 0.0.0.0

这时候你就可以在同一局域网的任意设备去访问 OpenVScode Server 了,但是访问的链接有一大串token要输入,这显然不是我们想要的,所以要自定义token:

$ ./openvscode-server --host 0.0.0.0 --connection-token [你的token,随便输一个好敲的字符串即可]

然后在你的电脑上访问这个网址即可:http://[目标主机IP地址]:[服务所在的端口,默认为3000]/?tkn=[你的token]

经过一些时间的熟悉和调整,整体使用起来和宇宙第一 VSCode 的体验几乎一样一样了。

使用建议

  1. OpenVSCode Server 目前不支持中文:Github - Some extensions cannot be installed and used #313
  2. 可以开个 Tmux,在 Tmux 的一个会话中运行 OpenVSCode Server 的服务进程。
  3. OpenVSCode Server 看起来暂时不会开发原生 Windows 版:Github - Build for windows #483
  4. OpenVSCode Server 目前可能无法查看图片,如果要查看可以右键下载图片然后在本地查看。
  5. OpenVSCode Server 目前可能无法打开 Jupyter NoteBook,如果需要查看可以开个 Jupyter Lab

Jupyter Lab


Jupyter

知乎 - 教你如何在Linux上搭建Jupyter Lab:写的很详细,我就不多赘述了。

知乎 - Jupyter Lab对比Jupyter Notebook有什么优点和不足?:一些使用技巧。

微信公众号 - 熬了半宿!梳理了 JupyterLab 上 20 个超级实用的插件!:标题党,但是插件都还是很实用的。