sshfs基于ssh挂载远程目录

清风了了 2018-06-01

为了像本地一样访问远程主机上的目录,通常我们会在远程主机上使用nfs来导出目录,并在本地主机上mount这个nfs文件系统。如果是Windows系统,则使用cifs或samba的方式来访问。

但可能我们忽略了一个远程连接最通用的工具:ssh。其实很多和远程有关的行为,基于ssh都能完成,即使是实现像NFS一样的功能。

如何通过ssh来挂载远程目录?需要安装fuse-sshfs包,这个包在epel中提供。使用fuse-sshfs包提供的sshfs工具可以基于ssh直接挂载远程目录,不用像NFS一样还要export。
$ yum -y install fuse-sshfs

$ rpm -ql fuse-sshfs
/usr/bin/sshfs
/usr/share/doc/fuse-sshfs-2.5
/usr/share/doc/fuse-sshfs-2.5/AUTHORS
/usr/share/doc/fuse-sshfs-2.5/COPYING
/usr/share/doc/fuse-sshfs-2.5/ChangeLog
/usr/share/doc/fuse-sshfs-2.5/FAQ.txt
/usr/share/doc/fuse-sshfs-2.5/NEWS
/usr/share/doc/fuse-sshfs-2.5/README
/usr/share/man/man1/sshfs.1.gz

例如,挂载192.168.100.150上的根目录"/"到本地的/mnt上。注意:只能挂载远程目录,像普通文件、块设备(如/dev/sda2)等无法挂载。
sshfs [email protected]:/ /mnt

如此一来,以后可以直接访问本地/mnt来访问远程的根目录。例如复制文件、移动文件、新建文件等等操作。

如果要卸载挂载点。直接umount即可。
umount /mnt

相比于NFS,sshfs更简洁,它是基于fuse模块来实现的,可以认为sshfs所挂载的文件系统是fuse文件系统的一种实现。所谓fuse文件系统,它全称为filesystem in userspace,显然,它是用户空间的文件系统(其实是一个虚拟文件系统),其功能非常强大,可用于实现自己的文件系统。详细信息可以sshfs -h,man sshfs,man fusermount,man mount.fuse。

但是NFS比sshfs要完整的多,nfs毕竟是"小型"分布式文件系统,对数据的一致性、完整性实现的都比较完美,访问权限控制也比sshfs要丰富的多。

总的来说,sshfs可以临时用来快速访问远程文件。

更详细的sshfs,参见下面的

如何使用 SSHFS 通过 SSH 挂载远程的 Linux 文件系统或者目录

写这篇文章的主要目的就是提供一步一步的指导,关于如何使用 SSHFS 通过 SSH 挂载远程的 Linux 文件系统或目录。

这篇文章对于那些无论出于什么目的,希望在他们本地的系统中挂载远程的文件系统的用户或者系统管理员有帮助。我们通过 Linux 系统中的一个安装了 SSHFS 客户端进行实际测试,并且成功的挂载了远程的文件系统。

在我们进一步安装之前,让我们了解一下 SSHFS 的相关内容,以及它是如何工作的。

Sshfs 挂载远程的 Linux 文件系统或者目录

什么是 SSHFS?

SSHFS(Secure SHell FileSystem)是一个客户端,可以让我们通过 SSH 文件传输协议(SFTP)挂载远程的文件系统并且在本地机器上和远程的目录和文件进行交互。

SFTP 是一种通过 SSH 协议提供文件访问、文件传输和文件管理功能的安全文件传输协议。因为 SSH 在网络中从一台电脑到另一台电脑传输文件的时候使用数据加密通道,并且 SSHFS 内置在 FUSE(用户空间的文件系统)内核模块,允许任何非特权用户在不修改内核代码的情况下创建他们自己的文件系统。

在这篇文章中,我们将会向你展示在任意 Linux 发行版上如何安装并且使用 SSHFS 客户端,在本地 Linux 机器上挂载远程的 Linux 文件系统或者目录。

步骤 1:在 Linux 系统上安装 SSHFS

默认情况下,sshfs 包不存在所有的主流 Linux 发行版中,你需要在你的 Linux 系统中启用 epel 仓库,在 Yum 命令行的帮助下安装 SSHFS 及其依赖。

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> install sshfs</span>
  2. <span class="com">#</span><span class="pln"> dnf install sshfs </span><span class="pun">【在</span><span class="typ">Fedora</span><span class="lit">22</span><span class="pun">+</span><span class="pun">发行版上】</span>
  3. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install sshfs </span><span class="pun">【基于</span><span class="typ">Debian</span><span class="pun">/</span><span class="typ">Ubuntu</span><span class="pun">的系统】</span>

步骤 2:创建 SSHFS 挂载目录

当你安装 SSHFS 包之后,你需要创建一个挂载点目录,在这儿你将要挂载你的远程文件系统。例如,我们在 /mnt/tecmint 下创建挂载目录。

  1. <span class="com">#</span><span class="kwd">mkdir</span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mkdir</span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint </span><span class="pun">【基于</span><span class="typ">Debian</span><span class="pun">/</span><span class="typ">Ubuntu</span><span class="pun">的系统】</span>

步骤 3:使用 SSHFS 挂载远程的文件系统

当你已经创建你的挂载点目录之后,现在使用 root 用户运行下面的命令行,在 /mnt/tecmint 目录下挂载远程的文件系统。视你的情况挂载目录可以是任何目录。

下面的命令行将会在本地的 /mnt/tecmint 目录下挂载一个叫远程的一个 /home/tecmint 目录。(不要忘了使用你的 IP 地址和挂载点替换 x.x.x.x)。

  1. <span class="com">#</span><span class="pln"> sshfs tecmint@x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint</span><span class="str">/ /</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> sshfs </span><span class="pun">-</span><span class="pln">o allow_other tecmint@x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint</span><span class="str">/ /</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint </span><span class="pun">【基于</span><span class="typ">Debian</span><span class="pun">/</span><span class="typ">Ubuntu</span><span class="pun">的系统】</span>

如果你的 Linux 服务器配置为基于 SSH 密钥授权,那么你将需要使用如下所示的命令行指定你的公共密钥的路径。

  1. <span class="com">#</span><span class="pln"> sshfs </span><span class="pun">-</span><span class="pln">o </span><span class="typ">IdentityFile</span><span class="pun">=~</span><span class="str">/.ssh/</span><span class="pln">id_rsa tecmint@x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint</span><span class="str">/ /</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> sshfs </span><span class="pun">-</span><span class="pln">o allow_other</span><span class="pun">,</span><span class="typ">IdentityFile</span><span class="pun">=~</span><span class="str">/.ssh/</span><span class="pln">id_rsa tecmint@x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint</span><span class="str">/ /</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint </span><span class="pun">【基于</span><span class="typ">Debian</span><span class="pun">/</span><span class="typ">Ubuntu</span><span class="pun">的系统】</span>

步骤 4:验证远程的文件系统挂载成功

如果你已经成功的运行了上面的命令并且没有任何错误,你将会看到挂载在 /mnt/tecmint 目录下的远程的文件和目录的列表

  1. <span class="com">#</span><span class="kwd">cd</span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint</span>
  2. <span class="com">#</span><span class="kwd">ls</span>
  3. <span class="pun">[</span><span class="pln">root@ tecmint</span><span class="pun">]#</span><span class="kwd">ls</span>
  4. <span class="lit">12345.jpg</span><span class="pln"> ffmpeg</span><span class="pun">-</span><span class="pln">php</span><span class="pun">-</span><span class="lit">0.6</span><span class="pun">.</span><span class="lit">0.tbz2</span><span class="typ">Linux</span><span class="pln"> news</span><span class="pun">-</span><span class="pln">closeup</span><span class="pun">.</span><span class="pln">xsl s3</span><span class="pun">.</span><span class="pln">jpg</span>
  5. <span class="pln">cmslogs gmd</span><span class="pun">-</span><span class="pln">latest</span><span class="pun">.</span><span class="pln">sql</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">bz2 </span><span class="typ">Malware</span><span class="pln"> newsletter1</span><span class="pun">.</span><span class="pln">html sshdallow</span>
  6. <span class="pln">epel</span><span class="pun">-</span><span class="pln">release</span><span class="pun">-</span><span class="lit">6</span><span class="pun">-</span><span class="lit">5.noarch</span><span class="pun">.</span><span class="pln">rpm json</span><span class="pun">-</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1</span><span class="pln"> movies_list</span><span class="pun">.</span><span class="pln">php pollbeta</span><span class="pun">.</span><span class="pln">sql</span>
  7. <span class="pln">ffmpeg</span><span class="pun">-</span><span class="pln">php</span><span class="pun">-</span><span class="lit">0.6</span><span class="pun">.</span><span class="lit">0</span><span class="pln"> json</span><span class="pun">-</span><span class="lit">1.2</span><span class="pun">.</span><span class="lit">1.tgz</span><span class="pln"> my_next_artical_v2</span><span class="pun">.</span><span class="pln">php pollbeta</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">bz2</span>

步骤 5:使用 df -hT 命令检查挂载点

如果你运行 df -hT命令,你将会看到远程文件系统的挂载点。

  1. <span class="com">#</span><span class="kwd">df</span><span class="pun">-</span><span class="pln">hT</span>

样本输出:

  1. <span class="typ">Filesystem</span><span class="typ">Type</span><span class="typ">Size</span><span class="typ">Used</span><span class="typ">Avail</span><span class="typ">Use</span><span class="pun">%</span><span class="typ">Mounted</span><span class="pln"> on</span>
  2. <span class="pln">udev devtmpfs </span><span class="lit">730M</span><span class="lit">0</span><span class="lit">730M</span><span class="lit">0</span><span class="pun">%</span><span class="pun">/</span><span class="pln">dev</span>
  3. <span class="pln">tmpfs tmpfs </span><span class="lit">150M</span><span class="lit">4.9M</span><span class="lit">145M</span><span class="lit">4</span><span class="pun">%</span><span class="pun">/</span><span class="pln">run</span>
  4. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 ext4 </span><span class="lit">31G</span><span class="lit">5.5G</span><span class="lit">24G</span><span class="lit">19</span><span class="pun">%</span><span class="pun">/</span>
  5. <span class="pln">tmpfs tmpfs </span><span class="lit">749M</span><span class="lit">216K</span><span class="lit">748M</span><span class="lit">1</span><span class="pun">%</span><span class="str">/dev/</span><span class="pln">shm</span>
  6. <span class="pln">tmpfs tmpfs </span><span class="lit">5.0M</span><span class="lit">4.0K</span><span class="lit">5.0M</span><span class="lit">1</span><span class="pun">%</span><span class="str">/run/</span><span class="pln">lock</span>
  7. <span class="pln">tmpfs tmpfs </span><span class="lit">749M</span><span class="lit">0</span><span class="lit">749M</span><span class="lit">0</span><span class="pun">%</span><span class="str">/sys/</span><span class="pln">fs</span><span class="pun">/</span><span class="pln">cgroup</span>
  8. <span class="pln">tmpfs tmpfs </span><span class="lit">150M</span><span class="lit">44K</span><span class="lit">150M</span><span class="lit">1</span><span class="pun">%</span><span class="str">/run/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">1000</span>
  9. <span class="pln">tecmint@192</span><span class="pun">.</span><span class="lit">168.0</span><span class="pun">.</span><span class="lit">102</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint fuse</span><span class="pun">.</span><span class="pln">sshfs </span><span class="lit">324G</span><span class="lit">55G</span><span class="lit">253G</span><span class="lit">18</span><span class="pun">%</span><span class="str">/mnt/</span><span class="pln">tecmint</span>

步骤 6:永久挂载远程文件系统

为了永久的挂载远程的文件系统,你需要修改一个叫 /etc/fstab 的文件。照着做,使用你最喜欢的编辑器打开文件。

  1. <span class="com">#</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">fstab </span><span class="pun">【基于</span><span class="typ">Debian</span><span class="pun">/</span><span class="typ">Ubuntu</span><span class="pun">的系统】</span>

移动到文件的底部并且添加下面的一行,保存文件并退出。下面条目表示使用默认的设置挂载远程的文件系统。

  1. <span class="pln">sshfs</span><span class="com">#</span><span class="pln">tecmint@x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint</span><span class="str">/ /</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint fuse</span><span class="pun">.</span><span class="pln">sshfs defaults </span><span class="lit">0</span><span class="lit">0</span>

确保服务器之间允许 SSH 无密码登录,这样系统重启之后才能自动挂载文件系统。

如果你的服务器配置为基于 SSH 密钥的认证方式,请加入如下行:

  1. <span class="pln">sshfs</span><span class="com">#</span><span class="pln">tecmint@x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">.</span><span class="pln">x</span><span class="pun">:</span><span class="str">/home/</span><span class="pln">tecmint</span><span class="str">/ /</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint fuse</span><span class="pun">.</span><span class="pln">sshfs </span><span class="typ">IdentityFile</span><span class="pun">=~</span><span class="str">/.ssh/</span><span class="pln">id_rsa defaults </span><span class="lit">0</span><span class="lit">0</span>

接下来,你需要更新 fstab 文件使修改生效。

  1. <span class="com">#</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">a</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">a </span><span class="pun">【基于</span><span class="typ">Debian</span><span class="pun">/</span><span class="typ">Ubuntu</span><span class="pun">的系统】</span>

步骤 7:卸载远程的文件系统

为了卸载远程的文件系统,只需要发出以下的命令即可。

  1. <span class="com">#</span><span class="kwd">umount</span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">tecmint</span>

目前为止就这样了,如果你在挂载远程文件系统的时候遇到任何问题或者需要任何帮助,请通过评论联系我们,如果你感觉这篇文章非常有用,请分享给你的朋友们。

相关推荐