SSH – SSH Connection Closes Immediately After RemoteCommand

ssh

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?

Best Answer

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
Related Question