Linux – Keeping working directory across ssh

linuxssh

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.

Best Answer

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
fi
Related Question