I'm trying to get ssh
to automatically change to a particular directory when I log in. I tried to get that behaviour working using the following directives in ~/.ssh/config
:
Host example.net
LocalCommand "cd web"
but whenever I log in, I see the following:
/bin/bash: cd web: No such file or directory
although though there is definitely a web
folder in my home directory. Even using an absolute path gives the same message. To be clear, if I type cd web
after logging in I get to the right folder.
What am I missing here?
EDIT:
Different combinations of quotes/absolute paths give different error messages:
LocalCommand "cd web"
/bin/bash: cd web: No such file or directory
LocalCommand cd web
/bin/bash: line 0: cd: web: No such file or directory
LocalCommand cd /home/gareth/web
/bin/bash: line 0: cd: /home/gareth/web: Input/output error
This makes me think that the quotes shouldn't be there, and that there's another error happening.
This works:
To create a directory if it doesn't exist:
If you don't add
bash
to the end of path then you exit after thecd
comand runs. And If you don't add--login
then your~/.profile
isn't sourced.~/.bash_profile
cd /path/to/your/destination
/path/to/your/destination
cd is a shell builtin. LocalCommand is executed as:What you're looking to do can't really be accomplished via SSH; you need to modify the shell in some way, e.g. viabashrc/bash_profile
.<Editing almost a decade later...>
LocalCommand
isn't what you want, anyway. That's run on your machine.You want
RemoteCommand
. Something like this worked for me:Have you enabled this directive in your ssh config?
The default for that is
no
, in which case yourLocalCommand
directive would be ignored.Alternatively, have you tried adding the command to your
.bashrc
file?More robust solution for the case when you need to ssh and override default .bashrc file. This is useful in cases when remote server environment has multiple docroots and used by multiple users.
More detailed:
.bashrc_tmp
file in/tmp
directory on the remote server with a contentcd /path/to/dir
bash
with config file specified in step 1. This allows to switch to desired dir and suppress default.bashrc
.alias
, so on ssh client command prompt it can be called asssh_myserver
So I have searched a lot of websites (stackoverflow, this one) but did not find what I wanted. I ended up creating my own shell script. I am pasting it here. Also you can copy it a directory in the $PATH.
So after this. I do:
Note: You may need to log out and log in again after changing the path variable
In your
~/.ssh/config
:At the end of your
~/.bashrc
:I tried doing this using variable passing, including with an exported variable, but these commands get run in different shells.
When testing what you were trying to do I didn't get an error when I used the unquoted absolute path, by the way. I added
; pwd
at the end of the command and the correct directory is displayed, however the directory I end up in is~
. There are nocd
commands in my shell startup files. I tried putting a differentcd somedir; pwd
in~/.ssh/rc
(with the other still in place). Theconfig
command is executed before the motd is issued and therc
command is issued afterwards, but before the shell startup files are sourced. So again, it's happening in a different shell.Try the passing-by-file technique and let me know if it works for you.
Have you tried it without the quotes around it? The only examples I've seen don't have them so with them it could be trying to execute cd\ web as a command.
You could also try this, with the option
-t
:Try using
RemoteCommand
instead, e.g.