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服務端/客戶端安裝

服務端安裝與配置

[root@test-a ~]# yum install -y nfs-utils rpcbind
[root@test-a ~]# vim /etc/exports
[root@test-a ~]# cat /etc/exports
/home/nfstestdir 192.168.77.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

[root@test-a ~]# mkdir /home/nfstestdir
[root@test-a ~]# chmod 777 /home/nfstestdir
[root@test-a ~]# 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

[root@test-a ~]# systemctl start nfs  # 啓動nfs服務
[root@test-a ~]# 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

[root@test-a ~]# systemctl enable nfs # 允許nfs開機啓動

客戶端安裝

[root@centos0 ~]# 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

測試

客戶端

[root@centos0 ~]# 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)

[root@centos0 ~]# setenforce 0 
[root@centos0 ~]# getenforce
Permissive
[root@centos0 ~]# systemctl stop firewalld
[root@centos0 ~]# 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

# 掛載
[root@centos0 ~]# mount -t nfs 192.168.77.134:/home/nfstestdir /mnt/
[root@centos0 ~]# 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
[root@centos0 ~]# cd /mnt/ # 進入掛載目錄
[root@centos0 mnt]# ls
[root@centos0 mnt]# touch test.aaaa  

服務端

# 關閉防火牆,SELinux
[root@test-a ~]# systemctl stop firewalld
[root@test-a ~]# setenforce 0
# 在客戶端touch test.aaaa後查看
[root@test-a ~]# ls /home/nfstestdir/ -l
total 0
-rw-r--r--. 1 test03 grouptest01 0 Dec  9 02:45 test.aaaa
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章