ngrok 可以为没有公网 IP 的 Mac、Linux 或 Windows 主机创建临时 SSH 入口。临时访问可以使用 ngrok;长期访问更推荐 Tailscale。
准备工作
- SSH 已可在本地正常连接。
- 主机已安装 ngrok 并配置 Authtoken。
- 当前 ngrok 账号支持 TCP Endpoint。
部分套餐需要添加有效付款方式才能使用 TCP Endpoint,请以 ngrok Dashboard 显示为准。
安装并登录
macOS:
brew install ngrok
ngrok config add-authtoken YOUR_NGROK_AUTHTOKEN
Windows PowerShell:
ngrok config add-authtoken YOUR_NGROK_AUTHTOKEN
启动 SSH 转发
在需要访问的主机执行:
ngrok tcp 22
ngrok 会显示类似地址:
Forwarding tcp://0.tcp.ngrok.io:12345 -> localhost:22
在 Redock 中添加 Host
Host: 0.tcp.ngrok.io
Port: 12345
Username: <Remote Username>
Connection Mode: SSH
Auth: SSH Key
Host 只填写域名,不要包含 tcp://。
使用固定地址(可选)
随机 TCP 地址可能在重启后变化。需要固定地址时,在 ngrok Dashboard 创建 TCP Address,并使用分配的地址启动:
ngrok tcp 22 --url tcp://<reserved-address>
安全建议
- 推荐使用 SSH Key,不要使用弱密码。
- 使用完成后关闭 ngrok。
- 不要将弱密码 SSH 服务暴露到公网。
- Mosh 需要 UDP,不能通过普通 ngrok TCP 转发工作。
常见问题
- 无法连接: 确认 ngrok 正在运行,Host 和 Port 与当前 Forwarding 地址一致。
- Authentication failed: 检查 Username、Password 或 SSH Key。
- 地址发生变化: 更新 Redock Host,或使用固定 TCP Address。
- 无法创建 TCP Endpoint: 检查套餐、付款方式和 TCP Endpoint 权限。