How to mount a remote storage VPS to a compute one via SSHFS

189

A compute VPS may be powerful with high RAM and CPU, but sometimes it needs more disk space to do certain jobs. Fortunately, you can mount a remote storage VPS to it using SSHFS and use it as if it was a local drive. This tutorial will guide you to mount a remote storage VPS to a compute one using SSHFS properly.

Prerequisites

  • A compute VPS (usually, it’s a more powerful VPS than a storage one with higher RAM, more CPU power, and faster disk speeds but less disk space)
  • A storage VPS (preferably in the same data center as the compute one for best performance)

Installing SSHFS on the compute VPS

Install the SSHFS package on your compute VPS. If you’re using a non-Debian-based distro, make sure to install it properly from your distro’s repo.

sudo apt-get update
sudo apt-get install sshfs

Open the fuse configuration file located at /etc/fuse.conf.

sudo vi /etc/fuse.conf

Optional: Comment out the following line to allow other users to access a mounted fuse (recommended).

user_allow_other

Creating a mount point on both VPSs

Create a new directory at /mnt/storage on both compute VPS and storage one as a mount point or any location you prefer. We’ll use /mnt/storage in this tutorial. Change the ownership of the directory to your username as in the following. Do this on both VPSs.

sudo mkdir /mnt/storage
sudo chown -R user:user /mnt/storage

Generating an SSH key on the compute VPS

Generate an SSH key on your compute VPS without entering a passphrase. You can read more about generating an ed25519 SSH key in this tutorial. We do not use a passphrase for this to keep things simple to automatically connect to the storage VPS at reboot.

ssh-keygen -t ed25519 -C "you@youremail.com"

Copy the text inside the id_ed25519.pub file located at ~/.ssh/id_ed25519.pub. Use the cat command to get the text in the public key file.

cat .ssh/id_ed25519.pub

It will look something like this. Do not copy the text below because it’s just an example. Your public key is different.

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICe61E9qsJJxD6bks7cfJotRVvMcmWWlprP0evd9LKp0 you@youremail.com

Adding the public key on the storage VPS

Append the text copied from the public key to the authorized_keys file on the storage VPS located at ~/.ssh/authorized_keys in your home directory.

vi ~/.ssh/authorized_keys

Paste the text. Save changes and exit. Now both VPSs are ready to connect.

Editing the fstab file on the compute VPS

Open the fstab file located at /etc/fstab on the compute VPS.

sudo vi /etc/fstab

Append the following line to the fstab file so that the storage VPS will be automatically mounted to the compute one at reboot. Change the bold text to your own settings, such as username, remote host, and SSH port. Once done, save changes and exit.

remoteuser@remotehost:/mnt/storage /mnt/storage fuse.sshfs port=22,x-systemd.automount,_netdev,user,idmap=user,transform_symlinks,identityfile=/home/user/.ssh/id_ed25519,allow_other,reconnect,default_permissions,uid=1000,gid=1000 0 0

Mount the storage VPS to the compute one using the mount -a command.

sudo mount -a

Configuring a private network (Optional)

If a private network is provided in the same data center for both storage and compute VPSs, it’s highly recommended to connect them via a private network for the best local performance. Traffic in both directions isn’t counted either.

For example, given we have eth1 as the interface for the private network on both VPSs. You can append the following to the interfaces file located at /etc/network/interfaces on the compute VPS. The interface name and IP address may be different depending on your VPS provider.

auto eth1
iface eth1 inet static
    address 192.168.10.1/24

And do the same for the storage VPS but change the internal IP address to a new one. Note that it’s 192.168.10.2 in this example.

auto eth1
iface eth1 inet static
    address 192.168.10.2/24

Restart the networking service on both VPSs. Now you can connect them via a private network in the fstab file on the compute VPS using an internal IP.

sudo systemctl restart networking