mosh安装学习——一个基于UDP的ssh服务

mosh是一个远程终端应用,它采用udp的通信方式进行ssh连接。它的作用包括支持不稳定的连接,恢复会话时仍能保持连接。

它适用于网络不稳定的环境中,能将SSH连接的响应时间降到最低。

安装

Debian、Ubuntu:

1
2
apt-get update
apt-get install mosh

RHEL/CentOS/Fedora:

1
2
yum update
yum install mosh

使用

当我们通过包管理工具安装好mosh后,它已经默认启动了服务程序。例如

1
mosh-server new -c 8 -s -l LANG=zh_CN.UTF-8 -l LANGUAGE=zh_CN:en_US:en -l LC_NUMERIC=zh_CN.UTF-8 -l LC_TIME=zh_CN.UTF-8 -l LC_MONETARY=zh_CN.UTF-8 -l LC_PAPER=zh_CN.UTF-8 -l LC_NAME=zh_CN.UTF-8 -l LC_ADDRESS=zh_CN.UTF-8 -l LC_TELEPHONE=zh_CN.UTF-8 -l LC_MEASUREMENT=zh_CN.UTF-8 -l LC_IDENTIFICATION=zh_CN.UTF-8

端口问题

它默认采用udp的60001端口,在这种情况下需要我们关闭防火墙或者打开指定端口

1
iptables -I INPUT -p udp --dport 60001 -j ACCEPT

字符集设置问题

在正式连接之前,我们需要设置需要登录用户的字符集,如果不设置,就会报以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
The locale requested by LANG=en_US.UTF-8 isn't available here.
Running `locale-gen en_US.UTF-8' may be necessary.

The locale requested by LANG=zh_CN.UTF-8 isn't available here.
Running `locale-gen zh_CN.UTF-8' may be necessary.

mosh-server needs a UTF-8 native locale to run.

Unfortunately, the local environment (LANG=en_US.UTF-8) specifies
the character set "US-ASCII",

The client-supplied environment (LANG=zh_CN.UTF-8) specifies
the character set "US-ASCII".

locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:en_US:en
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=
Connection to www.conbot.space closed.
/usr/bin/mosh: Did not find mosh server startup message.

为此,我们需要在~/.bashrc中设置:

1
2
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

退出重新登录若出现以下问题:

1
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory

运行一下命令生成配置文件

1
localedef -v -c -i en_US -f UTF-8 en_US.UTF-8

登录

接下来,在本地服务器同样安装mosh。

像使用ssh一样使用mosh

1
2
3
4
5
6
7
8
9
10
mosh test@yourserver.com 

# 指定绑定的udp端口,默认启动时动态端口号从60001到60100
mosh test@yourserver.com -p 60001
mosh test@yourserver.com -p 60002
...
mosh test@yourserver.com -p 60100

# 指定ssh服务的端口号
mosh -ssh="ssh -p 22" test@yourserver.com

更多选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
mosh --help
===================
Usage: /usr/bin/mosh [options] [--] [user@]host [command...]
--client=PATH mosh client on local machine
(default: "mosh-client")
--server=COMMAND mosh server on remote machine
(default: "mosh-server")

--predict=adaptive local echo for slower links [default]
-a --predict=always use local echo even on fast links
-n --predict=never never use local echo
--predict=experimental aggressively echo even when incorrect

-p PORT[:PORT2]
--port=PORT[:PORT2] server-side UDP port or range

--ssh=COMMAND ssh command to run when setting up session
(example: "ssh -p 2222")
(default: "ssh")

--no-init do not send terminal initialization string

--help this message
--version version and copyright information

Please report bugs to mosh-devel@mit.edu.
Mosh home page: http://mosh.mit.edu