NFS
1.1NFS定義:
NFS(Network File System) 網絡文件系統,它的工作機制是基於rpc來實現網絡文件系統共享的。
1.2NFS作用
'1.通過局域網讓不同的主機系統之間可以共享文件或目錄
2.節省本地存儲空間,本地終端將可以減少自身存儲空間的使用
3.用戶不需要在網絡中的每個機器都建有Home目錄,home目錄可以放在NFS服務器,可以在網絡上被訪問
4.減少整個網絡上可移動介質設備的數量(存儲設備:軟驅、COROM、zip(一種高儲存密度的磁盤驅動器與磁盤)等可以在網絡上被別的機器使用。'
1.3nfs的體系組成
一臺nfs服務器
若干臺客戶機
如圖:
2.1RPC
RPC(Remote Procedure Call Protocol),遠程過程調用協議,通過網絡從遠程計算機程序上請求服務,在OSI網絡通信模型中,RPC跨越了傳輸層和應用層。
RPC採用客戶機/服務器模式。請求程序就是客戶機,而服務提供程序就是一個服務器。
2.2NFS系統原理
'Rpc.nfsd:它是基本的NFS守護進程,主要功能是管理客戶端是否能夠登錄服務器'
'Rpc.mount: 主要功能是管理NFS的文件系統。當客戶端順利通過nfs登錄NFS服務器後,在使用NFS服務所提供的文件前,還必須通過文件使用權限的驗證。
它會讀取NFS的配置文件/etc/exports來對比客戶端權限
Portmap:主要功能是進行端口映射工作’
3.1搭建nfs服務器環境準備
服務器系統 | NFS服務端(nfs-serverA) | NFS客服端(nfs-clientB) |
---|---|---|
Centos | 192.168.228.20 | 192.168.228.21 |
3.2基礎環境配置
開啓nfs服務之前,必須關閉防火牆
'開機不啓動防火牆'
[root@yaoxiaorong ~]# systemctl disable firewalld
'查看firewalld防火牆是否開啓'
[root@yaoxiaorong ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2018-07-30 08:39:34 HKT; 1 weeks 0 days ago
Docs: man:firewalld(1)
Main PID: 693 (firewalld)
CGroup: /system.slice/firewalld.service
└─693 /usr/bin/python -Es /usr/sbin/firewal...
Jul 30 08:39:28 yaoxiaorong systemd[1]: Starting firew...
Jul 30 08:39:34 yaoxiaorong systemd[1]: Started firewa...
Jul 30 08:39:36 yaoxiaorong firewalld[693]: WARNING: I...
Jul 30 08:39:36 yaoxiaorong firewalld[693]: WARNING: b...
Jul 30 08:39:36 yaoxiaorong firewalld[693]: WARNING: I...
Jul 30 08:39:36 yaoxiaorong firewalld[693]: WARNING: f...
Jul 30 08:39:36 yaoxiaorong firewalld[693]: WARNING: I...
Jul 30 08:39:36 yaoxiaorong firewalld[693]: WARNING: r...
Hint: Some lines were ellipsized, use -l to show in full.
'關閉firewalld防火牆'
[root@yaoxiaorong ~]# systemctl stop firewalld.service
關閉selinux防火牆
'Selinux(Security-Enhanced Linux)是美國國家安全局(NSA)對於強制訪問控制的實現,是linux歷史上最傑出的新安全子系統。Selinux默認安裝在Fedora和Red Hat Enterprise Linux上,也可以作爲其他發行版上容易安裝的包得到。強制訪問控制(MAC)安全系統,Selinux支持三種模式:'
'#Enforcing:強制模式,代表SElinux運作中,已經正確的開始限制domain/type'
'#Permissive:寬容模式,代表SElinux運作中,不過僅會有警告訊息並不會實際限制domain/type的存取。這種模式可以運來作爲selinux的debug只用'
'#Disabled:關閉'
'查看selinux防火牆的狀態'
[root@yaoxiaorong ~]# getenforce
Enforcing
'臨時關閉selinux防火牆'
[root@yaoxiaorong ~]# setenforce 0
[root@yaoxiaorong ~]# getenforce
Permissive
'永久關閉selinux防火牆,但是必須設置完畢之後重啓系統'
[root@yaoxiaorong ~]# vim /etc/selinux/config
*將SELINUX=enforcing修改成---->SELINUX=disabled*
3.3安裝NFS-server
'安裝nfs服務主程序'
[root@yaoxiaorong ~]# yum install nfs-utils rpcbind -y
3.4啓動NFS-Server
'啓動rpcbind服務'
[root@yaoxiaorong ~]# systemctl start rpcbind
'加入開機自啓動'
[root@yaoxiaorong ~]# systemctl enable rpcbind
'啓動nfs服務'
[root@yaoxiaorong ~]# systemctl start nfs-server
'加入開機自啓動'
[root@yaoxiaorong ~]# systemctl enable nfs-server
'查看rpc註冊信息'
[root@yaoxiaorong ~]# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
查看系統中運行的NFS相關進程
[root@yaoxiaorong ~]# ps -ef |egrep "rpc|nfs"
rpc 17208 1 0 15:09 ? 00:00:00 /sbin/rpcbind -w
rpcuser 17271 1 0 15:13 ? 00:00:00 /usr/sbin/rpc.statd
root 17272 2 0 15:13 ? 00:00:00 [rpciod]
root 17282 1 0 15:13 ? 00:00:00 /usr/sbin/rpc.mountd
root 17283 1 0 15:13 ? 00:00:00 /usr/sbin/rpc.idmapd
root 17293 2 0 15:13 ? 00:00:00 [nfsd4_callbacks]
root 17299 2 0 15:13 ? 00:00:00 [nfsd]
root 17300 2 0 15:13 ? 00:00:00 [nfsd]
root 17301 2 0 15:13 ? 00:00:00 [nfsd]
root 17302 2 0 15:13 ? 00:00:00 [nfsd]
root 17303 2 0 15:13 ? 00:00:00 [nfsd]
root 17304 2 0 15:13 ? 00:00:00 [nfsd]
root 17305 2 0 15:13 ? 00:00:00 [nfsd]
root 17306 2 0 15:13 ? 00:00:00 [nfsd]
root 17332 16143 0 15:16 pts/0 00:00:00 grep -E --color=auto rpc|nfs
3.4配置/etc/exports文件
'/etc/exports' 主配置文件,文件中的項格式相當簡單,要共享一個文件系統
nfs主配置文件中的常用選項:
'secure' 缺省項,它使用了1024以下的TCP/IP端口實現NFS的連接,指定insecure可以禁用這個選項
'rw' 允許客戶機進行讀/寫訪問
'async' 此選項可以改進性能,但如果沒有完全關閉NFS守護進程就重啓了NFS服務器,但可能會造成數據丟失
'no_wdelay' 選項關閉延時,如果設置了async,那麼NFS就會忽略此選項
'nohide' 若將一個目錄掛載到另一個目錄之上,則原來的目錄通常就被隱藏起來,或看起來像空的一樣,要禁用這種行爲,需啓動hide選項
'no_subtree_check' 此選項關閉子樹檢查,子樹檢查會執行一些不想忽略的安全性檢查。缺省選項是啓用子樹檢查
'no_auth_nlm' 此選項可作爲insecure_locks指定,它告訴NFS守護進程不要對鎖請求進行認證。若關心安全性問題,就要避免使用此選項,缺省選項是auth_nlm或secure_locks
'mp(mountpoint=path' 通過顯式的聲明此選項,NFS要求掛載所導出的目錄
'fsid=num' 此選項通常在NFS故障恢復時使用
'編輯nfs主配置文件,共享/gongxiang目錄至192.168.228.21,可讀寫'
[root@yaoxiaorong ~]# vim /etc/exports
[root@yaoxiaorong ~]# cat /etc/exports
/gongxiang 192.168.228.21(rw,async)
'創建需要共享的目錄'
[root@yaoxiaorong ~]# mkdir -p /gongxiang
'重啓nfs服務'
[root@yaoxiaorong ~]# systemctl restart nfs-server
'查看NFS分享出來的目錄的完整權限設置值的配置文件'
[root@yaoxiaorong ~]# cat /var/lib/nfs/etab
/gongxiang 192.168.228.21(rw,async,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,secure,root_squash,no_all_squash)
'查看65534屬於哪個用戶'
[root@yaoxiaorong ~]# grep "65534" /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
'遞歸賦予/gongxiang目錄屬主nfsnobody'
[root@yaoxiaorong ~]# chown -R nfsnobody.nfsnobody /gongxiang/
'給共享/gongxiang目錄權限777'
[root@yaoxiaorong ~]# chmod 777 /gongxiang/
'重啓nfs-server服務生效'
[root@yaoxiaorong ~]# systemctl restart nfs-server.service
3.5客戶端查看NFS服務器端共享的文件系統
'先安裝showmount'
[root@yaoxiaorong ~]# yum install showmount -y
[root@yaoxiaorong ~]# showmount -e 192.168.228.20
Export list for 192.168.228.20:
/gongxiang 192.168.228.21
3.6掛載NFS文件系統
[root@yaoxiaorong ~]# mount -t nfs 192.168.228.20:/gongxiang /mnt
[root@yaoxiaorong ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_yaoxiaorong-root 17G 1.7G 16G 10% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 13M 476M 3% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.228.20:/gongxiang 5.0G 1.6G 3.5G 31% /mnt
永久掛載
'開機自動掛載nfs:編輯/etc/fatab文件,添加如下格式的內容:'
192.168.228.20:/gongxiang /mnt nfs defautls 0 0
客戶端卸載
[root@yaoxiaorong ~]# umount /mnt
'注意:卸載的時候如果提示“umount.nfs: /mnt: device is busy'
1.退出掛載目錄在卸載
2.NFS Server 宕機,強制卸載umount -lt /mnt mount -a
自動掛載
自動掛載服務autofs,掛載方式如下兩種:
直接:direct /-子配置文件必須寫本地絕對路徑
間接:indirect /path子配置文件寫相對於/path的子目錄
客戶端安裝autofs自動掛載
yum install autofs -y
systemctl enable autofs
systemctl start autofs
直接掛載方式:本地的掛載點要寫絕對路徑
名稱後綴必須是autofs,/-代表這是一個直接掛載定義,根據/etc/auto.nfs_direct進行掛載全寫
[root@yaoxiaorong ~]# vim /etc/auto.master.d/nfs.autofs
/- /etc/auto.nfs_direct
填寫掛載配置
[root@yaoxiaorong ~]# vim /etc/auto.nfs_direct
/mnt -rw,sync,soft 192.168.228.20:/data
重啓autofs服務
[root@yaoxiaorong ~]# systemctl restart autofs
[root@yaoxiaorong ~]# mount
/etc/auto.nfs_direct on /mnt type autofs (rw,relatime,fd=18,pgrp=16781,timeout=300,minproto=5,maxproto=5,direct,pipe_ino=43596)
192.168.228.20:/data on /mnt type nfs4 (rw,relatime,sync,vers=4.1,rsize=131072,wsize=131072,namlen=255,soft,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.228.21,local_lock=none,addr=192.168.228.20)
3.8客戶端在共享的目錄中創建文件,服務端查看是否能創建目錄
'客戶端:'
[root@yaoxiaorong ~]# cd /mnt
[root@yaoxiaorong mnt]# touch abc
'服務端:'
[root@yaoxiaorong ~]# cd /gongxiang/
[root@yaoxiaorong gongxiang]# ls
abc
4.1shell腳本來搭建nfs服務器
'在服務端運用ssh,使服務端遠程訪問客戶端免密碼'
[root@yaoxiaorong ~]# ssh-keygen -t rsa
[root@yaoxiaorong ~]# ls .ssh
id_rsa id_rsa.pub
[root@yaoxiaorong ~]# ls .ssh
id_rsa id_rsa.pub
[root@yaoxiaorong ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@yaoxiaorong ~]# vim nfs.sh
#!/bin/bash
setenforce 0
sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config
systemctl stop firewalld
yum install nfs-utils -y
yum install rpcbind -y
systemctl start rpcbind
systemctl start nfs-server
echo "/data 192.168.228.21(rw,async)" >/etc/exports && mkdir -p /data
chmod 777 /data
chown -R nfsnobody.nfsnobody /data/
systemctl restart nfs-server
ssh [email protected] 'yum install showmount -y'
ssh [email protected] 'showmount -e 192.168.228.20'
ssh [email protected] 'mount -t nfs 192.168.228.20:/dat
a /mnt && mkdir mnt/123'
[root@yaoxiaorong ~]# chmod +x nfs.sh
[root@yaoxiaorong ~]# ./nfs.sh