使用 ttyd 可以用网页共享任意进程,共享 bash 就可以在网页上运行终端(不依赖 ssh 服务);
ttyd 的编译
编译过程中生成的 compile_commands.json 方便我们配置 VSCode 去阅读源码;
1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装依赖
sudo apt install git
sudo apt install build-essential cmake
sudo apt install libjson-c-dev libwebsockets-dev
# 下载 ttyd 源码
git clone https://github.com/tsl0922/ttyd.git
cd ttyd
# 编译安装 ttyd
mkdir build && cd build
cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=1 && make
sudo make install
共享终端
共享主机 bash
1
2
# port 指定端口,cwd 指定工作目录
ttyd --port 8080 --cwd / bash
浏览器访问 <主机 IP>:8080
就可以打开共享的 bash;
我这里是 wsl 中运行 ttyd 的,通过 localhost 可以访问 wsl 中的服务,所以输入的是 localhost:8080;
其他参数:
1
2
3
4
5
6
7
8
9
10
11
# 设置网页访问用户名/密码(basic authentication)
ttyd -c username:password bash
# 指定最大客户端数量
ttyd --max-clients 5 bash
# 指定用户 id 和群组 id
ttyd --uid 1000 --gid 1000 bash
# 特别的:让使用者使用 linux 系统的账号密码登录
sudo ttyd --port 8080 --cwd / login
共享容器 bash
1
ttyd --port 8080 docker run -it --rm ubuntu:latest bash
这样,浏览器访问 <主机 IP>:8080
就可以使用临时 linux 环境了;