Convenient file access on remote hosts

    On Habré, a lot of developers work with files on remote hosts. The IDE, the debugger, everything you need is launched on the local machine, and the files themselves are located on the server, where they are regularly backed up, auto-build, tested, and so on. Very convenient working option. Yes, I use it myself.
    The only problem that is inconvenient is that the files are still located on another computer, and access to them is not as simple and transparent as to “your own” ones.
    Under the cut - how to make such access as convenient as possible. So that it starts automatically when it is necessary, and most importantly - that it does not start when it is not necessary!

    Of all the options for remote access to files: SSHFS, NFS and Windows share - SMB (don't forget it by night), I chose SSHFS for the following reasons:
    - Maximum ease of configuration
    - It works almost everywhere, does not require the installation of tricky components on the server
    - Maximum security: client, server and transmitted data.

    Installing on Ubuntu is as simple as possible:
    $ sudo apt-get install sshfs
    It will automatically install fuse-utils and libfuse2 as dependencies.
    The fuse group should also be created and the loading of the fuse module will be written in modconf.d - however, it depends on your distribution, you may have to check it and fix it by hand.
    For Windows users, there is a samurai development of Dokan SSHFS . Download Dokan lib, then dokan sshfs. This thing works, but I say right away - I practically did not work with it, maybe during long-term operation some problems will come out.

    Now all this can be easily mounted with a console call:
    $ sshfs username@server.ru:/home/user mount-point/

    But this is not the most convenient use case - you need to mount a folder every time in the console, of course, you can write all this in / ets / fstab, but we have a better option - Automount FUSE . You can install again from the repositories:
    $ sudo apt-get install afuse

    After that, it is enough to start it with the necessary parameters:
    afuse -o mount_template="sshfs %r:/ %m" -o unmount_template="fusermount -u -z %m" ~/sshfs/
    After which, all calls to files and folders in the ~ / sshfs / folder will cause the corresponding folder to be mounted in ~ / sshfs /. After which the call will go further to the remote host. The most important thing for us - everything happens absolutely transparently when you first access the desired folder from any program .
    For instance:ls ~/sshfs/tmpvar@foobarhost.comat first it will slow down a little, then it will show the contents of the root folder / server foobarhost.com. Of course, it is not always convenient to write the full address and access parameters to the server tmpvar@foobarhost.com: 22, so we will transfer them to the ssh access settings.

    cat ~ / .ssh / config 
    Host file-storage
    	Hostname filestorage.server.ru
    	Port 2222 # a non-standard port can be used on the server
    	HostKeyAlias ​​fs 
    	User admin
    


    Now we can access our server using the short name fs in both ssh and sshfs:
    > ssh fs # go to the server - no extra settings are needed
    > ls ~ / sshfs / fs # look at the server’s root folder.

    If you mount the folder for the first time, then you will see a password entry window for access to the specified server. Once the connection is established, the window will no longer bother you. As you already understood - you can easily tell the window not to get out - by making authorization on the server by key.

    So what have we got?
    And we got a cool thing - transparent SSHFS-mounted folders on demand. This means that:
    - mounting is done automatically when needed. For example, in the morning I turn on my laptop and open Eclipse with the current project. As soon as it starts, it immediately opens the old files from the server, while the folder is mounted and all the hidden mechanics occur. But I, as a user, are no longer interested in it - I launch a program and work in it.
    - but there are things much more interesting: mounting does not start when it is not necessary! For example, you are sitting in an Internet cafe or in the country on a gprs modem. Tell me, do you really need all folders from the working server to be connected when starting the laptop? As a result, the download lasted half an hour and showed a mountain of errors? This is the main advantage over prescribing the settings in / etc / fstab - when not necessary, the system does not bother you. :)

    But a person quickly gets used to all the good. I dragged myself from this system for exactly 2 weeks - and then I started to forget corny to run afuse :). As a result, I had to make a small script that will do this for me:
    $ cat ~ / bin / afuse.sh 
    #! / bin / sh
    if [! -z `ls -d / tmp / afuse- * 2> / dev / null`]; then
      echo 'Afuse is already running'; 
    else
      / usr / bin / afuse -o mount_template = "sshfs% r: /% m" -o unmount_template = "fusermount -u -z% m" ~ / sshfs /
    fi
    

    and added it to autorun in GNOME.

    Now the system has become perfect :)

    Also popular now: