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

刹客网络科技资讯点击右侧关注,最新科技资讯!

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

安装ttyd

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

#下载ttydwget -O ttyd /download/1.6.0/ttyd_linux.x86_64#添加执行权限chmod +x ttyd#移动目录mv ttyd /usr/sbin

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

[root@hosta29d0ffef5 ~]# ttyd -vttyd version 1.6.0-c15cfb7

运行ttyd

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

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

新建服务

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

[Unit]Description=ttydAfter=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命令来进行管理了,命令如下:

#启动ttydsystemctl start ttyd#停止ttydsystemctl stop ttyd#重启ttydsystemctl restart ttyd#开机启动systemctl enable ttyd

Nginx反向代理

如果您不喜欢通过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 :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 :7681/$1;}

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

ttyd参数说明

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

USAGE:ttyd [options] <command> [<arguments...>]VERSION:1.6.0OPTIONS:-p, --portPort 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, --credentialCredential for Basic Authentication (format: username:password)-u, --uid User id to run with-g, --gid Group id to run with-s, --signalSignal to send to the command when exit it (default: 1, SIGHUP)-a, --url-arg Allow client to send command line arguments in URL (eg: :7681?arg=foo&arg=bar)-R, --readonlyDo 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-originDo not allow websocket connection from different origin-m, --max-clients Maximum clients to support (default: 0, no limit)-o, --onceAccept 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, --ipv6Enable IPv6 support-S, --ssl Enable SSL-C, --ssl-certSSL certificate file path-K, --ssl-key SSL key file path-A, --ssl-caSSL CA file path for client certificate verification-d, --debug Set log level (default: 7)-v, --version Print the version and exit-h, --helpPrint this text and exitVisitto get more information and report bugs.

总结

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

ttyd项目地址:

ttyd官方主页:

点分享

点收藏

点点赞

点在看