Shellby Shellby指南Guides
全部指南All guides 文档Docs ← 返回首页← Home

指南Guides ·

Mosh 是什么?与 SSH 的区别,以及手机上为什么更需要它What Is Mosh? How It Differs from SSH, and Why Phones Need It

在手机上用 SSH,最恼人的两件事是:网络一切换会话就断,以及信号差时每敲一个字都要等半秒才显示。Mosh 协议就是冲着这两件事去的。

SSH 在移动网络下的两个硬伤

第一,连接绑死在 TCP 上。SSH 跑在 TCP 之上,而一条 TCP 连接由「源 IP + 源端口 + 目标 IP + 目标端口」唯一确定。手机从 Wi-Fi 切到蜂窝,源 IP 变了,这条连接就此作废——会话直接断掉,你得重新登录、重新 cd 回去。

第二,回显要等一个往返。SSH 是字符流转发:你按下的键送到服务器,服务器决定该显示什么再送回来。链路 200ms 延迟,你就得等 200ms 才看到自己敲的字。在高铁上或信号弱的地方,这种滞后感非常明显。

Mosh 怎么解决

Mosh(mobile shell)换了两个思路:

  • 用 UDP,连接不认 IP 认会话。它给每个会话一个独立标识,客户端 IP 变了也能继续——服务器认的是会话身份而不是四元组。所以切网、断网再回来,会话还在。
  • 状态同步而不是字符流。它同步的是「终端屏幕应该长什么样」这个状态,而不是逐个字符转发。因此客户端可以预测显示你敲的内容(并用下划线标注未确认部分),不必等服务器确认——输入立刻可见。

两者对比

维度SSHMosh
传输层TCPUDP(初次登录仍走 SSH)
换网 / 换 IP断开自动续上
弱网输入等一个往返本地预测,立即回显
滚动历史保留不保留(协议限制)
端口转发支持不支持
服务器要求sshd(几乎都有)需额外安装 mosh-server

它的两个真实局限

一、没有滚动历史。这是协议设计的直接结果——它同步的是当前屏幕状态,不是完整的输出流。所以你不能往上翻看几分钟前刷过去的日志。需要翻历史时,用 lessjournalctl 分页,或者干脆用普通 SSH 连。

二、服务器得装 mosh-server。大多数发行版一条命令的事(apt install mosh / dnf install mosh),但你得有权限装,而且防火墙要放行 UDP 60000–61000 端口段。托管环境或受控的堡垒机里不一定允许。

该用哪个

  • 用 Mosh 的场景:手机 / 平板上的长时间交互,通勤路上、信号不稳的地方,或者你只是想让会话一直挂着不断。
  • 用 SSH 的场景:需要端口转发、需要翻大量滚动历史、传文件(SFTP)、或者服务器上装不了 mosh-server。

实际用法是两者并存:同一台主机配两种传输方式,按当下的事情选。

在 Shellby 里怎么用

Shellby 实现了「Mosh 兼容连接」——在新建主机时把传输方式选成它即可,界面上会有徽标标明当前会话走的是哪种。服务器没装 mosh-server 的话,测试连接时就会发现并按你的发行版给出安装命令,不至于连到一半才报错。

由于协议不保留滚动历史,第一次往上翻时应用会说明原因并给出替代做法,不会让你以为是 bug。

「Mosh 兼容」是依据公开的 Mosh 协议文档独立实现的,与 Mosh 项目无隶属或背书关系。

Two things make SSH painful on a phone: switching networks kills the session, and on a weak link every keystroke takes half a second to appear. The Mosh protocol exists to fix exactly those two.

Where SSH struggles on mobile

The connection is pinned to TCP. A TCP connection is identified by source IP + source port + destination IP + destination port. Move from Wi-Fi to cellular, your source IP changes, and the connection is void — you log in again and cd back to where you were.

Echo costs a round trip. SSH forwards a character stream: your keystroke goes to the server, which decides what to display and sends it back. On a 200ms link you wait 200ms to see what you typed. On a train, that lag is unmistakable.

What Mosh does instead

  • UDP, with sessions not sockets. Each session gets its own identity, so a changed client IP doesn't matter — the server recognises the session, not the four-tuple. Switch networks, go offline and come back: the session is still there.
  • State synchronisation, not a byte stream. It syncs “what the screen should look like” rather than forwarding characters. That lets the client predict your typing locally (underlining what isn't confirmed yet) instead of waiting for the server.

Side by side

SSHMosh
TransportTCPUDP (login still over SSH)
Changing network / IPDropsResumes automatically
Typing on a bad linkOne round tripPredicted locally, instant
ScrollbackKeptNot kept (by design)
Port forwardingYesNo
Server requirementsshd (everywhere)mosh-server must be installed

Two limitations worth knowing

No scrollback. A direct consequence of syncing screen state rather than the output stream. You can't scroll up to logs that flew past. Page with less or journalctl, or use plain SSH when you need history.

The server needs mosh-server. One command on most distros (apt install mosh, dnf install mosh), but you need the privilege to install it and UDP ports 60000–61000 open. Managed or locked-down bastion hosts may not allow either.

Which to use

  • Mosh: long interactive work from a phone or tablet, commuting, flaky signal, or when you just want the session to stay put.
  • SSH: port forwarding, lots of scrollback, file transfer over SFTP, or servers where mosh-server isn't an option.

In practice you keep both — configure the same host twice and pick per task.

Using it in Shellby

Shellby implements a Mosh-compatible connection — choose it as the transport when adding a host; a badge shows which one the current session uses. If mosh-server isn't installed, the connection test detects it and offers the install command for your distribution, rather than failing halfway.

Because the protocol keeps no scrollback, the first time you scroll up the app explains why and suggests what to use instead — so it doesn't read as a bug.

“Mosh-compatible” is an independent implementation of the publicly documented Mosh protocol. Not affiliated with or endorsed by the Mosh project.