NFS服务端和客户端的配置

NFS(NFS介绍,NFS服务端和客户端配置)

一、NFS服务介绍
NFS是 Network File system的缩写
分为2.3.4三个版本,2和3由sun公司起草开发,4.0开始netapp公司参与并主导开发
NFS数据传输基于RPC协议:
应用场景:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。
NFS原理图:(NFS服务不监听任何端口,但是RPC服务中的模块,rpcbind默认监听111端口,)

二、NFS服务端和客户端配置

[root@litongyao ~]# yum install -y nfs-utils         (客户端和服务端都要按章这个包)

接下来在服务端编辑:

[root@fuwuduan ~]# vim /etc/exports
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

第一段来定义共享目录的绝对路径  第二段指定ip和一些选项
rw 读写
ro 只读
sync 同步模式,内存数据实时写入磁盘
async 非同步模式
no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

保存配合文件以后,因为共享目录不存在,所以做以下操作。

[root@fuwuduan ~]# mkdir /home/nfstestdir                       (创建共享目录)
[root@fuwuduan ~]# chmod 777 /home/nfstestdir/                    (权限设置为777)

我们可以看一下监听的端口

[root@fuwuduan ~]# netstat -lntp
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      4451/rpcbind   

rpcbind监听的111端口。

启动服务并设置开机启动:

[root@fuwuduan ~]# systemctl start nfs
[root@fuwuduan ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

查看进程

[root@fuwuduan ~]# ps ax |grep nfs
 6209 ?        S<     0:00 [nfsd4_callbacks]
 6215 ?        S      0:00 [nfsd]
 6216 ?        S      0:00 [nfsd]
 6217 ?        S      0:00 [nfsd]
 6218 ?        S      0:00 [nfsd]
 6219 ?        S      0:00 [nfsd]
 6220 ?        S      0:00 [nfsd]
 6221 ?        S      0:00 [nfsd]
 6222 ?        S      0:00 [nfsd]
 6264 pts/1    R+     0:00 grep --color=auto nfs

客户端操作:

[root@kehu ~]# showmount -e 192.168.52.101     (查看远程共享信息)
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

我们发现报错,这是由于网络不通造成,是由防火墙导致我,所以我们把客户端和服务端的防火墙和selinux关闭

[root@fuwuduan ~]# systemctl stop firewalld
[root@fuwuduan ~]# setenforce 0

下面就可以正常了。

[root@kehu ~]# showmount -e 192.168.52.101    (我们可以看到ip为101的机器上为我们共享的目录)
Export list for 192.168.52.101:
/home/nfstestdir 192.168.52.100/24

挂载目录并与远程共享目录同步:

[root@kehu ~]# mount -t nfs 192.168.52.101:/home/nfstestdir /mnt/  (指定格式是nfs)

查看挂载目录:

[root@kehu ~]# df -h
文件系统                         容量  已用  可用 已用% 挂载点
/dev/sda3                         18G  4.7G   14G   27% /
devtmpfs                         483M     0  483M    0% /dev
tmpfs                            493M     0  493M    0% /dev/shm
tmpfs                            493M   13M  480M    3% /run
tmpfs                            493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                        197M  109M   88M   56% /boot
tmpfs                             99M     0   99M    0% /run/user/0
192.168.52.101:/home/nfstestdir   18G  3.6G   15G   20% /mnt

实验:

在客户端建立一个文件,看看服务端有没有

nfs 服务端和客户端的配置 RPC安装

10.14.40.151为例
nfs服务配置
服务端安装nfs
yum -y install nfs-utils

安装nfs+rpc

yum -y install nfs-utils rpcbind
配置在服务端创建一个共享目录
mkdir -p /data1/nfsshare
chmod 666 /data1/nfsshare
修改 NFS 配置文件 /etc/exports
cat /etc/exports
/data1/nfsshare *(rw,sync,insecure,no_subtree_check,no_root_squash)
重载nfs
[root@test151 ~]# exportfs -rv
exporting *:/data1/nfsshare

先启动 RPC 服务

systemctl start rpcbind
#设置开机启动
systemctl enable rpcbind

检查启动是否成功

rpcinfo -p localhost ,如果显示rpc 服务器注册的端口列表(端口:111),则启动成功

[root@test151 ~]# rpcinfo -p localhost
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper

启动 NFS 服务

systemctl start nfs
systemctl enable nfs

客户端的安装 10.14.40.124 为例

安装nfs服务

yum -y install nfs-utils

检测rpc服务

rpcinfo -p
[root@test124 node]# rpcinfo -p
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper

查看服务器端挂在目录

showmount -e 10.14.40.151
[root@test124 node]# showmount -e 10.14.40.151
Export list for 10.14.40.151:
/data1/nfsshare *

挂载服务器端的目录

rm -fr /data1/nfsshare;mkdir /data1/nfsshare
mount -t nfs 10.14.40.151:/data1/nfsshare /data1/nfsshare

整理完成脚本

#!/bin/sh
# 
# config nfs service for nodelocal
#

#安装配置nfs 客户端
function nfs_cli(){
	nfsshare=$1
	nfsip=$2
	if [[ -z ${nfsshare} ]];then
		echo "input nfsshare dir"
		exit 1
	fi
	if [[ -z ${nfsip} ]];then
		echo "input nfs server ip"
		exit 2
	fi
	yum -y install nfs-utils
	if [[ $? -ne 0 ]];then
	       echo "install nfs—utils failed"
	       exit 3
       fi	       
	rpcinfo -p
	if [[ $? -ne 0 ]];then
		echo "check rpc info failed"
		exit 1
	fi
	showmount -e ${nfsip}
	if [[ $? -ne 0 ]];then
		echo "check remote nfs server failed"
		exit 1
	fi
	rm -fr ${nfsshare}
	mkdir ${nfsshare}
	mount -t nfs ${nfsip}:${nfsshare} ${nfsshare}
	if [[ $? -ne 0 ]];then
		echo "mount remote nfs share dir failed"
		exit 1;
	fi
}


#安装配置 nfs server 服务
function nfs_server(){
	nfsshare=$1
	nfsip=$2
	if [[ -z ${nfsshare} ]];then
		echo "input nfsshare dir"
		exit 1
	fi
	if [[ -z ${nfsip} ]];then
		echo "input nfs server ip"
		exit 2
	fi
	yum -y install nfs-utils
	if [[ $? -ne 0 ]];then
	       echo "install nfs—utils failed"
	       exit 3
       fi	       
	rpcinfo -p
	if [[ $? -ne 0 ]];then
		echo "check rpc info failed"
		exit 1
	fi
	mkdir ${nfsshare}

	echo "${nfsshare} *(rw,sync,insecure,no_subtree_check,no_root_squash)" >/etc/exports
	exportfs -rv
	if [[ $? -ne 0 ]];then
		echo "mount remote nfs share dir failed"
		exit 1;
	fi
	#启动rpc服务
	systemctl start rpcbind
	systemctl enable rpcbind

	#启动nfs服务
	systemctl start nfs
	systemctl enable nfs
}

#帮助信息
function usage(){
	echo "input param num 3" 
	echo "param1 nfs share dir"  
	echo "param2 nfs server ip info"
	echo "param3 only support {cli|server}"
}

if [[ $# -ne 3 ]];then
	usage;
	exit
fi

nfsshare=$1
nfsip=$2
optional=$3
case "$3" in
	cli)
		echo "config nfs client info start ......"
		nfs_cli ${nfsshare} ${nfsip}
		;;
	server)
		echo "config nfs server info start ......"
		nfs_server ${nfsshare} ${nfsip}
		;;
	*)
		usage;
		echo "Usage:$3 {cli|server}"
		exit 1
esac
posted @ 2022-05-05 17:00  鸟不拉诗  阅读(240)  评论(0编辑  收藏  举报