RHCE---第五章:nfs服务

第五章:nfs服务

1、简介

NFS(Network File System,网络文件系统)是FreeBSD支持的文件系统中的一种,它允许网络中的计算机(不同的计算机、不同的操作系统)之间通过TCP/IP网络共享资源,主要在unix系列操作系统上使用。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。

在这里插入图片描述

​ NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中看来,那个远程主机的目录就好像是自己的一个磁盘分区一样。
由于NFS支持的功能比较多,而不同的功能都会使用不同的程序来启动,每启动一个功能就会启用一些端口来传输数据,因此NFS的功能所对应的端口并不固定,而是随机取用一些未被使用的小于1024的端口用于传输。但如此一来就会产生客户端连接服务器的问题,因为客户端需要知道服务器端的相关端口才能够连接。

​ 此时就需要RPC(Remote Procedure Call,远程过程调用)的服务。由于当服务器在启动NFS时会随机选取数个端口号,并主动向RPC注册,所以RPC知道每个NFS功能所对应的端口号,RPC将端口号通知给客户端,让客户端可以连接到正确的端口上去。RPC采用固定端口号port 111来监听客户端的需求并向客户端响应正确的端口号。
注:在启动NFS之前,要先启动RPC,否则NFS会无法向RPC注册。另外,RPC若重新启动,原来注册的数据会消失不见,因此RPC重启后,它管理的所有服务都需要重新启动以重新向RPC注册。

2、nfs配置

案例一:nfs共享文件

[root@localhost ~]# yum install rpcbind
[root@localhost ~]# yum install nfs-utils
#systemctl stop firewalld
#setenforce 0
#systemctl start nfs-server
#服务端(192.168.168.128)
[root@server ~]# mkdir  /public
[root@server ~]# touch /public/{1..10}
[root@server public]# vim /etc/exports
/public   *(ro)
		#/public *(ro,no_root_squash)
		#/public *(ro,all_squash) 
#共享目录  主机名(权限)
#可以使用完整的IP或者是网络号,例如172.24.8.128或172.24.8.0/24或者172.24.8.128/255.255.255.0;也可以使用*表示所有主机
#权限相关参数可以写多个,多个参数之间用逗号隔开,具体相关参数说明如下:
[root@server public]# exportfs -r 

Client
#yum install nfs-utils -y
#mkdir /nfs
#mount   192.168.10.135:/public  /nfs
------------------------------------------
#showmount -e 192.168.10.135    查看共享的文件系统

注:client 管理员root访问nfs文件系统默认做了用户映射root --nobody(65534)

 #mount -o remount,ro  /nfs      
 #mount -o ro 192.168.10.130:/public /nfs
注:
一个挂载点目录可以同时挂载多个文件系统,通过挂载点查看是最后一次挂载文件系统中的数据文件。
参数值说明
rw,ro该目录共享的权限是可读写还是只读,但最终能否读写,还是与 文件系统的rwx有关
sync,asyncsync代表数据会同步写入到内存与硬盘中,async则代表数据会 先暂存于内存当中,而非直接写入硬盘
no_root_squash root_squash若客户端在共享目录里创建的文件的所属者和所属组是root用户 和root组,那么显示文件的属主和属组时有以下两种情况: no_root_squash表示,文件的所属者和所属组是root用户和 root组;root_squash表示将root用户和组映射为匿名用户和组 (默认设置)。
all_squash no_all_squashall_squash:客户端所有用户创建文件时,客户端会将文件的用户 和组映射为匿名用户和组no_all_squash:客户端普通用户创建的 文件的UID和GID是多少,服务端就显示为多少(默认设置)
anonuid= anongid=将文件的用户和组映射为指定的UID和GID,若不指定默认为 65534(nfsnobody)
[root@server data]# chmod o+w  /data/
[root@server data]# systemctl disable firewalld --now
[root@server data]# getenforce
Enforcing
[root@server data]# setenforce  0
[root@server data]# systemctl restart nfs-server
[root@server data]# showmount -e 192.168.168.128
Export list for 192.168.168.128:
/data 192.168.168.140
#客户端(192.168.168.140)
[root@client ~]# showmount -e 192.168.168.128
Export list for 192.168.168.128:
/data 192.168.168.140
[root@client ~]# mkdir -p /nfsclient/client-data/
[root@client ~]# mount  192.168.168.128:/data  /nfsclient/client-data/
[root@client ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   37G  1.8G   36G   5% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
192.168.168.128:/data     40G  6.2G   34G  16% /nfsclient/client-data

测试操作:

[root@localhost ~]# dnf whatprovides nfs   # 查看需要安装的包的安装程序

# 安装nfs服务
[root@localhost ~]# dnf install nfs-utils -y

[root@localhost ~]# systemctl restart nfs-server.service 
[root@localhost ~]# systemctl status rpcbind
[root@localhost ~]# systemctl status nfs-server
[root@localhost ~]# rpcinfo -p

# rpc协议既支持tcp协议又支持udp协议

[root@localhost ~]# vim /etc/exports
# 共享目录,访问主机(参数--权限)
# /pub 共享地址(参数:ro--只读权限,rw--读写权限)
/pub *(rw)
[root@localhost ~]# mkdir /pub
[root@localhost ~]# touch /pub/{1..5}

注意:正常多个文件系统可以挂载到同一挂载点目录,但是nfs4文件系统默认一个挂载点下只能挂载一个nfs4文件系统。

[root@client ~]# vim /etc/fstab 
# 永久挂载
192.168.235.128:/home/tom   /test3  nfs4  defaults,sync  0 0  
# 光盘开机永久挂载
/dev/sr0   /mnt  iso9660    defaults   0 0 

实验:架设一台NFS服务器

并按照以下要求配置:

1、开放/nfs/shared目录,供所有用户查询资料

2、开放/nfs/upload目录,为192.168.xxx.0/24网段主机可以上传目录,并将所有用户及所属的组映射为nfs-upload,其UID和GID均为210

3、将/home/tom目录仅共享给192.168.xxx.xxx这台主机,并只有用户tom对该目录有读写权限

配置:

服务端server:

# 实验前配置:
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# mount /dev/sr1 /mnt
[root@localhost ~]# dnf install nfs-utils -y
[root@localhost ~]# dnf install nginx -y
[root@localhost ~]# systemctl restart nfs-server.service 
[root@localhost ~]# systemctl status rpcbind
[root@localhost ~]# systemctl status nfs-server.service 
# 实验配置:
# 编辑三个共享文件
[root@server ~]# vim /etc/exports
/nfs/shared  *(ro)             # 1,对所有用户都开放shared目录
/nfs/upload  192.168.235.0/24(rw,all_sqush,anonuid=210,anongid=210)    # 2,对192.168.235.0/24网段开放upload目录
/home/tom  192.168.235.131/24(rw)            # 3,指定用户tom上传文件,指定共享地址
# 创建测试路径文件
[root@server ~]# mkdir /nfs/{shared,upload}/{1..5} -pv
mkdir: created directory '/nfs'
mkdir: created directory '/nfs/shared'
mkdir: created directory '/nfs/shared/1'
mkdir: created directory '/nfs/shared/2'
mkdir: created directory '/nfs/shared/3'
mkdir: created directory '/nfs/shared/4'
mkdir: created directory '/nfs/shared/5'
mkdir: created directory '/nfs/upload'
mkdir: created directory '/nfs/upload/1'
mkdir: created directory '/nfs/upload/2'
mkdir: created directory '/nfs/upload/3'
mkdir: created directory '/nfs/upload/4'
mkdir: created directory '/nfs/upload/5'
# 允许上传
[root@server ~]# chmod o+w /nfs/upload
# 并将所有用户及所属的组映射为nfs-upload,其UID和GID均为210
[root@server ~]# useradd nfs-upload -u 210 -r
# 查看id
[root@server ~]# id nfs-upload
uid=210(nfs-upload) gid=210(nfs-upload) groups=210(nfs-upload)

# 添加用户tom
[root@server ~]# useradd tom
[root@server ~]# ll /home/tom -d
drwx------. 3 tom tom 78 Oct 27 14:51 /home/tom
# 查看tom ID
[root@server ~]# id tom
uid=1001(tom) gid=1001(tom) groups=1001(tom)
# 导出文件
[root@server ~]# exportfs -ra

客户端client:

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# mount /dev/sr0 /mnt
[root@localhost ~]# dnf install nfs-utils -y
[root@localhost ~]# dnf install nginx -y
systemctl restart nginx.service 
systemctl restart nfs-server.service 
systemctl status nginx.service 
systemctl status nfs-server.service 

# 创建三个对应列表
[root@client ~]# mkdir /{1..3}
# 挂载IP地址
[root@client ~]# showmount -e 192.168.235.128 
Export list for 192.168.235.128:
/nfs/shared *
/home/tom   192.168.235.131/24
/nfs/upload 192.168.235.0/24
# 挂载192.168.235.128相应文件的文件路径
[root@client ~]# mount 192.168.235.128:/nfs/shared /1
[root@client ~]# mount 192.168.235.128:/nfs/upload /2
[root@client ~]# mount 192.168.235.128:/home/tom /3

测试:

[root@client ~]# cd /1
[root@client 1]# ll
total 0
drwxr-xr-x. 2 root root 6 Oct 27 14:45 1
drwxr-xr-x. 2 root root 6 Oct 27 14:45 2
drwxr-xr-x. 2 root root 6 Oct 27 14:45 3
drwxr-xr-x. 2 root root 6 Oct 27 14:45 4
drwxr-xr-x. 2 root root 6 Oct 27 14:45 5

[root@client 2]# cd /2
[root@client 2]# ll
total 0
drwxr-xr-x. 2 root root 6 Oct 27 14:45 1
drwxr-xr-x. 2 root root 6 Oct 27 14:45 2
drwxr-xr-x. 2 root root 6 Oct 27 14:45 3
drwxr-xr-x. 2 root root 6 Oct 27 14:45 4
drwxr-xr-x. 2 root root 6 Oct 27 14:45 5

[root@client 2]# cd /3
bash: cd: /3: Permission denied
[root@client ~]# id tom
uid=1001(tom) gid=1001(tom) groups=1001(tom)
[root@client ~]# su tom
[tom@client root]$ cd /3
[tom@client 3]$ ll
total 0
[tom@client 3]$ touch a

# 在客户端上/1上传文件失败,原因是shared文件对于客户端用户来说只有读权限
[root@client 1]# touch 11111
touch: cannot touch '11111': Read-only file system

# 在服务端查看tom用户上传的文件
[root@server ~]# ll /home/tom
total 0
-rw-r--r--. 1 tom tom 0 Oct 27 15:02 a

自动挂载:

在一般NFS文件系统的使用过程中,如果客户端要使用服务端所提供的文件系统,可以在 /etc/rc.d/rc.local 中设置开机时自动挂载( /etc/rc.d/rc.local 文件中写入的命令,在每次启动系统用户登录之前都会执行一次);也可以在登录系统后手动利用mount来挂载。
由于网络的问题,NFS服务器与客户端的连接不会一直存在,当我们挂载了NFS服务器之后,任何一方脱机都可能造成另外一方等待超时。为了解决这样的问题,就出现了下面的想法:

  • 当客户端在有使用NFS文件系统的需求时才让系统自动挂载。
  • 当NFS文件系统使用完毕后,让NFS自动卸载。

于是就产生了autofs这个服务。

autofs这个服务是在客户端的上面,它会持续的检测某个指定的目录,并预先设置当使用到该目录的某个子目录时,将会取得来自服务器端的NFS文件系统资源,并进行自动挂载的操作。

# 客户端安装autofs
[root@client ~]# dnf install autofs -y
# 编辑自动挂载文件的位置
[root@client ~]# vim /etc/auto.master
/client /etc/auto.zdgz
# 查看
[root@client ~]# grep zdgz /etc/auto.master
/client /etc/auto.zdgz
# 配置自动挂载文件
[root@client ~]# vim /etc/auto.zdgz
[root@client ~]# cat /etc/auto.zdgz
shared 192.168.235.128:/nfs/shared
upload 192.168.235.128:/nfs/upload
tom 192.168.235.128:/home/tom
# 重启自动挂载服务
[root@client ~]# systemctl restart autofs.service 

# 测试
# 切换路径并创建文件
[root@client ~]# cd /client
[root@client client]# cd upload
[root@client upload]# touch aaaaaaa

# 在服务端查看是否上传
[root@server ~]# ll /nfs/upload
total 0
-rw-r--r--. 1 nfs-upload nfs-upload 0 Oct 27 15:35 aaaaaaa
#### 上传成功!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值