no bady:NFS文档共享服务搭建与挂载,及java调用

NFS文档共享服务搭建与挂载,及java调用

1、安装nfs

yum install nfs*

2、安装rpc

yum install rpc*

3、创建共享文件夹

mkdir /usr/share/fengsheng

4、创建一个文件

echo "Youtube -FengSheng" > /usr/share/fengsheng/demon.txt

5、查看文件内容

cat /usr/share/fengsheng/demon.txt

6、设置共享目录所有者为nfsnobady

chown nfsnobady /usr/share/fengsheng/

7、编辑挂载文件

vim /etc/exports

文件内容为:

/usr/share/fengsheng 172.20.10.0/24(rw,sync,root_squash,insecure)

其中,/usr/share/fengsheng为共享目录。

172.20.10.0/24 为客户端,即访问共享服务的ip,0/24的写法,表示172.20.10网段均可访问。

(rw,sync,root_squash,insecure)为权限。其中insecure控制客户端接口,如果未设置,可能导致客户端无法获取共享服务连接。

注释:权限相关参数

ro:共享目录只读;

rw:共享目录可读可写;
all_squash:所有访问用户都映射为匿名用户或用户组;
no_all_squash(默认):访问用户先与本机用户匹配,匹配失败后再映射为匿名用户或用户组;

root_squash(默认):将来访的root用户映射为匿名用户或用户组;

no_root_squash:来访的root用户保持root帐号权限;

anonuid=:指定匿名访问用户的本地用户UID,默认为nfsnobody(65534);
anongid=:指定匿名访问用户的本地用户组GID,默认为nfsnobody(65534);

secure(默认):限制客户端只能从小于1024的tcp/ip端口连接服务器;

insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
wdelay(默认):检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率;

no_wdelay:若有写操作则立即执行,应与sync配合使用;
subtree_check(默认) :若输出目录是一个子目录,则nfs服务器将检查其父目录的权限;

no_subtree_check :即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

8、生效配置参数

exportfs -r

9、查看配置参数

exportfs -v

注释:exportfs用法

-a* :全部mount或者unmount /etc/exports中的内容*
-r* :重新mount /etc/exports中分享出来的目录*
-u* :umount 目录*
-v* :将详细的信息输出到屏幕上

10、启动nfs服务

systemctl restart nfs

11、设置开机启动项

systemctl enable nfs

12、查看nfs服务状态

systemctl status nfs

13、如果防火墙开启,则设置防火墙(步骤13至17)

firewall-cmd --permanent --add-service=nfsshowmount -e unknown keyword "rw.sync.root_squash"

由于nfs服务需要开启 mountd,nfs,nlockmgr,portmapper,rquotad这5个服务,需要将这5个服务的端口加到iptables里面*

而nfs* 和 portmapper两个服务是固定端口的,nfs为2049,portmapper为111。其他的3个服务是用的随机端口,那就需要*

先把这3个服务的端口设置成固定的。

14、查看当前这5个服务的端口并记录下来 用*rpcinfo -p

\#rpcinfo -p

这里显示 nfs 2049, portmapper 111, 将剩下的三个服务的端口随便选择一个记录下来*

mountd 976

rquotad 966

nlockmgr 33993

15*、*将这3个服务的端口设置为固定端口

vim /etc/services

在文件的最后一行添加:

mountd 976/tcp

mountd 976/udp

rquotad 966/tcp

rquotad 966/udp

nlockmgr 33993/tcp

nlockmgr 33993/udp

保存并退出。

16、重启下nfs服务。

service nfs restart

17、在防火墙中开放这5个端口

编辑iptables配置文件

vim /etc/sysconfig/iptables

添加如下行:

-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 111 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 976 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 2049 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 966 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 33993 -j ACCEPT

-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 111 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 976 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 2049 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 966 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 33993 -j ACCEPT

保存退出并重启iptables*

18、linux客户端挂载nfs

mount -t nfs 192.168.0.10:/sharedisk /sharedisk

19、windows10客户端挂载nfs

打开控制面板 > 程序 > 启用或关闭 Windows 功能,找到NFS服务打开子目录勾选NFS客户端与管理工具。

img

NFS客户端:通过界面操作挂在NFS

打开我的电脑点击此电脑 > 映射网络驱动器

img

img

如果连接成功你会发现在此电脑多了一个网络盘符

这里写图片描述

下来就可以打开查看文件了

20、增加读写权限

需要读写权限的需要修改注册表
通过修改注册表将windows访问NFS时的UID和GID改成0即可,步骤如下
1、在运行中输入regedit,打开注册表编辑器;
2、进入HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default条目;
3、选择新建----QWORD值,新建AnonymousUid,AnonymousGid两个值,值为0;
4、重启电脑 注册表才会生效;

img

右键查看属性发现读写权限跟隐藏文件都打开了

img

21、卸载

umount 盘符

例如:umount V:
如果要卸载全部的NFS挂载:

umount -f -a

22、引入包文件

<dependency><groupId>com.emc.ecs</groupId><artifactId>nfs-client</artifactId><version>1.0.3</version></dependency>

23、java连接nfs服务器上传,下载文件

package com.svw.ematrix.center.document.manager.util;import com.emc.ecs.nfsclient.nfs.NfsCreateMode;import com.emc.ecs.nfsclient.nfs.NfsSetAttributes;import com.emc.ecs.nfsclient.nfs.io.Nfs3File;import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;import com.emc.ecs.nfsclient.rpc.CredentialUnix;import java.io.*;import java.util.logging.Level;import java.util.logging.Logger;/** * @作者 saw * @版本 V1.0 * @描述 NFS工具类 */public class NfsUtil { private static final String NFS_IP = "172.20.30.61"; private static final String NFS_DIR = "/usr/share/fengsheng"; /** * 上传文件到NFS服务器 * @param path NFS 存储的相对路径 * @param fileName 文件名称包括文件后缀 * @param content 文件二进制内容 * @return */ public static boolean upload(String path, String fileName, byte []content){ NfsFileOutputStream outputStream = null; InputStream inputStream = null; NfsSetAttributes nfsSetAttr = new NfsSetAttributes(); nfsSetAttr.setMode((long) (0x00100 + 0x00080 + 0x00040 + 0x00020 + 0x00010 + 0x00008 + 0x00004 + 0x00002)); try {// 注意,此处uid和gid需设置成0,代表root用户,如未设置成0,则可能没有对共享目录的读写权限 Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3); String paths[] = path.substring(1).split("/");//去掉第一个/之后进行分割处理 StringBuilder p = new StringBuilder(); //首先判断目录是否存在,如果不存在则进行创建目录 for(String s:paths){ p.append("/").append(s); Nfs3File filePath = new Nfs3File(nfs3, p.toString()); if (!filePath.exists()) { filePath.mkdir(nfsSetAttr); } } //创建文件 Nfs3File desFile = new Nfs3File(nfs3, path+"/"+fileName); desFile.create(NfsCreateMode.GUARDED, nfsSetAttr, null); outputStream = new NfsFileOutputStream(desFile); outputStream.write(content); return true; } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ if(null!=outputStream){ try { outputStream.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } } return false; } /** * 文件下载 * @param filePath NFS上面的文件路径信息 * @return */ public static byte[] download(String filePath){ ByteArrayOutputStream bos = null; NfsFileInputStream inputStream = null; BufferedInputStream bis = null; try { Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3); Nfs3File file = new Nfs3File(nfs3, filePath); inputStream = new NfsFileInputStream(file); bis = new BufferedInputStream(inputStream); bos = new ByteArrayOutputStream(); int date = -1; while ((date = bis.read()) != -1) { bos.write(date); } return bos.toByteArray(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ if(null!=bos){ try { bos.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } if(null!=bis){ try { bis.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } if(null!=inputStream){ try { inputStream.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } } return null; }}

相关推荐

相关文章