I have defined a ssh_config file with all the hosts to which I connect on a regular basis. I like to start/connect to a tmux session upon connection to the host, so I've added the line RemoteCommand tmux new -ADs remote in my config. The problem is that if at some point I want to use rsync over ssh (which I do every now and then), I have the following error:

Cannot execute command-line and remote command.

In order to solve it, I have to comment out the RemoteCommand line in my config file, and not forget to uncomment it afterwards, which is a bit annoying...

Potential solutions (undesirable or not working):

  • I have tried to use the flag -N which means "do not execute remote command" but the command hangs indefinitely.
  • I would prefer not to create alias hosts in my config file, because that means that the number of entries would increase by a factor of 2 for only one minor change.
  • I was not able to use Match Host to only execute a remote command when not using rsync

Anyone knows a configuration trick or a workaround that might help me in this case?

Thank you very much!


Don't put RemoteCommand in the configuration file. Having RemoteCommand is occasionally useful to define an alias for a host such that ssh myalias runs a specific command. It isn't useful in a general-purpose entry. As you've noticed, it prevents doing anything other than running that specific command: you can't use rsync, sftp, sshfs, or even run ssh myhost specific-command interactively.

If you want to default to running tmux when you connect to a host, there are two sensible solutions:

A Match host directive wouldn't help since you aren't interested in doing something differently based on the host name. I don't think the ssh client has a way to do things different depending on whether a command was passed to ssh. You can execute code with Match exec. I don't think there's a clean way to detect whether ssh was invoked with a command, but a dirty way might be good enough for you.

Host myhost
Match exec "ps -o args= $PPID | grep -v ' .* '"
    RemoteCommand if [ -t 0 ]; then exec tmux new -ADs remote; fi

If ssh was invoked with just a host name and no option, run tmux. If ssh was invoked with at least one option or with a command in addition to the host name, the RemoteCommand directive isn't applied. Also don't run tmux if the input doesn't come from a terminal (e.g. echo ls | ssh myhost). This should take care of most cases, erring on the side of not running tmux (e.g. ssh -L … myhost won't run tmux).

  • Thank you for your answer. 1st solution does not work for me because I don't want to automatically connect to tmux each time I use ssh (I need an override option on the client side). 2nd option: kinda works if I name the function ssh (otherwise, it defeats the purpose since I tend to forget tmux and regret). I guess this is my best alternative. The Match exec looked promising too but the provided example yields an error and putting the condition between quotes makes ssh always land in plain shell and not in a tmux session for some reason
    – qmeeus
    Jan 13 '21 at 11:50
  • @qmeeus Upon checking, the code run by Match exec doesn't have access to ssh's stdin and stdout (which does makes sense: you wouldn't want it to read from stdin!). But a different approach does work: inspect the command line of the ssh process. I've edited my answer with a tested exec directive. That being said, I still think the best approach is to start tmux on the server. That still gives you a bypass: it won't run tmux if you give an explicit command. If you want to run a login shell without tmux, run something like ssh -t myhost '. ~/.profile; bash' Jan 13 '21 at 12:14

Since OpenSSH 7.6, ssh has a RemoteCommand command line option. It can be used to override the corresponding directive in ssh_config. Setting it to none clears the RemoteCommand parameter.

Hence, you can use ssh -o RemoteCommand=none ... (or, possibly, in your case, rsync -e 'ssh -o RemoteCommand=none' ...) to let ssh ignore the remote command set in your configuration file.

There are at least a couple of (currently open) bug reports asking for the setting in ssh_config to be automatically overridden by the command specified on the command line (if any).

  • This can be combined like ProxyJump proxy -o RemoteCommand=none in ssh_config which is awesome! Mar 31 '21 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.