17

When connecting to my development server via ssh, I can forward remote ports to local ports via:

ssh [email protected] -L 5432:localhost:5432

However I'd rather use mosh because my connection tends to drop. I tried extending my usual mosh command (that works) with the --ssh parameter:

mosh --ssh "ssh -L 5432:localhost:5432" [email protected]

Which gets me connected without error - but doesn't do anything for my ports.

Is there a way to make port forwarding work when connecting via mosh?

4 Answers 4

20

I found an open issue for this exact feature at Mosh's GitHub. And an open bounty at bountysource currently at $616.

So it looks like it's not possible yet.

--

As a workaround for my SSH disconnect issue I added the following lines to my server's /etc/ssh/sshd_config:

ClientAliveInterval 60  # send null packet every x seconds to clients
ClientAliveCountMax 720  # time them out after doing so y times

Followed by a restart of the SSH daemon and a re-login via SSH.

sudo /etc/init.d/ssh restart
sudo service ssh restart
sudo systemctl restart ssh

This of course doesn't help with situations like changing cell towers on mobile connections like mosh does.

3

You can also setup autossh which will reconnect when tunnel stops working

3
  • You should ideally show how to solve the issue. This does not use mosh though. Commented Jun 30, 2024 at 14:09
  • autossh is really good, except it will make a new connection instead of caching old ones? maybe you need some sort of fifo stream over TCP with a long lifetime, no idea, hard to implement. Commented Jun 30, 2024 at 14:42
  • Id use autossh for the tunnels, and then also connect to the terminal via mosh. At the end of the day, it's all SSH, mosh just changes the display behavior a bit - more like a remote desktop. Commented Nov 25, 2024 at 14:11
1

This feature is not yet supported. However, there is always the workaround that you can run an SSH session in parallel.

2
  • 1
    Welcome to the site, and thank you for your contribution. You may want to edit your post to address the OPs issue "I'd rather use mosh because my connection tends to drop." which was the reason not to use SSH port forwarding in the first place. Commented May 28, 2021 at 14:42
  • Any updates on this please? Commented Aug 6, 2021 at 11:22
1

Mosh opens an ssh session to setup the initial connection. Keeping that open would be a good way to maintain port forwarding while still using mosh for interaction. I've tried setting this up without any success:

mosh --ssh="ssh -fN" # hangs forever
mosh --ssh="ssh -f" # ssh gets terminated
3
  • Thanks for sharing these failed attempts. It's great to know "what doesn't work" Commented Jun 19, 2024 at 15:06
  • 1
    I've largely abandoned mosh even when I have an unreliable internet connection. Persistent tcp connections are not supported in mosh, they recommend using eternalterminal instead github.com/mobile-shell/mosh/issues/337#issuecomment-1693794943 Commented Nov 9, 2024 at 16:45
  • Yeah, I'm thinking the focus should be on a tool like autossh and tweaking the timeouts, or making it into a tunnel. Then run mosh over that connection. I'll checkout eternalterminal Commented Nov 25, 2024 at 14:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.