PDA

View Full Version : Easy sshfs tutorial: mounting remote server folder as local folder



Fli
02-10-2014, 06:46 PM
Having 2 PCs: Linux workstation with graphical interface AND external Linux server located in LAN or in different country for data storage.
My workstation is lightweight, no big data stored i want to be able to play music, videos, open documents from external backup server like they are on my local PC. Thanks to software SSHFS i can achieve this. It will "mount" external server folder like an local folder. Downside: you need fast internet and low latency to work quickly. In my case im connecting via Tor network and playing mp3s, without any issues. but accessing the remote folder is slow (high latency..?)

What you will learn:
- how to mount an external server folder as a local computer/server folder via SSH (secure shell), when mounted one can open external server documents, play music, videos like local files. Also this way easy backups can be made.

Here is how to do it in Linux via SSHFS (there is Windows ver. too. google: sshfs windows ; an alternative to SSHFS is NFS, here is NFS benchmark (http://prokop.uek.krakow.pl/projects/fs_benchmark.html) and here tutorial (http://internetlifeforum.com/linux-forums/2491-serving-big-data-remote-server-local-server/))

NOTE!!! here is a newer sshfs tutorial which is probably more simple/clean --> click here (https://internetlifeforum.com/linux-forums/8355-easy-sshfs-tutorial-mounting-remote-server-folder-local-folder-version-2-a/)

Starting on the client computer (not the storage server):

debian type linux:

apt-get install sshfs

redhat type linux:

yum install sshfs

THIS STEP MAY NOT BE REQUIRED: sshfs using fuse to mount remote server directory as local, add your workstation pc username to fuse like:

adduser amnesia fuse
(amnesia is my home PC linux username in this tutorial, add yours)

THIS STEP MAY NOT BE REQUIRED:

sudo gpasswd -a amnesia fuse;
(again replace amnesia by your username)

to read more about just installed SSHFS, do command:

man sshfs

To setup the whole thing, first i create new folder on local PC and remote server. So lets do on local PC first...


mkdir /home/amnesia/remotebckp

then i login to my remote backup server via SSH (add "-p PORTNUMBER" after ssh if you have custom SSH port):


ssh myusername@mybakcupserverIPorhostname

and i create a new folder where backups will be stored, example:

mkdir /home/remotebckp

then i exit remote server ssh connection typing:

exit

So i have folders created on both PCs so i can run SSHFS program to mount remote server folder as an local folder on my home PC so i can work with remote files like they are stored on my local PC..

From my workstation home linux PC i connect my backup server via SSH (im not loged in as root user on my workstation) and mount remote directory to my local pc

sshfs remoteserverusername@remoteserverip:/home/remotebckp /home/amnesia/remotebckp -o idmap=user,reconnect,ServerAliveInterval=15,Server AliveCountMax=3,port=22
(sshfs login@ip:/backupserver/directory /localpc/directory) - VIOLET color font you should change

If it succeeded, i can now see remote server folder mounted as local directory. If i delete any files from that folder, it will get deleted on the backup server!!!! there are no 2 data locations, only 1 (im working remotelly with backup server data)

To unmount folder, do:

fusermount -u /localpc/folder

To mount remote folder without asking for password:
try to add: echo "yourexternalserverpassword" | before: sshfs -o idmap......... PS: its a security risk. Another possibility is setting up ssh access key for password-less access between two servers. Here are the commands to be made on the remote server which holds the data we want to mount to local home computer: (1) ssh-keygen -t rsa (2) ssh-copy-id user@local_server_ip_featuring_ssh_access (or google: setup ssh key password less access)

Automount SSHFS on boot:


USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY /LOCAL/MOUNTPOINT fuse.sshfs defaults,_netdev 0 0

https://wiki.archlinux.org/index.php/Sshfs#On_boot

PS: mount directory manually at least once so ssh key is added into .ssh/known_hosts so auto mounting @boot time will work

Troubleshooting

read: Connection reset by peer OR ssh_exchange_identification: Connection closed by remote host
-> make sure your server which folder you trying to mount is running properly, you may try to access it, restart it. If there is any failure with start or it cant mount (returns other error messages, try to paste error message on this webpage along with commands you did).