sshfs远程文件系统挂载

1 篇文章 0 订阅

135272029

sshfs简介:

一种通过SSH协议访问远程文件系统的用户空间文件系统。可以把远程主机上的文件系统挂载到本地使用。由于使用ssh协议连接,安全上有保障,使用起来也很方便。

Sshfs的实现依靠于linux内核模块fuse对用户空间文件系统的支持。关于fuse,详解参考维基百科这里的说明http://zh.wikipedia.org/wiki/FUSE

优势:

  • 不用把远程文件手动复制一份到本地修改:

修改远程文件我们可以用ftp、scp、sftp把这些远程文件拉取到本地对其进行修改再上传回去,但是这样就显得很麻烦了。Sshfs在这方面就显得很方便了,只要你有能ssh到该远程主机的ssh帐号就能把远程主机上文件系统挂载到本地,然后像操作本地文件和目录那样方便操作它们,期间所作的修改会自动保存到远程对应的目录及文件。

  • 安全性好

Sshfs使用ssh协议连接,而对ssh协议的安全性可以参考这里http://zh.wikipedia.org/wiki/Secure_Shell

 

安装准备:

1、Sshfs基于SSH协议 ,所以首先你本地以及远程主机上要安装有实现SSH协议的openssh或SSH Communications 软件,ssh协议看这里http://www.programfish.com/blog/?p=124

2、你的本地Linux发行版内要安装有fuse模块,因为sshfs需要fuse模块的支持。查看你的系统有没有这个模块请使用如下命令:

lsmod | grep fuse

如果没有输出则没有这个模块。

 

以上条件都满足后就变得很简单了,只需要在本地系统上安装sshfs,而在很多发行版都能在自己的源里找到这个包,直接用apt-get install sshfs、zypper install sshfs,如果源里没有这个包可以到github这里下载源码编译安装https://github.com/osxfuse/sshfs

 

使用:

  • 挂载:

在本地创建一个目录比如:

mkdir  /mnt/remote

然后挂载远程主机上的某目录到刚创建的目录: (比如我要挂载111.111.111.111这台远程主机上的/home目录到本地的/mnt/remote目录)

sshfs  -o  allow_other  user@111.111.111.111:/home  /mnt/remote

-o allow_other 选项是指挂载后/mnt/remote能被你所在系统上的其它用户访问。(更多选项请:man sshfs)

执行这个命令后输入密码然后cd 到/mnt/remote目录下就可以在里面看到远程主机上/home目录的内容,操作就像操作本地目录和文件一样就可以了。

  • 卸载:

使用命令:

fusermount -u /mnt/remote   (/mnt/remote为你的挂载点)

开机自动挂载远程主机文件系统

上面的操作是针对开机之后的,如果想要开机自动挂载该远程文件系统,需要做两步操作。

  • 在 /etc/fstab 添加挂载项(这里以上面例子为例):

sshfs#user@111.111.111.111:/home  /mnt/remote  fuse  defaults,auto,allow_other  0  0

  • 设置ssh无密码登陆:

设置ssh无密码登陆远程主机(public key认证),方法不再列出,因为笔者另一篇文章里已经说得比较详细明了,参考http://www.programfish.com/blog/?p=124的public key认证(免密码认证)部分。

这样以后就能开机自动把远程主机上的文件系统挂载到本地使用了。


 欢迎访问本人网站:http://www.programfish.com 

LinuxCoder社区: http://linuxcoder.org

注意:转载请注明 “作者:广州Linux爱好者+云计算 刁金明”

远程文件系统客户端源码, 超过7000行C++源代码, 其中包含RF32c.dll动态库整个工程源码, 源代码文件清单如下: \RFClient\unit_rc\CmdQueue.cpp \RFClient\unit_rc\CmdQueue.h \RFClient\unit_rc\RCClient.h \RFClient\unit_rc\RCClientObj.cpp \RFClient\unit_rc\RCClientObj.h \RFClient\units\RFCenter.cpp \RFClient\units\RFCenter.h \RFClient\units\RFCExports.cpp \RFClient\units\RFCExports.h \RFClient\units\RFCFileObj.cpp \RFClient\units\RFCFileObj.h \RFClient\units\RFCFindObj.cpp \RFClient\units\RFCFindObj.h \RFClient\units\RFConnObj.cpp \RFClient\units\RFConnObj.h \RFClient\units\RFCUtils.cpp \RFClient\units\RFCUtils.h 接口定义如下: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 接口相关函数 */ // 接口初始化/释放函数 void __stdcall RFCInitialize(); void __stdcall RFCUninitialize(); // 接口版本信息, 格式: "x.x.x.x (build yyyy.mm.dd)" // AVersion 返回版本号, 如: 0x0708090A 表示 7.8.9.10 char* __stdcall RFCGetVersion(long* AVersion); // 取当前操作的最后错误码(注: 用于 RFConnCreate, RFCFindFirst 和 RFCFileCreate) long __stdcall RFCGetLastError(); // 接口属性操作 long __stdcall RFCGetInt(long Attrib, long* AValue); long __stdcall RFCGetStr(long Attrib, char* AValue, long* ALen); long __stdcall RFCSetInt(long Attrib, long AValue); long __stdcall RFCSetStr(long Attrib, const char* AValue, long ALen); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 连接的相关函数 */ // 连接创建/释放 void* __stdcall RFConnCreate(const char* AHost, long APort, const char* APassword); void __stdcall RFConnFree(void* AConnObj); // 连接打开/关闭 long __stdcall RFConnOpen(void* AConnObj); void __stdcall RFConnClose(void* AConnObj); // 连接属性操作 long __stdcall RFConnGetInt(void* AConnObj, long Attrib, long* AValue); long __stdcall RFConnGetStr(void* AConnObj, long Attrib, char* AValue, long* ALen); long __stdcall RFConnSetInt(void* AConnObj, long Attrib, long AValue); long __stdcall RFConnSetStr(void* AConnObj, long Attrib, const char* AValue, long ALen); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 文件/目录操作的相关函数 */ // 读取/设置文件属性 long __stdcall RFCGetFileAttr(void* AConnObj, const char* AFileName, long* Attrib); long __stdcall RFCSetFileAttr(void* AConnObj, const char* AFileName, long Attrib); // 文件存在/删除/移动文件或目录 long __stdcall RFCFileExisted(void* AConnObj, const char* AFileName); long __stdcall RFCDeleteFile(void* AConnObj, const char* AFileName); long __stdcall RFCMoveFile(void* AConnObj, const char* AOldName, const char* ANewName); // 目录存在/创建/删除 long __stdcall RFCDirExisted(void* AConnObj, const char* APathName); long __stdcall RFCCreateDir(void* AConnObj, const char* APathName, bool AForced = false); long __stdcall RFCRemoveDir(void* AConnObj, const char* APathName, bool AForced = false); // 磁盘操作相关函数 long __stdcall RFCDriveType(void* AConnObj, const char* ADrive, long* AType); long __stdcall RFCDiskSpace(void* AConnObj, const char* APath, __int64* ATotalSpace, __int64* AFreeSpace); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 文件查找的相关函数 */ void* __stdcall RFCFindFirst(void* AConnObj, const char* APath, TRFCFindItem* AItem); long __stdcall RFCFindNext(void* AFindObj, TRFCFindItem* AItem); long __stdcall RFCFindClose(void* AFindObj); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 文件读写的相关函数 */ // 创建/释放文件对象 void* __stdcall RFCFileCreate(void* AConnObj, const char* AFileName, long AMode, bool ANeedCreate, long AFileAttr); long __stdcall RFCFileFree(void* AFile); // 读写数据 long __stdcall RFCFileRead(void* AFile, void* ABuffer, long ASize); long __stdcall RFCFileWrite(void* AFile, const void* ABuffer, long ASize); // 文件定位/清除文件缓存/设置文件末尾 long __stdcall RFCFileSeek(void* AFile, long* AOffset, long AOrigin); long __stdcall RFCFileSeek64(void* AFile, __int64* AOffset, long AOrigin); long __stdcall RFCFileFlush(void* AFile); long __stdcall RFCFileSetEOF(void* AFile); // 读取文件信息 long __stdcall RFCFileGetInfo(void* AFile, TRFCFileInfo* AInfo); long __stdcall RFCFileGetSize(void* AFile, __int64* ASize); // 读取/设置文件时间 long __stdcall RFCFileGetTime(void* AFile, double* ACreateTime, double* AModifyTime); long __stdcall RFCFileSetTime(void* AFile, const double* ACreateTime, const double* AModifyTime);
远程文件系统服务器源码, 超过9000行C++源代码, 其中包含服务程序RFSvc.exe和RF32s.dll动态库源码, 文件清单如下: \RFServer\unit_rc\RCServer.h \RFServer\unit_rc\RCServerObj.cpp \RFServer\unit_rc\RCServerObj.h \RFServer\units\RFAppObj.cpp \RFServer\units\RFAppObj.h \RFServer\units\RFCenter.cpp \RFServer\units\RFCenter.h \RFServer\units\RFConnObj.cpp \RFServer\units\RFConnObj.h \RFServer\units\RFExports.cpp \RFServer\units\RFExports.h \RFServer\units\RFFileObj.cpp \RFServer\units\RFFileObj.h \RFServer\units\RFFindObj.cpp \RFServer\units\RFFindObj.h \RFServer\units\RFUtils.cpp \RFServer\units\RFUtils.h \RFServer\units_shell\RFServer.h \RFServer\units_shell\RFSvc.cpp 接口定义如下: // 初始化/释放接口 void __stdcall RFInitialize(); void __stdcall RFUninitialize(); // 接口版本信息, 格式: "x.x.x.x (build yyyy.mm.dd)" char* __stdcall RFGetVersion(long* AVersion); // 设置(注: 若要调用则必须在 RFInitialize 之前调用) // 1. RFSetLogRoot 设置日志根路径, 默认值: CurrAppFilePath + "log_rf\\" // 2. RFSetCfgFile 设置配置文件名, 默认值: CurrAppFilePath + "RFServer.ini" void __stdcall RFSetLogRoot(const char* ALogRoot); void __stdcall RFSetCfgFile(const char* ACfgFile); // 接口属性相关函数 long __stdcall RFGetInt(long Attrib, long* AValue); long __stdcall RFGetStr(long Attrib, char* AValue, long* ASize); long __stdcall RFSetInt(long Attrib, long AValue); long __stdcall RFSetStr(long Attrib, const char* AValue, long ASize); // 启动/停止函数 long __stdcall RFStart(); void __stdcall RFStop();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值