1. 程式人生 > >NFS安裝與配置使用

NFS安裝與配置使用

NFS介紹

  • NFS是 Network File System 的縮寫,即網路檔案系統
  • NFS最早由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發
  • NFS資料傳輸基於RPC協議,RPC為Romote Procedure Call的簡寫
  • NFS的應用場景:A,B,C三臺機器上需要保證被訪問到的檔案是一樣的,A共享資料出來,B和C分別去掛載A共享的資料目錄,從而B和C訪問到的資料和A上的一致。

NFS架構圖

NFS原理圖

NFS服務端/客戶端安裝

服務端安裝與配置

[[email protected]
~]# yum install -y nfs-utils rpcbind [[email protected] ~]# vim /etc/exports [[email protected] ~]# cat /etc/exports /home/nfstestdir 192.168.77.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) [[email protected] ~]# mkdir /home/nfstestdir [[email protected] ~]# chmod 777 /home/nfstestdir [[email protected]
~]# netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2376/master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1714/nginx: master tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1714/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1206/sshd tcp6 0 0 ::1:25 :::* LISTEN 2376/master tcp6 0 0 :::3306 :::* LISTEN 2405/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1206/sshd [
[email protected]
~]# systemctl start nfs # 啟動nfs服務 [[email protected] ~]# ps aux | grep nfs root 9022 0.0 0.0 0 0 ? S< 01:59 0:00 [nfsd4] root 9023 0.0 0.0 0 0 ? S< 01:59 0:00 [nfsd4_callbacks] root 9027 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9028 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9029 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9030 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9031 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9032 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9033 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9034 0.0 0.0 0 0 ? S 01:59 0:00 [nfsd] root 9050 0.0 0.0 112704 972 pts/0 R+ 01:59 0:00 grep --color=auto nfs [[email protected] ~]# systemctl enable nfs # 允許nfs開機啟動

客戶端安裝

[[email protected] ~]# yum install -y nfs-utils

NFS選項說明

NFS選項 功能描述
ro 只讀共享
rw 讀寫
sync 同步模式,記憶體資料實時寫入磁碟
async 非同步模式,即非同步模式
no_root_squash 客戶端掛載NFS共享目錄後,root使用者不受約束,許可權很大
root_squash 與上面選項相對,客戶端上的root使用者受到約束,被限定成某個普通使用者
all_squash 客戶端上所有使用者在使用NFS共享目錄時都被限定為一個普通使用者
anonuid/anongid 和上面幾個選項搭配使用,定義被限定使用者的uid和gid

測試

客戶端

[[email protected] ~]# showmount -e 192.168.77.134 # 看192.168.77.134這臺機器上開啟的nfs服務是否能訪問,提示通訊失敗,同時關閉服務端和客戶端的防火牆和SELinux
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

[[email protected] ~]# setenforce 0 
[[email protected] ~]# getenforce
Permissive
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# showmount -e 192.168.77.134 # 都關閉後再次測試OK
showmount -e 192.168.77.134
Export list for 192.168.77.134:
/home/nfstestdir 192.168.77.0/24

# 掛載
[[email protected] ~]# mount -t nfs 192.168.77.134:/home/nfstestdir /mnt/
[[email protected] ~]# df -h
檔案系統                         容量  已用  可用 已用% 掛載點
/dev/sda3                         26G  879M   25G    4% /
devtmpfs                         489M     0  489M    0% /dev
tmpfs                            494M     0  494M    0% /dev/shm
tmpfs                            494M   13M  481M    3% /run
tmpfs                            494M     0  494M    0% /sys/fs/cgroup
/dev/sda1                        197M   76M  122M   39% /boot
tmpfs                             99M     0   99M    0% /run/user/0
192.168.77.134:/home/nfstestdir   26G  9.4G   17G   37% /mnt
[[email protected] ~]# cd /mnt/ # 進入掛載目錄
[[email protected] mnt]# ls
[[email protected] mnt]# touch test.aaaa  

服務端

# 關閉防火牆,SELinux
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
# 在客戶端touch test.aaaa後檢視
[[email protected] ~]# ls /home/nfstestdir/ -l
total 0
-rw-r--r--. 1 test03 grouptest01 0 Dec  9 02:45 test.aaaa