For an easy way to obtain temporary access to a directory in a remote location using the sshfs command just type in your terminal:
sshfs remote_user@ip_address:/path_to_dir path_to_local_dir
Make sure you have have already created the local directory to mount the remote directory, that you have the credentials of the remote user and all necessary permissions.
For example:
sshfs admin@192.168.1.130:/media/backup ~/nfs
Now in your local file manager you will have access to the remote directory for the current session. Performance may be reduced with this method in contrast with other alternatives like a network file system setup but for managing small files it’s just perfect if you prefer to have a single secure connection to a remote location without having to use to much the terminal.
NOTE: If you are a local non root user, you need to make sure to create the folder where you want to mount the remote disk somewhere in your home folder so you can have access to it either with CLI or a file explorer.
To disconnect:
fusermount3 -u /path/to/mount/point
To add a persistence across reboots edit the /etc/fstab and add this line at the bottom.
user@192.168.100.10:/home/user/.local/bin /home/local_user/projects/bin fuse.sshfs noauto,x-systemd.automount,_netdev,reconnect,IdentityFile=/home/bee/.ssh/athena,allow_other,default_permissions 0 0
Persistence via systemd
Create a systemd service
First generate keys for root and send the pub key to the remote server. You need to log in manually the first time only to answer “yes” to the fingerprint verification prompt:
You also need to create a folder in /mnt/web.com (or anywhere you desire) to mount the remote folder
mkdir /mnt/web.com
ssh-keygen -t ed25519 -b 100 -f /root/.ssh/key
ssh-copy-id -i /root/.ssh/key.pub remoteadmin@192.168.10.100
ssh remoteadmin@192.168.10.100
Create the .mount file for systemd
sudo nano /etc/systemd/system/mnt-web.com.mount
add the following content:
[Unit]
Description=SSHFS Remote Mount to remote server web.com folder
#Documentation=man:sshfs(1)
[Mount]
What=admin@192.168.10.100:/var/www/web.com/ # Or the folder you want to land into.
Where=/mnt/web.com # A folder in your local system (local mount point)
Type=fuse.sshfs
Options=IdentityFile=/root/.ssh/key,allow_other,default_permissions,reconnect,_netdev
[Install]
WantedBy=multi-user.target
Create the .automount file
sudo vi /etc/systemd/system/mnt-web.com.automount
add the following content:
[Unit]
Description=SSHFS On-Demand Automount
[Automount]
Where=/mnt/web.com
[Install]
WantedBy=multi-user.target
Reload the daemon and restart services, then trigger the mount listing the local mount folder
systemctl daemon-reload
systemctl enable mnt-web.com.automount
systemctl start mnt-web.com.automount
# Trigger the mount
ls /mnt/web.com
At this point it should appear in dolphin or thunar.
Permission should be managed in the remote server if you want to access /etc or /var and edit files. If you have to edit files in var:
# Add admin to the web server group
sudo usermod -aG www-data admin
# Change ownership of the web folder so the group can write to it
sudo chown -R www-data:www-data /var/www
sudo chmod -R 775 /var/www/web.com