Skip to content

Instantly share code, notes, and snippets.

@0187773933
Last active August 14, 2020 08:21
Show Gist options
  • Select an option

  • Save 0187773933/5949c4b4e43e9e091d65f5884da68bc6 to your computer and use it in GitHub Desktop.

Select an option

Save 0187773933/5949c4b4e43e9e091d65f5884da68bc6 to your computer and use it in GitHub Desktop.
SSHF Mount Local Directory on Remote Cloud VPS via SSH Remote Port Forward
# 3.) Mount From Local to Remote
# This Gets Run on Local Raspberry Pi
# It Mounts the "Local" Source Directory to the "Remote" Destination Directory
# Local = Raspberry Pi
# Remote = Cloud VPS
# However, there is confusion
# Since we are "Reverse Port Forwarding" ,
# the "local" and "remote" terms get switched after the initial ssh command is run
# So for the "sshfs" command , whatever you get the point
==============================================================================
#!/bin/bash
# Reverse Binds 13317 on Localhost of Raspberry Pi to 22 on the Remote VPS
# Then Runs SSHFS Command to Mount Local Source Directory to Remote Destination Directory
# ================ Config Variables =========================
REMOTE_PORT_TO_BIND_TO=13317
REMOTE_USERNAME="morphs"
REMOTE_IP="XX.XX.XX.XX"
REMOTE_SSH_PRIVATE_KEY_PATH="/home/morphs/.ssh/linuxmisc3" # this is the path on raspberry pi
REMOTE_DIRECTORY="/home/morphs/RECEIVING/RaspiMountTest2"
LOCAL_SOURCE_DIRECTORY="/home/morphs/TMP2/MountTest"
LOCAL_USERNAME="morphs"
LOCAL_PORT_TO_BIND_FROM=22
LOCAL_SSH_PRIVATE_KEY_PATH="/home/morphs/.ssh/raspberrypichromecastbox" # this is the path on cloud vps
# ================ Config Variables =========================
_destination=localhost:$REMOTE_DIRECTORY
_sshfs_ssh_command="ssh -i $LOCAL_SSH_PRIVATE_KEY_PATH"
_sshfs_command="sudo umount -l $REMOTE_DIRECTORY || echo '' && \
sudo umount -f $REMOTE_DIRECTORY || echo '' && \
sudo sshfs -d -C -p $REMOTE_PORT_TO_BIND_TO \
-o idmap=user,nonempty \
-o debug,sshfs_debug,loglevel=debug \
-o reconnect,allow_other \
-o ServerAliveInterval=15 \
-o ssh_command=\"$_sshfs_ssh_command\" \
$LOCAL_USERNAME@127.0.0.1:$LOCAL_SOURCE_DIRECTORY \
$REMOTE_DIRECTORY"
ssh -vvv -R $REMOTE_PORT_TO_BIND_TO:localhost:$LOCAL_PORT_TO_BIND_FROM \
$REMOTE_USERNAME@$REMOTE_IP \
-o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o IdentitiesOnly=yes \
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o ExitOnForwardFailure=yes \
-o LogLevel=ERROR -F /dev/null \
-i $REMOTE_SSH_PRIVATE_KEY_PATH \
"$_sshfs_command"
==========================================================================
# 3.) Mount From Local to Remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment