LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Easy File Sharing With sshfs

Tags: , , Date/Time: July 19, 2020 @ 8:58 am, by raindog308

Easy File Sharing with sshfs on your VPS Server

scp, sftp, and other protocols are useful for one-time file transfers, but what if you want to have a permanent connection that allows you to treat a remote filesystem as local? You could look at nfs, but nfs was never designed for WAN usage and requires extra protocols (IPSEC, Kerneros, etc.) to encrypt in-flight data.

There are cloud solutions such as Dropbox, but the simplest solution is probably sshfs. sshfs is a technology that allows you to mount a remote filesystem and treat it as local storage. You can cp, rm, mv, etc. just as you would with a local filesystem.

In this tutorial we’ll setup an sshfs mount.

Installation and Setup

I’ll be using two servers, named server1.lowend.party (in Fremont, CA) and server2.lowend.party (in Dallas, TX). Both are running Debian 10.

After imaging, installing sshfs is as simple as

sudo apt-get -y install sshfs

On both servers, I’ll create a user called raindog308, and be careful to make sure the UIDs were the same on both sides.

root@server1:~# useradd -u 1001 -m -s /bin/bash raindog308
root@server1:~# passwd raindog308
New password: 
Retype new password: 
passwd: password updated successfully
root@server1:~#

I also added raindog308 to the sudo group:

root@server1:~# usermod -G sudo raindog308

I’m going to use a passwordless ssh key for authentication, which I create via ssh-keygen. On server2, I’ll switch to the raindog308 user and create the key:

root@server2:~# su - raindog308
raindog308@server2:~$ ssh-keygen -f /home/raindog308/.ssh/sshfs-key -t ed25519
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/raindog308/.ssh/sshfs-key.
Your public key has been saved in /home/raindog308/.ssh/sshfs-key.pub.
The key fingerprint is:
SHA256:D3EuLxSdHJy+lLWEgvII94eL7tYu91dRkCqR91YxZss raindog308@server2.lowend.party
The key's randomart image is:
+--[ED25519 256]--+
| . o.o.o=. |
| . o . +o=o++oo |
| o = .o==* +E |
| . + o== = |
| . oSo.o . |
| . .. =. . |
| . . . o. |
| + o .. |
| o.+.... |
+----[SHA256]-----+

I’ll copy the ssh public key:

raindog308@server2:~$ cat .ssh/sshfs-key.pub 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOLc4JrHS1hqLhUGSfL+DQgyJ+cOq3WyN/ONCdEP7rmG raindog308@server2.lowend.party

Then on server1:

raindog308@server1:~$ cd
raindog308@server1:~$ mkdir .ssh
raindog308@server1:~$ echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOLc4JrHS1hqLhUGSf
L+DQgyJ+cOq3WyN/ONCdEP7rmG raindog308@server2.lowend.party" > .ssh/authorized_keys
raindog308@server1:~$ chmod 600 .ssh/authorized_keys 
raindog308@server1:~$ chmod 700 .ssh

On server1, raindog308 created a directory in his home directory called “shared”:

root@server1:~# su - raindog308
raindog308@server1:~$ cd
raindog308@server1:~$ mkdir shared

Next, I populated it with a test file:

raindog308@server1:~$ cat > ~/shared/test.txt
This is a test file. It is very exciting and is very important.
raindog308@server1:~$

On server2, I also created the directory ‘shared’ as raindog308:

raindog308@server2:~$ logout
root@server2:~# su - raindog308
raindog308@server2:~$ cd
raindog308@server2:~$ mkdir shared

Mounting sshfs

Now I’m ready to mount server1’s /home/raindog308/shared directory on server2.

raindog308@server2:~$ sshfs -o default_permissions,IdentityFile=/home/raindog308/.
ssh/sshfs-key raindog308@server1.lowend.party:/home/raindog308/shared /home/raindo
g308/shared
raindog308@server2:~$ ls -ld shared
drwxr-xr-x 1 raindog308 raindog308 4096 May 21 09:00 shared
raindog308@server2:~$ ls -l shared
total 4
-rw-r--r-- 1 raindog308 raindog308 65 May 21 09:00 test.txt

Looking at df and mount:

raindog308@server2:~$ df -h | grep shared
raindog308@server1.lowend.party:/home/raindog308/shared 25G 1.1G 22G 5% /home/raindog308/shared
raindog308@server2:~$ mount | grep shared
raindog308@server1.lowend.party:/home/raindog308/shared on /home/raindog308/shared type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=1001,group_id=1001,default_permissions)

Looking in shared:

raindog308@server2:~$ ls -l shared
total 4
-rw-r--r-- 1 raindog308 raindog308 65 May 21 09:00 test.txt
raindog308@server2:~$ cat shared/test.txt 
This is a test file. It is very exciting and is very important.
raindog308@server2:~$

Let’s create a file on server2:

raindog308@server2:~$ cat > shared/test2.txt
Life, liberty, and the pursuit of the $7 VPS.
raindog308@server2:~$ ls -l shared
total 8
-rw-r--r-- 1 raindog308 raindog308 46 May 21 09:13 test2.txt
-rw-r--r-- 1 raindog308 raindog308 65 May 21 09:00 test.txt
raindog308@server2:~$

And back on server1:

raindog308@server1:~$ ls -l ~/shared/
total 8
-rw-r--r-- 1 raindog308 raindog308 46 May 21 09:13 test2.txt
-rw-r--r-- 1 raindog308 raindog308 65 May 21 09:00 test.txt
raindog308@server1:~$ cat ~/shared/test2.txt 
Life, liberty, and the pursuit of the $7 VPS.

Permanently Mounting sshfs

If you want this mount to be automatically mounted at boot-time, you can add a line like this in /etc/fstab:

raindog308@server1.lowend.party:/home/raindog308/shared /home/raindog308/shared fuse.sshfs delay_connect,defaults,IdentityFile=/home/raindog308/.ssh/sshfs 0 0

Unmounting

Unmounting is as simple as

raindog308@server2:~$ sudo umount /home/raindog308/shared
[sudo] password for raindog308: 
raindog308@server2:~$ mount | grep shared

I'm Andrew, techno polymath and long-time LowEndTalk community Moderator. My technical interests include all things Unix, perl, python, shell scripting, and relational database systems. I enjoy writing technical articles here on LowEndBox to help people get more out of their VPSes.

4 Comments

  1. Simon:

    Some of the commands run off the screen, so cant be copy pasted :(

    July 19, 2020 @ 9:37 am | Reply
    • aaaaaaaaaaaaaaaaaa:

      Simon, You can select until the beginning of the next line.

      raindog308, change the “#content pre {overflow: hidden}” to “#content pre {overflow: auto}” in the css.

      July 19, 2020 @ 5:11 pm | Reply
      • Simon:

        Tried that, some of the end of the lines still appear to be missing.
        Sorted it tho, if you view the pages source code, you can see it in the raw HTML.

        July 19, 2020 @ 5:13 pm | Reply
        • raindog308:

          So sorry about that. Let me talk with Jon B about maybe fixing up the template.

          July 19, 2020 @ 6:17 pm | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *