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
Related Posts:
- Artificial Intelligence Wants Lower Pricing on LowEndBox’s Black Friday - November 26, 2024
- Cheater Pants! Some Providers are Posting Black Friday Deals Early…and You’ll Love Them - November 25, 2024
- LowEndBoxTV: AMD Shootout: Ryzen vs. Epyc – Which is Right For You? - November 24, 2024
Some of the commands run off the screen, so cant be copy pasted :(
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.
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.
So sorry about that. Let me talk with Jon B about maybe fixing up the template.