Ubuntu下使用sshfs挂载远程目录到本地(和Windows挂载盘一样)

时间:2023-03-08 19:27:46
Ubuntu下使用sshfs挂载远程目录到本地(和Windows挂载盘一样)

访问局域网中其他Ubuntu机器,在不同机器间跳来跳去,很是麻烦,如果能够把远程目录映射到本地无疑会大大方面使用,就像Windows下的网络映射盘一样。在Linux的世界无疑也会有这种机制和方式,最近在使用的过程中选择了sshfs这个工具来达到把远程目录映射到本地的功能。

SSHFS可在本地安装的文件系统中,通过SSH获得所有加密的通信优势。使用sshfs可以把远程目录直接映射到本地,无需修改远程机器的设置,仅要求有ssh连接的权限(ssh都没有的话,还能干啥?)

官网地址: http://fuse.sourceforge.net/sshfs.html

一、SSHFS for Ubuntu

首先简单介绍一下SSHF。

关于SSH的介绍和安装使用可参考:SSH简介及工作机制以及Ubuntu环境下SSH的安装及使用这两篇文章。

SSH 是一个强大且安全的工具,我们除了可以用它来远程管理主机外,还可以通过它建立 SSH tunnel作 Proxy用,远程传输文件等等。而这里我想要介绍另外一个功能,那就是结合 sshfs 这个工具可以把远程主机的文件系统映射到本地主机上,透过 SSH 把远程文件系统挂载到本机上,这样我们可以不必使用 scp 工具就可以做到直接复制及删除远程主机的文件了,就像操作本地磁盘一样方便。

SSH是连接两*立计算机,进行远程控制任务的受信任的方式。

SSHFS最炫的地方在于可在本地安装的文件系统中,通过SSH获得所有加密的通信优势。sshfs 是基于 FUSE 构建的 SSH 文件系统客户端程序,通过它远程主机的配置无需作任何改变,就可以透过 SSH 协议来挂载远程文件系统了,非常方便及安全。

二、Ubuntu 上安装sshfs

Ubuntu源中已经包含了sshfs,以及所需的fuse包,直接安装即可:

  1. sudo apt-get install sshfs

安装的时候会自动创建用户组fuse,要使用sshfs的用户需要先加到这个用户组里才行(记得完成后还要重新登录),不然会没有权限执行fusermount:

fuse: failed to exec fusermount: Permission denied

三、挂载远程文件系统/目录

挂载的一般格式为:

  1. sudo sshfs {{user id}}@{{server hostname}}:{{desiredremote share}} {{desired local mount point}} -o idmap=user -o allow_other -ouid={{local user id}} -o gid={{local group id}}

其中重点选项:

-o transform_symlinks 表示转换绝对链接符号为相对链接符号

-o follow_symlinks 沿用服务器上的链接符号

-C 压缩,或者-o compression=yes

-o reconnect 自动重连

-o cache=yes

-o allow_other

挂载格式看起来比较头晕,这里结合我的使用给出一个简洁直观的格式:

  1. sshfs -o transform_symlinks -ofollow_symlinks  user@hostname: [dir]mountpoint

实例如下:

  1. sshfs –o cache=yes,allow_other user@192.168.1.200:/home/user/code home/user/code

说明:

1、 连接的时候可能需要管理员权限,在挂载命令前sudo即可。

2、更多参数请 man sshfs ,如果碰到其它问题请参考官方的 FAQ 。

3、如果想不输入密码的话,可以使用 SSH key 认证方式。

4、执行命令后 Ubuntu 会自动在桌面上显示挂载的 sshfs 文件系统盘,现在你就可以像操作本地磁盘一样操作这个映射的磁盘了。

四、卸载

fusemount –u /home/user/code

五、sshfs选项

  1. general options:
  2. -o opt,[opt...]        mount options
  3. -h   --help            print help
  4. -V   --version         print version
  5. SSHFS options:
  6. -p PORT                equivalent to '-o port=PORT'
  7. -C                     equivalent to '-o compression=yes' #启用压缩,建议配上
  8. -F ssh_configfile      specifies alternative ssh configuration file #使用非默认的ssh配置文件
  9. -1                     equivalent to '-o ssh_protocol=1' #不要用啊
  10. -o reconnect           reconnect to server               #自动重连
  11. -o delay_connect       delay connection to server
  12. -o sshfs_sync          synchronous writes
  13. -o no_readahead        synchronous reads (no speculative readahead) #提前预读
  14. -o sshfs_debug         print some debugging information
  15. -o cache=BOOL          enable caching {yes,no} (default: yes) #能缓存目录结构之类的信息
  16. -o cache_timeout=N     sets timeout for caches in seconds (default: 20)
  17. -o cache_X_timeout=N   sets timeout for {stat,dir,link} cache
  18. -o workaround=LIST     colon separated list of workarounds
  19. none             no workarounds enabled
  20. all              all workarounds enabled
  21. [no]rename       fix renaming to existing file (default: off)
  22. [no]nodelaysrv   set nodelay tcp flag in sshd (default: off)
  23. [no]truncate     fix truncate for old servers (default: off)
  24. [no]buflimit     fix buffer fillup bug in server (default: on)
  25. -o idmap=TYPE          user/group ID mapping, possible types are:  #文件权限uid/gid映射关系
  26. none             no translation of the ID space (default)
  27. user             only translate UID of connecting user
  28. -o ssh_command=CMD     execute CMD instead of 'ssh'
  29. -o ssh_protocol=N      ssh protocol to use (default: 2) #肯定要2的
  30. -o sftp_server=SERV    path to sftp server or subsystem (default: sftp)
  31. -o directport=PORT     directly connect to PORT bypassing ssh
  32. -o transform_symlinks  transform absolute symlinks to relative
  33. -o follow_symlinks     follow symlinks on the server
  34. -o no_check_root       don't check for existence of 'dir' on server
  35. -o password_stdin      read password from stdin (only for pam_mount)
  36. -o SSHOPT=VAL          ssh options (see man ssh_config)
  37. Module options:
  38. [subdir]
  39. -o subdir=DIR       prepend this directory to all paths (mandatory)
  40. -o [no]rellinks     transform absolute symlinks to relative
  41. [iconv]
  42. #字符集转换,对我这种UTF8控,默认已经是最好的
  43. -o from_code=CHARSET   original encoding of file names (default: UTF-8)
  44. -o to_code=CHARSET      new encoding of the file names (default: UTF-8)

六、总结:

在Ubuntu环境下,直接访问网络上其他电脑的文件时由于权限问题,会出现很多问题;最近特别突出的是远程电脑和本地的文件比较,文件夹比较的时候,不能使用工具,极度不方便。使用sshsf将远程电脑上的文件夹挂载到本地以后,不但可以很方便的访问远程文件而且也能使用比较工具将远程文件/文件夹和本地文件/文件夹进行比较。
比如使用beyond Compare时不能将远程文件、文件夹和本地的进行比较。挂载远程文件夹之后,此问题迎刃而解。
http://blog.csdn.net/netwalk/article/details/12952719