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.
-
ucar.edu/csac/userdocs/ucarsshhowto.htmljoe– joe2009年09月25日 15:43:29 +00:00Commented Sep 25, 2009 at 15:43
-
3Joe, 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– innaM2009年09月25日 16:44:18 +00:00Commented Sep 25, 2009 at 16:44
4 Answers 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:
- put
SendEnv SSH_PWDin yourssh_configandAcceptEnv SSH_PWDin yoursshd_config. - 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
-
(see AcceptEnv in the sshd_config manpage: manpagez.com/man/5/sshd_config )BrianH– BrianH2009年09月25日 16:54:16 +00:00Commented Sep 25, 2009 at 16:54
-
This only works if you have root access to the machine you ssh to, doesn't it?Nova– Nova2013年08月25日 18:50:28 +00:00Commented 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.Heath– Heath2013年09月03日 18:17:44 +00:00Commented Sep 3, 2013 at 18:17
-
Change
if [ -n "$SSH_PWD" ]; thenintoif [ -n "$SSH_PWD" ] && [ -d "$SSH_PWD" ]; thento onlycdif the directory exists.Koen.– Koen.2014年04月07日 15:39:22 +00:00Commented Apr 7, 2014 at 15:39 -
Sorry, but this does not seem to work any longer.Seamus– Seamus2024年02月02日 07:05:30 +00:00Commented Feb 2, 2024 at 7:05
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.
-
Will that give you a full shell, though? Will it execute your .bash_profile or .bash_rc upon logging in?BrianH– BrianH2009年09月25日 17:26:13 +00:00Commented Sep 25, 2009 at 17:26
-
It does in my tests. At least my .bashrc gets sourced.innaM– innaM2009年09月25日 17:54:19 +00:00Commented 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).BrianH– BrianH2009年09月25日 18:13:12 +00:00Commented 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.innaM– innaM2009年09月25日 18:20:10 +00:00Commented Sep 25, 2009 at 18:20
-
I tried this method with zsh and can confirm it works.Nova– Nova2013年08月25日 18:55:41 +00:00Commented Aug 25, 2013 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
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!
You can use the following:
#!/bin/sh
set -eu
gnome-terminal -x ssh -t "$@" "cd $PWD && $SHELL -il"
-
If you're putting in code or commands, start the line with 4 spaces.2010年07月07日 18:07:55 +00:00Commented Jul 7, 2010 at 18:07