Effortless Remote File System Access: Mounting with SSHFS and fstab

Accessing remote files as if they were local is a common need for developers and system administrators. SSHFS provides a simple yet powerful solution, allowing you to mount a remote directory over an SSH connection. This guide covers both temporary and permanent mounting methods using SSHFS and the fstab configuration.

Getting Started with SSHFS

First, ensure SSHFS is installed on your local system.

sudo apt-get install sshfs

Temporary Mounting

For quick access, you can mount a remote directory directly from the command line.

1. Create a Mount Point

Start by creating a local directory where the remote content will be mounted.

sudo mkdir /mnt/my_remote_folder

2. Mount the Remote Directory

Now, connect to the remote folder using SSHFS. Replace remote_user, remote_host, and /remote/path with your specific details.

sudo sshfs -o allow_other,default_permissions remote_user@remote_host:/remote/path /mnt/my_remote_folder

3. Unmount a Remote Directory

When the remote folder is no longer needed, unmount it using the umount command.

sudo umount /mnt/my_remote_folder

Permanent Mounts with fstab

For directories you need to access regularly, configuring them in ~/etc/fstab ensures they are mounted automatically at boot. This file defines various file systems to be mounted on your system.

Open the ~/etc/fstab file with a text editor (e.g., sudo nano /etc/fstab) and add your mount entries.

Here's an example for an SSHFS mount:

# Example SSHFS fstab entry
remote_user@remote_host:/home/remote_user/shared/ /mnt/shared fuse.sshfs x-systemd.automount,x-systemd.requires=network-online.target,rw,_netdev,allow_other,identityfile=/home/local_user/.ssh/my_ssh_key.pem,uid=1001,gid=1001,user,idmap=user,transform_symlinks,exec   0   0

Explanation of SSHFS fstab options:

  • x-systemd.automount: Mounts the file system only when it's accessed.
  • x-systemd.requires=network-online.target: Ensures the network is available before attempting to mount.
  • _netdev: Indicates a network device, preventing the system from trying to mount it before the network is up.
  • allow_other: Allows non-root users to access the mounted file system.
  • identityfile=/home/local_user/.ssh/my_ssh_key.pem: Specifies the path to your SSH private key for authentication.
  • uid=1001,gid=1001: Sets the user and group ID for files on the mounted file system, ensuring proper permissions for local_user.
  • user: Allows any user to mount and unmount the file system.
  • idmap=user: Maps remote user IDs to local user IDs.
  • transform_symlinks: Converts absolute symlinks on the remote filesystem to relative ones on the local mount.
  • exec: Allows execution of binaries on the mounted filesystem.

Other Network File Systems

While this guide focuses on SSHFS, fstab is versatile for various network file systems. For instance, here's an example for mounting an S3 bucket using s3fs:

# Example S3FS fstab entry
bucket-assets /mnt/assets fuse.s3fs rw,_netdev,allow_other,use_path_request_style,use_cache=/tmp,umask=007,uid=1000,gid=1000,multireq_max=5,url=https://s3-eu-west-1.amazonaws.com   0 0

Further Reading