1. 程式人生 > 其它 >NFS服務端和客戶端的配置

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