How to Mount Remote Directories in Linux with SSHFS

Sshfs Featured

There’s certainly no shortage of solutions when it comes to syncing files between two devices. While Google Drive and Microsoft OneDrive may not give you official Linux clients, NextCloud and others do. But maybe you don’t trust a big corporation to store your files. Or, maybe, you want more security than NextCloud offers. With all the components that go into building something like NextCloud, chances are it has more undiscovered security holes than simpler solutions.

Why Use SSHFS?

From the end-user perspective, this is very easy to use, clean and simple. It’s also incredibly secure since it relies on the battle-hardened and proven OpenSSH server. Encryption is also top notch, so you can rest assured that no one can steal your files while they are in transit. A correctly configured up-to-date server that only listens for SSH connections is usually impossible to crack for all but the most skilled attackers (think NSA, security specialists, etc.). And they probably don’t want your files.

Why You Wouldn’t Want to Use SSHFS

If you want the best possible transfer speed, you might want to use other solutions such as NFS. SSHFS is not the slowest if your Internet connection is of high quality, but it’s not the fastest either. And if you want to transfer directories that contain hundreds of small files, it becomes horrible. Also, if you want to fine-tune file sharing settings, based on individual users or other factors, you might want to use other software.

To summarize, if all you need is an easy, secure way to synchronize a remote directory with a local one and aren’t in a huge hurry, you will probably be satisfied with this solution.

Install SSHFS

On Arch Linux based installations, use this command:

sudo pacman -S sshfs

If you’re on a Fedora-based distro, use:

sudo dnf install sshfs

On Debian, Ubuntu and family, use:

sudo apt install sshfs

For those of you that use OpenSUSE, enter this command:

sudo zypper install sshfs

Server Configuration

If you rent a server or VPS, the OpenSSH server daemon is already configured. Follow the steps recommended by your cloud service provider to configure a regular user (non-root). Some let you do this directly from their web control panel and even let you import the public key to allow SSH access. In this case, generate the key pairs locally, with the ssh-keygen command. After, import the public key from “/home/your_username/.ssh/id_rsa.pub.”

If the cloud provider doesn’t offer a tool to easily import SSH public keys, do it manually. At the very least, disallow root login and disable password logins. Use SSH keys exclusively instead: they are impossible to brute-force, as is the case with passwords.

If you want to sync files between two computers at home, just treat one as the server (install openssh-server package and configure) and the other one as the client. The same steps apply.

Mount a Remote Directory Locally with SSHFS

First, create a directory that will be synced with the remote side.

mkdir $HOME/sshfs

Next, mount the remote directory locally through SSHFS. Replace “user” with the actual username created on your server and “203.0.113.1” with the actual IP address of your remote instance.

sshfs user@203.0.113.1:/home/user $HOME/sshfs

Of course, if you don’t want to sync the entire home directory of the user on the server side, just replace “/home/user” with “/home/user/some_other_directory” after you create it on the server.

When you want to unmount, use this command:

cd && fusermount -u $HOME/sshfs

Conclusion

If you want a directory to permanently sync to the remote side, add a command like sshfs user@203.0.113.1:/home/user $HOME/sshfs in your autostart manager. Each graphical manager has a different autostart configuration manager, so consult your desktop environment help manual(s). Some sources recommend adding an entry to “/etc/fstab,” but we advise you to avoid that, as failure to mount the directory may result in your system failing to boot entirely.

Hopefully this covers all your needs. But, if it doesn’t, you can read about more command line options in the online SSHFS manual.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Alexandru Andrei

Fell in love with computers when he was four years old. 27 years later, the passion is still burning, fueling constant learning. Spends most of his time in terminal windows and SSH sessions, managing Linux desktops and servers.