Skip to content

Instantly share code, notes, and snippets.

@tano297
Last active June 12, 2023 11:26
Show Gist options
  • Save tano297/166f37389c275f3c090bdf688d3048df to your computer and use it in GitHub Desktop.
Save tano297/166f37389c275f3c090bdf688d3048df to your computer and use it in GitHub Desktop.
Mount and unmount remotes with sshfs
# remote mount functions
remote-mount()
{
# Mount a remote ssh root directory in /media/$USER/remotes/[remote alias]
# This function requires 1 argument, which is the ssh alias you made for the
# remote in your .ssh/config.
# If the directory is not created, it will try to, but this part is probably only
# going to be called the first time for each remote, unless you change the alias.
# To unmount, see remote-unmount().
# check that I am passing one argument (remote name)
if [ "$#" -ne 1 ];
then
echo "At least 1 parameter should be passed, with the name of the remote"
return 22 # invalid argument
fi
# check that the argument is a valid ssh target (check existance of root directory)
if ! ssh -q $1 [[ -d / ]];
then
echo "$1 is an invalid ssh target. Check your ~/.ssh/config"
return 22 # invalid argument
else echo "$1 is a valid ssh target"
fi
# verbose
echo "Trying to mount remote $1 in /media/$USER/remotes/$1"
# if directory doesnt exist, create it
if [ ! -d "/media/$USER/remotes/$1" ]
then
# create it
echo "Directory /media/$USER/remotes/$1 DOES NOT exist. Creating it"
mkdir -p /media/$USER/remotes/$1
# check if successful or exit
if [ -d "/media/$USER/remotes/$1" ]
then echo "Success creating directory!"
else echo "Failed creating directory. Maybe check permissions?"; return 2 # no such file
fi
else echo "Directory /media/$USER/remotes/$1 exists!"
fi
# Check if the directory is empty, otherwise mounting will fail
if [ "$(ls -A /media/$USER/remotes/$1)" ]
then
echo "Directory /media/$USER/remotes/$1 is not empty, mount will fail."
echo "This means it's either already mounted or used for something else."
echo "Exiting..."
return 39 # directory not empty
else echo "Directory is empty, everything ready for mount :)"
fi
# Mount
if sshfs -o reconnect $1:/ /media/$USER/remotes/$1/
then echo "Success mounting! Enjoy your remote data access."
else echo "Something went wrong with sshfs. I am not a general purpose AI agent, so you'll have to ask Andres..."
fi
}
remote-unmount()
{
# reverses the sshfs mount that was done with remote-mount()
# check that I am passing one argument (remote name)
if [ "$#" -ne 1 ];
then
echo "At least 1 parameter should be passed, with the name of the remote"
return 22 # invalid argument
fi
# check if it is effectively mounted
if grep -qs "/media/$USER/remotes/$1 " /proc/mounts
then echo "The remote is actually mounted. Proceeding with unmount"
else
echo "I can't find $1 in mounted volumes"
return 22
fi
echo "Trying to unmount remote $1"
if fusermount -u /media/$USER/remotes/$1
then echo "Successful unmount!"
else
UNMOUNT_ERROR_CODE=$?
echo "Something went wrong :/"
return $UNMOUNT_ERROR_CODE
fi
}
@tano297
Copy link
Author

tano297 commented Mar 18, 2020

This code adds two functions to mount a remote workstation using sshfs...

  • First step: Add this code to your ~/.bashrc
  • Second step: Mount or unmount!

Example ~/.ssh/config

Host workstation
  User andresworkstation # user in remote workstation
  Hostname 8.8.8.8 # ip of remote workstation
  Port 22 # port for ssh (can be different)

If it is behind a firewall, you need to add a ProxyJump

Host bastion # the computer that has ssh open to outside world and can bridge to your workstation
  User andresbastion # user in bastion
  Hostname 2.2.2.2 # ip of bastion
  Port 9999 # port of ssh in bastion (usually not 22)

Host workstation
  User andresworkstation # user in remote workstation
  Hostname 192.168.1.32 # static ip of remote workstation (could also be hostname.local if your bastion supports avahi resolution)
  Port 22 # port of 
  ProxyJum bastion

Mount root of workstation:

andreslocal@laptop:~$ remote-mount workstation
andreslocal@laptop:~$ ls /media/andreslocal/remotes/workstation
dev         initrd.img.old  media  root    software  var
bin                      etc         lib             mnt    run     srv       vmlinuz
boot                     home        lib32           opt    sbin    sys       vmlinuz.old
cache                    home_local  lib64           proc   shared  tmp
data                     initrd.img  lost+found      queue  snap    usr

Unmount:

andreslocal@laptop:~$ remote-unmount workstation

@tano297
Copy link
Author

tano297 commented Mar 18, 2020

PS: the reason to mount the root directory is completely personal, because I have multiple disks mounted on many different places with a weird personal preference configuration. If you feel more comfortable with it, you can replace the "/" paths in the mount, with the path to the remote home directory (/home/user/) or your path of preference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment