GoTTY 浏览器终端共享工具

GoTTY 是一款用 Go 语言编写的轻量级工具,可以将终端程序共享到 Web 浏览器中。通过 GoTTY,你可以在浏览器中直接操作服务器终端,无需安装 SSH 客户端。它支持只读共享和可写模式,非常适合远程协作、教学演示和应急运维场景。在搬瓦工 VPS 上部署 GoTTY 只需要一个二进制文件即可运行。

一、安装 GoTTY

1.1 下载预编译二进制文件

apt update && apt upgrade -y
wget https://github.com/sorenisanerd/gotty/releases/latest/download/gotty_linux_amd64.tar.gz
tar -xzf gotty_linux_amd64.tar.gz
mv gotty /usr/local/bin/
chmod +x /usr/local/bin/gotty
rm gotty_linux_amd64.tar.gz

验证安装:

gotty --version

1.2 使用 Go 安装(可选)

go install github.com/sorenisanerd/gotty@latest

二、基本使用

2.1 只读模式(默认)

# 共享 top 命令的输出
gotty top

GoTTY 默认在 8080 端口启动,通过浏览器访问 http://YOUR_VPS_IP:8080 即可看到终端输出。默认模式下浏览器端是只读的,无法输入命令。

2.2 可写模式

# 启用可写模式,允许浏览器端输入
gotty -w bash

警告:可写模式下浏览器端可以执行任何命令,务必配合认证使用。

2.3 自定义端口和地址

# 指定端口和绑定地址
gotty -p 9090 -a 0.0.0.0 -w bash

三、安全配置

3.1 启用基本认证

# 设置用户名和密码
gotty -w --credential admin:YourStrongPassword bash

3.2 启用 TLS 加密

# 生成自签名证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/gotty/server.key \
  -out /etc/gotty/server.crt \
  -subj "/CN=your-domain.com"

# 使用 TLS 启动
gotty -w --tls-crt /etc/gotty/server.crt --tls-key /etc/gotty/server.key \
  --credential admin:YourStrongPassword bash

3.3 配置文件

创建 ~/.gotty 配置文件,避免每次手动输入参数:

mkdir -p /etc/gotty
cat > /etc/gotty/config <<'EOF'
port = "8080"
address = "127.0.0.1"
permit_write = true
credential = "admin:YourStrongPassword"
title_format = "GoTTY - {{ .Command }}"
reconnect = true
reconnect_time = 5
max_connection = 10
once = false
EOF
gotty --config /etc/gotty/config bash

四、配合 Nginx 反向代理

建议使用 Nginx 反向代理并配置 SSL 证书,提供更安全的访问方式:

cat > /etc/nginx/sites-available/gotty <<'EOF'
server {
    listen 443 ssl http2;
    server_name terminal.your-domain.com;

    ssl_certificate /etc/letsencrypt/live/terminal.your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/terminal.your-domain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}
EOF

ln -sf /etc/nginx/sites-available/gotty /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

五、使用 systemd 管理

cat > /etc/systemd/system/gotty.service <<'EOF'
[Unit]
Description=GoTTY Web Terminal
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/gotty --config /etc/gotty/config bash
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable gotty
systemctl start gotty

管理命令:

systemctl status gotty
systemctl restart gotty
journalctl -u gotty -f

六、应用场景

6.1 共享特定命令输出

# 共享系统监控
gotty htop

# 共享日志查看
gotty tail -f /var/log/syslog

# 共享网络监控
gotty iftop

6.2 受限 Shell

为安全起见,可以使用受限的 Shell 环境:

# 创建受限用户
useradd -m -s /bin/rbash gotty-user

# 只允许执行特定命令
mkdir -p /home/gotty-user/bin
ln -s /usr/bin/top /home/gotty-user/bin/
ln -s /usr/bin/htop /home/gotty-user/bin/
ln -s /usr/bin/df /home/gotty-user/bin/

# 以受限用户身份启动
gotty -w su - gotty-user

6.3 一次性终端

# --once 参数使终端只能连接一次,断开后自动关闭
gotty --once -w bash

七、使用 Docker 部署

docker run -d --name gotty \
  -p 8080:8080 \
  --restart always \
  sorenisanerd/gotty:latest \
  --permit-write --credential admin:YourStrongPassword \
  bash

八、常见问题

WebSocket 连接失败

确保 Nginx 反向代理正确配置了 WebSocket 相关的头信息,特别是 UpgradeConnection 头。

终端显示异常

设置正确的终端环境变量:

export TERM=xterm-256color

总结

GoTTY 是一款实用的浏览器终端共享工具,在搬瓦工 VPS 上可以快速搭建 Web 终端,方便远程管理和协作。使用时务必做好安全配置,建议配合认证和 TLS 加密。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 折扣。相关工具可参考 TTYD 教程WebSSH 教程

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。