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