Skip to content

vfs/shell: add MC_SSH_COMMAND and MC_SSH environment variable support#5080

Open
melnikovsky wants to merge 1 commit intoMidnightCommander:masterfrom
melnikovsky:feature/mc-ssh-command
Open

vfs/shell: add MC_SSH_COMMAND and MC_SSH environment variable support#5080
melnikovsky wants to merge 1 commit intoMidnightCommander:masterfrom
melnikovsky:feature/mc-ssh-command

Conversation

@melnikovsky
Copy link
Copy Markdown

Add support for MC_SSH_COMMAND and MC_SSH environment variables (analogous to GIT_SSH and GIT_SSH_COMMAND). Enables custom wrapper usage, particularly autossh

MC_SSH_COMMAND, if set, is invoked via 'sh -c' so that it can contain
options and arguments.  MC's generated flags (compression, port, login
name, hostname, remote command) are passed as positional parameters and
are available as "$@" inside the command string, mirroring the
GIT_SSH_COMMAND convention.

MC_SSH, if set, replaces the 'ssh' binary with a plain executable path
and no shell interpretation, analogous to GIT_SSH.  MC_SSH_COMMAND
takes precedence over MC_SSH.

Examples:
  MC_SSH_COMMAND='ssh -i ~/.ssh/mykey' mc
  MC_SSH_COMMAND='ssh -o ProxyJump=bastion -o StrictHostKeyChecking=no' mc
  MC_SSH=/usr/local/bin/myssh mc

Both variables are ignored for rsh connections (port == SHELL_FLAG_RSH).
@zyv
Copy link
Copy Markdown
Member

zyv commented Apr 6, 2026

I don't quite see how it properly fixes #2256:

My understanding is that the pipe to ssh command closes and we don't reopen it properly. Therefore, I thought that all that is needed to get it fixed is to try to re-open the pipe if it's closed. If autossh closes the connection (for whatever reason), then the same will happen with it as well.

Why try to solve this problem by introducing support for an external ssh command? Is it not possible to just attempt to re-open the pipe? Are there any other advantages that an external command brings?

@melnikovsky
Copy link
Copy Markdown
Author

Admittedly, this PR does not fix #2256 but kind of resolves it. A proper fix would require going deeper in the mc codebase, which I tried to avoid.

Instead of implementing reopen (how many attempts?) and proper cleanup afterward - which is arguably even more important, since currently one has to restart mc to reconnect - I suggest offloading this responsibility to an external tool. autossh already does a decent job of re-establishing connection (up to AUTOSSH_MAXSTART times) and keeping the pipe alive. Works for me...

Speaking of "other advantages", this approach could potentially allow unifying ssh/rsh code paths. That is assuming rsh is still relevant nowadays (I think the last time I used it was ~20 years ago)...

@zyv
Copy link
Copy Markdown
Member

zyv commented Apr 6, 2026

Alright, thanks for the explanation. I understand now why you submitted this PR, but hopefully you can also see the problem I have with it as a maintainer.

I can take it in to introduce a new (so far undocumented) way to swap out the default ssh command. Without documentation, it will be undiscoverable. With documentation, less so, but nevertheless, it will have to be discovered and configured by every user who wishes to have it. On top of that, you'd have to have an external tool (autossh) installed and working. All in all, terrible user experience for some new code pathways and extra documentation.

At the same time, one could just put the following in the ~.ssh/config file to the same effect:

Host *
  ServerAliveInterval 60

This is how I solve the problem for me at the moment.

Also, I think a minimal halfway proper implementation is rather simple. All you have to do is to check if the pipe is alive in the VFS command thing, and if not, close VFS with an error. That's it. The user will be able to reconnect manually afterwards without having to restart mc. If you want to make it nicer, then you can try to re-open the pipe at this point once, and if it doesn't work, then see above.

I think this will be perfect in terms of user experience, will work out of the box for everyone without any external tools, and should not be that hard to implement. I wish you could look into that as an alternative to your current solution.

@melnikovsky
Copy link
Copy Markdown
Author

At the same time, one could just put the following in the ~.ssh/config file to the same effect:

Host *
  ServerAliveInterval 60

This is how I solve the problem for me at the moment.
... All you have to do is to check if the pipe is alive in the VFS command thing, and if not, close VFS with an error.

Probably I am missing something important. My ssh setup does have finite ServerAliveInterval and the connection does die after a while. But I can not reconnect to the same host without restarting mc. How should I?

I would not bother fixing anything if I were able to simply reconnect!

@zyv
Copy link
Copy Markdown
Member

zyv commented Apr 9, 2026

Probably I am missing something important. My ssh setup does have finite ServerAliveInterval and the connection does die after a while.

Well, it doesn't for me. It might die for you though. Why not? Consider genuine connection loss after ServerAliveInterval is exceeded, because exactly at this moment you happened to have a hiccup with your internet connection.

But I can not reconnect to the same host without restarting mc. How should I?

You can't. That's a bug. It seems that you have misinterpreted my statement. What I was saying is that fixing this bug so that you are able to reconnect (or even try to reconnect once automatically) is not too hard, instead of adding a workaround of questionable usability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

shell link via ssh fails to restore after connection lost

2 participants