1.nfs介紹(網絡文件系統)
它是一種分佈式系統協議,它允許客戶端上訪問服務器, nfs使用的端口是2049tcp/utp的套接字上,它的實現需要rpc(遠程過程調用協議)
優點:
節省本地存儲空間,將常用的數據存放在一臺NFS服務器上且可以通過網絡訪問,那麼本地終端將可以減少自身存儲空間的使用
用戶不需要在網絡中的每個機器上都建有Home目錄,Home目錄可以放在NFS服務器上且可以在網絡上被訪問使用
一些存儲設備如軟驅、CDROM和Zip(一種高儲存密度的磁盤驅動器與磁盤)等都可以在網絡上被別的機器使用。這可以減少整個網絡上可移動介質設備的數量
缺點:
曾經不能在windows上掛載,現在可以通過其他方式實現
它只能用ip去掛載,無法通過域名
2.實現NFS服務器
開放/nfs/shared目錄,供所有用戶查閱資料
開放/nfs/upload目錄爲172.16.0.0/24網段的數據上傳目錄,並將所有用戶及所屬的用戶組都映射爲nfs-upload,其UID與GID均爲300
3.實戰思路
設備介紹 服務器 Rtest ip 172.16.0.1 和 客戶端 Centos6.5 ip 172.16.0.2
3.1 在Server和Client上同時安裝nfs-utils
3.2 在Server的配置文件裏面添加nfs的共享文件夾以及權限(可讀可寫,anonuid和anongid,no_all_squash), 同時將文件設置爲777權限.
3.3 在客戶端上創建一個用戶叫nfs-upload,並且uid和gid爲300
4.實戰操作 + (防火牆)
4.1 開啓防火牆
server# systemctl start firewalld client# systemctl start firewalld
4.2 在服務器上的防火牆上添加nfs需要的服務(nfs,rpc-bind,mountd),在客戶端的防火牆上添加rpc-bind
server# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.0.0/24 service name=nfs accept' --permanent server# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.0.0/24 service name=rpc-bind accept' --permanent server# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.0.0/24 service name=mountd accept' --permanent client# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=172.16.0.0/24 service name=rpc-bind accept' --permanent
4.3 安裝nfs服務.
server# yum install nfs-utils -y server# systemctl enable nfs-server server# systemctl restart nfs-server client# yum install nfs-utils -y
4.4 在Server上配置文件
server# echo '/nfs/shared 172.16.0.0/24(rw,anonuid=300,anongid=300,no_all_squash)' >> /etc/exports server# exportfs -r server# showmount -e localhost ##可以看到文件
4.5 在客戶端上創建用戶,且掛載
client# useradd -r -u 300 nfs-upload client# mount 172.16.0.1/nfs/shared /mnt
4.6 驗證,在裏面創建文件看是不是需求的用戶
client# cd /mnt client# touch aaa client# ll aaa ##可以看到文件的屬主和屬組是nfs-upload
5.腳本運行
5.1server端
#! /bin/bash rm -rf /etc/yum.repos.d/* cat > /etc/yum.repos.d/xx.repo < EOF [xx] baseurl=file:///yum gpgcheck=0 EOF yum install nfs-utils -y systemctl restart nfs-server firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.16.0.2 service name=nfs accept' --permanent firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.16.0.2 service name=rpc-bind accept' --permanent firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.16.0.2 service name=mountd accept' --permanent firewall-cmd --reload cat > /etc/exports < EOF /nfs/share 172.16.0.0/24(rw,no_all_squash,anonuid=300,anongid=300) EOF mkdir -p /nfs/share chmod 777 /nfs/share exportfs -r showmount -e localhost
5.2client端
#!/bin/bash rm -rf /etc/yum.repos.d/* touch /etc/yum.repos.d/ftp.repo cat > /etc/yum.repos.d/ftp.repo < EOF [xx] baseurl=ftp://172.16.0.1:/yum/ gpgcheck=0 EOF yum install nfs-utils -y userdel -r nfs-upload useradd -r -u 300 nfs-upload iptables -A INTPUT -dport -p 111 tcp -j ACCEPT mount 172.16.0.1:/nfs/share /mnt echo "172.16.0.1:/nfs/share /mnt nfs defaults,_netdev 0 0" >> /etc/fstab cd /mnt/ && touch test ll |grep nfs-upload