[tip] in windows, use sshfs win to mount SFTP disk to the local, and conveniently manage the files in Linux

Time:2020-11-4

In the maintenance of Linux server, file management has always been a thorny problem: FTP / SMB / WebDAV installation steps are cumbersome, and the configuration of user rights is also more complex. The most important thing is that the above-mentioned transport protocols are not encrypted by default, and configuration encryption and other operations need to pay extra time and energy. Openssh’s SFTP can be used out of the box with encryption. However, SFTP is only a file transfer protocol. Under Linux, we can use sshfs with fuse to mount it as a disk. In windows, sshfs win also helps us achieve this function. This paper will explain the principle of sshfs win, installation, use and some details in the process of use.
This paper was originally published atUnnamed stationFrom the author himself to Zhihu, please indicate the original author’s blog address or this link, thank you!

Introduction to the principle of 0x01 sshfs win

The principle of sshfs win under windows is basically the same as that of sshfs under Linux, that is, they are all based onUser space file systemBased on the application.

Sshfs transforms SFTP into the interface of fuse, and fuse exposes this interface to the kernel as a file system; while sshfs win transforms SFTP into the interface provided by winfsp (Windows file system agent). Winfsp can do three things as follows:

  1. Register a device driver so that windows can mount it
  2. Create file, readfile, WriteFile and other APIs under NT kernel to manage read and write queues
  3. Call the fuse interface corresponding to the application program mounted on winfsp

The kernel extension and driver of windows are very complex, so developing kernel file system for windows needs more energy than developing kernel file system under Linux. As a bridge between fuse interface and windows file system (that is the origin of agent in the name), winfsp can reduce the file system development with more comprehensive and complete API and good compatibility with fuse The difficulty of hair.

The specific principle and mechanism of winfsp can refer to winfsp’sfileNow, let’s talk about how to install and use the winfsp dependent sshfs win.

Installation and use of 0x02 sshfs win

As mentioned above, sshfs win relies on winfsp, so we need to install winfsp first:https://github.com/billziss-gh/winfsp

After winfsp installation, we will install sshfs winhttps://github.com/billziss-gh/sshfs-win

After both are installed, click map network drive in Windows file explorer

[tip] in windows, use sshfs win to mount SFTP disk to the local, and conveniently manage the files in Linux

In the pop-up dialog box, we select the drive letter to be assigned, and then enter the following contents in the folder:

\\Sshfs \ \ user name @ IP address! Port number

[tip] in windows, use sshfs win to mount SFTP disk to the local, and conveniently manage the files in Linux

It should be noted that the port number is not what we usually use:, but!After clicking finish, enter the password corresponding to the user name to connect successfully.

Some details that need attention

Although the operation of sshfs win is relatively simple, if you want to do more operations, you will find all kinds of strange “bugs”. Therefore, in this section, the author will list two details that need attention.

1. How to mount subdirectories

By default, after entering the connection string mentioned in the second section, we will map the user’s home directory to windows. If we just want to manage the website directory, do backup or use it as NAS, we may need to mount other subdirectories. However, we may find that using the following connection string will cause an “location unavailable” error when connecting:

\\Sshfs \ \ user name @ IP address! Port No. / home

[tip] in windows, use sshfs win to mount SFTP disk to the local, and conveniently manage the files in Linux

This is because the path in the connection string is a relative path referring to the user’s home directory, that is, if we need to map/homeDirectory, you need to use the following connection string

\\Sshfs \ \ user name @ IP address! Port No. \.. \.. \ home

among..\We need to decide on the actual situation so that we can successfully map to the directory we need

[tip] in windows, use sshfs win to mount SFTP disk to the local, and conveniently manage the files in Linux

In this way, it is easy to cause misunderstanding and complicate the connection string. Therefore, sshfs win supports a more concise syntax for connection strings

\\Sshfs. R \ \ user name @ IP address! Port No

thererrefer toroot, that is, the default mount is from the root directory instead of the home directory, so there is no need to use the ugly..\Back to the root directory, we can start directly from the root directory and enter the absolute path to mount the folder we need.

In fact, in addition tosshfs.rIn addition, there are other connection string rules that can help sshfs win become more convenient. For details, please refer to:https://github.com/billziss-gh/sshfs-win#unc-syntax

2. How to cancel mounting

When I use sshfs win, I often can’t unload the drive letter, whether through the right-click disconnection of file resource manager or through the execution of CMDNet use drive letter / deleteOperation, you will find that the drive letter still exists after the prompt for successful unloading.

According to this issue:billziss-gh/sshfs-win#119This may be due to conflicts with other applications. The conflict may not be easy to solve, but we can solve this problem from the perspective of winfsp by using the principle described above, that is, winfsp provides support for sshfs.

We open the command prompt (or PowerShell, depending on your preference), and locate the directory to the winfsp installation directory. The default isC:\Program Files (x86)\WinFsp\bin\, and then execute the following command:

.\launchctl-x64.exe list

At this time, it will list the mount tasks that are running, as shown in the following figure:

[tip] in windows, use sshfs win to mount SFTP disk to the local, and conveniently manage the files in Linux

Here, we can see that the syntax of the first mount task is incorrect, which may be the reason why it can’t cancel the mount normally. At this time, we can execute the following command to cancel the mount:

 .\launchctl-x64.exe stop sshfs [email protected]

After the command is executed successfully, we open the file explorer and find that the drive letter that could not be unmounted has disappeared successfully.

Other substitutes

Although sshfs win is simple and direct, if readers need more functions (such as graphical interface, cache, etc.), they need other alternatives to achieve the same function. Here, the author recommends several alternatives:

  • rclone: a cross platform open source software known as “mounting any storage service”. It supports a wide range from Amazon S3 to Google drive, or more traditional FTP, SFTP and even memory! Rclone provides more than 30 storage targets, and provides full support of custom options, which can realize complex configuration such as cache and permissions. The drawback is that rclone does not provide a graphical interface, but it is more complex to write service configuration files under windows, so the software is suitable for users with more user-defined requirements.
  • raidrive: compared with rclone, raidrive’s biggest feature is that it provides graphical interface support, which makes it easier to manage the mount directory. However, this is a commercial software, and the free package only supports less mount targets and cannot support advanced functions such as cache.

There are many similar software, such asSFTP DriveReaders can choose the software suitable for themselves according to their own needs.

It should be noted that the principles of these software are mostly similar, and many of them are secondary development based on winfsp. Therefore, they are similar in terms of performance and stability.