Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large number of Linux machines all of which mount my home directory over NFS. If I'm in ~/foo/bar/baz I'd like to be able to ssh to another machine and automatically start using that as my working directory. There doesn't appear to be an easy way to do this; I can think of some hacky ones but would like to check before trying them.

share|improve this question
Joe, your profile page states that you are quitting from Su. May I ask how much longer we will have to live with comments like the above? – innaM Sep 25 '09 at 16:44
up vote 4 down vote accepted

Check out SendEnv (in ssh_config) and AcceptEnv (in sshd_config). You might be able to send PWD; though at the receiving end just getting PWD won't be enough to make the new shell start in the desired directory.

So you could do something like:

  1. put SendEnv SSH_PWD in your ssh_config and AcceptEnv SSH_PWD in your sshd_config.
  2. Add the following to your .profile or .bash_profile:


alias ssh='env SSH_PWD="$PWD" /bin/ssh'
if [ -n "$SSH_PWD" ]; then
   cd "$SSH_PWD"
   unset SSH_PWD
share|improve this answer
(see AcceptEnv in the sshd_config manpage: ) – BrianH Sep 25 '09 at 16:54
This only works if you have root access to the machine you ssh to, doesn't it? – Erik Aug 25 '13 at 18:50
Yes, you typically need root access to edit sshd_config. Once that is done, though, root is not required. – Heath Sep 3 '13 at 18:17
Change if [ -n "$SSH_PWD" ]; then into if [ -n "$SSH_PWD" ] && [ -d "$SSH_PWD" ]; then to only cd if the directory exists. – Koen. Apr 7 '14 at 15:39

How about this bash function (assuming that bash really is your shell):

sshcd() {
    ssh -t $1 "cd ~$HOME_BASED;bash -l"

You could then do sshcd foobar to ssh to foobar, change the directory there and run bash.

share|improve this answer
Will that give you a full shell, though? Will it execute your .bash_profile or .bash_rc upon logging in? – BrianH Sep 25 '09 at 17:26
It does in my tests. At least my .bashrc gets sourced. – innaM Sep 25 '09 at 17:54
Cool - I don't have a linux box around to test on. I tested on AIX/Unix, and it didn't work (The login and cd to the dir worked, but then I just got the $ prompt, not my usual prompt). – BrianH Sep 25 '09 at 18:13
Try changing "bash" to "bash -l" then. I'll edit my answer because that option won't do any harm anyway. – innaM Sep 25 '09 at 18:20
I tried this method with zsh and can confirm it works. – Erik Aug 25 '13 at 18:55

I don't know if this is considered "Hacky" or not, but here's an idea:

Since your homedir is shared across all servers, the same profile will be executed at login. You could have your .profile do something like this at the bottom:

CURR_DIR=$(cat ~/.current_dir)
if [[ -n $CURR_DIR ]] && [[ -d $CURR_DIR ]]; then
  cd $CURR_DIR

Then, alias your SSH command to run a script that will put your current dir into your ~/.current_dir file before running the SSH command, and remove the ~/.current_dir file once the SSH connection has been closed.

I have not tested this, but it could be a starting point.

Good luck!

share|improve this answer

You can use the following:

set -eu
gnome-terminal -x ssh -t "$@" "cd $PWD && $SHELL -il"
share|improve this answer
If you're putting in code or commands, start the line with 4 spaces. – random Jul 7 '10 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .