据说mosh好使,那么怎么在Mac本下使用mosh来登陆Ubuntu及AWS服务器呢?html
mosh官网在:https://mosh.org/
代码开源在:https://github.com/mobile-shell/moshgit
卖点就是经过UDP传输,网络切换能够自动重连,不会致使执行中的命令断掉。github
Ubuntu下安装mosh服务器:redis
$ sudo apt-get install mosh
Mac下安装mosh客户端:shell
$ brew install mosh
Ubuntu下运行mosh-server
:ubuntu
$ mosh-server MOSH CONNECT 60001 y8btZAE3fjpmzQu7s/4XWg mosh-server (mosh 1.3.2) [build mosh 1.3.2] Copyright 2012 Keith Winstein <mosh-devel@mit.edu> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. [mosh-server detached, pid = 7195]
看到mosh-server detached
不要慌,毕竟它也不监听端口。安全
假设服务器地址为123.123.123.123
,用户名是etworker
,则Mac下运行:服务器
$ mosh etworker@123.123.123.123
若是一切顺利,链接起来就像ssh同样。
若是服务器ssh的端口是2222,则用下面的方式链接:网络
$ mosh --ssh="ssh -p 2222" etworker@123.123.123.123
Mosh使用的UDP协议链接的,使用的端口是从60000到61000,若是开启了防火墙服务器上就须要打开相应的UDP端口。
一个Mosh链接就会打开一个UDP端口,好比创建两个链接就是6000一、60002,以此类推。ssh
假设Mosh使用60001 UDP端口,则在服务器上运行
$ iptables -I INPUT -p udp –dport 60001 -j ACCEPT
这样就在服务器上打开60001这个UDP端口。
固然,最好是把上一条命令写入服务器iptables的规则中,这样没必要要每次都手动打开这个端口。
我在Amazon上的服务器是经过key文件作的鉴权,因此先说一下Mac怎么ssh上去。
先将密钥文件拷贝到Mac上,比方说文件名叫xxx.pem
,而后修改权限:
$ chmod 400 xxx.pem
假设服务器地址是111.111.111.111,进到xxx.pem
同目录下,ssh以下:
$ ssh -i xxx.pem ubuntu@111.111.111.111
此时在服务器上安装mosh,方式同上。
注意须要在安全组里面配置端口,开放6000-61000的UDP端口。
安全组的端口过滤方法,应该不须要再细说了吧。
接着就可使用Mac来链接了:
$ mosh --ssh="/usr/bin/ssh -i xxx.pem" ubuntu@111.111.111.111