如何在 Linux 上使用 SSHfs 挂载一个远程文件系统
SSHfs ssh
你曾经想过用安全 shell 挂载一个远程文件系统到本地吗？如果有的话，SSHfs 也许就是你所需要的。它通过使用 SSH 和 Fuse（LCTT 译注：Filesystem in Userspace，用户态文件系统，是 Linux 中用于挂载某些网络空间，如 SSH，到本地文件系统的模块） 允许你挂载远程计算机（或者服务器）到本地。


**注意**： 这篇文章假设你明白[SSH 如何工作并在你的系统中配置 SSH](https://www.maketecheasier.com/setup-ssh-ubuntu/)。


![](/data/attachment/album/201511/16/010840b4jipb9sursbj6ir.jpg)


### 准备


在使用 SSHfs 挂载之前，需要进行一些设置 - 在你的系统上安装 SSHfs 以及 fuse 软件包。你还需要为 fuse 创建一个组，添加用户到组，并创建远程文件系统将会驻留的目录。


要在 Ubuntu Linux 上安装两个软件包，只需要在终端窗口输入以下命令：



```
sudo apt-get install sshfs fuse

```

![ubuntu 安装 sshfs-fuse](/data/attachment/album/201511/16/010935gjnjcrc6rztc86t8.jpg)


如果你使用的不是 Ubuntu，那就在你的发行版软件包管理器中搜索软件包名称。最好搜索和 fuse 或 SSHfs 相关的关键字，因为取决于你运行的系统，软件包名称可能稍微有些不同。


在你的系统上安装完软件包之后，就该创建好 fuse 组了。在你安装 fuse 的时候，应该会在你的系统上创建一个组。如果没有的话，在终端窗口中输入以下命令以便在你的 Linux 系统中创建组：



```
sudo groupadd fuse

```

添加了组之后，把你的用户添加到这个组。



```
sudo gpasswd -a "$USER" fuse

```

![sshfs 添加用户到组 fuse](/data/attachment/album/201511/16/010936rvva653999996125.png)


别担心上面命令的 `$USER`。shell 会自动用你自己的用户名替换。处理了和组相关的工作之后，就是时候创建要挂载远程文件的目录了。



```
mkdir ~/remote_folder

```

在你的系统上创建了本地目录之后，就可以通过 SSHfs 挂载远程文件系统了。


### 挂载远程文件系统


要在你的机器上挂载远程文件系统，你需要在终端窗口中输入一段较长的命令。



```
sshfs -o idmap=user username@ip.address:/remote/file/system/ ~/remote

```

![sshfs 挂载文件系统到本地目录1](/data/attachment/album/201511/16/010936wz1qe2555tzqlzik.png)


**注意**： 也可以通过 SSH 密钥文件挂载 SSHfs 文件系统。只需要在上面的命中用 `sshfs -o IdentityFile=~/.ssh/keyfile`, 替换 `sshfs -o idmap=user` 部分。


输入这个命令之后，会提示你输入远程用户的密码。如果登录成功了，你的远程文件系统就会被挂载到之前创建的 `~/remote_folder` 目录。


![sshfs挂载文件系统到本地目录2](/data/attachment/album/201511/16/010937toiotinelfoohxuy.jpg)


使用完了你的远程文件系统，想要卸载它？容易吗？只需要在终端输入下面的命令：



```
sudo umount ~/remote_folder

```

这个简单的命令会断开远程连接同时清空 remote\_folder 目录。


### 总结


在 Linux 上有很多工具可以用于访问远程文件并挂载到本地。但是如之前所说，如果有的话，也只有很少的工具能充分利用 SSH 的强大功能。我希望在这篇指南的帮助下，也能认识到 SSHfs 是一个多么强大的工具。


你觉得 SSHfs 怎么样呢？在下面的评论框里告诉我们吧！




---


via: <https://www.maketecheasier.com/sshfs-mount-remote-filesystem-linux/>


作者：[Derrik Diener](https://www.maketecheasier.com/author/derrikdiener/) 译者：[ictlyh](http://mutouxiaogui.cn/blog/) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Sshfs Featured](https://www.maketecheasier.com/assets/uploads/2019/06/sshfs-featured.png)

There’s certainly no shortage of solutions when it comes to syncing files between two devices. While Google Drive and Microsoft OneDrive may not give you official Linux clients, NextCloud and others do. But maybe you don’t trust a big corporation to store your files. Or, maybe, you want more security than NextCloud offers. With all the components that go into building something like NextCloud, chances are it has more undiscovered security holes than simpler solutions.

## Why Use SSHFS?

From the end-user perspective, this is very easy to use, clean and simple. It’s also incredibly secure since it relies on the battle-hardened and proven OpenSSH server. Encryption is also top notch, so you can rest assured that no one can steal your files while they are in transit. A correctly configured up-to-date server that only listens for SSH connections is usually impossible to crack for all but the most skilled attackers (think NSA, security specialists, etc.). And they probably don’t want your files.

## Why You Wouldn’t Want to Use SSHFS

If you want the best possible transfer speed, you might want to use other solutions such as NFS. SSHFS is not the slowest if your Internet connection is of high quality, but it’s not the fastest either. And if you want to transfer directories that contain hundreds of small files, it becomes horrible. Also, if you want to fine-tune file sharing settings, based on individual users or other factors, you might want to use other software.

To summarize, if all you need is an easy, secure way to synchronize a remote directory with a local one and aren’t in a huge hurry, you will probably be satisfied with this solution.

## Install SSHFS

On Arch Linux based installations, use this command:

sudo pacman -S sshfs

If you’re on a Fedora-based distro, use:

sudo dnf install sshfs

On Debian, Ubuntu and family, use:

sudo apt install sshfs

For those of you that use OpenSUSE, enter this command:

sudo zypper install sshfs

## Server Configuration

If you rent a server or VPS, the OpenSSH server daemon is already configured. Follow the steps recommended by your cloud service provider to configure a regular user (non-root). Some let you do this directly from their web control panel and even let you import the public key to allow SSH access. In this case, [generate the key pairs locally](https://www.maketecheasier.com/generate-public-private-ssh-key/), with the `ssh-keygen`

command. After, import the public key from “/home/your_username/.ssh/id_rsa.pub.”

If the cloud provider doesn’t offer a tool to easily import SSH public keys, do it manually. At the very least, disallow root login and disable password logins. [Use SSH keys exclusively instead](https://www.maketecheasier.com/enable-passwordless-ssh-logins-linux/): they are impossible to brute-force, as is the case with passwords.

If you want to sync files between two computers at home, just treat one as the server (install `openssh-server`

package and configure) and the other one as the client. The same steps apply.

## Mount a Remote Directory Locally with SSHFS

First, create a directory that will be synced with the remote side.

mkdir $HOME/sshfs

Next, mount the remote directory locally through SSHFS. Replace “user” with the actual username created on your server and “203.0.113.1” with the actual IP address of your remote instance.

sshfs user@203.0.113.1:/home/user $HOME/sshfs

Of course, if you don’t want to sync the entire home directory of the user on the server side, just replace “/home/user” with “/home/user/some_other_directory” after you create it on the server.

When you want to unmount, use this command:

cd && fusermount -u $HOME/sshfs

## Conclusion

If you want a directory to permanently sync to the remote side, add a command like `sshfs user@203.0.113.1:/home/user $HOME/sshfs`

in your autostart manager. Each graphical manager has a different autostart configuration manager, so consult your desktop environment help manual(s). Some sources recommend adding an entry to “/etc/fstab,” but we advise you to avoid that, as failure to mount the directory may result in your system failing to boot entirely.

Hopefully this covers all your needs. But, if it doesn’t, you can read about more command line options in the [online SSHFS manual](https://linux.die.net/man/1/sshfs).

Our latest tutorials delivered straight to your inbox