CentOS 7使用ttyd搭建一个WEB共享终端(WebSSH) 您所在的位置:网站首页 docker安装webssh CentOS 7使用ttyd搭建一个WEB共享终端(WebSSH)

CentOS 7使用ttyd搭建一个WEB共享终端(WebSSH)

2023-10-01 22:30| 来源: 网络整理| 查看: 265

ttyd 是一个简单的命令行工具,用于在 Web 上共享终端,简单点说就是可以实现在网页上使用SSH终端服务,并且该软件是免费开源的。

安装ttyd

ttyd作者已经提供编译好的二进制文件,直接下载即可使用,最新版下载地址为:https://github.com/tsl0922/ttyd/releases,这里以CentOS 7为例:

#下载ttyd wget -O ttyd https://github.com/tsl0922/ttyd/releases/download/1.6.0/ttyd_linux.x86_64 #添加执行权限 chmod +x ttyd #移动目录 mv ttyd /usr/sbin

通过上面的几个步骤,我们已经完成ttyd安装,输入命令ttyd -v可查看当前版本:

[root@hosta29d0ffef5 ~]# ttyd -v ttyd version 1.6.0-c15cfb7运行ttyd

输入命令ttyd bash运行ttyd,注意防火墙需要放行7681端口,然后浏览器访问http://IP:7681即可打开WEB终端,如下图。

不过ttyd并没有保持后台运行,访问7681也不需要任何密码验证,非常不安全,接下来我们为ttyd创建一个systemd服务并设置用户名、密码验证。

新建服务

创建一个ttyd.service文件:vi /etc/systemd/system/ttyd.service内容如下:

[Unit] Description=ttyd After=network.target [Service] ExecStart=/usr/sbin/ttyd -c xiaoz:xiaoz.me bash [Install] WantedBy=multi-user.target

创建完毕后输入命令:systemctl daemon-reload让daemon生效。

上面使用了-c参数,这个参数的含义是设置用户名、密码验证,格式为-c 用户名:密码,上方设置的用户名为xiaoz,密码为xiaoz.me,请自行修改为自己的用户名、密码。

服务创建后,我们可以使用systemd命令来进行管理了,命令如下:

#启动ttyd systemctl start ttyd #停止ttyd systemctl stop ttyd #重启ttyd systemctl restart ttyd #开机启动 systemctl enable ttydNginx反向代理

如果您不喜欢通过IP + 端口的访问形式,也可以设置Nginx反向代理通过域名访问,配置如下:

如果是网站根目录

location / { proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://127.0.0.1:7681; }

如果是网站二级目录

location ~ ^/ttyd(.*)$ { proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass http://127.0.0.1:7681/$1; }

注意上面的ttyd可以修改为自己想要的路径。

ttyd参数说明

输入ttyd -h可以查看ttyd帮助,说明如下:

USAGE: ttyd [options] [] VERSION: 1.6.0 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) -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 Visit https://github.com/tsl0922/ttyd to get more information and report bugs.总结

使用ttyd可以很方便快速的搭建一个WebSSH服务,但便利就意味着要承担更多的安全风险,虽然ttyd提供了基本的密码验证,但这种验证方式仍然不安全,使用ttyd的同时意味着你的服务器也多了一个入口,所以不建议用在生产环境,自己折腾倒是无所谓。

ttyd项目地址:https://github.com/tsl0922/ttydttyd官方主页:https://tsl0922.github.io/ttyd/


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有