Ubuntu下使用sshfs挂载远程目录到本地

访问局域网中其他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包,直接安装即可:

sudo apt-get install sshfs  

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

fuse: failed to exec fusermount: Permission denied

 

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

挂载的一般格式为:

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

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

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

实例如下:

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选项

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

 

 

六、总结:

在Ubuntu环境下,直接访问网络上其他电脑的文件时由于权限问题,会出现很多问题;最近特别突出的是远程电脑和本地的文件比较,文件夹比较的时候,不能使用工具,极度不方便。使用sshsf将远程电脑上的文件夹挂载到本地以后,不但可以很方便的访问远程文件而且也能使用比较工具将远程文件/文件夹和本地文件/文件夹进行比较。
 
比如使用beyond Compare时不能将远程文件、文件夹和本地的进行比较。挂载远程文件夹之后,此问题迎刃而解。
posted @ 2017-07-10 15:50  553490191  阅读(1087)  评论(0编辑  收藏  举报