Using SSHFS to Mount Remote Directories
Updated by Linode Written by Linode
SSHFS (Secure Shell FileSystem), is a tool that allows users to securely access remote filesystems over the SSH protocol. This guide will get you started with SSHFS on your Linode. SSHFS can eliminate the need to use FTP/SFTP to transfer files to and from a remote server.
Before You Begin
This guide will assume you have two systems set up:
A remote system running Ubuntu 18.04 which will serve your files over SSH.
A client system which will connect to the remote server using SSHFS. This system also runs Ubuntu 18.04.
Limited Linux users (non-root
) with the same username should also exist on both systems. If you have not already set up a limited user, review the How to Secure your Server guide.
The username for this limited user is assumed to be example_user
. Replace all instances of example_user
in this guide with your limited user’s name. As well, the IP address of the remote system is assumed to be 192.0.2.4
, so replace all instances of this IP with your remote system’s address.
Notesshfs
can be installed on any Linode distribution, so you can adapt this guide if you are not using Ubuntu.
Install SSHFS
Log in to your client system and update your packages:
apt-get update && apt-get upgrade
Install SSHFS:
apt-get install sshfs
Note
Thesshfs
package is available with every Linux package manager. Use the commands specific to your distribution if you are not using Debian or Ubuntu.
Setting Up your Linux Client
In order to mount file systems using SSHFS from a normal user account, you’ll need to add the user to the fuse
group first.
NoteIf you are unfamiliar with users, groups, and file permissions, visit the Users and Groups guide for a brief introduction.
To check if the
fuse
group exists run:cat /etc/group | grep 'fuse'
If the group exists, execute the following command with
sudo
, substituting your user account name in place ofexample_user
:sudo usermod -a -G fuse example_user
If the group does not exist it has to be created and the user added to the
fuse
group:sudo groupadd fuse sudo usermod -a -G fuse example_user
Log out from the client system and log back in to activate the group membership.
Mounting the Remote File System
You can use the command sshfs
to mount a remote filesystem. The syntax for mounting a filesystem with sshfs
is:
sshfs [user@]host:[directory] mountpoint [options]
NoteYou can read more aboutsshfs
in the sshfs manual.
Create a directory as a destination for the mounted folder.
mkdir sshfs-dir
Mount the home directory of the remote system’s user to the new directory on your client system:
sshfs example_user@192.0.2.4:/home/example_user sshfs-dir
List the contents of the mounted directory. You should see the content of the folder that was mounted on the remote system:
ls -al sshfs-dir
To unmount the filesystem, use the
umount
command:umount sshfs-dir
Persistent Mounts
To keep your server’s directory mounted on your system through reboots, create a persistent mount. This is accomplished by updating your system’s /etc/fstab
file.
Set Up Key-Based Authentication for SSH
When setting up a mount listed in /etc/fstab
, your client system will not be able to accept a password for the SSH connection. Instead, you can use public/private keypairs to authenticate with the remote server. This section describes how to create a keypair if you do not already have one.
CautionThis command will overwrite an existing RSA key pair, potentially locking you out of other systems.
If you’ve already created a key pair, skip this step. To check for existing keys, run ls ~/.ssh/id_rsa*.
If you accidentally lock yourself out of your Linode, use Lish to update your authorized_keys file and regain SSH access.
Generate a keypair with the
ssh-keygen
command; accept the default values for the options it presents:ssh-keygen -t rsa
From the client system, copy your new public SSH key to the remote user’s
authorized_keys
file:ssh-copy-id -i ~/.ssh/id_rsa.pub example_user@192.0.2.4
Note
If your system is older, this file may be named
authorized_keys2
. Consult your Linode’s/etc/ssh/sshd_config
if you are unsure:grep authorized_keys /etc/ssh/sshd_config
At this point, you should be able to log into the remote server as
example_user
without entering a password. Confirm this:ssh example_user@192.0.2.4
Update fstab
On a new line, add a mount directive to your
/etc/fstab
file which matches the following syntax:- /etc/fstab
-
1
example_user@192.0.2.4:/home/example_user /home/example_user/sshfs-dir fuse.sshfs noauto,x-systemd.automount,_netdev,follow_symlinks,identityfile=/home/example_user/.ssh/id_rsa,allow_other,default_permissions,reconnect 0 0
Note
You will need to usesudo
privileges to edit this file from your limited user.Reboot your system. Then, list the contents of the mounted directory. You should see the content of the folder that was mounted on the remote system:
ls -al /home/example_user/sshfs-dir
Next Steps
After completing this guide you will be able to transfer files to a remote server from your client machine without using an FTP client. If you still want to learn how to use an FTP client, check out our guide: Transfer Files with FileZilla.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.