Ssh – How to configure SSH with a RemoteCommand only for interactive sessions (i.e. without command, or not sftp)

sftpsshssh-config

Currently I use Fish as my main shell on local and remote hosts.

I connect to remote hosts via ssh and sftp. I wanted to open or reuse a remote tmux whenever I connect, automatically, by default; so I added this to my ~/.ssh/config:

Host example.com
RemoteCommand tmux a; or tmux
RequestTTY yes

The problem is that now I cannot connect through sftp, nor can I run a direct command from my local CLI:

➤ ssh example.com ping localhost
Cannot execute command-line and remote command.

➤ sftp example.com
Cannot execute command-line and remote command.
Connection closed

So, my question is: How can I define a default command to be executed when opening a new interactive SSH session, but make it overridable?

Best Answer

Option 1 )

You can the option Match ( see man ssh_config )

Match Host example.com exec "test $_ = /usr/bin/ssh"
     RemoteCommand tmux a; or tmux
     RequestTTY yes

This will only differentiate difference between ssh & sftp

Option 2

You create some placeholder config for your diffe command , example :

Host tmux.example.com
  HostName example.com
  HostKeyAlias example.com
  RemoteCommand tmux a; or tmux
  RequestTTY yes

And after you can still use example.com for you sftp / ping usage .

Related Question