NFS服務的搭建與配置

NFS的服務器介紹:mysql

NFS是Network File System的縮寫
 NFS最先由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,最新爲4.1版本
 NFS數據傳輸基於RPC協議,RPC爲Remote Procedure Call的簡寫。
 NFS應用場景是:A,B,C三臺機器上須要保證被訪問到的文件是同樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致linux

NFS的原理圖:nginx

NFS服務器的安裝與配置:sql

yum install -y nfs-utils rpcbind
 vim /etc/exports //加入以下內容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
 保存配置文件後,執行以下準備操做
 mkdir /home/nfstestdir
 chmod 777 /home/nfstestdir
 systemctl start rpcbind
 systemctl start nfs
 systemctl enable rpcbind
 systemctl enable nfsvim

 

[root@aming01 ~]# vim /etc/exports  //編輯配置文件信息
[root@aming01 ~]# cat /etc/exports
/home/nfstestdir  192.168.88.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
[root@aming01 ~]# mkdir  /home/nfstestdir  //建立共享的目錄
[root@aming01 ~]# chmod 777 /home/nfstestdir/
[root@aming01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
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      944/nginx: master p
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      890/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      988/master          
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      944/nginx: master p
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      890/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      988/master          
tcp6       0      0 :::3306                 :::*                    LISTEN      1326/mysqld         
[root@aming01 ~]# systemctl start nfs     //啓動nfs
[root@aming01 ~]# ps aux|grep nfs
root       2675  0.0  0.0      0     0 ?        S<   15:19   0:00 [nfsd4_callbacks]
root       2681  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2682  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2683  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2684  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2685  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2686  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2687  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2688  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2722  3.0  0.0 112724   988 pts/0    R+   15:19   0:00 grep --color=auto nfs
[root@aming01 ~]#服務器

NFS配置選項:app

 rw 讀寫
 ro 只讀
 sync 同步模式,內存數據實時寫入磁盤
 async 非同步模式
 no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大
 root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶
 all_squash 客戶端上全部用戶在使用NFS共享目錄時都被限定爲一個普通用戶
 anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和giddom

客戶端掛載:ssh

yum install -y nfs-utils
 showmount -e 192.168.133.130 //該ip爲NFS服務端ip
 mount -t nfs 192.168.133.130:/home/nfstestdir /mnt
 df -h
 touch /mnt/aminglinux.txt
 ls -l /mnt/aminglinux.txt //能夠看到文件的屬主和屬組都爲1000async

 

exportfs的命令

 經常使用選項
 -a 所有掛載或者所有卸載
 -r 從新掛載
 -u 卸載某一個目錄
 -v 顯示共享目錄
 如下操做在服務端上
vim /etc/exports //增長
/tmp/ 192.168.133.0/24(rw,sync,no_root_squash)
 exportfs -arv //不用重啓nfs服務,配置文件就會生效

[root@aming01 ~]# df -h
文件系統        容量  已用  可用 已用% 掛載點
/dev/sda3        38G  4.1G   34G   11% /
devtmpfs        479M     0  479M    0% /dev
tmpfs           489M     0  489M    0% /dev/shm
tmpfs           489M  6.6M  482M    2% /run
tmpfs           489M     0  489M    0% /sys/fs/cgroup
/dev/sda1       252M  100M  153M   40% /boot
tmpfs            98M     0   98M    0% /run/user/0
[root@aming01 ~]# exportfs -arv
exporting 192.168.88.0/24:/home/nfstestdir
[root@aming01 ~]# vim /etc/exports
[root@aming01 ~]# cat /etc/exports
/home/nfstestdir  192.168.88.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp   192.168.88.129(rw,sync,no_root_squash)
[root@aming01 ~]# exportfs -arv
exporting 192.168.88.129:/tmp
exporting 192.168.88.0/24:/home/nfstestdir
[root@aming01 ~]# ls /tmp/

 

 如下操做在客戶端
 mkdir /aminglinux
 mount -t nfs -onolock 192.168.133.130:/tmp /aminglinux
 touch /aminglinux/test.txt
 ls -l !$
 -oremount,nfsvers=3

[root@aming02 mnt]# showmount -e 192.168.88.128
Export list for 192.168.88.128:
/home/nfstestdir 192.168.88.0/24
/tmp             192.168.88.129
[root@aming02 mnt]# mount -t nfs 192.168.88.128:/tmp/  /mnt/
[root@aming02 mnt]# ls /mnt/

 

客戶端文件屬主屬組nobody

 NFS 4版本會有該問題  客戶端掛載共享目錄後,無論是root用戶仍是普通用戶,建立新文件時屬主、屬組爲nobody  客戶端掛載時加上 -o nfsvers=3  客戶端和服務端都須要  vim /etc/idmapd.conf //  把「#Domain = local.domain.edu」 改成 「Domain = xxx.com」 (這裏的xxx.com,隨意定義吧),而後再重啓rpcidmapd服務

相關文章
相關標籤/搜索