简体   繁体   English

我如何重新连接到一个独立的mosh会话?

[英]How do I reattach to a detached mosh session?

How do I reattach to a detached mosh session or otherwise get rid of 我如何重新连接到一个独立的mosh会话或以其他方式摆脱

Mosh: You have a detached Mosh session on this server (mosh [XXXX]).

ie what's the mosh equivalent of 也就是什么是mosh相当于

screen -D -R

or possibly 或者可能

screen -wipe

Furthermore, where can this answer be found in documentation? 此外,在文档中可以找到这个答案的位置?

For security reasons, you can not reattach, see https://github.com/keithw/mosh/issues/394 出于安全原因,您无法重新连接,请参阅https://github.com/keithw/mosh/issues/394

To kill the detached session, use the PID number displayed in that message (that's the 'XXXX' part.) For example, if you see -- 要终止分离的会话,请使用该消息中显示的PID编号(即'XXXX'部分。)例如,如果您看到 -

Mosh: You have a detached Mosh session on this server (mosh [12345]).

And can run this command: 并且可以运行此命令:

kill 12345

Also, to close all mosh connections you can: 此外,要关闭所有 mosh连接,您可以:

kill `pidof mosh-server`

Note that if you are currently connected via mosh, this last command will also disconnect you. 请注意,如果您当前通过mosh连接,则此最后一个命令也会断开您的连接。

To my amazement, I used CRIU ( https://criu.org ) to checkpoint and restart a mosh client and it worked. 令我惊讶的是,我使用CRIU( https://criu.org )来检查点并重新启动一个mosh客户端并且它有效。

Shocking. 令人震惊的。

Find your mosh-client's PID: 找到你的mosh-client的PID:

$ ps -ef | $ ps -ef | grep mosh grep mosh

Then, install CRIU according to their instructions. 然后,根据他们的说明安装CRIU。

Then, checkpoint it like this: 然后,检查点就像这样:

$ mkdir checkpoint $ mkdir checkpoint

$ sudo ./criu dump -D checkpoint -t PID --shell-job $ sudo ./criu dump -D checkpoint -t PID --shell-job

Then, restore it: 然后,恢复它:

$ sudo ./criu restore -D checkpoint --shell-job $ sudo ./criu restore -D checkpoint --shell-job

And, there it is. 而且,就是这样。 Your mosh client is back. 你的mosh客户回来了。

One thing to note, however, is that if your laptop reboots (which is the whole point of what we're trying to protect against), mosh uses a monotonic clock to track time on the client side, which doesn't work across reboots. 然而,有一点需要注意的是,如果您的笔记本电脑重新启动(这是我们试图防范的全部内容),mosh使用monotonic时钟来跟踪客户端的时间,这在重新启动时无效。 This will NOT work, however, if your laptop just flat out crashes it won't work because mosh sequence numbers will be out of sync with the version that was checkpointed (the binary will resume, but communication will stop). 但这不起作用,如果您的笔记本电脑刚刚崩溃,它将无法工作,因为mosh序列号将与检查点的版本不同步(二进制文件将恢复,但通信将停止)。

In order to fix this, you need to tell mosh to stop doing that and download the mosh source code. 为了解决这个问题,你需要告诉mosh停止这样做并下载mosh源代码。 Then, edit this file: 然后,编辑此文件:

cd mosh cd mosh

vim configure.ac vim configure.ac

Then, search for GETTIME and comment out that line. 然后,搜索GETTIME并注释掉该行。

Then do: 然后做:

autoreconf # or ./autogen.sh if you've just cloned it for the first time autoreconf#或./autogen.sh,如果你刚刚第一次克隆它

./configure 。/配置

make 使

make install make install

After that, your CRIU-checkpointed mosh client sessions will survive reboots. 之后,您的CRIU-checkpointed mosh客户端会话将重新启动。

(Obviously you'd need to write something to perform the checkpoints regularly enough to be useful. But, that's an exercise for the reader). (显然你需要写一些东西来定期执行检查点才能有用。但是,这对读者来说是一种练习)。

I realize this is an old post, but there is a very simple solution to this, as suggested by Keith Winstein, mosh author, here: https://github.com/mobile-shell/mosh/issues/394 我意识到这是一个很老的帖子,但有一个非常简单的解决方案,正如Keith Winstein,mosh作者所建议的那样: https//github.com/mobile-shell/mosh/issues/394

"Well, first off, if you want the ability to attach to a session from multiple clients (or after the client dies), you should use screen or tmux. Mosh is a substitute (in some cases) for SSH, not for screen. Many Mosh users use it together with screen and like it that way." “好吧,首先,如果你想要能够从多个客户端连接到一个会话(或者在客户端死亡之后),你应该使用screen或tmux .Mosh是SSH的替代品(在某些情况下),而不是屏幕。许多Mosh用户将它与屏幕一起使用,就像那样。“

Scenario: I'm logged into a remote server via mosh. 场景:我通过mosh登录了远程服务器。 I've then run screen and have a process running in the screen session, htop, for example. 然后我运行屏幕并在屏幕会话中运行一个进程,例如htop。 I lose connection (laptop battery dies, lose network connection, etc.). 我失去连接(笔记本电脑电池死机,丢失网络连接等)。 I connect again via mosh and get that message on the server, 我通过mosh再次连接并在服务器上获取该消息,

Mosh: You have a detached Mosh session on this server (mosh [XXXX]). Mosh:你在这台服务器上有一个独立的Mosh会话(mosh [XXXX])。

All I have to do is kill the prior mosh session 我所要做的就是杀死之前的mosh会话

kill XXXX 杀XXXX

and reattach to the screen session, which still exists . 重新连接到仍然存在的屏幕会话

screen -r 屏幕-r

Now, htop (or whatever process was running) is back just as it was without interruption.This is especially useful for running upgrades or other processes that would leave the server in a messy, unknown state if suddenly interrupted. 现在,htop(或正在运行的任何进程)就像没有中断一样回来了。这对于运行升级或其他进程尤其有用,如果突然中断,服务器将处于凌乱的未知状态。 I assume you can do the same with tmux, though I have not tried it. 我假设你可以用tmux做同样的事情,虽然我没有尝试过。 I believe this is what Annihilannic and eskhool were suggesting. 我相信这就是Annihilannic和eskhool的建议。

作为Varta答案的补充,我使用以下命令关闭除当前连接之外的所有mosh连接:

pgrep mosh-server | grep -v $(ps -o ppid --no-headers $$) | xargs kill

As @varta pointed out, the mosh owners are very against reattaching from different clients for security reasons. 正如@varta指出的那样,出于安全原因,mosh所有者非常反对从不同的客户端重新连接。 So if your client is gone (eg you restarted your laptop) your only option is to kill the sessions. 因此,如果您的客户已经离开(例如您重新启动了笔记本电脑),您唯一的选择就是杀死会话。

To kill only detached sessions you can use the following line (which I have as an alias in my .bashrc ). 要仅杀死分离的会话,您可以使用以下行(我在.bashrc作为别名)。

who | grep -v 'via mosh' | grep -oP '(?<=mosh \[)(\d+)(?=\])' | xargs kill

That command depends on the fact that who lists connected users including mosh sessions, only attached mosh sessions have "via mosh", and that mosh sessions have their pid in square brackets. 该命令取决于who列出连接用户(包括mosh会话),只有附加的mosh会话具有“通过mosh”,并且mosh会话将其pid放在方括号中。 So it finds the pids for just the detached mosh sessions and passes them to kill using xargs. 所以它找到了仅用于分离的mosh会话的pids并使用xargs将它们传递给kill。

Here is an example who result for reference: 下面是一个例子who导致供参考:

$ who
theuser    pts/32       2018-01-03 08:39 (17X.XX.248.9 via mosh [193891])
theuser    pts/17       2018-01-03 08:31 (17X.XX.248.9 via mosh [187483])
theuser    pts/21       2018-01-02 18:52 (mosh [205286])
theuser    pts/44       2017-12-21 13:58 (:1001.0)

An alternative is to use the mosh-server environment variable MOSH_SERVER_SIGNAL_TMOUT . 另一种方法是使用mosh-server环境变量MOSH_SERVER_SIGNAL_TMOUT You can set it to something like 300 in your .bashrc on the server side . 您可以在服务器端的 .bashrc中将其设置为300。 Then if you do a pkill -SIGUSER1 mosh-server it will only kill mosh-servers that have not been connected in the last 300 seconds (the others will ignore the SIGUSER1). 然后,如果你做一个pkill -SIGUSER1 mosh-server ,它将只杀死在过去300秒内没有连接的mosh服务器(其他人将忽略SIGUSER1)。 More info in the mosh-server man page . 有关mosh-server手册页的更多信息。 I am using the command above because, once aliased, it seems simpler to me. 我正在使用上面的命令,因为一旦出现别名,对我来说似乎更简单。

Note, as mentioned by @Annihilannic, if you are using tmux/screen inside your mosh sessions then those tmux/screen sessions are still around after you kill the mosh sessions. 注意,正如@Annihilannic所提到的,如果你在你的mosh会话中使用tmux / screen,那么那些tmux / screen会话在你杀死mosh会话之后仍然存在。 So you can still attach to them (so you really don't lose much by killing the mosh sessions themselves). 所以你仍然可以依附于他们(所以你真的不会因为杀死mosh会话而损失很多)。

The answers here claiming that killing mosh-server is the only option are largely obsolete, as we can use criu and reptyr to recover and reattach arbitrary processes. 这里的答案声称杀死mosh-server是唯一的选择,这在很大程度上是过时的,因为我们可以使用criureptyr来恢复和重新连接任意进程。

Not to mention that nowadays we can kill -USR1 mosh-server to only kill detached sessions in a clean and safe way, without resorting to unsafe who output or cumbersome commands to avoid killing our own session. 且不说现在我们可以kill -USR1 mosh-server在一个干净和安全的方式只有杀分离会话,而不诉诸不安全who输出或繁琐的命令,以避免杀害我们自己的会话。

Next to the criu answer from Michael R. Hines, there is the slightly more "light-weight" reptyr which can be used to reattach processes started by mosh-server (ie not the mosh-server itself). 在Michael R. Hines的criu答案旁边,有一个稍微“轻量级”的reptyr ,可用于重新附加由mosh-server启动的进程(即不是mosh-server本身)。 I typically use 我通常使用

pstree -p <mosh-server PID>

to list the tree of processes under the detached mosh-server, and then 列出分离的mosh-server下的进程树,然后

reptyr PID

to reattach the desired process to my current terminal. 将所需的过程重新连接到我当前的终端。 After repeating the procedure for all processes I care about, I 在重复我关心的所有过程的程序之后,我

kill -USR1 <mosh-server PID>

whereas I take care to only kill sessions I know are mine (shared system). 而我只注意杀死我认识的会话(共享系统)。

Use ps command for getting the list of running task or use ps -ef | 使用ps命令获取正在运行的任务列表或使用ps -ef | grep mosh grep mosh

Kill the mosh PID using this command: 使用此命令终止mosh PID:

kill <pid>

Also, to close all mosh connections you can: 此外,要关闭所有mosh连接,您可以:

Note that if you are currently connected via mosh, then this also disconnect you 请注意,如果您当前通过mosh连接,那么这也会断开您的连接

kill `pidof mosh-server`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM