Linux系统搭建NFS网络共享存储
一、NFS概述;
NFS是一种基于TCP/IP传输的网络文件系统协议,最初由SUN公司开发。
通过NFS协议,客户机可以像访问本地目录一样访问远程服务器中的共享资源。
NFS得到了如NAS等网络存储的设备极好支持。也是LVS共享存储的首选。
二、安装
1.服务端安装nfs-utils、rcpbind软件包;
rpcbind软件包提供RPC(远程系统调用)机制支持,nfs软件包提供共享服务
[root@nfs ~]# yum -y install nfs-utils rpcbind
设置为开机启动
[root@nfs ~]# for i in rpcbind nfs;do systemctl enable $i; done
2.服务端设置共享目录;
[root@nfs ~]# mkdir /opt/wwwroot
设置权限
[root@nfs ~]# chmod 777 /opt/wwwroot
nfs配置共享位置的配置文件
[root@nfs ~]# vi /etc/exports
/opt/wwwroot 192.168.100.0/24(rw,sync,no_root_squash)
注解:
文件格式:目录 主机(权限)
权限:rw读写 ro只读 sync同步写入 no_root_squash表示客户机以root身份访问时,赋予其本地root权限,默认是root_squash,表示以nfsnobody用户降权使用
3.服务器端启动nfs服务程序;
先启动rpcbind服务,在启动nfs服务,不然容易启动失败
[root@nfs ~]# systemctl start rpcbind
[root@nfs ~]# systemctl start nfs
验证端口,NFS端口随机分配
[root@nfs ~]# netstat -utpln |grep rpc
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 1656/rpc.mountd
tcp 0 0 0.0.0.0:51806 0.0.0.0:* LISTEN 1429/rpc.statd
udp 0 0 0.0.0.0:953 0.0.0.0:* 1630/rpcbind
udp 0 0 0.0.0.0:42998 0.0.0.0:* 1429/rpc.statd
udp 0 0 0.0.0.0:20048 0.0.0.0:* 1656/rpc.mountd
udp 0 0 0.0.0.0:111 0.0.0.0:* 1630/rpcbind
udp 0 0 127.0.0.1:757 0.0.0.0:* 1429/rpc.statd
查看本机共享目录
[root@nfs ~]# showmount -e 192.168.100.103
Export list for 192.168.100.103:
/opt/wwwroot 192.168.100.0/24
4.客户端安装NFS客户端程序;
[root@client ~]# yum -y install nfs-utils rpcbind
查看共享目录
[root@client ~]# showmount -e 192.168.100.103
Export list for 192.168.100.103:
/opt/wwwroot 192.168.100.0/24
清除系统缓存
[root@client ~]# echo 3 >/proc/sys/vm/drop_caches
5.客户端mount方式挂载使用nfs共享;
创建本地挂载点
[root@client ~]# mkdir /test
挂载
[root@client ~]# mount 192.168.100.103:/opt/wwwroot /test/
验证
[root@client ~]# mount |grep nfs
192.168.100.103:/opt/wwwroot on /test type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.100.104,local_lock=none,addr=192.168.100.103)
通过网络方式进行永久挂载
[root@client ~]# vi /etc/fstab
192.168.100.103:/opt/wwwroot /test nfs defaults,_netdev 0 0
注释:
_netdev表示设备需要网络
客户端创建测试文件
[root@client ~]# touch /test/test.txt
[root@client ~]# ls /test/
test.txt
服务端验证
[root@nfs ~]# ls /opt/wwwroot/
test.txt