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.
Linux – Keeping working directory across ssh
linuxssh
Related Solutions
I would give each use their own separate home directory. Use permissions to lock down settings for some applications and use hardlinks to make common settings.
I don't think you can get finer granularity than that. I imagine that applications that expect to be able to change (some of) their settings may fail in unexpected ways when users attempt to change something - so careful testing is needed.
It might be instructive to read what JWZ did. Though his use case is more extreme.
I have never tried, but I can think of something that may work: You do not let SSH start your login shell, but take matters in your own hands. You can tell SSH to run arbitrary commands. Instead of
ssh remote_host
you would run something along the lines of
ssh remote_host -t "
. /etc/profile;
. /etc/bash.bashrc;
DAT_ORIGIN_HOST=$(ifconfig eth0|grep -Po 't addr:\K[\d.]+') /bin/bash -l -i"
What this does is, it gives SSH something else to do instead of launching a login shell. That something is a string, which will be run as command, remotely. We use this to launch a shell in a very special manner: we give it an environmental variable DAT_ORIGIN_HOST
, which contains our ip on eth0 (you may need to change that).
The trick we perform is that we put the command to execute remotely in double qoutes "
. The double quotes (at least in Bash) mean, that BEFORE THE STRING IS PASSED TO SSH, our shell scans it and performs expansions/replacements, where appropriate. This means our shell will evaluate the `$(ifconfig ...) part, to our current ip address and pass ssh a string which contains the definition for an environmental variable with our local ip address.
Once logged in to the remote machine, echo $DAT_ORIGIN_HOST
should print your IP address.
To develop that call to SSH I shamelessly took from here for extracting the IP address and here for the -t and how to launch something interactive
Disclaimer: I am unsure about the -l
and -i
option to /bin/bash
. Maybe you need to omit them.
Best Answer
Check out SendEnv (in
ssh_config
) and AcceptEnv (insshd_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:
SendEnv SSH_PWD
in yourssh_config
andAcceptEnv SSH_PWD
in yoursshd_config
.: