Introduction
Often, it may be inconvenient to work locally on some files that are only on your Cloud Server, to which you must necessarily connect remotely.
Fortunately, there is a way to access a folder on your server (or the whole machine) as if it were present locally on your computer: SSHFS (SSH Filesystem), a tool that allows you to mount the filesystem of the remote machine on your computer through an SSH connection.
In this tutorial, you will learn how to use SSHFS to access remote network drives and directories on your Ubuntu 18.04 server via SSH.
Installing SSHFS
SSHFS is a tool that can be installed directly through the official Ubuntu repositories. Its advantage over other file systems is also that it does not require any additional configuration.
First, update the dependencies via the following command:
sudo apt-get update
To install SSHFS, just type:
sudo apt-get install sshfs
Temporarily mounting a remote folder
The first thing to do is to create a directory where to mount it. Then, type:
sudo mkdir /mnt/folder_name
N.B. 'folder_name' can be replaced with another name of your choice, but remember to replace it in all the future instructions where 'folder_name' recurs.
At this point, connect to the remote folder using SSH. To do so, use the following command:
sudo sshfs -o allow_other,default_permissions user@server_ip:/path/to/folder /mnt/folder_name
Pay attention when replacing:
- 'user' with the name of the user that you want to access the server with;
- 'server_ip' with the ip of your server (or domain that points it);
- 'root / my_server_folder' with the path of the remote folder you want to connect to;
- '/ mnt / folder_name' with the path of your previously created local folder.
In this case, a specific folder of a server is being connected. However connecting directly to the root folder by specifying '/' instead of '/ root / my_server_folder' is also possible.
As soon as the command is run, if provided, you will be requested to enter the password for the specified user, precisely because it is an SSH connection.
If, on the contrary, the authorization SSH key to access the server is needed, instead of executing the command shown above, proceed as follows:
sudo sshfs -o allow_other,default_permissions,IdentityFile=~/.ssh/id_rsa user@server_ip:/ /mnt/folder_name
At this point, as you can imagine, some kind of continuous synchronization takes place between your computer and the one just associated. Although it may seem that you are working locally, you are actually operating directly on the disk of your Cloud Server.
In the created directory (/ mnt / server_folder), there can be seen the same files present on the associated server, and more precisely, in the path of the linked directory. Try to create a file on your computer in the folder where you connected the server to. The newly generated file will also be detected on the remote machine.
This happens because despite having generated the file in a local path of your computer, it has actually been transferred in real time also to the connected server. The fact that it can be seen "locally" is due to the mounting operation with SSHFS.
N.B. Remember that, if the server is turned off or restarted, the procedure to connect the folder locally again has to be repeated.
Disconnecting a remote folder
When a folder mounted locally on your server is no longer needed, perform the unmounting by typing:
sudo umount /mnt/server_folder
Permanently mounting a remote folder
To avoid doing the mounting operation again when the server is disconnected, SSHFS provides the possibility to mount a directory permanently, ifa configuration file is modified.
Then, type:
sudo nano /etc/fstab
Scroll the file to the bottom and then paste the following text:
sshfs#user@server_ip:/path/to/mount /mnt/server_folder
As before, replace the 'user', 'server_ip', and 'server_folder' parameters. Also replace 'path / to / mount' with the path of the folder to connect to from your local machine.
After the modification, save the file (CTRL + X / Y / Enter) and, if necessary, restart.
Although this operation is very convenient, it also entails a considerable disadvantage. In this way there will be a direct channel between your machine and the remote one. If the server were to suffer some kind of attack, it would be possible for the attackers to also access your local computer. So, carry out this type of permanent assembly, unless strictly necessary, is not recommended.
Conclusions
In this tutorial you learned how to use SSHFS to mount remote directories via SSH. Mounting a remote machine locally can be very useful in several scenarios.
For example, if you have a VPS that hosts your websites, thanks to SSHFS the code of the website pages can be directly modified with your IDE, without having to perform continuous synchronizations or copies. In fact, one would operate on these files just as if they were saved locally on one's own machine, with considerable advantages in terms of rapid modification and updating.