网络文件系统,英文Network File System(NFS)。是由SUN公司研制的UNIX表示层协议(pressentation layer protocol),能使使用者访问网络上别处的文件就像在使用自己的计算机一样。
NFS是基于UDP/IP协议的应用,其实现主要是采用远程过程调用RPC机制,RPC提供了一组与机器、操作系统以及低层传送协议无关的存取远程文件的操作。RPC采用了XDR的支持。XDR是一种与机器无关的数据描述编码的协议,他以独立与任意机器体系结构的格式对网上传送的数据进行编码和解码,支持在异构系统之间数据的传送。【百度百科】
环境
三台Redhat7.0虚拟机,一台为服务器server用于提供yum源及相关证书下载。两台客户端system1和system2用于安装配置NFS及验证。
安装NFS
在system1和system2上安装NFS
[root@system1 Desktop]# yum install nfs-utils -y
设置开机自启
[root@system1 ~]# systemctl enable nfs-server nfs-secure-server
设定防火墙规则
让防火墙允许nfs,rpc-bind,mountd三个服务
[root@system1 Desktop]# firewall-cmd --permanent --add-service=nfs
success
[root@system1 Desktop]# firewall-cmd --permanent --add-service=rpcbind
Error: INVALID_SERVICE: rpcbind
[root@system1 Desktop]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@system1 Desktop]# firewall-cmd --permanent --add-service=mountd
success
创建nfs共享目录
mkdir ‐p /public /protected/project
更改目录属主为andres
[root@system1 Desktop]# chown andres /protected/project
为共享目录设置selinux标签
[root@system1 Desktop]# semanage fcontext -a -t public_content_t "/protected(/.*)"
为project目录添加selinux标签,使其为读写
[root@system1 Desktop]# semanage fcontext -a -t public_content_rw_t "/protected/project(/.*)"
使selinux标签生效
[root@system1 Desktop]# restorecon -Rv /protected/
下载证书(RHCE考试中有证书,自己做实验可以不用证书)
wget ‐O /etc/krb5.keytab http://server.group8.example.com/pub/keytabs/system1.keytab
配置nfs,其中/public目录只读,/protected目录读写,只允许*.group8.example.com的域名访问
[root@system1 Desktop]# vim /etc/exports
/public *.group8.example.com(ro,sec=sys,sync)
/protected *.group8.example.com(rw,sec=krb5p,sync)
修改 nfs 启动参数
[root@system1 ~]# vim /etc/sysconfig/nfs
....
RPCNFSDARGS="‐V 4.2"
重启nfs服务
[root@system1 ~]# systemctl restart nfs‐server.service
刷新
[root@system1 ~]# exportfs
验证导出资源
[root@system1 ~]# exportfs
/public
/protected
在system2上创建挂载点用于system1上共享目录挂载
[root@system2 ~]# mkdir /mnt/nfsmount /mnt/nfssecure
system2下载 kerberos 证书(使用证书安全性更强,如果没有证书也可以)
[root@system2 ~]# wget ‐O /etc/krb5.keytab http://server.group8.example.com/pub/keytabs/system2.keytab
system2 fstab中添加持久化挂载项
[root@system2 Desktop]# vim /etc/fstab
system1:/public /mnt/nfsmount nfs defaults,sec=sys,sync 0 0
system1:/protected /mnt/nfssecure nfs defaults,sec=krb5p,v4.2,sync 0 0
设定服务开机启动并且马上运行服务(由于用到了 kerberos 验证,所以需要启动该服务)(RHCE考试中所有服务都要设置开机启动,否则重启之后服务都会起不来)
[root@system2 ~]# systemctl enable nfs‐secure
[root@system2 ~]# systemctl start nfs‐secure.service
测试是否共享成功
[root@system2 Desktop]# mount -a
[root@system2 Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.8G 3.1G 6.7G 32% /
devtmpfs 765M 0 765M 0% /dev
tmpfs 773M 140K 773M 1% /dev/shm
tmpfs 773M 8.9M 765M 2% /run
tmpfs 773M 0 773M 0% /sys/fs/cgroup
/dev/sdb1 2.0G 6.2M 1.9G 1% /mnt/data
//system1/devops 9.8G 3.3G 6.5G 34% /mnt/dev
system1:/public 9.8G 3.3G 6.5G 34% /mnt/nfsmount
system1:/protected 9.8G 3.3G 6.5G 34% /mnt/nfssecure
可以看到已经实现了共享
注意:最后验证挂载/protected 失败,可以通过 systemctl status nfs‐secure
查看服务是否启动失败,启动失败一般都是上 面的 kerberos 证书下载出错、system1 和 system2 的时间不一致导致
验证/protected目录是否可读写
[root@system2 ~]# su ‐ andres
‐bash‐4.2$ kinit
Password for andres@GROUP8.EXAMPLE.COM:
redhat
‐bash‐4.2$ cd /mnt/nfssecure/project/
‐bash‐4.2$ touch testfile
‐bash‐4.2$ ls ‐l
total 0
‐rw‐rw‐r‐‐. 1 andres andres 0 Feb 12 10:01 testfile
andres 用户成功创建文件
注意:如果写入文件失败,检查 system1 是否修改了 nfs 启动参数为 ”‐V 4.2” 并对目录设定了正确的 selinux
上下文,再检查 system2 是否设定了正确挂载参数 ”v4.2”
以上就是nfs共享的配置,也是RHCE考试nfs配置过程