天天看點

nfs

伺服器端:(192.168.10.10)

首先建立一個測試的共享目錄,修改exports檔案

[root@server ~]# mkdir /nfs_share

[root@server ~]# vim /etc/exports

/nfs_share *(rw)

~

[root@server ~]# systemctl restart nfs-server

[root@server ~]# systemctl enable nfs-server.service

• 1

• 2

• 3

• 4

• 5

• 6

nfs伺服器已經配置好了,不過還得需要開啟防火牆,把nfs服務添加到防火牆中去指令如下

[root@server ~]# firewall-cmd --permanent --add-service=nfs

success

[root@server ~]# firewall-cmd --reload

挂載方式一:客戶機端(192.168.10.20)

首先需要ping通伺服器,保證網絡的互通

修改客戶機的/etc/fstab的檔案 ,添加下面這一行,然後儲存

[root@client ~]# vim /etc/fstab

192.168.10.10:/nfs_share /nfs nfs sync 0 0

"/etc/fstab" 14L, 697C written

執行mount -a指令 沒有報錯,說明挂載成功,df -h指令檢視

[root@client ~]# mount -a

[root@client ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda2 15G 2.9G 12G 20% /

devtmpfs 906M 0 906M 0% /dev

tmpfs 914M 80K 914M 1% /dev/shm

tmpfs 914M 9.0M 905M 1% /run

tmpfs 914M 0 914M 0% /sys/fs/cgroup

/dev/sr0 3.5G 3.5G 0 100% /yum

/dev/sda3 15G 125M 15G 1% /var

/dev/sda1 297M 91M 207M 31% /boot

192.168.10.10:/nfs_share 15G 2.9G 12G 20% /nfs

• 7

• 8

• 9

• 10

• 11

• 12

問題1:在挂載的目錄裡建立一個檔案,發現建立失敗,這是因為預設的權限選項是root_squash,禁止遠端使用者具有root權限

[root@client ~]# touch /nfs/myhost

touch: cannot touch ‘/nfs/myhost’: Permission denied

在伺服器端檢視cat /var/lib/nfs/etab 檔案,發現有很多選項

[root@server ~]# cat /var/lib/nfs/etab

/nfs_share (rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)

解決方案1:

在伺服器端的/etc/exports添加以下配置

/nfs_share (rw,no_root_squash)

[root@server ~]# exportfs -a #不要輕易重新開機nfs服務,否則客戶機端會出現卡頓的現象,盡量使用exportfs指令

解決方案2:伺服器端恢複之前的配置

/nfs_share (rw)

#在伺服器端把共享的目錄other的權限添加寫入的權限

[root@server ~]# chmod o+w /nfs_share -R

[root@server ~]# ll -d /nfs

drwxrwxrwx. 2 root root 33 Jul 19 10:37 /nfs_share

在客戶機端測試

[root@client ~]# touch /nfs/myhost1

[root@client ~]# ll /nfs/

total 0

-rw-rw-rw-. 1 root root 0 Jul 19 10:29 myhost

-rw-r--r--. 1 nfsnobody nfsnobody 0 Jul 19 10:37 myhost1

完美解決!!!!

另外,客戶機端還有一種方式可以用來自動挂載nfs服務的,那就是autofs服務

挂載方式二:在客戶機端(192.168.10.10)

• autofs服務的間接映射

首先在伺服器建立/nfs_share1目錄,并且這個目錄共享出去

mkdir /nfs_share1

echo -e“/nfs_share1\t*(rw)”>>/etc/exports

然後在客戶機安裝autofs服務

yum -y install autofs

重新開機autofs服務并且加入自啟動項中

[root@client ~]# systemctl enable autofs

ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'

[root@client ~]# systemctl restart autofs

編輯autofs的主映射檔案

#這裡的主映射檔案的字尾名一定要以autofs結尾,其中/auto為基礎目錄,就是挂載點的上級目錄,/etc/nice.auto為二級映射檔案,檔案名字可以随便

[root@client ~]# vim /etc/auto.master.d/nice.autofs

/auto /etc/nice.auto

編輯autofs的二級映射檔案

#nice為挂載點,挂載的路徑相當于/auto/nice 中間為挂載的權限,必須以-開頭,最後192.168.1.11:/nfs_share1表示伺服器的位址以及共享目錄,中間以":"隔開.

[root@client ~]# vim /etc/nice.auto

nice -rw 192.168.10.10:/nfs_share1

下一篇: nfs

繼續閱讀