Menü schliessen
Created: January 9th 2025
Last updated: January 9th 2025
Categories: Common Web Development,  IT Development
Author: Tim Fürer

Linux: How to Mount SFTP File System

Tags:  FTP,  guide,  Linux,  SSH
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Using a nifty little program called SSHFS, one can mount SFTP (SSH/Secure File Transfer Protocol) file systems for convenient access. Here's how to do it.


Installation

To install SSHFS, run the following command:

apt install sshfs

Temporary Mount

We can use the sshfs command to temporarily mount an SFTP file system.

Command template:

sshfs user@host:/starting/directory/ -p 1234 -o IdentityFile=/path/to/key /mount/target/
  • "user" — SSH user name.
  • "host" — SSH server host; for example "example.com" or "8.5.12.16".
  • "/starting/directory/" — Path on the SFTP file system whereto start mounting from.
  • "-p 1234" — SSH server port.
  • "-o IdentityFile=/path/to/key" — Path to SSH key.
  • "/mount/target/" — Path to directory whereto mount SFTP file system to.

To unmount again, use:

umount /mount/target/

Mount on Startup

To mount an SFTP file system on startup, we can add an entry to the fstab (file systems table), which is located at "/etc/fstab"

Entry template:

user@host:/starting/directory/ /mount/target/ fuse.sshfs defaults,_netdev,IdentityFile=/path/to/key,port=1234,allow_other,nofail,reconnect,ConnectTimeout=5 0 0
  • "defaults" — Use standard mount options.
  • "_netdev" — Is network device; wait for network before mounting.
  • "allow_other" — Allow other users (than root) to access mounted file system.
  • "nofail" — Continue boot process if unable to connect.
  • "reconnect" — Automatically attempt reconnect if connection lost.
  • "ConnectTimeout=5" — Abort connection if taking longer than 5 seconds.

A prerequisite for allow_other to be available is that we add the line "user_allow_other" to the fuse.conf at "/etc/fuse.conf".

If you're looking for a more secure and controlled alternative to allow_other, you may define a user group instead (notice the "gid=foobar"):

user@host:/starting/directory/ /mount/target/ fuse.sshfs defaults,_netdev,IdentityFile=/path/to/key,port=1234,gid=foobar,nofail,reconnect,ConnectTimeout=5 0 0
  • "gid=foobar" — Allow access to mounted file system for group "foobar".