SSHFS itself is a file system in user space (FUSE) that uses the SSH File Transfer Protocol (SFTP) to mount a remote file system. The sshfs command is a client tool for using SSHFS to mount a remote file system from another server locally on your machine.

Note: User space (or userland) is all operating system code that runs outside of the Kernel.

While not as performant as a local file system, sshfs is an easy way to work with files on another computer using local programs and tools. Best of all, sshfs can be used by non-privileged users on the local machine (in fact, it is recommended to not be root or to elevate one’s privileges while using sshfs), and it requires no special software on the remote machine other than SSH with SFTP enabled—a common setup.

Installing sshfs

The sshfs tool is available from most distributions’ standard repositories and is most easily installed using that distribution’s package manager. For Fedora, installing sshfs is as easy as: dnf install fuse-sshfs. On Debian-based systems, the package is simply sshfs.

Mounting a remote file system

Once sshfs is installed, mounting a remote file system safely over SSH is simple. According to the sshfs manual page (man sshfs), the syntax for the command is: 

sshfs [user@]host:[dir] mountpoint [options]

That syntax should look familiar to anyone who has used SSH or SCP before.

Let’s take a look at sshfs in action. First, create (or select an existing) directory to use as a mount point. Note that the local user invoking the sshfs command must have write access to the mount point. As use of the root account is discouraged, that policy might make using ~/mnt preferable to /mnt. Then, it is just a matter of using the sshfs command to connect to the remote machine using your SSH credentials, and specifying which remote location to mount to the mount point:

# Create the mountpoint
[chris@marvin ~ ]$ mkdir ~/mnt

# Invoke SSHFS with your SSH credentials and the remote location to mount
[chris@marvin ~ ]$ sshfs chris@zaphod:/home/chris/Pictures ~/mnt

# Access the newly mounted filesystem
[chris@marvin ~ ]$ ls ~/mnt/
'Screenshot from 2019-11-17 19-34-34.png'

Note: Fun fact, sshfs doesn’t expand ~ on a remote machine to the user’s home directory.

The mounted file systems can be accessed and used the same way a local file system is, both from the command line or using other tools:

SSHFS example

When you’re done with the remote file system, unmount it with the fusermount command:

# Unmount the remote FS
[chris@marvin ~ ]$ fusermount -u ~/mnt

Mount options

In most cases, the default sshfs command can be used as-is, but sshfs is far from a simple command. There are tons of options and flags that cover a variety of situations, including SSH and SFTP options, compression, etc. Check out the sshfs manual (man sshfs) for more details on what is available.

One of the most useful options, however, is reconnect, which allows the client to re-establish the connection to the remote system it is interrupted:

# Use "-o reconect" to allow the client to reconnect
[chris@marvin ~ ]$ sshfs chris@zaphod:/home/chris/Pictures ~/mnt -o reconnect

Mounting a remote file system on startup

The sshfs command can also be used in the client system’s /etc/fstab file to automatically mount the remote file system. Use the normal fstab syntax, including any options required, and use sshfs in place of the file system type:

# file: /etc/fstab
chris@zaphod:/home/chris/Pictures /home/chris/mnt sshfs

Note that using sshfs with fstab file mounting the file system must be done as root, so appropriate adjustments need to be made for SSH credentials, and adequate precautions must be taken.

Transport endpoint is not connected

You might receive a Transport endpoint is not connected error when the connection is interrupted (say, if the client machine changes networks, or is suspended). In this case, unmount the file system with the fusermount command, as described above:

[chris@marvin ~ ]$ ls ~/mnt
ls: cannot access '/home/chris/mnt': Transport endpoint is not connected
[chris@marvin ~ ]$ fusermount -u ~/mnt

Hopefully, this article gave you the basics needed to work with sshfs. It is a great tool for working with remote file systems securely over an encrypted SSH connection while mounted locally on your client computer. And as mentioned, thanks to sshfs being a FUSE file system, even unprivileged users can use it without requiring an administrator to set it up.

Want to try out Red Hat Enterprise Linux? Download it now for free.


关于作者

Chris Collins is an SRE at Red Hat and a Community Moderator for Opensource.com. He is a container and container orchestration, DevOps, and automation evangelist, and will talk with anyone interested in those topics for far too long and with much enthusiasm.

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来