The NFS (Network File system), which is one of the file systems supported by FreeBSD, allows computers in the network to share resources across TCP/IP networks. In an NFS application, a local NFS client application can transparently read and write to files located on the remote NFS server, just as you would access a local file.
Use Scenario: Personal recommended NFS file system is best built in the intranet, suitable for small and medium-sized network services file sharing
1. Installation
[[email protected] ~]# yum -y install nfs-utils rpcbind[[email protected] ~]# rpm -ql nfs-utils/usr/lib/systemd/system/nfs.service/usr/lib/systemd/system/nfs-lock.service/usr/lib/systemd/system/nfs-mountd.service/usr/lib/systemd/system/rpc-statd.service....
2. Create a directory to export
[[email protected] /]# mkdir /nfsshared/node1 -pv
3. Export Configuration file Description
/etc/exports或/etc/exports.d/* /PATH/TO/SOME_DIR clients1(export_options, ...) clients2(export_options, ...) clients: single host:ipv4, ipv6, FQDN; network:address/netmask, 支持长短格式的掩码; wildcards:主机名通配,例如:*.zander.com; netgroups:NIS域内的主机组;@group_name; anonymous:使用*通配所有主机; General Options: ro:只读 rw:读写; sync:同步; async:异步; secure:客户端端口小于1024,否则就要使用insecure选项; User ID Mapping: root_squash:压缩root用户,一般指将其映射为nfsnobody; no_root_squash:不压缩root用户; all_squash:压缩所有用户; anonuid and anongid:将压缩的用户映射为此处指定的用户;
3.1. Configuration files
[[email protected] /]# vim /etc/exports/nfsshared/node1 172.16.86.210/24(rw)#mount端口应该固定防止占用其他端口[[email protected] node1]# vim /etc/sysconfig/nfs MOUNTD_PORT=892
4. Start
[[email protected] /]# systemctl start nfs#nfs内部实现机制太复杂了nfs-idmapd.service loaded active running NFSv4 ID-name mapping service id名字映射nfs-mountd.service loaded active running NFS Mount Daemon 认证挂载nfs-server.service loaded active exited NFS server and services 主服务rpc-statd.service loaded active running NFS status monitor for NFSv2/3 locking. 状态监控rpcbind.service loaded active running RPC bind service nfs注册到rpc服务
5, the Client
[[email protected] test]# yum install nfs-utils[[email protected] test]# showmount -e 172.16.86.210Export list for 172.16.86.210:/nfsshared/node1 172.16.86.210/24#挂载[[email protected] test]# mount.nfs 172.16.86.210:/nfsshared/node1 /mnt/t4[[email protected] test]# mount|grep nfs172.16.86.210:/nfsshared/node1 on /mnt/t4 type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.86.209,local_lock=none,addr=172.16.86.210)客户端挂载优化参数timeo增加,rsize wsize增大
6. Write
#写入时候文件都是看当前用户的id,id解析成用户名压缩理解客户端如果是root ID=0服务器如果不压缩,文件写入id=0 服务器解释id=0 为root,客户端也是root服务器如果压缩,文件写入id是服务器nfsnobody对应id 假如是100 客户端反接100得到用户名普通用户,客户端写入时候是zander id=10000 ,显示zander,服务器端显示id=10000的用户#服务器[[email protected] node1]# chmod o+w f1/[[email protected] node1]# lsf1#客户端在f1下面写入[[email protected] f1]# touch a.txt[[email protected] f1]# lltotal 0-rw-r--r-- 1 nfsnobody nfsnobody 0 May 20 22:09 a.txt
6.1 The best way to write
指定用户客户端无论什么身份运行,服务器都是按照指定的用户存储,客户端反解服务器指定用id来显示,前提是服务器指定的用户要有对目录操作权限服务器[[email protected] f1]# useradd marvin[[email protected] f1]# id marvinuid=1003(marvin) gid=1003(marvin) 组=1003(marvin)[[email protected] f1]# vim /etc/exports/nfsshared/node1 172.16.86.210/24(rw,anonuid=1003,anongid=1003)#reload[[email protected] f1]# exportfs -ravexporting 172.16.86.210/24:/nfsshared/node1#客户端[[email protected] f1]# touch c.txt[[email protected] f1]# lsc.txt[[email protected] f1]# lltotal 0-rw-r--r-- 1 1003 1003 0 May 20 22:27 c.txt
Linux NFS Introduction