180116 NFS介绍
NFS是Network File System的缩写
NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本
NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。
NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致
NFS服务端安装配置
- yum install -y nfs-utils rpcbind
- vim /etc.exports /home/nfstestdir 192.168.9.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
- mkdir /home/nfstestdir
- chmod 777 /home/nfstestdir
- systemctl start rpcbind
- systemctl start nfs
- systemctl enable rpcbind
- systemctl enable nfs
[[email protected] ~]# yum install -y nfs-utils rpcbind
[[email protected] ~]# vi /etc/exports
[[email protected] ~]# cat /etc/exports
/home/nfstestdir 192.168.88.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
[[email protected] ~]# mkdir /home/nfstestdir/
[[email protected] ~]# chmod 777 /home/nfstestdir
[[email protected] ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 938/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1110/master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 938/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1110/master
tcp6 0 0 :::3306 :::* LISTEN 1152/mysqld
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# ps aux |grep nfs
root 2948 0.0 0.0 0 0 ? S< 09:20 0:00 [nfsd4_callbacks]
root 2954 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2955 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2956 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2957 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2958 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2959 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2960 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2961 0.0 0.0 0 0 ? S 09:20 0:00 [nfsd]
root 2966 0.0 0.0 112676 984 pts/3 S+ 09:20 0:00 grep --color=auto nfs
[[email protected] ~]# ps aux |grep rpc
rpcuser 2918 0.2 0.1 42376 1748 ? Ss 09:20 0:00 /usr/sbin/rpc.statd
rpc 2920 0.1 0.1 64956 1332 ? Ss 09:20 0:00 /sbin/rpcbind -w
root 2921 0.0 0.0 0 0 ? S< 09:20 0:00 [rpciod]
root 2925 0.0 0.0 43816 548 ? Ss 09:20 0:00 /usr/sbin/rpc.idmapd
root 2928 0.0 0.0 42564 944 ? Ss 09:20 0:00 /usr/sbin/rpc.mountd
root 2969 0.0 0.0 112676 984 pts/3 S+ 09:20 0:00 grep --color=aut
[[email protected] ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
NFS配置选项
rw 读写
ro 只读
sync 同步模式,内存数据实时写入磁盘
async 非同步模式
no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid
客户端挂载
yum install -y nfs-utils
showmount -e 192.168.133.130 //该ip为NFS服务端ip
mount -t nfs 192.168.133.130:/home/nfstestdir /mnt
df -h
touch /mnt/aminglinux.txt
ls -l /mnt/aminglinux.txt //可以看到文件的属主和属组都为1000
[[email protected] ~]# yum install -y nfs-utils
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# getenforce
Disabled
[[email protected] ~]# showmount -e 192.168.88.15
Export list for 192.168.88.15:
/home/nfstestdir 192.168.88.0/24
[[email protected] ~]# mount -t nfs 192.168.88.15:/home/nfstestdir /mnt/
[[email protected] ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 40G 4.3G 36G 11% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.88.15:/home/nfstestdir 55G 3.5G 52G 7% /mnt