NFS服務器配置
NFS(Network File System),網絡文件系統。其目的就是使不同機器,不同操作系統可以彼此共享數據文件。但是在Unix Like系統主機之間採用NFS服務器比SAMBA服務器快速和方便許多。NFS服務是RPC(Remote Procedure Call)服務的一種,要使用NFS服務首先要啓動RPC(rpcbind)。
1.NFS服務所須要的軟件。
RPC主程序: rpcbind
rpcbind軟件安裝後即可運行RPC服務,該服務監聽111端口,所有RPC服務比如NFS服務啓動的隨機端口都要想111端口進行註冊(port mapping)。
NFS主程序: nfs-utils
該軟件主要提供rpc.nfsd與rpc.mountd這兩個NFS daemons與其他相關docments與說明文件、可執行文件等。
2.NFS的軟件結構。
主要配置文件:/etc/exports
NFS文件系統維護命令: /usr/sbin/exportfs
這個命令用於重新共享/etc/exports更新的目錄資源,將NFS Server共享的目錄卸載和重新共享等。
共享資源的日誌文件:/var/lib/nfs/*tab
其中etab記錄目錄的完整的權限值,xtab記錄連接到NFS Server的相關客戶端的數據。
客戶端查詢服務器共享資源命令: /usr/sbin/showmount
該命令主要用在客戶端,用以查看NFS Server共享出來的目錄資源。
3.安裝NFS服務器相關軟件。
yum install rpcbind –y
yum install nfs-utils –y
4.啓動NFS服務器相關服務。
/etc/init.d/rpcbind start
chkconfig rpcbind on
/etc/init.d/nfs start
chkconfig nfs on
/etc/init.d/nfslock start 該服務爲可選服務用以在多人單文件情況下保持文件的一致性。
chkconfig nfslock on
如果啓動沒有問題則觀察啓動的端口:
[root@linux ~]# netstat -tulnp | grep -E '(rpc|nfs)'
tcp 0 0 0.0.0.0:875 0.0.0.0:* LISTEN 4472/rpc.rquotad
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 4395/rpcbind
tcp 0 0 0.0.0.0:47893 0.0.0.0:* LISTEN 4594/rpc.statd
tcp 0 0 0.0.0.0:35871 0.0.0.0:* LISTEN 4488/rpc.mountd
tcp 0 0 :::111 :::* LISTEN 4395/rpcbind
tcp 0 0 :::52789 :::* LISTEN 4594/rpc.statd
udp 0 0 0.0.0.0:875 0.0.0.0:* 4472/rpc.rquotad
udp 0 0 0.0.0.0:111 0.0.0.0:* 4395/rpcbind
udp 0 0 0.0.0.0:754 0.0.0.0:* 4395/rpcbind
udp 0 0 0.0.0.0:38690 0.0.0.0:* 4594/rpc.statd
udp 0 0 0.0.0.0:50226 0.0.0.0:* 4488/rpc.mountd
udp 0 0 0.0.0.0:954 0.0.0.0:* 4594/rpc.statd
udp 0 0 :::111 :::* 4395/rpcbind
udp 0 0 :::754 :::* 4395/rpcbind
udp 0 0 :::37546 :::* 4594/rpc.statd
使用rpcinfo命令查看本機RPC服務的註冊情況。
[root@linux ~]# 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
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 46092 nlockmgr
100021 3 udp 46092 nlockmgr
100021 4 udp 46092 nlockmgr
100021 1 tcp 58577 nlockmgr
100021 3 tcp 58577 nlockmgr
100021 4 tcp 58577 nlockmgr
100005 1 udp 50226 mountd
100005 1 tcp 35871 mountd
100005 2 udp 50226 mountd
100005 2 tcp 35871 mountd
100005 3 udp 50226 mountd
100005 3 tcp 35871 mountd
100024 1 udp 38690 status
100024 1 tcp 47893 status
固定NFS服務相關端口
由於NFS服務會隨機啓用端口,這使得我們無法制定相關防火牆規則,所以要先固定相關端口。
vi /etc/sysconfig/nfs
#RQUOTAD_PORT=875
#LOCKD_TCPPORT=32803
#LOCKD_UDPPORT=32769
#MOUNTD_PORT=892
找到以上四行,然後將“#”號去掉。
重啓NFS服務。
[root@linux ~]# /etc/init.d/nfs restart
關閉 NFS mountd:[確定]
關閉 NFS 守護進程:[確定]
關閉 NFS quotas:[確定]
啓動 NFS 服務: [確定]
關掉 NFS 配額:[確定]
啓動 NFS 守護進程:[確定]
啓動 NFS mountd:[確定]
再次查看本機nfs端口情況。
[root@linux ~]# 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
100024 1 udp 38690 status
100024 1 tcp 47893 status
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 32769 nlockmgr
100021 3 udp 32769 nlockmgr
100021 4 udp 32769 nlockmgr
100021 1 tcp 32803 nlockmgr
100021 3 tcp 32803 nlockmgr
100021 4 tcp 32803 nlockmgr
100005 1 udp 892 mountd
100005 1 tcp 892 mountd
100005 2 udp 892 mountd
100005 2 tcp 892 mountd
100005 3 udp 892 mountd
100005 3 tcp 892 mountd
編寫防火牆規則。
[root@linux ~]# vi /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 111 -j ACCEPT
-A INPUT -p udp -m udp --dport 111 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT
-A INPUT -p udp -m udp --dport 2049 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 875 -j ACCEPT
-A INPUT -p udp -m udp --dport 875 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 892 -j ACCEPT
-A INPUT -p udp -m udp --dport 892 -j ACCEPT
-A INPUT -p udp -m udp --dport 32769 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 32803 -j ACCEPT
[root@linux ~]# /etc/init.d/iptables restart
iptables:清除防火牆規則:[確定]
iptables:將鏈設置爲政策 ACCEPT:filter [確定]
iptables:正在卸載模塊:[確定]
iptables:應用防火牆規則:[確定]
編輯NFS配置文件。
建立/redhat目錄, 僅允許tanhq.com域對該目錄有讀寫權限且若登錄用戶爲root則具有root權限, 其他用戶僅有隻讀權限。
[root@linux ~]# mkdir -p /redhat
[root@linux ~]# vi /etc/exports
/redhat tanhq.com(rw,sync,no_root_squash) *(ro)
[root@linux ~]# /etc/init.d/nfs restart
關閉 NFS mountd:[確定]
關閉 NFS 守護進程:[確定]
關閉 NFS quotas:[確定]
啓動 NFS 服務: [確定]
關掉 NFS 配額:[確定]
啓動 NFS 守護進程:[確定]
啓動 NFS mountd:[確定]
在客戶端測試。
首先客戶端與服務器端都要安裝並啓動RPC服務。
[root@client ~]# yum install rpcbind –y
[root@client ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
使用rpcinfo查看是否可以連接到服務器的nfs服務。
[root@client ~]# rpcinfo -p 192.168.0.1
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
100024 1 udp 38690 status
100024 1 tcp 47893 status
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 32769 nlockmgr
100021 3 udp 32769 nlockmgr
100021 4 udp 32769 nlockmgr
100021 1 tcp 32803 nlockmgr
100021 3 tcp 32803 nlockmgr
100021 4 tcp 32803 nlockmgr
100005 1 udp 892 mountd
100005 1 tcp 892 mountd
100005 2 udp 892 mountd
100005 2 tcp 892 mountd
100005 3 udp 892 mountd
100005 3 tcp 892 mountd
連接正常。
查看掛載目錄。nfslock功能客戶端與服務器端都需同時啓動纔可使用。所以再客戶端也要安裝nfs-utils。
[root@client ~]# yum install nfs-utils -y
[root@client ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
Starting RPC idmapd: [ OK ]
[root@client ~]# /etc/init.d/nfslock start
Starting NFS statd: [ OK ]
[root@client ~]# showmount -e 192.168.0.1
Export list for 192.168.0.1:
/redhat (everyone)
在客戶端掛載nfs服務器目錄。
[root@client~]#mount –t nfs –o nosuid,noexec,nodev,rw,bg,soft,rsize=32768,wsize=32768
含義:取消suid功能,掛載分區僅爲數據分區,沒有執行二級制文件的權限,不保留設備文件的特殊功能,可讀寫,執行掛載時該掛載行爲在後臺進行,RPC在超時後重複呼叫,但非持續呼叫。讀出和寫入區塊大小爲32768(bytes)。
查看是否成功掛載:
[root@client ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 8.6G 2.4G 5.8G 29% /
tmpfs 244M 24K 244M 1% /dev/shm
/dev/sda1 291M 30M 246M 11% /boot
/dev/sr0 3.2G 3.2G 0 100% /media/RHEL_6.0 x86_64 Disc 1
192.168.0.1:/redhat 20G 5.8G 13G 31% /mnt/nfs
autofs自動掛載
NFS服務器與客戶端之間的網絡連接可能會斷線,任何一方脫機都會造成對方等待超時,NFS服務器上得資源可能不會經常使用,爲了能夠在有需要時才掛載NFS目錄,就要使用autofs自動掛載。在客戶端不是有NFS時,目錄會被自動卸載。
先卸載之前的手動掛載。
umount -t nfs 192.168.0.1:/redhat
安裝並啓動autofs服務。
[root@client ~]# yum install autofs –y
[root@client ~]# /etc/init.d/autofs start
Loading autofs4: [ OK ]
Starting automount: [ OK ]
配置autofs自動掛載
[root@client ~]# vi /etc/auto.master
/nfsfile /etc/auto.nfs
autofs會主動建立/nfsfile目錄。如果事先建立該目錄可能會出現問題。/etc/auto.nfs這個數據對應文件是不存在的,我們要自行建立,然後再這個文件中指定要掛載的服務器端目錄。
[root@client ~]# vi /etc/auto.nfs
redhat -rw,bg,soft,rsize=32768,wsize=32768 192.168.0.1:/redhat
重啓autofs服務後,會建立/nfsfile/redhat目錄。
[root@client ~]# /etc/init.d/autofs stop
Stopping automount: [ OK ]
[root@client ~]# /etc/init.d/autofs start
Starting automount: [ OK ]
/nfsfile目錄已經被主動建立
[root@client ~]# ll -d /nfsfile
drwxr-xr-x. 2 root root 0 Nov 17 12:46 /nfsfile
查看是否自動掛載。
[root@client ~]# cd /nfsfile/redhat
[root@client redhat]# mount | grep addr
192.168.0.1:/redhat on /nfsfile/redhat type nfs (rw,soft,rsize=32768,wsize=32768,sloppy,vers=4,addr=192.168.0.1,clientaddr=192.168.0.2)
[root@client redhat]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 8.6G 2.4G 5.8G 29% /
tmpfs 244M 24K 244M 1% /dev/shm
/dev/sda1 291M 30M 246M 11% /boot
/dev/sr0 3.2G 3.2G 0 100% /media/RHEL_6.0 x86_64 Disc 1
192.168.0.1:/redhat 20G 5.8G 13G 31% /nfsfile/redhat