How can I automatically change directory on ssh login?
cd is a shell builtin. LocalCommand is executed as:
/bin/sh -c <localcommand>
What you're looking to do can't really be accomplished via SSH; you need to modify the shell in some way, e.g. via bashrc/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:
Host example.net
RemoteCommand cd / && exec bash --login
RequestTTY yes
This works:
ssh server -t "cd /my/remote/directory; bash --login"
To create a directory if it doesn't exist:
ssh server -t "mkdir -p newfolder; cd ~/newfolder; pwd; bash --login"
If you don't add bash
to the end of path then you exit after the cd
comand runs. And If you don't add --login
then your ~/.profile
isn't sourced.
- Login to your box
- Edit
~/.bash_profile
- On the end of the file add
cd /path/to/your/destination
- Save it & exit
- Logout from the box
- Login again and then you should land on
/path/to/your/destination
Note (Dec 2021): As @Matthias points out on some machines you may need need to append your command to
~/.bashrc
instead of to~/.bash_profile
.