nfs
一 概念
網絡文件系統(NFS)是Unix系統和網絡附加存儲文件管理器常用的網絡文件系統,允許多個客戶端通過網絡共享文件訪問。它可用於提供對共享二進制目錄的訪問,也可用於允許用戶在同一工作組中從不同客戶端訪問其文件。
二 firewalld的設定
在防火牆開啓的情況下,要想訪問nfs則firewalld要添加以下策略:
1 firewall-cmd --permanent --add-service=nfs###允許nfs###
2 firewall-cmd --reload
3 firewall-cmd --permanent --add-service=rpc-bind###nfs的訪問端口不確定,是由rpc-bind分配端口###
4 firewall-cmd --reload
5 firewall-cmd --permanent --add-service=mountd###允許掛載###
6 firewall-cmd --reload
7 systemctl start nfs
過程如下:
[root@localhost ~]# firewall-cmd --permanent --add-service=nfs###允許nfs###
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --permanent --add-service=rpc-bind###nfs的訪問端口不確定,是由rpc-bind分配端口###
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --permanent --add-service=mountd###允許掛載###
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# systemctl start nfs
測試:
[kiosk@foundation12 Desktop]$ showmount -e 172.25.254.112###顯示指定nfs服務器中的共享目錄列表,若只是showmount-e 則顯示當前主機中nfs服務器的輸出列表###
Export list for 172.25.254.112:
[kiosk@foundation12 Desktop]$
三 共享目錄
1)共享給所有人
nfsf服務端:
mkdir /public###建立共享目錄###
touch /public/westostest{1..3}
vim /etc/exports
內容:
/public*(sync)###public共享給所有人並且數據同步(ro)是隻讀###
共享目錄共享方式
exportfs -rv###讀取/etc/exports爲文件中的設置並生效###
測試:
客戶端:
showmount -e 172.25.12.11
mount 172.25.12.11:/public /mnt/###將共享目錄掛載到/mnt下###
過程如下:
服務端:
[root@server ~]# vim /etc/exports
[root@server ~]# exportfs -rv
exporting 172.25.12.0/24:/public
客戶端:
[root@desktop12 ~]# showmount -e 172.25.12.11
Export list for 172.25.12.11:
/public *
[root@desktop12 ~]# mount 172.25.12.11:/public /mnt/###掛載###
[root@desktop12 ~]# cd /mnt/
[root@desktop12 mnt]# ls
westostest westostest1 westostest2 westostest3
[root@desktop12 mnt]# touch file
touch: cannot touch ‘file’: Read-only file system
2)共享給某個主機(例:172.25.12.10)
nfs服務端:
vim /etc/exports
內容:
/public172.25.12.10(sync)###public共享給主機172.25.12.10並且數據同步###
共享目錄共享方式
exportfs -rv###生效###
測試:
服務端(172.25.12.10):
3)共享給某個網段(例:172.25.78.0/24)
nfs服務端:
1 vim /etc/exports
內容:
/public 172.25.78.0/24(sync)###public共享給在該網段的所有主機並且數據同步###
2 exportfs -rv
測試:
客戶端:
[root@foundation78 Desktop]# mount 172.25.78.11:/public /mnt/
[root@foundation78 Desktop]# cd /mnt/
[root@foundation78 mnt]# ls
westostest1 westostest2 westostest3
3)共享給某個域
nfs服務端:
1 vim /etc/exports
內容:
/public *.example.com(sync)###public共享給在該域的所有主機並且數據同步###
2 exportfs -rv
測試:
showmount -e 172.25.78.11
過程如下:
nfs服務端:
[root@server ~]# vim /etc/exports
[root@server ~]# exportfs -rv
exporting *.example.com:/public
[root@server ~]#
測試:
[kiosk@foundation78 Desktop]$ showmount -e 172.25.78.11
Export list for 172.25.78.11:
/public *.example.com
[kiosk@foundation78 Desktop]$ rht-vmctl view desktop
[kiosk@foundation78 Desktop]$
4)允許共享給多主機
nfs服務端:
1 vim /etc/exports
內容:
/public 172.25.78.10(sync) 172.25.78.250(rw,sync)###共享給10主機和12主機,並且允許12主機進行寫操作###
2 exportfs -rv
3 chmod 777 /public###當訪問共享目錄時,用戶的超級身份發生了改變,對該目錄來說,是other,所以要將該目錄修改權限爲777###
注意:NFS服務器將NFS客戶端上的root視爲用戶nfsnobody。即,如果root嘗試訪問掛載的
導出中的文件,服務器會將其視作用戶nfsnobody訪問
過程如下:
nfs服務端:
[root@server ~]# vim /etc/exports
[root@server ~]# ls -ld /public/###該目錄只有超級用戶可寫###
drwxr-xr-x. 2 root root 63 Jun 6 23:27 /public/
[root@server ~]# chmod 777 /public/###修改權限,使得其他人可寫###
[root@server ~]# exportfs -rv
exporting 172.25.78.11:/public
exporting 172.25.78.250:/public
測試:
172.25.78.10主機:(不能對共享目錄進行寫操作)
[root@desktop ~]# mount 172.25.78.11:/public /mnt/
[root@desktop ~]# cd /mnt/
[root@desktop mnt]# ls
westoslinux1 westoslinux2 westoslinux3
[root@desktop mnt]# rm -fr *
rm: cannot remove ‘westoslinux1’: Read-only file system###只具有讀權限#
rm: cannot remove ‘westoslinux2’: Read-only file system
rm: cannot remove ‘westoslinux3’: Read-only file system
172.25.78.250主機:(具有讀寫權限)
[root@foundation78 Desktop]# mount 172.25.78.11:/public /mnt/
[root@foundation78 Desktop]# cd /mnt/
[root@foundation78 mnt]# ls
westoslinux1 westoslinux2 westoslinux3
[root@foundation78 mnt]# touch file1
[root@foundation78 mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 6 23:31 file1###超級用戶發生了改變###
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux1
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux2
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux3
[root@foundation78 mnt]#
5)當客戶端使用超級用戶掛載,不轉換超級用戶
nfs服務端:
1 vim /etc/exports
內容:
/public 172.25.78.250(rw,sync,no_root_squash) ###共享給和12主機,並且允許12主機進行寫操作且不轉換超級用戶###
2 exportfs -rv
過程如下:
nfs服務端:
[root@server ~]# vim /etc/exports
[root@server ~]# exportfs -rv
exporting 172.25.78.250:/public
測試:
[root@foundation78 ~]# mount 172.25.78.11:/public /mnt/
[root@foundation78 ~]# cd /mnt/
[root@foundation78 mnt]# ls
file1 westoslinux1 westoslinux2 westoslinux3
[root@foundation78 mnt]# touch file2
[root@foundation78 mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 6 23:31 file1
-rw-r--r--. 1 root root 0 Jun 6 23:44 file2###超級用戶沒有改變###
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux1
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux2
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux3
[root@foundation78 mnt]#
5)指定訪問時的超級用戶
nfs服務端:
1 vim /etc/exports
內容:
/public 172.25.78.250(rw,sync,anonuid=1001,anongid=1001) ###anonuid=1001,anongid=1001,指定用戶的uid和gid###
2 exportfs -rv
過程如下:
nfs服務端:
[root@server ~]# useradd westos
[root@server ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
[root@server ~]# vim /etc/exports
[root@server ~]# exportfs -rv
exporting 172.25.78.250:/public
[root@server ~]#
測試:
[root@foundation78 ~]# mount 172.25.78.11:/public /mnt/
[root@foundation78 ~]# cd /mnt/
[root@foundation78 mnt]# ls
file1 file2 westoslinux1 westoslinux2 westoslinux3
[root@foundation78 mnt]# touch file3
[root@foundation78 mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 6 23:31 file1
-rw-r--r--. 1 root root 0 Jun 6 23:44 file2
-rw-r--r--. 1 1001 1001 0 Jun 6 23:51 file3###用戶和組均爲指定的uid和gid###
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux1
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux2
-rw-r--r--. 1 root root 0 Jun 6 23:27 westoslinux3
[root@foundation78 mnt]#
6)永久掛載nfs文件系統:
vim /etc/fstab
內容:
172.25.78.11:/public /mnt nfs defaults 0 0
7)客戶端NFS掛載選項
1 rw:掛載可讀寫的文件系統
2 ro:掛載只讀文件系統
3 vers=4:嘗試只使用指定的NFS版本進行掛載。如果服務器不支持該版本,則掛載請求失敗
4 soft:如果NFS請求超時,三次嘗試後返回錯誤。權衡數據完整性與提高客戶端響應性。(默認行爲hard,將無限期地重試)
四 利用kerberos保護nfs輸出
當客戶端將共享目錄掛載到自己系統的某個目錄下,那麼其他用戶登入到該主機,並且進入到該目錄下,就可以看到共享的所有目錄和文件,只要具有相應的權限,就可以對文件進行相應的操作,因此要保護nfs的輸出。
nfs服務端:
1 yum install sssd krb5-workstation authconfig-gtk -y
2 authconfig-gtk###開啓kerberos,得到ldap用戶###
3 su - student
4 su - ldapuser1###只有輸入密碼後才能得到鑰匙###
5 klist###查看票###
6 wget http://172.25.254.254/pub/keytabs/server12.keytab -O /etc/krb5.keytab###得到證書###
7 ktutil###查看證書是否下載對###
8 rkt /etc/krb5.keytab
9 list
10 systemctl restart nfs-secure-server
11 systemctl enable nfs-secure-server
12 vim /etc/exports
內容:
/public*(rw,sec=krb5p)###sec=krb5p指認證方式###
13 exportfs -rv
nfs客戶端:
1 yum install sssd krb5-workstation authconfig-gtk -y
2 authconfig-gtk###開啓kerberos,得到ldap用戶###
3 su - student
4 su - ldapuser1###只有輸入密碼後才能得到鑰匙###
5 klist###查看票###
6 wget http://172.25.254.254/pub/keytabs/desktop12.keytab -O /etc/krb5.keytab###下載證書###
7 systemctl start nfs-secure
8 systemctl enable nfs-secure
9 mount -o sec=krb5p 172.25.12.11:/public /mnt/###掛載###
測試:
有key的可以掛載,有證書的可以訪問nfs共享目錄
[root@desktop12 Desktop]# mount -o sec=krb5p 172.25.12.11:/public /mnt/
[root@desktop12 Desktop]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3228684 7245216 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 140 496568 1% /dev/shm
tmpfs 496708 13056 483652 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/sr0 3654720 3654720 0 100% /run/media/root/RHEL-7.0 Server.x86_64
172.25.12.11:/public 10473984 3207168 7266816 31% /mnt
[root@desktop12 Desktop]# cd /mnt/
[root@desktop12 mnt]# ls
westoslinux1 westoslinux2 westoslinux3
[root@desktop12 mnt]# cd
[root@desktop12 ~]# su - student
Last login: 六 6月 3 23:30:14 EDT 2017 on pts/0
[student@desktop12 ~]$ cd /mnt###普通用戶沒有key和證書不能訪問###
-bash: cd: /mnt: Permission denied
[student@desktop12 ~]$ su - ldapuser1###ldapuser1有證書和key可以訪問###
Password:
Last login: Sat Jun 3 23:30:30 EDT 2017 on pts/0
su: warning: cannot change directory to /home/guests/ldapuser1: No such file or directory
mkdir: cannot create directory '/home/guests': Permission denied
-bash-4.2$ cd /mnt/
-bash-4.2$ ls
westoslinux1 westoslinux2 westoslinux3
-bash-4.2$