linux的NFS服务

mac2026-05-06  7

NFS:Network File System 网络文件系统

是一种将远程主机的目录共享出来,由网络挂载到客户端上供其使用的一种东西。

主要用于类Unix系统间的文件共享。

在局域网范围内,NFS的传输速度最快,但是缺点是只能在类Unix系统中使用,不能跨平台。

配置思路:

1、安装软件。启动服务。 2、写配置文件,这个配置文件是手写的。包含了要分享出去的目录,操作权限等等。 3、测试

就是一个服务端一个客户端,服务端共享出一个目录,客户端挂载这个目录到本地目录就ok了。

[root@localhost ~]# yum install nfs-* Dependency Installed: gssproxy.x86_64 0:0.7.0-4.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-27.el7 libcollection.x86_64 0:0.6.2-27.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.0-27.el7 libnfsidmap.x86_64 0:0.25-17.el7 libpath_utils.x86_64 0:0.2.1-27.el7 libref_array.x86_64 0:0.1.5-27.el7 libtirpc.x86_64 0:0.2.4-0.10.el7 libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-14.el7 quota-nls.noarch 1:4.01-14.el7 rpcbind.x86_64 0:0.2.0-42.el7 tcp_wrappers.x86_64 0:7.6-77.el7 Complete! [root@localhost ~]# rpm -qa| grep nfs nfs-utils-1.3.0-0.48.el7.x86_64 libnfsidmap-0.25-17.el7.x86_64 [root@localhost ~]#

exports配置文件

[root@localhost ~]# rpm -qf /etc/exports setup-2.8.71-7.el7.noarch 可以查看这个配置文件,并不是nfs生成的,而是这个setup生成的

nfs服务端

[root@localhost ~]# vi /etc/exports /tmp/nfsshare/ 172.16.0.0/16(rw)

前边是目录,一定是服务端存在的目录,也就是想要用nfs共享出来的目录,后边是网段,/16 是子网掩码16位,如果不写子网掩码表示单个IP。 我的环境是CentOS7 如果是6 的话还要再安装和启动一个rpc的 7以后就不用。 (rw)nfs共享选项,rw表示读写权限,还可以有很多选项,在后面细说。

[root@localhost ~]# exportfs -rv //重新加载配置文件 exporting 172.16.0.0/16:/tmp/nfsshare [root@localhost ~]# showmount -e localhost //在服务端 查看是否被共享 Export list for localhost: /tmp/nfsshare 172.16.0.0/16 [root@localhost ~]# cp /etc/passwd /tmp/nfsshare/ 拷贝一个文件到共享目录

nfs客户端

客户端想用服务端共享内容需要挂载

[root@www /]# mkdir /nfsshare-client //创建挂载点 [root@www /]# mount 172.16.12.14:/tmp/nfsshare /nfsshare-client/ //挂载。 [root@www /]# cd /nfsshare-client/ [root@www nfsshare-client]# ls passwd

但是要注意一点,虽然我给了共享选项rw,理论上可读可写,但是实际上,客户端并不能操作,因为这个目录/tmp/nfsshare/本身没有权限进行操作。虽然都是root但是还是有区别的。

客户端创建文件

[root@www nfsshare-client]# touch 111 touch: cannot touch ‘111’: Permission denied

服务端修改权限:

[root@localhost ~]# chmod 757 /tmp/nfsshare/

客户端创建文件:

[root@www nfsshare-client]# touch 111 [root@www nfsshare-client]# ls 111 passwd

其实可以测试下客户端用户身份:

服务端改为750权限。

[root@localhost ~]# chmod 750 /tmp/nfsshare/

客户端就不能做操作了:

[root@www nfsshare-client]# ls ls: reading directory .: Permission denied

连查看文件都不能看,显示没权限,所以客户端对于这个共享目录来说是others。

ok。。。。!!!!

NFS常用共享选项:

ro —— 只读共享 rw —— 读写(但是真正能否可写,还要看目录的权限) sync —— 同步写入 async —— 异步写入 all_squash —— 所有用户都映射为nfsnobody no_all_squash —— 默认的,用户不映射,会携带身份和权限映射为服务器中的用户身份 root_squash —— 默认的,root会映射为nfsnobody no_root_squash —— root不映射,不携带身份

同步写入和异步写入是有区别的,正常来说写到硬盘的才叫写入,同步写入就是直接写到硬盘中,异步写入就是写到内存中,等稳定了在写到硬盘中。

all_squash 意思是所有客户端创建的文件,文件的属主和属组都显示为nfsnobody。

我用"-s"表示服务端创建的文件。"-c"表示客户端创建的文件。

[root@www nfsshare-client]# ll total 4 -rw-r--r-- 1 root root 0 Nov 2 12:21 222-s -rw-r--r-- 1 nfsnobody nfsnobody 0 Nov 2 12:22 333-c -rw-r--r-- 1 root root 1020 Nov 2 12:01 passwd [root@www nfsshare-client]#

默认情况下开启了root的映射,所以客户端root创建的被识别成nfsnobody用户

切了下用户 ,客户端切成 nfsclient1用户。 客户端

[nfsclient1@www nfsshare-client]$ ll total 4 -rw-r--r-- 1 root root 0 Nov 2 12:21 222-s -rw-r--r-- 1 nfsnobody nfsnobody 0 Nov 2 12:22 333-c -rw-rw-r-- 1 nfsclient1 nfsclient1 0 Nov 2 12:25 444-c -rw-r--r-- 1 root root 1020 Nov 2 12:01 passwd

默认是不映射用户,所以就是本身。nfsclient1. 服务端

[root@localhost nfsshare]# ll total 4 -rw-r--r--. 1 root root 0 Nov 2 12:21 222-s -rw-r--r--. 1 nfsnobody nfsnobody 0 Nov 2 12:22 333-c -rw-rw-r--. 1 1003 1003 0 Nov 2 12:25 444-c -rw-r--r--. 1 root root 1020 Nov 2 12:01 passwd

因为服务端没有这个 客户端中的 nfsclient用户,所以表示为 uid和gid 当然如果1003这个uid有用户 就会显示 uid为1003的用户的名字。客户端也一样。

NFS的自动挂载:

NFS的自动挂载和光盘、文件、等的自动挂载一样,只不过是挂载的源是一个网络源。

[root@www nfsshare-client]# rpm -qa | grep autofs autofs-5.0.7-69.el7.x86_64

软件就是这个autofs自动挂载的软件。

[root@www /]# rpm -qc autofs /etc/auto.master //自动挂载的主配置文件 ,用户自己定义挂载的 /etc/auto.misc //光盘自动挂载配置文件 /etc/auto.net //nfs自动挂载配置文件 /etc/auto.smb /etc/autofs.conf /etc/autofs_ldap_auth.conf /etc/sysconfig/autofs /usr/lib/systemd/system/autofs.service auto.master配置文件中加一行,最后一行: # same will not be seen as the first read key seen takes # precedence. # +auto.master /nfs-auto /etc/nfs-auto.conf

其中。/nfs-auto是自动挂载监听,目录,也可以理解成挂载点的父目录, 但是要注意,如果在这里设置了这个,那么目录里原有的东西就无法访问了,一定要注意技术之间的相互关系不要冲突。

/etc/nfs-auto.conf 是子配置文件,自己写的。

[root@www nfs-172.16.12.14]# cat /etc/nfs-auto.conf nfs-172.16.12.14 -fstype=nfs 172.16.12.14:/tmp/nfsshare

这个配子置文件中nfs172.16.12.14是挂载点,也就是在挂载点父目录/nfs-auto下的,-fstype是挂载的文件类型,这里是nfs型,后边的是网络的源,挂载的源文件。

配置完之后重新启动服务

[root@www /]# systemctl restart autofs [root@www /]# cd /nfs-auto //进入父目录,也就是监听目录, [root@www nfs-auto]# ls //查看内容什么都没有 [root@www nfs-auto]# cd nfs-172.16.12.14 //但是直接进入挂载点,却能进入 [root@www nfs-172.16.12.14]# ls //查看,能看到nfs共享出来的东西。 222-s 333-c 444-c 555-s passwd

自动挂载就是这样,正常是不显示的,在一段时间不操作之后就会自动消失。随着用随着出现,不用就消失。

注:NFS可以挂载使用嘛,所以有很多应用场景,因为,比如给硬盘扩容,共享yum源。就是共享出来,然后让他挂载就可以了。比 lvm 什么的要简单些。

最新回复(0)