You may already be familiar with NFS and Samba for sharing files over a network. While these are both great distributed filesystem solutions, they require extra configuration and setup overhead in order to get them to work. If you want quick and easy access to a remote filesystem then SSHFS may be your best shot.
SSHFS (Secure SHell FileSystem) is a file system for Linux capable of operating on files on a remote computer using just a secure shell login. It is based on sftp (SSH File Transfer Protocol). Setup is easy on the server side, since most servers support SSH out of the box there is nothing to do. On the client side, mounting the filesystem is as easy as logging in with SSH. The end user can seamlessly and securely interact with remote files as if they were local to your machine.
Advantages of SSHFS over NFS/Samba:
- Utilizes SSH and is therefore very secure.
- Allows secure access to remote filesystems outside of your local network.
- Requires no special configuration on the server side.
Disadvantages:
- Slightly slower, although the difference is fairly small.
- Does not show filesystem usage statistics.
- Requires a user account on the server side.
- Not a true distributed file system, single point to point sharing.
Setup
The first step is to install SSHFS.
In Ubuntu:
$ sudo apt-get install sshfs
or in Gentoo:
$ sudo emerge -av sshfs-fuse
Create the mount point on your local machine. This is where you are going to access the remote filesystem.
$ sudo mkdir /mnt/share
Your user must have permission to access this mountpoint.
$ sudo chown ryan /mnt/share
Start Sharing
Now use the sshfs command to mount the remote filesystem. If the username is different on the server you are connecting, use the “username@host:” format, otherwise you can simply specify “host:”.
$ sshfs ryan@fileserver:/remote/share /mnt/share
If you are not using keys with SSH you will be prompted for a password.
ryan@fileserver's password:
Once you are finished you can easily unmount the filesystem.
as regular user:
$ fusermount -u /mnt/share
or as root:
$ sudo umount /mnt/share
Configuration
You can add an entry for this share to /etc/fstab to make the mounting process more seamless.
sshfs#ryan@fileserver:/remote/share /mnt/share fuse user,noauto 0 0
user – allow any user to mount this share.
noauto – stop the shared directory from being automatically mounted at startup.
If you want it automatically mounted, ensure that your SSH configuration uses keys and not passwords so it doesn’t ask for a password at startup. Once keys are in use you can safely remove the noauto option.
With fstab updated you can now mount the share as a normal user with this simple mount command. Again, if ssh is configured to use passwords you will still be prompted for one.
$ mount /mnt/share