CentOS7 下安裝 NFS,Linux/Windows 做爲客戶端

 

1、簡介

1. 定義

NFS (Network File System),最初由 Sun Microsystems 於1984年開發的分佈式系統協議,容許客戶端上的用戶經過網絡訪問文件,其方式與訪問本地存儲的方式相似。基於 Open Network Computing Remote Procedure Call (ONC RPC) 協議,NFS 是經過 Request for Comments(RFC) 定義的開放標準,容許任何人實現該協議。html

2. 版本和變化

Version RFC Date Variations
NFSv2 RFC 1094 March 1989 UDP,無狀態;32位,僅容許讀取文件的前2GB
NFSv3 RFC 1813 June 1995 支持TCP;64位,突破2GB;異步寫入;在許多響應報文中額外增長文件屬性
NFSv4 RFC 3010 December 2000 集成了對文件鎖定和掛載協議的支持;增長了對強安全性(及其協商)、複合操做、客戶端緩存和國際化的支持
NFS 4.1 RFC 5661 January 2010 會話、目錄委託、並行NFS (pNFS)
NFS 4.2 RFC 7862 November 2016

3. 部署說明

本文描述如何在 CentOS 7 上安裝 NFS,並在 Linux 和 Windows 下使用 NFS 客戶端進行鏈接。linux

hostname ip role 描述
nfs-server 192.168.0.135 server Linux nfs server
nfs-client 192.168.0.136 client Linux client
Windows 192.168.0.120 client Windows client

 

2、服務端

1. 關閉防火牆

# systemctl stop firewalld
# systemctl disable firewalld

2. 安裝 nfs

# yum -y install nfs-utils

3. 配置說明

經過文件 /etc/exports 來對 NFS 進行配置。shell

It follows the following syntax rules:windows

  • Comments start with the hash mark (#).
  • Blank lines are ignored by default.
  • Each host’s options must be placed in parentheses directly after the host identifier, without any spaces separating the host and the first parenthesis.
  • Each exported file system should be on its own individual line.
  • A list of authorized hosts needs to be separated by space characters.
  • Long lines can be wrapped with a backslash ().

NFS export default options are:centos

  • ro: The exported file system is read-only and remote hosts cannot make any changes to the files shared on the file system. To allow hosts to make both reads and writes, specify the rw option instead.
  • sync: Aith this option, NFS server does not reply to requests before changes made by previous requests are written to disk. To enable asynchronous writes instead, specify the option async.
  • root_squash: This prevents root users that connect remotely from having root privileges. Instead, the NFS server will assign them the user ID nfsnobody. This effectively 「squashes」 the power of the remote root user to the lowest local user, preventing possible unauthorized writes on the remote server. To disable root squashing, specify no_root_squash.
  • To squash every remote user (including root), use all_squash. To specify the user and group IDs that the NFS server should assign to remote users from a particular host, use the anonuid and anongid options.
  • wdelay: This reduces disk write overhead by delaying writing to the disk if it suspects another write request is imminent. This can be disabled using no_wdelay, when default sync is on.
  • subtree_check: This option enables subtree checking. It can be disabled using no_subtree_check.

4. 配置共享目錄

(1)修改 exports
這裏將 /mnt/data 做爲共享目錄,開放讀寫權限緩存

# vi /etc/exports
/mnt/data 192.168.0.0/24(rw,no_root_squash)

注:這裏的共享目錄可使用 Ceph 塊設備掛載的文件夾,關於如何使用塊設備,請參考 塊設備快速入門安全

(2)使配置生效服務器

# exportfs -r

(3)查看 exports網絡

# exportfs -v
/mnt/data       192.168.0.0/24(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash)

5. 啓動服務

(1)啓動 rpcbind

//開機啓動
# systemctl enable rpcbind

//啓動
# systemctl start rpcbind
//重啓
# systemctl restart rpcbind

(2)啓動 nfs-server

//開機啓動
# systemctl enable nfs-server

//啓動
# systemctl start nfs-server
//重啓
# systemctl restart nfs-server

6. 確認啓動成功

# rpcinfo -p
# exportfs
/mnt/data       192.168.0.0/24

 

3、Linux 客戶端

1. 安裝 nfs

# yum -y install nfs-utils

2. 啓動 rpcbind

//開機啓動
# systemctl enable rpcbind
//啓動
# systemctl start rpcbind
//重啓
# systemctl restart rpcbind

3. 掛載

建立目錄

# mkdir /data

掛載 nfs,-o 指定版本

# mount -t nfs -o vers=3 192.168.0.135:/mnt/data /data
or
# mount -t nfs -o vers=4 192.168.0.135:/mnt/data /data

查看掛載結果

# df -hT | grep /data
192.168.0.135:/mnt/data nfs        17G  985M   17G    6% /data
or
192.168.0.135:/mnt/data nfs4       17G  985M   17G    6% /data
# dh -h
文件系統                 容量  已用  可用 已用% 掛載點
192.168.0.135:/mnt/data   17G  982M   17G    6% /data

4. 自動掛載

磁盤被手動掛載以後,須要把掛載信息寫入 /etc/fstab 這個文件中,不然下次開機啓動時仍然須要從新掛載。

例如對於 NFSv3,修改 /etc/fstab

192.168.0.135:/mnt/data  /data nfs defaults,vers=3 0 0
or
192.168.0.135:/mnt/data /data nfs vers=3,proto=tcp,hard,intr,rsize=32768,wsize=32768,noatime 0 0

執行掛載命令

# mount -a

查看掛載結果

# df -hT | grep /data
192.168.0.135:/mnt/data nfs        17G  985M   17G    6% /data

5. 解掛

# umount /data

 

4、Windows 客戶端

1. 打開NFS服務

(1)Windows 功能 - 啓用或關閉 Windows 功能

(2)經過命令提示符顯示 NFS 服務器

showmount -e 192.168.0.135
/mnt/data                          192.168.0.0/24

2. 掛載

方法1:映射網絡驅動器

方法2:經過命令掛載

mount 192.168.0.135:/mnt/data Z:

3. 查看

方式1:打開個人點腦,就能夠在網絡位置看到 Z:盤了

方式2:經過命令查看

mount

本地    遠程                                 屬性
-------------------------------------------------------------------------------
Z:       \\192.168.0.135\mnt\data               UID=-2, GID=-2
                                                rsize=262144, wsize=262144
                                                mount=soft, timeout=3.2
                                                retry=1, locking=yes
                                                fileaccess=755, lang=GB2312-80
                                                casesensitive=no
                                                sec=sys

4. 修改權限

對掛載盤進行寫操做時,提示權限不足!

若是出現這種狀況,解決辦法:
(1)在運行中輸入regedit,打開註冊表編輯器。
(2)進入 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default 條目。
(3)選擇新建 QWORD,新建 AnonymousUid,AnonymousGid 兩個值,值爲0。
(4)重啓 NFS 服務 或 電腦。

5. 解掛

umount Z:

 

參考連接

https://wiki.archlinux.org/index.php/NFS
https://en.wikipedia.org/wiki/Network_File_System
https://computingforgeeks.com/configure-nfsv3-and-nfsv4-on-centos-7/

相關文章
相關標籤/搜索