Mosh
0x01 介紹mosh
Mosh表示移動Shell(Mobile Shell),是一個用於從客戶端跨互聯網連接遠程服務器的命令行工具。它能用於SSH連接,但是比Secure Shell功能更多。它是一個類似於SSH而帶有更多功能的應用。程序最初由Keith Winstein 編寫,用於類Unix的操作系統中,發布於GNU GPL V3協議下。
Mosh最大的特點是基於UDP方式傳輸,支持在服務端創建一個臨時的Key供客戶端一次性連接,退出后失效;也支持通過SSH的配置進行認證,但數據傳輸本身還是自身的UDP方式。
另外,Mosh還有兩個我覺得非常有用的功能
- 會話的中斷不會導致當前正在前端執行的命令中斷,相當於你所有的操作都是在screen命令中一樣在后台執行。
- 會話在中斷過后,不會立刻退出,而是啟用一個計時器,當網絡恢復后會自動重新連接,同時會延續之前的會話,不會重新開啟一個。
Mosh的功能
- 它是一個支持漫游的遠程終端程序
- 在所有主流的類 Unix 版本中可用,如 Linux、FreeBSD、Solaris、Mac OS X和Android
- 支持不穩定連接
- 支持智能的本地回顯
- 支持用戶輸入的行編輯
- 響應式設計及在 wifi、3G、長距離連接下的魯棒性
- 在IP改變后保持連接。它使用UDP代替TCP(在SSH中使用),當連接被重置或者獲得新的IP后TCP會超時,但是UDP仍然保持連接
- 在很長的時候之后恢復會話時仍然保持連接
- 沒有網絡延遲。立即顯示用戶輸入和刪除而沒有延遲
- 像SSH那樣支持一些舊的方式登錄
- 包丟失處理機制
0x02 安裝mosh
-
centos7 上安裝
yum install mosh
-
deepin debian ubuntu 上安裝
apt-get install mosh
-
源碼安裝
$ git clone https://github.com/mobile-shell/mosh
$ cd mosh
$ ./autogen.sh
$ ./configure
$ make
# make install
0x03 防火牆設置
要在服務器的防火牆上面設置開啟 60000 到 61000 upd 端口。mosh 不安全的一點就在於這里它服務端的端口是固定的這個范圍。默認是從 60000 開始然后 +1 的增加。比如第一個連接是 60001,這個連接沒斷后面就會是 60002 端口連接,以此類推。
現在開始設置服務端防火牆
如果是 iptables
sudo iptables -I INPUT 1 -p udp --dport 60000:61000 -j ACCEPT
如果是 ufw
sudo ufw allow 60000:61000/udp
我的是 firewalld
# 先確定活動區域
[root@ivoivo ~]# firewall-cmd --get-active-zones
public
interfaces: eth0
# 編輯防火牆規則
[root@ivoivo ~]# firewall-cmd --permanent --zone=public --add-port=60000-61000/udp
# 讓防火牆生效
[root@ivoivo ~]# firewall-cmd --reload
0x04 使用MOSH
采用臨時key一次性認證
- 采用臨時Key的方式進行一次性認證
先需要在服務端創建Key,然后客戶端通過這個Key進行登錄,該Key會在會話結束十分鍾后自動失效。
創建一個臨時的Key和端口供Client登錄
$ mosh-server
MOSH CONNECT 60001 S7KI+lnT56j4efWRhMc23g
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 = 141741]
定義好MOSH_KEY的值
$ export MOSH_KEY=S7KI+lnT56j4efWRhMc23g
使用臨時Key進行登陸
$ mosh-client 10.1.1.1 60001
$ exit
logout
[mosh is exiting.]
注:mosh-client后面只能跟服務器具體的IP地址和臨時端口,不支持主機名或域名方式
假設服務器地址為 10.1.1.1
ssh 私鑰存放在 /root/.ssh/id_rsa
- 沒有更改ssh 22 端口的用法
mosh root@10.1.1.1
- 沒有更改ssh 22 端口,但是想用服務端某個特定的端口
這種情況是在防火牆上面只開啟了 60000-61000 中間的 1 個或幾個端口比如只開啟了 60010 這一個端口,那么用的時候在客戶端上后面要加 -p 的參數
mosh -p 60010 root@10.1.1.1
- 如果改動過ssh端口 22 改為 2500
mosh --ssh="ssh -p 2500" root@10.1.1.1
- 如果改動過ssh端口 22 改為2500還要用服務端 60010 udp 端口
mosh -p 60010 --ssh="ssh -p 2500" root@10.1.1.1
- 不用密碼使用的秘鑰登錄
mosh --ssh="/usr/bin/ssh -i /root/.ssh/id_rsa" root@10.1.1.1
- 不用密碼使用的秘鑰登錄,改 22 為 2500
mosh --ssh="/usr/bin/ssh -i /root/.ssh/id_rsa -p 2500" root@10.1.1.1
- 不用密碼使用的秘鑰登錄,改 22 為 9999,且使用服務端 60010 udp 端口
mosh -p 60010 --ssh="/usr/bin/ssh -i /root/.ssh/id_rsa -p 2500" root@10.1.1.1
utf-8編碼問題,解決方案
In my case, put these lines in ~/.bashrc
(both on server and local machine)
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
Edit: recently, when I try on another machine, I have to put those line in ~/.profile. So, you should try with ~/.bashrc and ~/.profile
引用