本机的目录挂载用的 mount, 这个命令相信大家都很熟悉了,而远程 mount 一般会使用 nfs 之类的,但相对配置麻烦些。今天介绍一个配置比 nfs 来的简单的多的远程挂载方法: SSHFS
SSHFS 顾名思义,走的是 SSH 的通道。而且挂载的时候,远程服务器啥都不用动,只需要客户端安装 SSHFS 就可以,并且挂载就一行命令搞定,非常方便。
适用 KVM 。( openvz 我测试了下没成功就没继续研究了。。)
在客户机上安装 SSHFS
CentOS :
yum install -y sshfs
Ubuntu/Debian :
sudo apt-get install -y sshfs
挂载远程目录
sshfs -o allow_other,transform_symlinks [email protected]:/etc/letsencrypt /etc/letsencrypt
首先注意的是 -o allow_other 这个参数是必要的,不然会出现权限不够的问题。
如果有 ln 链接之类的 , 还需要加上 transform_symlinks 参数 , 不然链接就都没了
这个命令就是把 http://www.91yun.org 下的 /etc/letsencrypt 目录挂载到本机的 /etc/letsencrypt 目录
使用 ssh key 免密登陆
如果你远程服务器设置了 key 登陆,那么 sshfs 也可以用这个私钥来实现免密登陆哦。指定证书登陆的参数是: -o IdentityFile=/root/.ssh/91yun
完整的挂载命令就是:
sshfs -o allow_other,transform_symlinks -o IdentityFile=/root/.ssh/91yun [email protected]:/etc/letsencrypt /etc/letsencrypt
随系统自动启动
只需要把执行代码写入 /etc/rc.local 就可以了
卸载远程挂载目录
fusermount -u /etc/letsencrypt
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)
91云(91yun.co)
技术博客必须顶