You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't use a ProxyCommand-based SSH connection #285
The NAT/firewall-busting problems of issue #48 notwithstanding, mosh also has issues with the "ProxyCommand" style of connecting to machines behind NATs. For specificity, here's an example from my .ssh/config:
This is convenient, because it lets me type ssh mauler, and ssh will connect to linax1.cims.nyu.edu, and the 'outer' ssh run will then run over the tunnel thus set up. I understand this is hard for mosh to imitate, given UDP connections and what not, so let's assume that there's a hole poked into the firewall at port 60000 UDP on linax1.cims.nyu.edu that lets me get to mauler.
The main problem then is that mosh tries to resolve mauler locally to find the host to connect to, which of course doesn't succeed. I'd argue that it could potentially be smarter about finding the public IP of the target host--when it starts, it is executing codes on both ends of the connection after all. Failing that, I'd like to hhave an option to specify which host is actually meant.
Reacted by Jeremiah Campbell, Raphael Hoegger, Stefan, iyknk, mikey, Mani Ka, Ant Somers, Raymond Yuen, Ryan Hayle, Fabian-Robert Stöter and 33 more
You're right that you can't use a ProxyCommand, but the reason is even simpler. Mosh doesn't try to resolve your hostnames -- it lets SSH do that. You can have a host alias in your SSH configuration and that will work fine. However, mosh itself uses the ProxyCommand feature (because it needs to know the IP address it's connecting to, in case there are multiple A records).
If you can find a way to start up the mosh-server on the remote end, and you can give the mosh-client an IP address to connect to, that's really all you need.
As you can see, I'm using ssh over a https proxy to form a tunnel to my home gateway, and then using that tunnel to connect to my home desktop machine.
The https proxy server that we have at my day job is unbelievably unreliable, disconnecting several times an hour, but it's the only way to punch out of the building.
I use autossh to keep a psuedo-persistent connection open, but still have to deal with my existing session being interrupted.
If mosh could be set up to pass its data through the ssh session used to start it (or a new one created with the same parameters), then mosh would be a viable option for me when at work.
+1 on this, I'd greatly appreciate ProxyCommand support.
My setup is simpler tho, support for something like this:
Host foo
Host bar
ProxyCommand ssh foo nc %h %p
would be enough for my needs - and make mosh a drop in solution as well.
Reacted by Frank Villaro-Dixon, wojciech-kopras, Ozan Caglayan, Robert Foss, Frieder Bluemle, Richard James Acton, NAIT BELKACEM, Chris, Dagor, Luke Fraser and 7 more
If you need ProxyCommand to connect a host but it can be reached from the internet via UDP, you could use a script like this. It starts mosh-server on the remote machine (using SSH and the ProxyCommand from .ssh/config), parses the output and then connects using mosh-client to the machine directly.
I guess you could use mosh on the hop if that the only place that has access to farside.
host hop
host farside
ProxyCommand mosh -- hop -W farsideaddr:22
(-W is sometimes better option than netcat.)
Instead of using ProxyCommand to determine the address mosh could use the LD_PRELOAD environment variable, overwrite/proxy the connect system call method (to the SSH server) and use that resolution result to determine/resume the specific connection of hop.
@grooverdan: mosh cannot be used as a ProxyCommand. That wouldn’t make any sense. The job of a ProxyCommand is passing on the raw, encrypted SSH stream; mosh doesn’t work that way.
You can, however, run mosh hop ssh farside, ssh hop mosh farside, or even mosh hop mosh farside, to use mosh for one or both legs of the connection. This obviously requires that you trust hop with the credentials you use to access farside.
I make quite heavy use of bastions, where the destination (farside) is not guaranteed to have mosh installed.
$ mosh hop ssh farside , seems to provide the benefits of mosh - but in a generic environment. I wondered if anyone has integrated this into their ssh config, to make the process simpler.
+1 for support of something like this, if its possible. I ssh into a bunch of VMs (farside) unaccessible from the Internet via a login node (hop) which is, but my situation is the opposite of @Daviey's. I have root on the VMs and so I can install mosh there, but I can't install it on the hop. Is there any way to get the benefits of mosh in this situation?
I came up with a potential solution that avoids ProxyCommand, but has its own downsides. There is a LocalCommand directive that causes ssh to execute a command on the localhost when the connection is established, with the same substitution rules (e.g. %h => remote hostname) as ProxyCommand. Of course, any existing LocalCommand directive will be clobbered, but I think that one is less likely to be used.
The downside is that LocalCommand requires the PermitLocalCommand=true option to be set, too, and that one enables local command execution via the !command escape sequence in ssh(1). So this has security implications -- can an exploit on the remote host use mosh-server to run arbitrary commands on the connecting host? I did a quick search, but couldn't find any history on the LocalCommand and PermitLocalCommand options, but it smells like something that was originally permitted by default, but locked down to avoid "surprise" shell execution vectors.
Oh, and I guess another upside to LocalCommand is that it avoids the need for the "fake proxy" used in ProxyCommand -- one fewer process and one fewer source of buffering to worry about!
Another possible solution that avoids both LocalCommand and ProxyCommand, but is more brittle -- run ssh with -v, and parse the verbose output to look for the line:
@akalin: Your LocalCommand proposal will cause the hostname to be resolved to an IP address separately by ssh and by mosh. They could get different results, either in the case of a DNS round-robin setup, or if there are IPv6 addresses involved.
Your -v proposal doesn’t solve the problem: if a user passes their own ProxyCommand, then ssh does not print such a debug message.
For some users (such as @akalin) this is a worthwhile tradeoff, obviously. We might want to support both methods.
Once OpenSSH has their library API available, using that and then examining the socket it creates may be a cleaner solution to all of this. That partly depends on how elegant that API turns out to be and how much code is needed to make it all go. There are various ssh libraries already, of course, but they vary in support for ssh_config and its options, and in quality of implementation.
@smaslennikov It’s similar but even more fundamental. Mosh uses the remote command to launch the remote mosh-server. (It uses the command argument instead of the RemoteCommand option, but SSH doesn’t allow you to use both, so SSH errors “Cannot execute command-line and remote command.”)
If you want to specify the command to launch inside the Mosh session, simply provide it after -- on the Mosh command line: mosh HOSTNAME -- screen -dr.
Reacted by Slava I. Maslennikov, Ersin Akinci and Mika Mäki
@andersk - I have hacked up your --experimental-remote-ip option for my own use case - please see attached patch (sorry, I'm a dinosaur who doesn't understand git) mosh.diff.txt
Basically allows setting a hostname/IP which is only used for the target of the UDP packets. This way I can use ProxyJump in .ssh/config, and then rush mosh like this: mosh --bind-server=<target LAN IP address>|any --experimental-remote-ip=gateway:<firewall external hostname> -p <port range opened on firewall for target host> <entry in .ssh/config with ProxyJump option>
I guess it's a bodge, but it works for me. Setting port ranges up etc. would be tedious where there is a multitude of hosts on the LAN, but I only have one or two hosts I'm likely to want to jump to like this.
@andersk - I have hacked up your --experimental-remote-ip option for my own use case - please see attached patch (sorry, I'm a dinosaur who doesn't understand git) mosh.diff.txt
Basically allows setting a hostname/IP which is only used for the target of the UDP packets. This way I can use ProxyJump in .ssh/config, and then rush mosh like this: mosh --bind-server=<target LAN IP address>|any --experimental-remote-ip=gateway:<firewall external hostname> -p <port range opened on firewall for target host> <entry in .ssh/config with ProxyJump option>
I guess it's a bodge, but it works for me. Setting port ranges up etc. would be tedious where there is a multitude of hosts on the LAN, but I only have one or two hosts I'm likely to want to jump to like this.
It seems despite the above patch file, seamlessly switching from SSH to mosh by just saying mosh some-server which has a ProxyCommand in ~/.ssh/config, still doesn't work. Are there any plans for fixing that? Is it even conceptually possible to ever have it work in a transparent fashion? My guess is there's a lot of people who can only / want to access a lot of hosts via ProxyCommand.
Reacted by Hector Orellana, jdamato-fsly, Eduard Staniloiu, Doron Atuar and Lorenzo Setale
The only thing that stops --experimental-remote-ip=local from working with ProxyCommands in ~/.ssh/config is the line $userhost = "$user$ip"; in /usr/bin/mosh around line 340. Comment that line out and ssh will be called with the original command-line argument. The mosh command will still resolve the host to an IP address locally, so you have to ensure that for wherever your ssh starts mosh-server, that the UDP packets from mosh-client get there too, e.g. NAT at the firewall. Any chance to have the remote-ip 'local' option without the change to $userhost?
The reason Mosh uses "$user$ip" there is to ensure that both ssh and mosh talk to the same address, in the case where the hostname has multiple A/AAAA records or round-robin config or dynamically changing results. So, your proposed fix breaks a significant use case.
...but Mosh could certainly add another variation on --experimental-remote-ip to handle this particular use case.
mosh could also use ssh -G to automatically detect an already-existing ProxyCommand and change its strategy. That starts to get a bit complex though, and could be surprising and confusing. This idea also applies to RemoteCommand discussed here and #1175.
I know I'm late but I'd like to share a small script made recently to use ssh config ProxyCommand with mosh. It relies on socat to relay mosh data via UDP: your computer <-> relay <-> mosh server
you have ssh access on the mosh server, though the relay, via a valid/working ProxyCommand configuration
socat is installed on the relay
What is does:
extracts the relay & mosh server hostnames and ssh port numbers from the user ssh config file
connects to the mosh server via bare ssh, through the relay. It starts a mosh-server there, and retrieve the port number (1) and the mosh encryption key (2). Finally closes this ssh connection.
connects to the relay via bare ssh. It starts socat to relay mosh UDP connection between your computer and mosh server on UDP port (1). Finally closes this ssh connection.
it starts locally mosh client, and open the connection with the relay on port (1) using encryption key (2).
From there, mosh is up and running
Cons:
socat is not installed everywhere. IT may see this tool as a security risk and can forbid it.
socat has no timeout to close after a period of inactivity. The script kills it manually when it closes, or try to do so if you press ctrl-c. But this is not bullet-proof.
the way to extract the hostname/port information from the ProxyCommand config is hacky (sed regex & co). This can be improved.
Reacted by Fabian Thomas, Benjamin Eberhardt, Huy Z and Muad Abd El Hay
Activity
keithw commentedon Jun 11, 2012
You're right that you can't use a ProxyCommand, but the reason is even simpler. Mosh doesn't try to resolve your hostnames -- it lets SSH do that. You can have a host alias in your SSH configuration and that will work fine. However,
moshitself uses the ProxyCommand feature (because it needs to know the IP address it's connecting to, in case there are multiple A records).If you can find a way to start up the
mosh-serveron the remote end, and you can give themosh-clientan IP address to connect to, that's really all you need.jonesmz commentedon Jul 31, 2012
My use case is far more complex than inducer's, but if mosh supported ProxyCommand, it would make mosh a drop in solution.
Here's a snippet of my ssh config
host home
ProxyCommand connect -H proxy:3128 %h 443
host home-desktop
ProxyCommand ssh -q home nc %h %p -w 30 2> /dev/null
As you can see, I'm using ssh over a https proxy to form a tunnel to my home gateway, and then using that tunnel to connect to my home desktop machine.
The https proxy server that we have at my day job is unbelievably unreliable, disconnecting several times an hour, but it's the only way to punch out of the building.
I use autossh to keep a psuedo-persistent connection open, but still have to deal with my existing session being interrupted.
If mosh could be set up to pass its data through the ssh session used to start it (or a new one created with the same parameters), then mosh would be a viable option for me when at work.
Thanks
nicolai86 commentedon Aug 24, 2012
+1 on this, I'd greatly appreciate ProxyCommand support.
My setup is simpler tho, support for something like this:
would be enough for my needs - and make mosh a drop in solution as well.
jpawlowski commentedon Jan 6, 2013
+1
tribut commentedon Apr 1, 2013
If you need ProxyCommand to connect a host but it can be reached from the internet via UDP, you could use a script like this. It starts mosh-server on the remote machine (using SSH and the ProxyCommand from .ssh/config), parses the output and then connects using mosh-client to the machine directly.
grooverdan commentedon Apr 26, 2013
I guess you could use mosh on the hop if that the only place that has access to farside.
(-W is sometimes better option than netcat.)
Instead of using ProxyCommand to determine the address mosh could use the LD_PRELOAD environment variable, overwrite/proxy the connect system call method (to the SSH server) and use that resolution result to determine/resume the specific connection of hop.
andersk commentedon Apr 26, 2013
@grooverdan: mosh cannot be used as a ProxyCommand. That wouldn’t make any sense. The job of a ProxyCommand is passing on the raw, encrypted SSH stream; mosh doesn’t work that way.
You can, however, run
mosh hop ssh farside,ssh hop mosh farside, or evenmosh hop mosh farside, to usemoshfor one or both legs of the connection. This obviously requires that you trusthopwith the credentials you use to accessfarside.Daviey commentedon Jun 23, 2013
I make quite heavy use of bastions, where the destination (farside) is not guaranteed to have mosh installed.
$ mosh hop ssh farside , seems to provide the benefits of mosh - but in a generic environment. I wondered if anyone has integrated this into their ssh config, to make the process simpler.
porterjamesj commentedon Sep 23, 2013
+1 for support of something like this, if its possible. I ssh into a bunch of VMs (farside) unaccessible from the Internet via a login node (hop) which is, but my situation is the opposite of @Daviey's. I have root on the VMs and so I can install mosh there, but I can't install it on the hop. Is there any way to get the benefits of mosh in this situation?
akalin commentedon Jul 12, 2015
I came up with a potential solution that avoids ProxyCommand, but has its own downsides. There is a LocalCommand directive that causes ssh to execute a command on the localhost when the connection is established, with the same substitution rules (e.g. %h => remote hostname) as ProxyCommand. Of course, any existing LocalCommand directive will be clobbered, but I think that one is less likely to be used.
The downside is that LocalCommand requires the PermitLocalCommand=true option to be set, too, and that one enables local command execution via the !command escape sequence in ssh(1). So this has security implications -- can an exploit on the remote host use mosh-server to run arbitrary commands on the connecting host? I did a quick search, but couldn't find any history on the LocalCommand and PermitLocalCommand options, but it smells like something that was originally permitted by default, but locked down to avoid "surprise" shell execution vectors.
Anyway, my changes to master are master...akalin:avoid-proxy-command , and my changes to the stable version (which I tested and works swimmingly) are mosh-stable...akalin:avoid-proxy-command-stable .
akalin commentedon Jul 12, 2015
Oh, and I guess another upside to LocalCommand is that it avoids the need for the "fake proxy" used in ProxyCommand -- one fewer process and one fewer source of buffering to worry about!
akalin commentedon Jul 12, 2015
Another possible solution that avoids both LocalCommand and ProxyCommand, but is more brittle -- run ssh with -v, and parse the verbose output to look for the line:
debug1: Connecting to x.x.x.x [x.x.x.x] port 22.andersk commentedon Jul 13, 2015
@akalin: Your
LocalCommandproposal will cause the hostname to be resolved to an IP address separately bysshand bymosh. They could get different results, either in the case of a DNS round-robin setup, or if there are IPv6 addresses involved.Your
-vproposal doesn’t solve the problem: if a user passes their ownProxyCommand, thensshdoes not print such a debug message.cgull commentedon Jul 13, 2015
For some users (such as @akalin) this is a worthwhile tradeoff, obviously. We might want to support both methods.
Once OpenSSH has their library API available, using that and then examining the socket it creates may be a cleaner solution to all of this. That partly depends on how elegant that API turns out to be and how much code is needed to make it all go. There are various ssh libraries already, of course, but they vary in support for
ssh_configand its options, and in quality of implementation.24 remaining items
andersk commentedon Nov 2, 2018
@smaslennikov It’s similar but even more fundamental. Mosh uses the remote command to launch the remote
mosh-server. (It uses the command argument instead of theRemoteCommandoption, but SSH doesn’t allow you to use both, so SSH errors “Cannot execute command-line and remote command.”)If you want to specify the command to launch inside the Mosh session, simply provide it after
--on the Mosh command line:mosh HOSTNAME -- screen -dr.srd424 commentedon Jul 28, 2019
@andersk - I have hacked up your --experimental-remote-ip option for my own use case - please see attached patch (sorry, I'm a dinosaur who doesn't understand git)
mosh.diff.txt
Basically allows setting a hostname/IP which is only used for the target of the UDP packets. This way I can use
ProxyJumpin .ssh/config, and then rush mosh like this:mosh --bind-server=<target LAN IP address>|any --experimental-remote-ip=gateway:<firewall external hostname> -p <port range opened on firewall for target host> <entry in .ssh/config with ProxyJump option>I guess it's a bodge, but it works for me. Setting port ranges up etc. would be tedious where there is a multitude of hosts on the LAN, but I only have one or two hosts I'm likely to want to jump to like this.
drewwells commentedon Apr 18, 2020
looks like you figured it out just fine. If you apply this diff on a fork, you can submit it as a PR to the repo: https://help.github.com/en/github/getting-started-with-github/fork-a-repo
xanoni commentedon Aug 17, 2021
What happened to this?
EDIT: found it ... switch is called
--experimental-remote-ip={proxy|local|remote}Can be closed I guess? Is it still experimental?
sixtyfive commentedon Apr 18, 2022
It seems despite the above patch file, seamlessly switching from SSH to mosh by just saying
mosh some-serverwhich has aProxyCommandin ~/.ssh/config, still doesn't work. Are there any plans for fixing that? Is it even conceptually possible to ever have it work in a transparent fashion? My guess is there's a lot of people who can only / want to access a lot of hosts viaProxyCommand.nostikj commentedon Oct 22, 2022
The only thing that stops
--experimental-remote-ip=localfrom working with ProxyCommands in ~/.ssh/config is the line$userhost = "$user$ip";in /usr/bin/mosh around line 340. Comment that line out and ssh will be called with the original command-line argument. The mosh command will still resolve the host to an IP address locally, so you have to ensure that for wherever your ssh startsmosh-server, that the UDP packets frommosh-clientget there too, e.g. NAT at the firewall. Any chance to have the remote-ip 'local' option without the change to $userhost?cgull commentedon Oct 26, 2022
The reason Mosh uses "$user$ip" there is to ensure that both ssh and mosh talk to the same address, in the case where the hostname has multiple A/AAAA records or round-robin config or dynamically changing results. So, your proposed fix breaks a significant use case.
cgull commentedon Oct 28, 2022
...but Mosh could certainly add another variation on
--experimental-remote-ipto handle this particular use case.moshcould also usessh -Gto automatically detect an already-existingProxyCommandand change its strategy. That starts to get a bit complex though, and could be surprising and confusing. This idea also applies toRemoteCommanddiscussed here and #1175.oliv5 commentedon Jan 29, 2024
I know I'm late but I'd like to share a small script made recently to use ssh config ProxyCommand with mosh. It relies on socat to relay mosh data via UDP: your computer <-> relay <-> mosh server
script is here: https://github.com/oliv5/profile/blob/master/bin/profile/mosh-proxy.sh
Requirements:
What is does:
From there, mosh is up and running
Cons:
sixtyfive commentedon Jan 29, 2024
That's pretty cool and definitely worth a shot on a private server. Thank you for sharing!
[hack] Attempt using `ProxyCommand` during local mosh-server IP disco…
[hack] Attempt using `ProxyCommand` during local mosh-server IP disco…
[hack] Attempt using `ProxyCommand` during local mosh-server IP disco…
[hack] Attempt using `ProxyCommand` during local mosh-server IP disco…