Bash – Executing a remote command

awsbashlinuxremotessh

  1. I can't edit the .bashrc of the fs I'm connecting to

  2. I have to use the ~/.ssh/config file

Here is my current config:

Host my-ssh
HostName <ip>
User <user>
IdentityFile <file location>
Compression yes
RemoteCommand cd <path to folder>

When I run ssh my-ssh nothing happens. The connection seems to automatically close. If I remove the RemoteCommand line it connects without an issue.

Is this something on the server config? It's an EC2 instance, either CentOS or RHEL and bash is the shell.

Best Answer

If you specify a remote command, then the ssh connection is going to close as soon as the remote command exits. A cd command will exit almost immediately.

A common way to do what you want is:

RemoteCommand cd /some/path && bash

(Substitute your desired shell in place of "bash"). This cd's to the path and then invokes a subshell if the cd operation succeeded. The ssh connection will close when the subshell exits.

You will also want to force ssh to allocate a PTY for the session:

RequestTTY yes

If you don't, then ssh won't request one by default, and you'll get a non-interactive shell session. Notably, you won't get a command prompt.

Related Question