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

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

Github地址:https://github.com/libfuse/sshfs

一、安装EPEL扩展源

[root@tokyo /]# rpm -Uvh http://ftp.iij.ad.jp/pub/linux/centos/7/extras/x86_64/Packages/epel-release-7-9.noarch.rpm

二、清理YUM缓存

[root@tokyo /]# yum clean all

三、使用YUM安装SSHFS

[root@tokyo /]#  yum install fuse-sshfs 

四、挂载远程目录

1.挂载服务器8.8.8.8上面的/data/tmp目录到本机的/tmp目录下,使用-o指定参数,port=ssh的端口号。

[root@tokyo /]#  sshfs -o port=2200 root@8.8.8.8:/data/tmp /tmp
root@8.8.8.8's password:

2.查看挂载结果

[root@tokyo /]# df -h  
Filesystem                        Size  Used Avail Use% Mounted on
devtmpfs                          993M     0  993M   0% /dev
tmpfs                            1000M     0 1000M   0% /dev/shm
tmpfs                            1000M  8.5M  991M   1% /run
tmpfs                            1000M     0 1000M   0% /sys/fs/cgroup
/dev/sda1                          30G  6.2G   22G  23% /
tmpfs                             200M     0  200M   0% /run/user/0
root@8.8.8.8:/data/tmp   30G  5.6G   23G  20% /tmp

五、卸载远程目录

卸载的方式和mount的方式一样,都是使用umount。(我查到的资料有用到fusermount -u mount_point)

[root@tokyo /]#  umount /tmp 

六、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'   # 指定ssh连接端口
    -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)
文章目录