2

I have a .ssh config file on my MacBook for connecting to a remote Ubuntu server. The config file looks like this:

Host remote_computer
    HostName 1.2.3.4
    Port 22
    User remote_username
    IdentityFile ~/.ssh/id_rsa
    RemoteCommand echo "Hello, World!"

Here's what happens when I try to connect:

local_computer:/ local_username$ ssh remote_computer
Hello, World!
local_computer:/ local_username$ 

The RemoteCommand runs successfully, but then the connection closes immediately after that. This happens with any RemoteCommand I try, but if I remove RemoteCommand from my config file, the connection stays open:

local_computer:/ local_username$ ssh remote_computer
remote_username@remote_computer:~$ 

Why does the connection close after successfully running the RemoteCommand from the config file, and what can I do to prevent that?

3
  • 1
    What's your intention, if not to run a command on the remote side? Do you want the remote session to always run something during login?
    – Jeff Schaller
    Jun 14, 2019 at 18:51
  • 4
    From man ssh: "If a command is specified, it is executed on the remote host instead of a login shell.", which is essentially what this configuration entry is. Jun 14, 2019 at 18:51
  • Thanks for the clarifications. I didn't make the connection between RemoteCommand and what @user4556274 pointed out. I think I have a better understanding of the purpose of RemoteCommand now.
    – user357910
    Jun 14, 2019 at 19:06

1 Answer 1

4

force a TTY and enter a shell:

Host remote_computer
    HostName 1.2.3.4
    Port 22
    User remote_username
    IdentityFile ~/.ssh/id_rsa
    RemoteCommand echo "Hello, World!" && bash
    RequestTTY force
5
  • Docs say it is the config equivalent of specifying -t, so it should work if -t does. computerhope.com/jargon/r/requesttty.htm Jun 14, 2019 at 18:54
  • Maybe "force" ? RequestTTY force Jun 14, 2019 at 18:55
  • 1
    Ah, then add a shell to the command (edited) Jun 14, 2019 at 18:58
  • 4
    Good idea, might be better to use ; instead of && though in case the first command is something different than echo and fails
    – jesse_b
    Jun 14, 2019 at 19:00
  • Thanks! This works really well, though I don't think force is necessary—I think the issue was not running bash. But with bash, RequestTTY yes works for me. Also, I followed @Jesse_b's advice and replaced the ampersands with a semicolon.
    – user357910
    Jun 14, 2019 at 19:11

You must log in to answer this question.