NFS:網絡文件系統
rpc:
[root@localhost ~]# rpm -ql nfs-utils 服務器端
[root@localhost ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@localhost ~]# service portmap restart
portmap: unrecognized service
centos中portmap這個服務叫做rpcbind ,主要是在nfs共享時候負責通知客戶端,服務器的nfs端口號的
[root@localhost ~]# service rpcbind status
rpcbind (pid 1020) is running...
[root@localhost ~]# rpcinfo -p localhost
查看一臺主機上所有rpc所監聽的端口
NFS將啓動三個主進程:nfsd(文件傳輸,NFS服務) mountd(掛載) quotad(配額)
nfsd:2049/tcp, 2049/udp
mountd:端口
quotad:端口
半隨機的
/etc/rc.d/init.d/nfslock NFS鎖的腳本。當一個主機在編輯這個文件時候RPC會給該文件上鎖,以保證不會多臺主機同時修改某一個文件
[root@localhost ~]# chkconfig nfs on
[root@localhost ~]# vim /etc/exports 此文件定義哪些客戶端可以使用NFS
/path/to/somedir CLIENT_LIST
多個客戶之間使用空格字符分割
每個客戶端後必須跟一個小括號,裏面定義此客戶端的訪問特性,如訪問權限等
172.16.0.0/16(ro,async) 192.168.1.0/24(rw,sync)
列如:
[root@localhost ~]# mkdir /shared 在根目錄下創建一個shared目錄,把次目錄共享出去
[root@localhost ~]# vim /etc/exports
/shared 192.168.1.0/24(ro) 共享權限ro
[root@localhost ~]# service nfs restart
showmount -a 顯示NFS服務器上所有被掛載的文件系統及其客戶端對應列表
showmount -e 顯示服務器上共享了哪些目錄
showmount -d 顯示NFS服務器上所有的導出的文件系統中被客戶端掛載了的文件系統列表
[root@localhost ~]# showmount -e 192.168.1.51
Export list for 192.168.1.51:
/shared 192.168.1.0/24
客戶端使用mount命令掛載
[root@localhost a]# mount -t nfs 192.168.1.51:/shared /mnt/a
exportfs命令:
-a: 跟-r或-u 選項同時使用,表示重新掛載所有文件系統或取消導出所有文件系統
-r重新導出文件系統
-u取消導出文件系統 (相當於讓所有掛載的NFS文件系統失效)
-v顯示詳細信息
[root@localhost shared]# exportfs -ra 192.168.1.51 後直接可以掛載不用重啓NFS服務
文件系統導出屬性:
ro:只讀
rw:讀寫
sync:同步
async:異步
root_squash:將root用戶映射爲來賓賬號,
no_root_squash:不映射成來賓賬號
all_squash:所有用戶都映射成來賓賬號
anonuid,anongid: 指定映射的來賓賬號的UID和GID
列如把所有用戶都映射爲來賓用戶,並UID GID都爲510
[root@localhost shared]# useradd -u 510 nfstest
[root@localhost shared]# touch /shared/nfstest
[root@localhost shared]# chown nfstest.nfstest /shared/nfstest
[root@localhost shared]# vim /etc/exports
/shared 192.168.1.0/24(rw,all_squash,anonuid=510,anongid=510)
[root@localhost shared]# exportfs -ra
[root@localhost mnt]# umount -t nfs 192.168.1.51:/shared
[root@localhost mnt]# mount -t nfs 192.168.1.51:/shared /mnt/a
此後通過客戶端創建的文件都會顯示UID510 ,GID510
[root@localhost nfstest]# touch aaaaaa
[root@localhost nfstest]# ll
total 4
-rw-r--r--. 1 510 510 0 Aug 27 2016 aaaaaa
[root@localhost nfstest]#
開機自動掛載
[root@localhost nfstest]# vim /etc/fstab
192.168.1.51:/shared /mnt/a nfs defaults,_rnetdev 0 0
rnetdev:在掛載的時候,如果掛載失敗就忽略掉。這樣不至於在服務器故障時導致客戶機開不了機
NFS:客戶端先找rpc,rpc工作在111端口(固定的),然後rpc會告訴客戶端mountd的端口(隨機的),然後mountd給客戶端令牌,客戶端拿着令牌去找nfsd服務
爲了避免mountd隨機端口跟其他服務造成衝突,需要手動設置固定端口
[root@localhost ~]# vim /etc/sysconfig/nfs 配置文件
MOUNTD_PORT=892 可以指定mountd 監聽端口
RQUOTAD_PORT=875
啓動鎖進程也需要監聽端口
LOCKD_TCPPORT=32803
# UDP port rpc.lockd should listen on.
LOCKD_UDPPORT=32769
[root@localhost ~]# service nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down RPC idmapd: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@localhost ~]#
[root@localhost ~]# 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 57027 status
100024 1 tcp 56861 status
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
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
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
[root@localhost ~]#