NFS網絡文件系統服務實戰(六)
NFS網絡文件系統服務實戰:
一、按要求搭建配置NFS服務前準備
準備三個服務器或虛擬機A、B、C配置NFS服務器:
要求:
在NFS服務端A上共享/data/w_shared及/data/r_shared兩個文件目錄,允許從NFS客戶端B,C上分別掛載共享目錄後可實現從B,C上只讀/data/r_shared,可寫/data/w_shared
NFS客戶端B上的掛載點為/data/b_w(寫),/data/b_r(讀)
NFS客戶端C上的掛載點為/data/w_你的名字英文(寫),/data/r_你的名字英文(讀)
3)從NFS客戶端B上的NFS可在掛載點目錄創建任意文件,從C上可以刪除這個創建的文件,反之也可以。
解答:
1.、系統環境準備
服務器系統 | 角色 | IP |
Centos64-130-server | NFS服務器A | 192.168.221.130 |
Centos64-131-client | NFS客戶端B | 192.168.221.131 |
Centos58-133-client | NFS客戶端C | 192.168.221.133 |
主機名或規範:
nfs-A-C64 192.168.221.130
nfs-B-C64 192.168.221.131
nfs-C-C58 192.168.221.133
2.臨時關閉selinux和iptables防火墻(將來工作中selinux可關,有外部ip的服務器要開iptables。)
關閉selinux
★修改配置文件需要重啟機器:
修改/etc/selinux/config 文件
vim /etc/selinux/config
將SELINUX=enforcing改為SELINUX=disabled
或者:
sed -i‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/selinux/config #修改配置文件永久生效,但必須重啟系統
★清空iptables
iptables -F #清理防火墻規則
iptables -L #查看防火墻規則
/etc/init.d/iptables save #保存防火墻配置信息
3.查看系統環境:
分別使用cat /etc/redhat-release
uname -n
uname -a
查看系統版本和內核版本,系統名等相關信息,
使用ifconfig分別查看A,B,C三臺虛擬機的ip
二、NFS服務端配置
1.開啟nfs 相關服務NFS和rpcbind(C5.8portmap),並設置開機自啟動
★安裝rpcbind (C5.8 portmap)
[[email protected] ~]# yum install rpcbind nfs-utils –y
★檢查啟動rpcbind
[[email protected] ~]# /etc/init.d/rpcbindstatus rpcbind is stopped [[email protected] ~]# /etc/init.d/rpcbindstart Starting rpcbind: [ OK ]
★檢查並啟動nfs
[[email protected] ~]# /etc/init.d/nfs status rpc.svcgssd is stopped rpc.mountd is stopped nfsd is stopped rpc.rquotad is stopped [[email protected] ~]# /etc/init.d/nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Stopping RPC idmapd: [ OK ] Starting RPC idmapd: [ OK ] Starting NFS daemon: [ OK ]
★設置rpcbind和nfs開機自啟動,
[[email protected] ~]# echo "#NFS sharedat mrxiong to 2017.6.5">>/etc/rc.local [[email protected] ~]# echo "/etc/init.d/rpcbindstart">>/etc/rc.local [[email protected] ~]# echo "/etc/init.d/nfsstart">>/etc/rc.local [[email protected] ~]# tail -3 /etc/rc.local #NFS shared at Mrxiong to 2017.6.5 /etc/init.d/rpcbind start /etc/init.d/nfs start
註:rpcbind一定要先於nfs啟動
2.統一所有服務器的nfs共享用戶,一般是nfsnobodyuid是65534。當然也可以自己創建一個NFS共享用戶
[[email protected] ~]# grep 65534 /etc/passwd nfsnobody:x:65534:65534:Anonymous NFSUser:/var/lib/nfs:/sbin/nologin [[email protected] ~]# grep 65534 /etc/passwd nfsnobody:x:65534:65534:Anonymous NFSUser:/var/lib/nfs:/sbin/nologin [[email protected] ~]# grep 65534 /etc/passwd nfsnobody:x:65534:65534:Anonymous NFSUser:/var/lib/nfs:/sbin/nologin
3.配置nfs服務端
★創建要共享的目錄
[[email protected]~]# mkdir -p /data/w_shared /data/r_shared
★根據需求授權指定權限(不要777,太大)
[[email protected] ~]# chown -R nfsnobody.nfsnobody /data/w_shared/data/r_shared [[email protected] ~]# ls -ld /data/* drwxr-xr-x 2 nfsnobody nfsnobody 4096 Jan 19 22:25 /data/r_shared drwxr-xr-x 2 nfsnobody nfsnobody 4096 Jan 19 22:25 /data/w_shared
★配置NFS配置文件/etc/exports
[[email protected] ~]# echo "#NFS shared at dingjian 2017.6.5">>/etc/exports [[email protected] ~]# echo "/data/w_shared 192.168.221.1/24(rw,sync)">>/etc/exports [[email protected] ~]# echo"/data/r_shared 192.168.221.1/24(ro,sync)">>/etc/exports [[email protected]~]# tail -3 /etc/exports #NFS shared at dingjian 2017.6.5 /data/w_shared 192.168.221.1/24(rw,sync) /data/r_shared 192.168.221.1/24(ro,sync)
★平滑重啟reload或exportfs -rv
[[email protected] ~]# exportfs -rv exporting 192.168.221.1/24:/data/r_shared exporting 192.168.221.1/24:/data/w_shared
★showmount -elocalhost 一下看配置有沒有成功
[[email protected] ~]# showmount -e localhost Export list for localhost: /data/r_shared 192.168.221.1/24 /data/w_shared 192.168.221.1/24
三:配置客戶端
.配置nfs-B-C64,客戶端
★安裝和開啟rpcbind(C5.8為portmap)服務,並加入到開機自啟動rc.local (不需要開啟nfs服務)
C64-B
[[email protected] ~]# yum install rpcbind -y [[email protected] ~]# /etc/init.d/rpcbindstatus rpcbind is stopped [[email protected] ~]# /etc/init.d/rpcbindstart Starting rpcbind: [ OK ] [[email protected] ~]# /etc/init.d/rpcbindstatus rpcbind (pid 1607) is running...
★創建掛載點,然後執行showmount命令查看掛載信息,如果不通可以使用ping ip或telnet ip port來檢查
C64-B
[[email protected] ~]# mkdir /data/b_w/data/b_r -p [[email protected] ~]# ls -l /data total 8 drwxr-xr-x. 2 root root 4096 Jan 19 22:36b_r drwxr-xr-x. 2 root root 4096 Jan 19 22:36b_w [[email protected] ~]# showmount -e192.168.221.130 ----->提示找不到這個命令 -bash: showmount: command not found [[email protected] ~]# yum install showmount-y [[email protected] ~]# showmount -e192.168.221.130 Export list for 192.168.221.130: /data/r_shared 192.168.221.1/24 /data/w_shared 192.168.221.1/24
★通過mount命令執行掛載
C64-B
[[email protected] ~]# mount -t nfs 192.168.221.130:/data/w_shared/data/b_w [[email protected] ~]# mount -t nfs 192.168.221.130:/data/r_shared/data/b_r
★配置mount掛載命令使開機自啟動
a./etc/rc.local裏。缺點:偶爾開機掛載不上,工作中對其監控掛載點
[[email protected] b_w]# echo "mount -tnfs 192.168.221.130:/data/w_shared /data/b_w">>/etc/rc.local [[email protected] b_w]# echo "mount-t nfs 192.168.221.130:/data/r_shared /data/b_r">>/etc/rc.local [[email protected] b_w]# tail -2 /etc/rc.local mount -t nfs 192.168.1.121:/data/w_shared /data/b_w mount -t nfs 192.168.1.121:/data/r_shared /data/b_r
.配置nfs-C-C64,客戶端
C58-C
[[email protected]~]# /etc/init.d/portmap status portmap (pid 2656)is running... [[email protected] ~]# mkdir /data/w_dingjian/data/r_dingjian -p [[email protected] ~]# ls -l /data total 8 drwxr-xr-x 2 root root 4096 Jan 19 22:38r_dingjian drwxr-xr-x 2 root root 4096 Jan 19 22:38w_dingjian [[email protected] ~]# showmount -e192.168.221.130 Export list for 192.168.221.130: /data/r_shared 192.168.221.1/24 /data/w_shared 192.168.221.1/24
C58-C
[[email protected] ~]# mount -t nfs 192.168.221.130:/data/r_shared/data/r_dingjian [[email protected] ~]# mount -t nfs 192.168.221.130:/data/w_shared/data/w_dingjian
★配置mount掛載命令使開機自啟動
/etc/rc.local裏。缺點:偶爾開機掛載不上,工作中對其監控掛載點
[[email protected] ~]# echo " mount -tnfs 192.168.221.130:/data/r_shared /data/r_dingjian ">>/etc/rc.local [[email protected] ~]#echo " mount -t nfs192.168.221.130:/data/w_shared /data/w_dingjian ">>/etc/rc.local [[email protected] ~]# tail -2 /etc/rc.local mount -t nfs 192.168.221.130:/data/r_shared/data/r_dingjian mount -t nfs 192.168.221.130:/data/w_shared/data/w_dingjian
四、使用df-h或者cat /proc/mounts查看掛載情況
C64-B
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 1.4G 16G 9% / tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 190M 27M 153M 15% /boot 192.168.221.130:/data/w_shared 19G 1.4G 17G 8% /data/b_w 192.168.221.130:/data/r_shared 19G 1.4G 17G 8% /data/b_r
C58-C
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 18G 2.1G 15G 13% / /dev/sda1 198M 13M 176M 7% /boot tmpfs 499M 0 499M 0% /dev/shm 192.168.221.130:/data/r_shared 19G 1.4G 17G 8% /data/r_dingjian 192.168.221.130:/data/w_shared 19G 1.4G 17G 8% /data/w_dingjian
★根據要求做實際測試檢查
分別對應要求來進行讀寫測試
如果把mount寫到/etc/fstab裏。缺點:NFS服務器端處不可用狀態時,那麽客戶端開機可能會導致無法啟動的風險。Fstab最後兩列,要設置0 0。工作中對監控掛載點
個人建義使用/etc/rc.local
察看掛載後的狀態。
[[email protected] ~]# mount |grep‘\<nfs\>‘ sunrpc on /var/lib/nfs/rpc_pipefs typerpc_pipefs (rw) 192.168.221.130:/data/w_shared on /data/b_wtype nfs (rw,vers=4,addr=192.168.221.130,clientaddr=192.168.221.131) 192.168.221.130:/data/r_shared on /data/b_rtype nfs (rw,vers=4,addr=192.168.221.130,clientaddr=192.168.221.131)
--------------------------------------------------------------------
可以看到已經掛到了當前的文件系統了。
本文出自 “Mr.Xiong`s 運維日誌” 博客,請務必保留此出處http://mrxiong2017.blog.51cto.com/12559394/1932434
NFS網絡文件系統服務實戰(六)