6

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.

asked Sep 25, 2009 at 15:32
2
  • ucar.edu/csac/userdocs/ucarsshhowto.html Commented Sep 25, 2009 at 15:43
  • 3
    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? Commented Sep 25, 2009 at 16:44

4 Answers 4

4

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
answered Sep 25, 2009 at 16:46
5
  • (see AcceptEnv in the sshd_config manpage: manpagez.com/man/5/sshd_config ) Commented Sep 25, 2009 at 16:54
  • This only works if you have root access to the machine you ssh to, doesn't it? Commented Aug 25, 2013 at 18:50
  • Yes, you typically need root access to edit sshd_config. Once that is done, though, root is not required. Commented Sep 3, 2013 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. Commented Apr 7, 2014 at 15:39
  • Sorry, but this does not seem to work any longer. Commented Feb 2, 2024 at 7:05
1

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

sshcd() {
 CWD=`pwd`
 HOME_BASED=${CWD#$HOME}
 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.

answered Sep 25, 2009 at 17:08
5
  • Will that give you a full shell, though? Will it execute your .bash_profile or .bash_rc upon logging in? Commented Sep 25, 2009 at 17:26
  • It does in my tests. At least my .bashrc gets sourced. Commented Sep 25, 2009 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). Commented Sep 25, 2009 at 18:13
  • Try changing "bash" to "bash -l" then. I'll edit my answer because that option won't do any harm anyway. Commented Sep 25, 2009 at 18:20
  • I tried this method with zsh and can confirm it works. Commented Aug 25, 2013 at 18:55
0

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
fi

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!

answered Sep 25, 2009 at 16:43
0

You can use the following:

#!/bin/sh
set -eu
gnome-terminal -x ssh -t "$@" "cd $PWD && $SHELL -il"
Gareth
19.1k15 gold badges60 silver badges70 bronze badges
answered Jul 7, 2010 at 18:06
1
  • If you're putting in code or commands, start the line with 4 spaces. Commented Jul 7, 2010 at 18:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.