NFS伺服器搭建與使用

2021-08-09 08:30:55 字數 3610 閱讀 4886

軟體包:

nfs-utils

kernel支援:

nfs.ko

埠:2049(nfsd), 其它埠由portmap(111)分配

配置檔案:

/etc/exports,/etc/exports.d/*.exports

差別:centos7不支援同一目錄同時用nfs和samba共享,因為使用鎖機制不同,centos6開portmaprpcbind代替,centos7服務名是nfs-server,centos6服務名是nfs

nfs服務主要程序:

rpc.nfsd 最主要的nfs程序,管理客戶端是否可登入

rpc.mountd 掛載和解除安裝nfs檔案系統,包括許可權管理

rpc.lockd 非必要,管理檔案鎖,避免同時寫出錯

rpc.statd 非必要,檢查檔案一致性,可修復檔案

日誌:/var/lib/nfs/

配置檔案格式/dir 主機1(opt1,opt2) 主機2(opt1,opt2)...主機格式:

單個主機: ipv4, ipv6, fqdn

ip networks:兩種掩碼格式均支援

172.18.0.0/255.255.0.0

172.18.0.0/16

wildcards:主機名通配,例如*.example.com, ip不可以

netgroups: nis域的主機組, @group_name

anonymous:表示使用*通配所有客戶端

選項:預設選項: (ro,sync,root_squash,no_all_squash)

ro,rw 唯讀和讀寫

async 非同步,資料變化後不立即寫磁碟,效能高

sync(1.0.0後為預設)同步,資料在請求時立即寫入共享

no_all_squash (預設)保留共享檔案的uid和gid

all_squash 所有遠端使用者(包括root)都變成nfsnobody

root_squash (預設)遠端root對映為nfsnobody,uid為65534,早期版本是4294967294 (nfsnobody)

no_root_squash 遠端root對映成root使用者

anonuid和anongid 指明匿名使用者對映為特定使用者uid和組gid,而非nfsnobody,可配合all_squash使用

在/etc/exports檔案中定義示例

/myshare server.example

.com

/myshare *.example

.com

/myshare server?.example

.com

/myshare server[0-20].example

.com

/myshare 172.25

.11.10

/myshare 172.25

.0.0/16

/myshare 2000:472:18:b51:c32:a21

/myshare 2000:472:18:b51::/64

/myshare *.example

.com

172.25

.0.0/16

/myshare desktop.example

.com(ro)

/myshare desktop.example

.com(ro) server[0-20].example

.com(rw)

/myshare diskless.example

.com(rw,no_root_squash)

rpcinfo

rpcinfo -p hostname

rpcinfo –s hostname 檢視rpc註冊程式

exportfs

–v 檢視本機所有nfs共享

–r 重讀配置檔案,並共享目錄

–a 輸出本機所有共享

–au 停止本機所有共享

showmount -e hostip 顯示nfs主機共享目錄

基於安全考慮,建議使用nosuid,nodev,noexec掛載選項

fg(預設)前台掛載, bg後台掛載

hard(預設)持續請求, soft 非持續請求

intr 和hard配合,請求可中斷

rsize和wsize 一次讀和寫資料最大位元組數, rsize=32768

_netdev 無網路不掛載

示例:mount -o rw,nosuid,fg,hard,intr 172.16.0.1:/testdir /mnt/nfs/

開機掛載:/etc/fstab

172.16.0.1:/public /mnt/nfs nfs defaults 0 0

可使用autofs按需要掛載nfs共享,在空閒時自動解除安裝

由autofs包提供

系統管理器指定由/etc/auto.master自動掛載器守護程序控制的掛載點

自動掛載監視器訪問這些目錄並按要求掛載檔案系統

檔案系統在失活的指定間隔5分鐘後會自動解除安裝

為所有匯出到網路中的nfs啟用特殊匹配 -host 至「browse」

更多選項檢視autofs(5)

[root@centos7 ~]# vi /etc/auto.master

/misc /etc/auto.misc

[root@centos7 ~]# vi /etc/auto.misc

cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

* -fstype=nfs 192.168.6.6

:/&#想掛載本地和遠端一樣的目錄可以使用這個方法,但只適用於相對路徑法

不會影響本地目錄結構

[root@centos7 ~]# vi /etc/auto.master

/- /etc/auto.direct

[root@centos7 ~]# vi /etc/auto.direct

:/data

配置nfs伺服器

vi /etc/fstab

/data/read /exports/read none bind 0

0 /data2/write /exports/write none bind 0

0vi /etc/exports

/exports *(fsid=o,rw,crossmnt)

/exports/read

192.168.0.0/24(ro) #針對192.168這個網段對read目錄設定唯讀許可權

/exports/write

192.168.0.0/24(rw)

配置nfs客戶端

mount 192.168.6.6

:/ /mnt/nfs

vi /etc/fstab

192.168.6.6

:/ /mnt/ nfs4 ro 0

0

NFS 伺服器搭建

首先在vmware上搭建兩個系統我選擇的兩個ubuntu系統 ubuntu1 ip位址192.168.1.49 ubuntu2 ip位址192.168.1.51 兩個系統分別執行下面的命令 sudo apt get install nfs kernel server 安裝 nfs 伺服器端 sudo...

搭建NFS伺服器

搭建乙個nfs伺服器 nfs應用場景及好處 nfs配置 在服務端和客戶端安裝nfs utils 在服務端啟動服務 systemctl start rpcbind nfs serversystemctl start rpcbind首先建立共享目錄,mkdir p nfs 在修改 etc exports...

搭建NFS伺服器。

本指導中的nfs客戶端指執行harmonyos核心的裝置。硬體連線設定。harmonyos核心裝置連線到nfs伺服器的網路。設定兩者ip,使其處於同一網段。比如,設定nfs伺服器的ip為10.67.212.178 24,設harmonyos核心裝置ip為10.67.212.3 24。harmonyo...