Easy way to mount/unmount SFTP connections

As a web host, I have a need to access files on my server for a number of different accounts to make quick changes and move files around. It’s really handy to be able to mount a remote SFTP connection locally. If you have that need, I have a tutorial for you. :smile:

Install sshfs

sudo apt-get install sshfs

If it’s not already installed.

Local Folder Setup

You’ll want to create a folder somewhere for your mount folders. In my case, I’ll create a folder that matches the domain name I’m connecting to, and I store all of these in ~/www. So let’s say I own example.com and example2.com. I’ll create two directories, as follows:

  • ~/www/example.com
  • ~/www/example2.com

Set up keys

Generate a key if you don’t have one

Reference: https://help.ubuntu.com/community/SSH/OpenSSH/Keys

If you don’t have an RSA key set up, check out the above. In summary:

mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa

I chose to use no password because I consider my desktop secure. Note that the above is a one-time setup.

Copy the key to the remote connection

Now that you have a key, you need to copy it to your remote host to be able to log in without a password, using that key:

ssh-copy-id [email protected]

You’ll be prompted for the password. As it tells you, once you complete that process, you’ll be able to run ssh [email protected] and connect without a password prompt.

Create the .bash_aliases functions

Now we’ll create two functions in .bash_aliases so we can mount and unmount at will. So open ~/.bash_aliases in your editor of choice. I recommend a graphical editor such as pluma. (note: see here for an easier way to edit .bash_aliases)

Here’s the two functions for our two example domains:

	if [ "$1" == e -o "$1" == example.com ]; then
		sshfs [email protected]:/home/e/ ~/www/example.com/
	elif [ "$1" == e2 -o "$1" == example2.com ]; then
		sshfs [email protected]:/home/e2/ ~/www/example2.com/
        echo "Valid sites include:"
        echo "e, e2"
	if [ "$1" == e -o "$1" == example.com ]; then
		fusermount -u ~/www/example.com
	if [ "$1" == e2 -o "$1" == example2.com ]; then
		fusermount -u ~/www/example2.com
		mount | grep www

Let’s walk through this. The command names I’ve chosen are s to mount (because it was short for “site” in my mind. Choose something that works for you, but run which [command] to see if it’s already in use out there first) and u to unmount.

The if statement basically says that if the first parameter is “e” or “example.com”, then run the next line. I like to have the username and domain name as options in case I remember one but not the other :smile:

The sshfs line is set up as follows:
sshfs [username]@[domain.ext]:/path/on/remote/server ~/www/[localfolder]/
If you have a cPanel server, your remote path is /home/[username]/.

I like to add the echo'd lines at the bottom so if I run the command with no parameter (or an incorrect one), it gives me a list of what I’ve already set up. It just occurred to me while writing this that an alternative command for the else section that would be good would be ls ~/www/ so it lists the directories you’ve set up. I’ve just updated my alias to do this. :slight_smile:

The u command is similar. I copy the if line when I add it to s, then copy one of the other fusermount lines and update the folder.

The last bit is that mount command in the else section. It will list all mounted folders that have www in the path, which in my case is only the ones I mounted using the s command. So it helps me see what’s actually mounted if I run a bare u command.

Make sense?

I tried to keep this short, so I might have made it too short. Does everything make sense? If not, poke me and I’ll try to answer questions and edit this so that it makes more sense. :slight_smile:

1 Like