Editing Website Files with SSHFS

Editing Website Files with SSHFS

The server’s files can be mounted to your local filesystem using SSHFS. This allows you to use any text/file editor on your computer to modify files and allows you to drag and drop files to and from the server.

Before you begin, you should have already added your local user’s SSH key each user on the server for the sites you wish to access. It is important that you mount each site using the system user associated with it. Mounting the files with root or using a different site’s user will result in file permissions issues. If you have not yet done this, you can follow this guide to do so. If you do not know which system user is associated with your site, you can use this command:

sw nginx list

Step 1. Install SSHFS:

OSX:

On macs this is through osxfuse. There are two things to install to get this to work, the first is the .dmg file for Fuse.

Once Fuse is installed, install the pkg file for sshfs.

Linux:

This is usually installed by default. However, if missing, it can usually be installed with one of these lines depending on your distro:

RHEL/CentOS/Fedora:

sudo yum install fuse-sshfs

Debian/Ubuntu:

sudo apt install sshfs

Step 2. Create Mount Point

As long as you have already set up SSH access for the server user you are connecting to, you can now create a folder on your local computer and mount the server’s folder there. I want to mount my folders in a VPS folder inside my documents folder, but you can choose any empty folder to mount to. If you do not have your folder created, you can create it in your file explorer or with a terminal command like this:

mkdir -p ~/Documents/VPS/example_username

Step 3. Mount the Remote Folder

Now we can mount the remote data to a local folder like this: (note that this is one line)

sshfs example_username@example.com:/home/example_username/ ~/Documents/VPS/example_username/

The “:/home/example_username/” part is the server’s path for the mount point.

Step 4 (once editing is complete). Unmount the Remote Folder

Once we are done, we can unmount the folder with this command:

umount ~/Documents/VPS/example_username/

Note that it is spelled umount and not unmount.

Step 5 (Optional). Create Scripts to Easily Mount and Unmount All Folders

Since mounting and unmounting may be done often and since you may want to be able to mount several locations at once, let’s make a new terminal command for this. To do that, open a text editor to a new filename:

nano /usr/local/bin/mount_vps

Like usual in a bash script, add this text to the top of the file:

#!/bin/bash

Then, add each “sshfs” mount command from step 3, each on a new line. Once you are done, save the file and mark the script as executable. To save, hit Ctrl + O (yes, Ctrl… even on a mac) and then Enter/Return to confirm the save path. Then hit Ctrl + X to exit. To mark the script as executable run this command:

chmod +x /usr/local/bin/mount_vps

Now let’s create the matching command to unmount the folder, adding the same bash line at the start of the file:

nano /usr/local/bin/umount_vps

Now add each command from step 4. Save, exit and don’t forget to mark the script as executable:

chmod +x /usr/local/bin/umount_vps

We can now mount and unmount all the VPS drives at once with no password prompts with the following two commands:

mount_vps
umount_vps

 

Leave a Reply

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