[tips] in windows, use sshfs win to mount SFTP disk to local, convenient management of Linux files

Time:2021-7-16

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 permissions is also more complex. The most important thing is that the above several transport protocols are not encrypted by default, and it takes extra time and energy to configure encryption and other operations. Openssh’s own SFTP can be used out of the box with encryption, but SFTP is only a file transfer protocol after all. Under Linux, we can use sshfs with fuse to mount it as a disk. Under windows, we can also use sshfs win to help us realize this function. This article will explain the principle, installation, use of sshfs win and some details that need attention in the process of use.
This article was originally published inUnnamed station, from the author himself to Zhihu, please indicate the original author’s blog address or this link for reprint, 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 of.

Sshfs transforms SFTP protocol into fuse interface, which is then exposed to kernel in the form of file system; Sshfs win transforms SFTP protocol into the interface provided by winfsp (Windows file system agent)

  1. Register a device driver so that windows can mount it
  2. Realize the API of create file, read file and write file under NT kernel to manage the read-write queue
  3. Call the fuse interface corresponding to the application program mounted on winfsp

The kernel extensions and drivers of windows are very complex, so developing kernel file system for windows needs more energy than developing kernel file system under Linux. Winfsp, as a bridge between fuse interface and windows file system (this is the origin of agent in the name), can provide more comprehensive and complete API and good compatibility with fuse, Reduce the difficulty of file system development.

The specific principle and mechanism of winfsp can refer to winfspfileWe will not go into details here. Next, we will explain how to install and use SSH FS win which depends on winfsp.

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 win:https://github.com/billziss-gh/sshfs-win

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

[tips] in windows, use sshfs win to mount SFTP disk to local, convenient management of Linux files

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

\\Sshfs user name @ IP address! Port number

[tips] in windows, use sshfs win to mount SFTP disk to local, convenient management of Linux files

It should be noted that the port number here is not what we usually use:Instead of!, click finish and 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 Section 2, we will map the user’s home directory to windows. If we only want to manage the website directory, make backup or use it as NAS, we may need to mount other subdirectories. However, we may find that using the following connection string will result in an error of “location unavailable” when connecting:

\\Sshfs user name @ IP address! Port number

[tips] in windows, use sshfs win to mount SFTP disk to local, convenient management of Linux files

This is because the path in the connection string is relative 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 number

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

[tips] in windows, use sshfs win to mount SFTP disk to local, convenient management of Linux files

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

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

thererrefer torootThat is, it is mounted from the root directory instead of the home directory by default, 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, apart fromsshfs.rIn addition, there are other connection string rules to help sshfs win become more convenienthttps://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, no matter through the File Explorer right-click disconnect, or through the CMD executionNet use drive letter / deleteOperation, you will find that the drive letter still exists after the prompt to unload successfully.

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

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 running mount tasks, as shown in the following figure:

[tips] in windows, use sshfs win to mount SFTP disk to local, convenient management of Linux files

Here, we can see that the syntax of the first mount task is wrong, which may be the reason why it cannot be unmounted 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 will open the file explorer and find that the drive letter that could not be unmounted before has disappeared successfully.

Other alternatives

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 functions. Here I recommend several alternatives:

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

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

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