27

I'd like to set ssh_config so after just typing ssh my_hostname i end up in specific folder. Just like I would type cd /folder/another_one/much_much_deeper/.

How can i achieve that?

EDIT. It's have been marked as duplicate of "How to ssh into dir..." yet it is not my question.

I know i can execute any commands by tailing them to ssh command. My question is about /ssh_config file not the command.

9
  • I've clarified my question.
    – Kazz
    Mar 29, 2017 at 14:53
  • There's nothing about that in man ssh_config. The closest is LocalCommand but it cannot be used for interactive command.
    – lgeorget
    Mar 29, 2017 at 14:54
  • 1
    Please could you explain (in your question) why you feel the change must be made to ssh_config? There are probably better places to configure the starting directory, and it would be helpful to understand why there is this particular restriction on a possible solution.
    – roaima
    Mar 29, 2017 at 20:31
  • 1
    The fact "it can't be done in ssh_config" does not make this a duplicate of "do it in Bash". "You can't do that" is a perfectly valid answer. Oct 25, 2017 at 18:54
  • 1
    @terdon However the fact "it isn't possible" may change over time. Yes, it is possible now. Now I (as well as anyone in future coming to see this) need a new answer, and we get stuck here. If this is not re-opened, I imagine that the desired path is to create a new question (and answer it)? Oct 26, 2017 at 14:33

3 Answers 3

37

There wasn't a way to do that, until OpenSSH 7.6. From manual:

RemoteCommand

Specifies a command to execute on the remote machine after successfully connecting to the server. The command string extends to the end of the line, and is executed with the user's shell. Arguments to RemoteCommand accept the tokens described in the TOKENS section.

So now you can have

RemoteCommand cd /tmp && bash

It was introduced in this commit.

5
  • 16
    Add RequestTTY force if the remote command requires a TTY. For example: tmux new -As my-session Apr 13, 2018 at 17:20
  • 1
    This is the killer ssh option Thanks!
    – nhed
    Feb 6, 2019 at 3:49
  • 4
    This makes it impossible to run other commands, for example ssh my_hostname ls will fail with Cannot execute command-line and remote command.. It makes other tools that use ssh like git and rsync also fail.
    – remram
    Jul 25, 2019 at 22:40
  • 2
    FYI && doesn't work in Ubuntu 18.04
    – salotz
    May 8, 2020 at 19:48
  • 2
    you may want to use bash -l instead, so initialization files are read (such as .bash_profile) Dec 5, 2020 at 22:47
2

I'd like to set ssh_config so after just typing ssh my_hostname

It is not possible. There is no option to achieve that in ssh_config. The closest to it is setting up a bash alias or bash function, such as

myssh() {
    ssh -t $1 "cd /dir/; bash"
}
1

I responded on a different ticket before somoene had pointed out this was the original.

If using a mac, my recommendation is to use iTerm2 and create a profile. Put ssh my_hostname in the command line, and then add the command cd /folder/another_one/much_much_deeper/ in the send text at start field. When you connect to the sessions it will ssh to your hostname and cd to the directory in your case, or execute a command.

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.