30

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.

Kazz
  • 403
  • I've clarified my question. – Kazz Mar 29 '17 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 '17 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. – Chris Davies Mar 29 '17 at 20:31
  • If there are other places please give some ideas. The end game is that i use ssh to dozens of servers and so far i manage to configure it so i just type ssh name and got used to it. It a few cases now i need to go to a very deep dir every single time. – Kazz Mar 30 '17 at 12:01
  • 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. – Franklin Yu Oct 25 '17 at 18:54
  • @FranklinYu and that is the answer it has received. Marking it as a duplicate means that the next person who wants to do this will be taken to a page that gives the right solution instead of one that simply states that the specific approach envisioned by the OP here isn't possible. – terdon Oct 26 '17 at 10:26
  • 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)? – Franklin Yu Oct 26 '17 at 14:33
  • @FranklinYu yes. Or editing the existing answer or even reopening this question at that point, but I doubt it will be an issue. But as a general rule, when someone asks "How can I use foo for bar", we prefer answering "you can use baz instead" over just saying "foo can't do that". In other words, if the OP has an idea which won't work, it is more useful to give a solution that works than to just say that this idea won't work. – terdon Oct 26 '17 at 15:28
  • Reopening since apparently this is now possible? (@FranklinYu) – Michael Mrozek Jan 15 '18 at 17:08

3 Answers3

39

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.

Franklin Yu
  • 1,237
  • 18
    Add RequestTTY force if the remote command requires a TTY. For example: tmux new -As my-session – Aaron Blenkush Apr 13 '18 at 17:20
  • 1
    This is the killer ssh option Thanks! – nhed Feb 06 '19 at 03:49
  • 6
    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 '19 at 22:40
  • 2
    FYI && doesn't work in Ubuntu 18.04 – salotz May 08 '20 at 19:48
  • 3
    you may want to use bash -l instead, so initialization files are read (such as .bash_profile) – igorsantos07 Dec 05 '20 at 22:47
  • @remram You can do ssh example.org -o RemoteCommand=none ls (I did not find none in the documentation for RemoteCommand but it works for me) and it should be possible to use something similar in GIT_SSH_COMMAND. This question has some other suggestions but ideally SSH would simply ignore RemoteCommand when another command was specified :-/ – Martin Oct 26 '23 at 13:53
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"
}
Jakuje
  • 21,357
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.

Govna
  • 318