linux命令之SSHFS远程挂载目录

linux命令之SSHFS远程挂载目录

1、什么是SSHFS

SSHFS(Secure SHell FileSystem)是一个客户端,可以让我们通过 SSH 文件传输协议(SFTP)挂载远程的文件系统并且在本地机器上和远程的目录和文件进行交互。SFTP 是一种通过 SSH协议提供文件访问、文件传输和文件管理功能的安全文件传输协议。因为 SSH 在网络中从一台电脑到另一台电脑传输文件的时候使用数据加密通道,并且 SSHFS 内置在 FUSE(用户空间的文件系统)内核模块,允许任何非特权用户在不修改内核代码的情况下创建他们自己的文件系统。

2、安装先决条件

目标主机必须安装ssh服务,确保服务启动和端口通信正常,要实现开机永久挂载,可添加fstab,需实现主机间免密即可,可用于docker容器间的目录挂载非常方便。

3、安装SSHFS

平台 IP 系统类型 主机类型
CentOS 7.8 64Bit 192.168.1.200 默认 Docker Server
1
2
3
4
5
6
7
8
9
[root@docker ~]# yum install epel-release -y
[root@docker ~]# yum install sshfs -y
#-p端口 用户@IP:远程目录 目标目录 -o确认无误
[root@docker ~]# sshfs -p22 [email protected]:/usr/local/nginx/html/ /soft/nginx_wwww/ -o nonempty
[email protected]:
[root@docker ~]# df -h
Filesystem Size Used Avail Use% Mounted on
...
[email protected]:/usr/local/nginx/html/ 98G 11G 87G 12% /soft/nginx_wwww

4、连接容器测试

1
2
3
4
5
6
#写入测试页面
[root@docker nginx_wwww]# echo 'sshfs mount test!' > index.html
[root@docker ~]# docker exec -it 89899938084b /bin/bash
#进入容器
[root@89899938084b html]# cat index.html
sshfs mount test!

5、开机永久挂载

1
2
3
4
5
6
7
8
9
10
11
12
#开机宿主机与容器间的免密
[root@docker ~]# ssh-keygen -t rsa
[root@docker ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub "[email protected]"
[root@docker nginx_wwww]# ssh [email protected] 'date'
Sun Jul 26 09:43:53 UTC 2020
#关于sshfs的其他参数可在"defaults,"后面追加
[root@docker ~]# echo '[email protected]:/usr/local/nginx/html/ /soft/nginx_wwww/ fuse.sshfs defaults,allow_other,nonempty 0 0' >> /etc/fstab
[root@docker ~]# mount -a
[root@docker ~]# df -h
Filesystem Size Used Avail Use% Mounted on
...
[email protected]:/usr/local/nginx/html/ 98G 11G 87G 12% /soft/nginx_wwww
-------------本文结束感谢您的阅读-------------
LiGuanCheng wechat
如有问题,请与我微信交流或通过右下角“daovoice”与我联系~。
请我喝一杯咖啡~