Ssh to current working dir on server

From FVue
Jump to: navigation, search

Problem

When you're on an NFS share or a Subversion checkout, you may find yourself wanting to connect to the server, but to the same directory as you're currently in. Most often ssh moves you into your $HOME directory, as specified in your server shell startup script:

   client                             server
+--------------+                   +-----------+
| /mnt/proj/x  |<====== NFS ======>| ~/proj/x  |
+--------------+                   +-----------+
+--------------+                   +-----------+ 
| ~/proj/y     |<== SUBVERSION ===>| ~/proj/y  |
+--------------+                   +-----------+
      -----                            -----
        |                                |
        v                                ^
        |                                |
        +------>------ SSH ------>-------+
          problem: SSH connects you to ~

Solution

Within bash, define this alias s to connect to your server – replace hostname with yours:

# Ssh to server
# If directory [/mnt]$PWD exists on server as [$HOME]$PWD, cd to it
alias s='ssh -t <i>hostname</i> "test -d \"${PWD/#\/mnt/$HOME}\" && cd \"${PWD/#\/mnt/$HOME}\"; exec bash --login"'

A drawback of this solution is that you might miss a message like this:

Last login: Fri Dec 28 12:03:55 2007 from 10.0.0.1

which might give you a clue if your system is compromised.

Journal

Giving yourself a quieter SSH login
Detailed explanation of .hushlogin file and alternatives

Comments

blog comments powered by Disqus