(1).安装部署
服务端:
[root@fw126 ~]# yum install nfs-utils
[root@fw126 ~]# systemctl start nfs
[root@fw126 ~]# firewall-cmd --permanent --add-service=nfs
[root@fw126 ~]# firewall-cmd --permanent --add-service=rpc-bind
[root@fw126 ~]# firewall-cmd --permanent --add-service=mountd
[root@fw126 ~]# firewall-cmd --reload
[root@fw126 ~]# firewall-cmd --list-all
[root@fw126 ~]# mkdir /westos/nfs -p
[root@fw126 ~]# vim /etc/exports
/westo/nfs *(sync)
[root@fw126 ~]# exportfs -rv
客户端:
[root@kh226 ~]# showmount -e 172.25.254.126 #查看
Export list for 172.25.254.126:
/westos/nfs *
[
[root@kh226 ~]# mount 172.25.254.126:/westos/nfs /mnt #挂载
[root@kh226 ~]# df
#2).客户端自动挂载
在不使用是还把nfs文件系统挂载在本机中会占用资源,可每次使用前都挂载会造成无谓的浪费。而自动挂载可以很好的解决这一矛盾。在使用时自动挂载,不用是自动卸载。
[root@kh226 ~]# yum install autofs.x86_64
[root@kh226 ~]# systemctl start autofs
[root@kh226 ~]# rpm -qc autofs #查找配置文件,因为不同的版本,文件路径会有区别。
vim /etc/sysconfig/autofs
vim /etc/autofs.conf #主配置文件
## Define default options for autofs.
#
# MASTER_MAP_NAME - default map name for the master map.
#
#MASTER_MAP_NAME="auto.master"
#
# TIMEOUT - set the default mount timeout in secons. The internal
# program default is 10 minutes, but the default installed
# configuration overrides this and sets the timeout to 5
# minutes to be consistent with earlier autofs releases.
#
TIMEOUT=300 #300秒不用自动卸载
#
[root@kh226 ~]# cd /net/172.25.254.126 #默认挂载目录
[root@kh226 172.25.254.126]# cd westos/nfs/ #挂载点
#300秒后查看
[root@kh226 nfs]# cd
[root@kh226 ~]# df
#3).设置挂载点
[root@kh226 ~]# vim /etc/auto.master
/westos/linux /etc/auto.nfs
#挂载点的上层目录 #子配置文件
[root@kh226 ~]# vim /etc/auto.nfs
nfs -rw 172.25.254.126:/westos/nfs
#挂载点 #挂载方式 #挂载设备
[root@kh226 ~]# cd /westos/linux/nfs
充气autofs服务后,文件/westos/linux/nfs会自动建立。
#4).访问控制
权限控制:
touch: cannot touch ‘file’: Read-only file system #服务不允许
[root@fw126 /]# vim /etc/exports
/westos/nfs *(sync,rw)
修改为文件后不能充气nfs服务,否则客户端的挂载会出现问题。
[root@fw126 /]# exportfs -rv
exporting *:/westos/nfs
touch: cannot touch ‘file’: Permission denied #权限不够
[root@fw126 /]# chmod 777 /westos/nfs
身份控制:
[root@fw126 /]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@fw126 /]# vim /etc/exports
/westos/nfs *(sync,rw,anonuid=1000,anongid=1004) #匿名用户用1000,1004的身份
exporting *:/westos/nfs
#客户端:
[root@kh226 nfs]# id kiosk
uid=1004(kiosk) gid=1004(kiosk) groups=1004(kiosk)
[root@kh226 nfs]# ls -l
total 0
-rw-r--r-- 1 student kiosk 0 Dec 8 22:27 file
[root@fw126 nfs]# ls -l
total 0
-rw-r--r--. 1 student westos 0 Dec 8 22:27 file
客户端和服务端存在的用户不同,所以文件文件属猪会以id的方式显示。
[root@fw126 /]# vim /etc/exports
/westos/nfs *(sync,rw,no_root_squash) #客户端的root用户登陆时仍保持root权限[root@kh226 nfs]# touch file
[root@kh226 nfs]# ls -ltotal 0
-rw-r--r-- 1 root root 0 Dec 8 22:17 file
访问控制的设置方式有很多,可以man exportfs查看。会有很多范例。可以根据不同的需求自行设定。