Linux下WEB终端工具:ttyd

ttyd 一款开源的轻量级web terminal工具,通过Web共享您的终端

安装:

1
2
wget https://github.com/tsl0922/ttyd/releases/download/1.7.2/ttyd.x86_64
chmod +x ttyd.x86_64

启动:

1
2
直接启动:./ttyd.x86_64 -p 8080 bash
带账户密码启动: ./ttyd.x86_64 -p 8080 -c root:123456 bash

设置服务开机自启:(适用于CentOS7)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
echo '
[UNIT]
#服务描述
Description=Media wanager Service
#指定了在systemd在执行完那些target之后再启动该服务
After=network.target

[Service]
#定义Service的运行类型
Type=simple

#程序工作目录
WorkingDirectory=/root/
#PID文件
PIDFile=/run/test.pid
#指定加载一个包含服务所需的环境变量的列表的文件,文件中的每一行都是一个环境变量的定义。
EnvironmentFile=-/etc/test

#定义systemctl start|stop|reload *.service 的执行方法(具体命令需要写绝对路径)
#注:ExecStartPre为启动前执行的命令
ExecStartPre=/usr/bin/test "x${NETWORKMANAGER}" = xyes
ExecStart=/root/ttyd.x86_64 -p 8080 -c root:123456 bash
ExecReload=
ExecStop=

#创建私有的内存临时空间
PrivateTmp=True
#最大文件打开数
LimitNOFILE=1048576

[Install]
#多用户
WantedBy=multi-user.target '> /usr/lib/systemd/system/tty.service
systemctl daemon-reload
systemctl start tty
systemctl enable tty

选项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
OPTIONS:
-p, --port Port to listen (default: 7681, use `0` for random port)
-i, --interface Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)
-c, --credential Credential for Basic Authentication (format: username:password)
-u, --uid User id to run with
-g, --gid Group id to run with
-s, --signal Signal to send to the command when exit it (default: 1, SIGHUP)
-a, --url-arg Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
-R, --readonly Do not allow clients to write to the TTY
-t, --client-option Send option to client (format: key=value), repeat to add more options
-T, --terminal-type Terminal type to report, default: xterm-256color
-O, --check-origin Do not allow websocket connection from different origin
-m, --max-clients Maximum clients to support (default: 0, no limit)
-o, --once Accept only one client and exit on disconnection
-B, --browser Open terminal with the default system browser
-I, --index Custom index.html path
-b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here, max length: 128)
-P, --ping-interval Websocket ping interval(sec) (default: 300)
-6, --ipv6 Enable IPv6 support
-S, --ssl Enable SSL
-C, --ssl-cert SSL certificate file path
-K, --ssl-key SSL key file path
-A, --ssl-ca SSL CA file path for client certificate verification
-d, --debug Set log level (default: 7)
-v, --version Print the version and exit
-h, --help Print this text and exit

注意:请修改ExecStart后面的ttyd.x86_64路径。脚本默认存在/root/ 且保持8080端口未被占用。

服务端单向认证(请自备证书):

1
./ttyd --ssl --ssl-cert server.crt --ssl-key server.key bash

如需 Nginx反向代理,请配置增加如下 ws 参数:

1
2
3
4
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_read_timeout 3600

最新版下载:

1
https://github.com/tsl0922/ttyd/releases

🔰本文标题: Linux下WEB终端工具:ttyd

🔞本文链接: https://193.gs/ttyd/index.html

🌡️本文总热度