📜 ⬆️ ⬇️

Convenient access to files on remote hosts

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

Of all the options for remote access to files: SSHFS, NFS, and windshield - SMB (don't remember the night), I chose SSHFS for the following reasons:
- Maximum ease of setup
- Works almost everywhere, does not require installation of tricky components on the server
- Maximum security: client, server and transmitted data.

Installation on Ubuntu is as simple as possible:
$ sudo apt-get install sshfs
This will automatically install as dependencies fuse-utils and libfuse2.
A group of fuse should also be created and the loading of the fuse module will be registered in modconf.d - however, it depends on your distribution, it may have to be checked and corrected by hand.
For Windows users, there is the samurai development Dokan SSHFS . We swing Dokan lib, then dokan sshfs. This thing works, but I say right away - I practically didn’t work with it, maybe with 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 - every time you need to mount a folder in the console, of course, you can put 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 run it with the necessary parameters:
afuse -o mount_template="sshfs %r:/ %m" -o unmount_template="fusermount -u -z %m" ~/sshfs/
After that, all accesses to files and folders in the ~ / sshfs / folder will cause the corresponding folder to be mounted in ~ / sshfs /. After that, the call will go further to the remote host. The most important thing for us is that everything happens absolutely transparently when you first access the necessary folder from any program .
For example: ls ~/sshfs/tmpvar@foobarhost.com first ls ~/sshfs/tmpvar@foobarhost.com little, then 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 by 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 root folder of the server.

If you mount the folder for the first time, then you will have a window for entering a password to access the specified server. After the connection is established, the window will no longer bother you. As you already understood, you can easily tell the window not to come out - by making the authorization to the server using the key.

So what did we get?
And we got a cool thing - transparent SSHFS-mount folders on demand. This means that:
- mount 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 up, it immediately opens the old files from the server, while the folder is mounted and all the hidden mechanics happen. But I, as a user, are no longer interested in it - I run a program and work in it.
- but there are things much more interesting: the mount does not start when not necessary! For example, you are sitting in an internet cafe or in a country house on a gprs modem. Tell me, do you really need to connect all folders from the production server when starting the laptop? As a result, the download lasted half an hour and gave a mountain of errors? This is the main advantage over writing a project in / etc / fstab - when not needed, the system does not bother you. :)

But a person quickly gets used to everything good. I started from this system for exactly 2 weeks - and then I started to forget to start afuse :). As a result, I had to make a small script player, which will do it 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 :)

Source: https://habr.com/ru/post/52310/


All Articles