Mount Remote Directories with SSHFS: Code Directly on Your Server Like a Local Drive

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Struggle of Managing Files on Remote Servers

If you manage servers, you’re likely familiar with this scenario: a VPS located in Singapore with 50-70ms latency, and you need to urgently edit dozens of configuration files. Usually, you’d use vi or nano directly in the terminal. Alternatively, you might use FileZilla to download files, edit them, and upload them back.

The old terminal-based workflow is quite restrictive. You can’t leverage the power of IDEs like VS Code for fast refactoring. And FTP? It’s incredibly time-consuming. On the Ubuntu 22.04 production server I manage, manual uploading/downloading wastes about 20-30 minutes during every debug session, not to mention the risk of overwriting files with older versions.

Why Traditional Methods Are Frustrating

The problem lies in the fragmentation. When using scp or rsync, every time you change a single comma, you have to run a command to push the file. Repeating this hundreds of times a day is exhausting.

What about NFS or Samba? They are too complex to configure over the Internet. You’d have to open multiple firewall ports, inadvertently creating dangerous security vulnerabilities. You need something as secure as SSH and as convenient as an external hard drive.

A Quick Comparison of Popular Solutions

  • SCP/SFTP: Slow because it transfers files individually; doesn’t support opening files directly.
  • NFS/Samba: Good speed but a nightmare to configure, requiring a VPN for security.
  • Rsync: Better suited for scheduled backups than real-time work.

SSHFS – Turning Server Directories into Local Drives

SSHFS (SSH Filesystem) is a total game-changer. This tool is based on FUSE (Filesystem in Userspace), allowing you to mount a server directory to your local machine via SSH (default port 22).

Once mounted, the server directory appears as a normal drive. You can open it in VS Code, press Ctrl+S to save, and the file on the server is updated instantly. No uploading, no waiting.

1. Install SSHFS in 30 Seconds

Most Linux distributions have SSHFS available in their official repositories.

On Ubuntu/Debian:

sudo apt update && sudo apt install sshfs

On CentOS/RHEL/AlmaLinux:

sudo yum install epel-release
sudo yum install sshfs

2. Steps to Mount a Remote Directory

Suppose you have a server at IP 1.2.3.4 and want to mount the /var/www/html directory to your local machine at ~/server_data.

First, create the mount point:

mkdir -p ~/server_data

Proceed with the mount using the following command:

sshfs [email protected]:/var/www/html ~/server_data

The system will prompt for your SSH password. However, I recommend using an SSH Key for a faster and more secure connection.

3. Verification and Usage

Run ls ~/server_data to see the result. At this point, any drag-and-drop or file editing in ~/server_data is actually happening directly on the server.

4. Disconnecting (Unmount)

When you’re finished, disconnect to free up resources:

fusermount -u ~/server_data

Optimization Tips for a Smooth SSHFS Experience

Using default settings might feel a bit laggy on weak networks. Here are two tweaks I always use.

Enable Compression and Keep-Alive

Add compression and auto-reconnect options for unstable connections:

sshfs -o compression=yes -o reconnect -o ServerAliveInterval=15 [email protected]:/var/www/html ~/server_data

Auto-mount on Startup

If you don’t want to type commands every day, you can configure /etc/fstab. Note that you must use an SSH Key so the system can authenticate without asking for a password.

Add this line to the end of the /etc/fstab file:

[email protected]:/var/www/html /home/user/server_data fuse.sshfs _netdev,allow_other,IdentityFile=/home/user/.ssh/id_rsa 0 0

Pro tip: To make allow_other work, uncomment the user_allow_other line in the /etc/fuse.conf file.

Conclusion

SSHFS isn’t the primary choice for transferring tens of gigabytes (where rsync is still better). However, for editing code, reading logs, or managing daily configs, it is the champion of convenience.

Since I started using SSHFS, I’ve completely eliminated FTP software from my machine. My workflow has become seamless, allowing me to focus entirely on code logic instead of wasting energy on moving data.

Share: