CentOS 7下通过SSHFS挂载sftp资源

环境

CentOS7
SSHFS:2.10

安装

安装装EPEL源

yum install epel-release

安装sshfs

yum install sshfs

配置

使用如下命令进行挂载

sshfs -p 22 root@192.168.1.21:/ /app1/
#输入密码后确认挂载

#允许其他用户访问
sudo sshfs -o allow_other tecmint@x.x.x.x:/home/tecmint/ /mnt/tecmint  

#无密码登录
sudo sshfs -o allow_other,IdentityFile=~/.ssh/id_rsa tecmint@x.x.x.x:/home/tecmint/ /mnt/tecmint 

设置开机自启动挂载
touch auto.sh
写入开机启动脚本: 
echo 远程主机密码 | sshfs 远程主机用户@远程ip:/OA/RP/  /root/test -o workaround=rename -o password_stdin -o allow_other
chmod +x auto.sh
vim /etc/rc.d/rc.local
写入  sh /home/autosu.sh
chmod +x  /etc/rc.d/rc.local   //开机自动启动    





#去挂载目录查看
cd /app1/
ll
#查看挂载情况

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
如需取消:

#umount掉挂载点即可
umount /app1

开机自动挂载方案:

1. 在系统启动时挂载
crontab -e
------------------------------------------
@reboot sh mountsshfs.sh

vim mountsshfs.sh
------------------------------------------
#! /bin/sh
while true
do 
    ping -c1 -w1 ssh_server_ip > /dev/null && break
done
sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 user@192.168.200.10:/pathto/dir /home/username/mount/dir

2.利用/etc/fstab
user@host:/remote/folder /mount/point  fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/home/user/.ssh/id_rsa,allow_other,reconnect 0 0
参数
noauto 不在boot时mount
x-systemd.automount 配置按需挂载
_netdev 避免“No such device”错误,指明sshfs是一个网络设备而非块设备

注意:在完成对/etc/fstab的修改后,需要执行如下命令
systemctl daemon-reload
systemctl restart <target>
这里 <target>可以使用如下命令查询
systemctl list-unit-files --type automount

注意事项

  1. 挂载在重启后失效
  2. 一般来说,linux系统都默认安装了sftp,挂载期间基本不用考虑sftp服务。
  3. Windows下可使用其他第三方软件开启sftp服务,或通过smb进行挂载 [CentOS]CentOS 7下通过mount cifs挂载smb资源