I often want to ssh into a different server, but retain my working directory. (All our servers have the same NFS and thus the same directory path usually exists on all servers.) Is it possible to do this?
If you can't change the remote servers's configuration and use key-based SSH logins with an ssh-agent (thus sparing you to have to enter your password or key passphrase every time), another hackish approach:
On your local machine, create a wrapper script which is in your path (eg. /usr/local/bin/cdssh)
I personally don't like to
alias ssh
. The fewest number of characters I found to do the job is :If the remote server has been preconfigured (in sshd_config) with
And you include
as the last line in the remote servers' ~/.bashrc, you can connect with
It's a workaround, but it'll...work.
I found a nice solution on superuser.com which is much like @adaptr's solution but without manually setting the environment variable.
By creating an alias for
ssh
you can set that variable automatically:Configure ssh in
~/.ssh/config
to send theSSH_PWD
variable:For a single host add this:
Or for all hosts add this to the bottom of the file:
And on the remote server in your
.profile
or.bashrc
:If you can't change the remote servers's configuration and use key-based SSH logins with an
ssh-agent
(thus sparing you to have to enter your password or key passphrase every time), another hackish approach:On your local machine, create a wrapper script which is in your path (eg.
/usr/local/bin/cdssh
)On the remote machine, add the following part to your
~/.bashrc
or~/.profile
:Obviously, this is a wild hack, but it works.