How can I ssh directly to a particular directory?

Solution 1:

You can do the following:

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash --login"

This way, you will get a login shell right on the directory_wanted.


Explanation

-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services.

Multiple -t options force tty allocation, even if ssh has no local tty.

  • If you don't use -t then no prompt will appear.
  • If you don't add ; bash then the connection will get closed and return control to your local machine
  • If you don't add bash --login then it will not use your configs because its not a login shell

Solution 2:

You could add

cd /some/directory/somewhere/named/Foo

to your .bashrc file (or .profile or whatever you call it) at the other host. That way, no matter what you do or where you ssh from, whenever you log onto that server, it will cd to the proper directory for you, and all you have to do is use ssh like normal.

Of curse, rogeriopvl's solution works too, but it's a tad bit more verbose, and you have to remember to do it every time (unless you make an alias) so it seems a bit less "fun".

Solution 3:

My preferred approach is using the SSH config file (described below), but there are a few possible solutions depending on your usages.

Command Line Arguments

I think the best answer for this approach is christianbundy's reply to the accepted answer:

ssh -t example.com "cd /foo/bar; exec \$SHELL -l"

Using double quotes will allow you to use variables from your local machine, unless they are escaped (as $SHELL is here). Alternatively, you can use single quotes, and all of the variables you use will be the ones from the target machine:

ssh -t example.com 'cd /foo/bar; exec $SHELL -l'

Bash Function

You can simplify the command by wrapping it in a bash function. Let's say you just want to type this:

sshcd example.com /foo/bar

You can make this work by adding this to your ~/.bashrc:

sshcd () { ssh -t "$1" "cd \"$2\"; exec \$SHELL -l"; }

If you are using a variable that exists on the remote machine for the directory, be sure to escape it or put it in single quotes. For example, this will cd to the directory that is stored in the JBOSS_HOME variable on the remote machine:

sshcd example.com \$JBOSS_HOME

SSH Config File

If you'd like to see this behavior all the time for specific (or any) hosts with the normal ssh command without having to use extra command line arguments, you can set the RequestTTY and RemoteCommand options in your ssh config file.

For example, I'd like to type only this command:

ssh qaapps18

but want it to always behave like this command:

ssh -t qaapps18 'cd $JBOSS_HOME; exec $SHELL'

So I added this to my ~/.ssh/config file:

Host *apps*
    RequestTTY yes
    RemoteCommand cd $JBOSS_HOME; exec $SHELL

Now this rule applies to any host with "apps" in its hostname.

For more information, see http://man7.org/linux/man-pages/man5/ssh_config.5.html

Solution 4:

I've created a tool to SSH and CD into a server consecutively – aptly named sshcd. For the example you've given, you'd simply use:

sshcd somehost:/some/directory/somewhere/named/Foo

Let me know if you have any questions or problems!