Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have connected one of my production server folders with my "ssh account" server via sshfs.

I often need to find out pwd (or realpath) of the file on the original (production) server, but the pwd returns the path to file on the connected ("ssh account") server.

Is there a way to get original path?


Edit: more about this: I am using this to link my ssh server to multiple different SFTP servers/shared hosting.

so, if i do this (on some of the servers): sshfs user@domain:/ mount_point, then I am connected to my FTP account, which acts as root for my user, but is more along something like /var/www/clients/c/a/account/pub/ and that's the path I need –

sshfs user@domain:/ mount_point # mount SFTP acc.
cd mount_point
XXXX file.ext # get back: /var/www/clients/…/pub/file.ext
pwd file.ext # returns /ssh-server/path/mount_point/file.ext

Hope it makes sense now.


share|improve this question
What do you get from readlink -f <file> ? – Xyon Nov 14 '12 at 12:12
/ssh-account/path/to/file… the wrong one – Adam Kiss Nov 14 '12 at 13:08
If your sshd on the server is run in a chroot such that its / is not the actual / on the server and you want that actual /, there is no way to get the actual / on the server via ssh. You could, however, still define an alias as suggested in my answer and simply replace the /var/www/mywebsite accordingly (for example, use /var/www/clients/c/a/account/pub or so). In order not to define multiple aliases, you could parse the output of mount, decide where this actually points to using the username given to sshfs and then build the output accordingly. – Claudius Nov 14 '12 at 13:20
@Claudius wow, I hoped there is something like pwd --see-through-every-mount-and-symlink :D – Adam Kiss Nov 14 '12 at 13:22
pwd --blatantly-defy-chroot --ignore-filesystem-restrictions --find-absolutely-the-absolute --accept-no-substitutes --never-surrender ? – Xyon Nov 15 '12 at 7:17

You can get the path mounted via sshfs from the output of mount (or simply remember it). Then you can either

  • build an alias that takes a path on the client, removes the mountpoint of the SSHfs and prepends the original name of the directory on the server. For example, if you mount the remote directory /var/www/mywebsite locally to /home/test/website (sshfs server:/var/www/mywebsite /home/test/website), then the following will work:

    $ alias mpwd="pwd | sed 's|^/home/test/website|/var/www/mywebsite|'"
    $ pwd
    $ mpwd
    $ cd gallery
    $ mpwd
    $ cd /etc
    $ mpwd

    Note how non-matching directories (/etc) are not mangled.

  • mount the remove directory at the same location locally, that is, execute sshfs for example like: sshfs server:/var/www/mywebsite /var/www/mywebsite. The directory obviously has to exist locally for that to work.

share|improve this answer
Thanks! This unfortunately won't work for me (I'm connecting to multiple servers… read more about my situation in the edited OP) – Adam Kiss Nov 14 '12 at 13:13

Don't you know the path that is exported/mounted on your local machine? Wouldn't all paths under mount point be relative to the "exported" path?

sshfs [user@]host:[dir] mountpoint [options]

so if you are in [dir]/foo/bar then the pwd on the real host is /[dir]/foo/bar.

If you are mounting you home folder (default if no [dir]), and it is in a "custom" place, then finding out your home path may be done like so:

$ ssh flode echo \$HOME

...then whatever I have at mountpoint/moo/hei is actually in /home/pvv/d/rakhmato/moo/hei

If the [dir] you are mounting is an alias to another folder (mount -o bind), then you can locate the original path by looking at the /etc/mtab file. Something like:

$ ssh localhost mount |grep '/media.*bind'
/mnt/media/media on /media type none (rw,bind)
/mnt/media/root-extension/usr/share/doc on /usr/share/doc type none (rw,bind)

...where localhost is the name of your server and '/media' is the [dir] that you mount via sshfs.

share|improve this answer
I don't have ssh access to [some] of the production servers, and some are just a shared hosting, actually (and I'm using SFTP to connect to them). I'll probably just write to support. Thank you though :) – Adam Kiss Nov 14 '12 at 13:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .