在 AI Coding / CLI Coding 逐渐成为主流的今天,很多开发者越来越依赖终端:
问题也很明显:
如果你不在电脑前,怎么快速登录你的工作机继续工作?
SSH 虽然很好用,但很多时候你只需要一个浏览器。
一种非常实用的方案是:
browser
↓
nginx (https)
↓
ttyd
↓
tmux
↓
bash
通过浏览器即可打开服务器终端。
涉及的核心工具:
最终访问地址:
https://domain.com/ttyd/
打开就是你的服务器终端。
ttyd 是一个非常轻量的 Web 终端服务器。
安装:
apt update
apt install ttyd -y
测试运行:
ttyd -W -p 7681 bash
浏览器访问:
http://服务器IP:7681
如果能看到终端说明安装成功。
其中:
-W
表示允许输入,否则终端是只读模式。
在 Web Terminal 里直接运行 bash 有个问题:
浏览器断线会丢失会话
解决方案是使用 tmux。
安装:
apt install tmux -y
之后 ttyd 会启动一个持久会话:
tmux new -A -s web
这样:
创建 ttyd 服务:
nano /etc/systemd/system/ttyd.service
写入:
[Unit]
Description=ttyd Web Terminal
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root
ExecStart=/usr/bin/ttyd \
-W \
-i 127.0.0.1 \
-p 7681 \
tmux new -A -s web
Restart=always
RestartSec=3
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=false
[Install]
WantedBy=multi-user.target
关键配置:
-i 127.0.0.1
只允许本机访问,提高安全性。
启动服务:
systemctl daemon-reload
systemctl enable ttyd
systemctl start ttyd
安装 Nginx:
apt install nginx -y
创建配置:
nano /etc/nginx/conf.d/ttyd.conf
写入:
location /ttyd/ {
auth_basic "Secure Terminal";
auth_basic_user_file /etc/nginx/.ttydpasswd;
proxy_pass http://127.0.0.1:7681/;
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_read_timeout 3600;
}
重载:
nginx -t
systemctl reload nginx
不要使用 ttyd 自带密码。
推荐使用 Nginx Basic Auth。
安装工具:
apt install apache2-utils -y
生成密码:
htpasswd -c /etc/nginx/.ttydpasswd admin
输入一个复杂密码即可。
安装:
apt install fail2ban
创建配置:
nano /etc/fail2ban/jail.local
示例:
[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 5
bantime = 3600
效果:
连续输错 5 次密码
IP 自动封禁 1 小时
打开:
https://domain.com/ttyd/
流程:
浏览器
↓
nginx (HTTPS + Auth)
↓
ttyd
↓
tmux
↓
bash
你会得到一个 随时随地可访问的服务器终端。
在 AI 编程时代,我们越来越依赖 CLI:
这些工具通常运行在 远程服务器或工作机。
一个 Web Terminal 可以让你:
换句话说:
浏览器就是你的 SSH 客户端。
随着 CLI Coding 与 AI Agent 的兴起,终端再次成为开发者的核心工作界面。
一个简单的 Web Terminal 方案,就能让你的服务器变成 随时可访问的远程工作站。
当你打开:
https://domain.com/ttyd/
其实打开的不是一个网页,而是你的 开发环境本身。