通常の SSH だと不意に通信が切断されてしまうと、通信が復帰してもセッションが切断されてしまいます。mosh を使うと通信が切断・復帰すると自動的にセッションを再接続してくれますので不安定な回線でも安心して作業出来ます。
macOS へインストールする
macOS には homebrew でインストールすることが可能です。
brew install mosh
インストールされました。
$ which mosh /usr/local/bin/mosh
尚、mosh を指定しても mobile-shell を指定しても(少なくても現時点では)同じものがインストールされるように見えました。以下は mosh の info を表示させた例です。
$ brew info mosh mobile-shell: stable 1.2.5 (bottled), devel 1.2.5.95rc1, HEAD Remote terminal application https://mosh.mit.edu/ Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mobile-shell.rb ==> Dependencies Build: pkg-config ✔ Required: openssl ✔, protobuf ✘ ==> Options --without-test Run build-time tests --devel Install development version 1.2.5.95rc1 --HEAD Install HEAD version
以下は mobile-shell の info を表示させた例です。先程の mosh と全く同じ表示結果になっていることが分かります。
$ brew info mobile-shell mobile-shell: stable 1.2.5 (bottled), devel 1.2.5.95rc1, HEAD Remote terminal application https://mosh.mit.edu/ Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mobile-shell.rb ==> Dependencies Build: pkg-config ✔ Required: openssl ✔, protobuf ✘ ==> Options --without-test Run build-time tests --devel Install development version 1.2.5.95rc1 --HEAD Install HEAD version
CentOS7 へインストールする
CentOS7 にも mosh をインストールします。mosh は epel リポジトリ上にあるので、予め epel-release をインストールしておきます。
yum -y install epel-release
info を見ると、現状では 1.2.6 がインストールされるようです。
# yum info mosh Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: ftp.iij.ad.jp * epel: ftp.riken.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp Available Packages Name : mosh Arch : x86_64 Version : 1.2.6 Release : 1.el7 Size : 237 k Repo : epel/x86_64 Summary : Mobile shell that supports roaming and intelligent local echo URL : https://mosh.mit.edu/ License : GPLv3+ Description : Mosh is a remote terminal application that supports: : - intermittent network connectivity, : - roaming to different IP address without dropping the connection, and : - intelligent local echo and line editing to reduce the effects : of "network lag" on high-latency connections.
インストールします。
yum -y install mosh
Ubuntu16 へインストールする
Ubuntu16 にも mosh をインストールします。現状では 1.2.5-2 がインストールされるようです。
$ apt show mosh Package: mosh Version: 1.2.5-2 Priority: optional Section: universe/net Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Keith Winstein <keithw@mit.edu> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 826 kB Pre-Depends: dpkg (>= 1.15.7.2) Depends: libc6 (>= 2.17), libgcc1 (>= 1:3.0), libprotobuf9v5, libssl1.0.0 (>= 1.0.0), libstdc++6 (>= 5.2), libtinfo5 (>= 6), libutempter0 (>= 1.1.5), zlib1g (>= 1:1.1.4), openssh-client Recommends: libio-socket-ip-perl Homepage: http://mosh.mit.edu Download-Size: 201 kB APT-Sources: http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages Description: Mobile shell that supports roaming and intelligent local echo Mosh is a remote terminal application that supports: - intermittent network connectivity, - roaming to different IP address without dropping the connection, and - intelligent local echo and line editing to reduce the effects of "network lag" on high-latency connections.
インストールします。
apt -y install mosh
使い方
コマンドが mosh になるだけで、基本的な使い方は ssh と同じです。~/.ssh/config に定義されている内容も ssh と同様に参照してくれます。
mosh root@192.168.1.1
mosh で接続しに行くと、(明示的にポート指定が無い限りは)従来の ssh と同様に 22/TCP へセッションを確立しに行きます。しかし、一度 ssh セッションが確立すると UDP の 60,000 ポート以降を Listen する mosh-server との通信へ切り替え、22/tcp は Close するようです。実際にパケットを見てみると 22/TCP 宛の通信を流れるのは「接続開始時のみ」であり、一旦接続が確立すると以降は 60000/UDP 以降で空いているポートを使って通信するようです。また、この UDP 通信には Protocol Buffers を利用しているようで、mosh のインストール時には依存関係により Protocol Buffers もインストールされていました。