NFS 掛載 + autofs
NFS:Network File System
RPC:Remote Procedure Call
一、手動掛載 (mount -t nfs 服務端IP:/共享目錄 /本地掛載點)
客戶端
1.安裝nfs:
[root@localhost ~]# yum install nfs* portmap -y
[root@localhost ~]# rpm -qa nfs*
nfs-utils-lib-1.1.5-11.el6.x86_64
nfs4-acl-tools-0.3.3-8.el6.x86_64
nfs-utils-lib-devel-1.1.5-11.el6.x86_64
nfs-utils-1.2.3-70.el6_8.1.x86_64
[root@localhost ~]# rpm -qa rpcbind
rpcbind-0.2.0-12.el6.x86_64
2.啟動rpc服務和nfs:
[root@localhost ~]# /etc/init.d/nfs restart
[root@localhost ~]# /etc/init.d/nfs restart
查看rpc服務註冊情況:
[root@localhost ~]# rpcinfo -p 192.168.190.138
program vers proto port service
100000 4 tcp 111 portmapper (rpc服務默認端口:111)
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 44957 status
100024 1 tcp 53988 status
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100005 1 udp 53978 mountd
100005 1 tcp 57642 mountd
100005 2 udp 47255 mountd
100005 2 tcp 54213 mountd
100005 3 udp 55342 mountd
100005 3 tcp 43895 mountd
100003 2 tcp 2049 nfs (nfs服務器默認端口:2049)
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 42653 nlockmgr
100021 3 udp 42653 nlockmgr
100021 4 udp 42653 nlockmgr
100021 1 tcp 57709 nlockmgr
100021 3 tcp 57709 nlockmgr
100021 4 tcp 57709 nlockmgr
3.編輯nfs主配置文件:
[root@localhost ~]# vim /etc/exports (第一次使用nfs服務需手動創建)
/home/share *(rw,no_root_squash,sync)
【共享目錄】 【主機名稱,*是匹配所有】 【權限控制】
主機名稱: 可使用IP、主機名或網段 192.168.0.1/24等同於192.168.0.1/255.255.255.0
權限控制: rw (可讀寫, ro(只讀) sync(數據同步寫入硬盤) async(暫存於內存,後寫入硬盤)
no_root_squash (root權限) root_squash(默認root權限會變成nfsnobody權限)
all_squash(默認所有用戶均匿名nofsnobody權限) anonuid(設置UID) anongid(設置GID)
4.創建本地共享目錄:
[root@localhost ~]# mkdir /home/share # 創建共享目錄
[root@localhost ~]# ll /home/share
total 0
5.重新加載配置文件使其生效:
[root@localhost ~]# exportfs -arv
exporting *:/home/share
此步驟等同於 重啟nfs服務 service nfs restart
6.關閉防火墻或者開啟相應的端口,否則客服端無法連接
[root@localhost ~]# service iptables stop
至此, Server端 配置結束
客戶端:
1.確定啟動rpcbind和nfs服務:
[root@localhost ~]# yum install nfs* -y
[root@localhost ~]# rpm -qa rpcbind
rpcbind-0.2.0-8.el6.i686
[root@localhost ~]# rpm -qa nfs*
nfs4-acl-tools-0.3.3-8.el6.i686
nfs-utils-lib-1.1.5-11.el6.i686
nfs-utils-lib-devel-1.1.5-11.el6.i686
nfs-utils-1.2.3-70.el6_8.1.i686
[root@localhost ~]# service rpcbind restart
[root@localhost ~]# service nfs restart
[root@localhost ~]# service nfs restart
2.查看NFS聯機狀態:
[root@localhost ~]# showmount -e 192.168.190.138
Export list for 192.168.190.138:
/home/share *
3.創建掛載點:
[root@localhost ~]# mkdir /bak
4. 使用mount命令掛載使用:
[root@localhost ~]# mount 192.168.190.138:/home/share /bak
5.測試:
客戶端:
[root@localhost ~]# cd /bak
[root@localhost bak]# mkdir l
[root@localhost bak]# touch 123
[root@localhost bak]# ll
total 4
-rw-r--r--. 1 root root 0 Sep 30 15:20 123
drwxr-xr-x. 2 root root 4096 Sep 30 15:20 l
創建目錄默認權限:755
創建文件默認權限:644
服務端:
[root@localhost ~]# cd /home/share
[root@localhost share]# ll
total 4
-rw-r--r--. 1 root root 0 Sep 30 15:20 123
drwxr-xr-x. 2 root root 4096 Sep 30 15:20 l
二、使用autofs自動掛載,默認5分鐘取消掛載 (/etc/auto.master、 /etc/auto.misc)
1. 服務端:創建共享目錄
[root@localhost ~]# mkdir /user
[root@localhost ~]# vim /etc/exports
/user *(rw,no_root_squash,sync)
[root@localhost user]# exportfs -arv
exporting *:/user
exporting *:/home/share
2. 客戶端:創建掛載點
[root@localhost ~]# mkdir /var/autofs
[root@localhost ~]# yum install autofs -y
3.編輯 auto.master (主要是控制作用,指定默認目錄及對應文件)
[root@localhost ~]# vim /etc/auto.master
/var/autofs /etc/auto.misc
[root@localhost ~]# cat /etc/auto.master | grep -v ‘#‘
/misc /etc/auto.misc
/var/autofs /etc/auto.misc
/net -hosts
+auto.master
4.編輯 auto.misc (主要是定義掛載點及指定下一級目錄,該目錄可以不存在)
[root@localhost ~]# vim /etc/auto.misc
test -rw,soft,intr 192.168.190.138:/user
註意:這裏test 是下級目錄,千萬不要畫蛇添足的加/ ,否則自動掛載不成功!!
[root@localhost ~]# cat /etc/auto.misc | grep -v ‘#‘
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
test -rw,soft,intr 192.168.190.138:/user
5.重啟autofs服務:
[root@localhost ~]# /etc/init.d/autofs restart
6.測試:
[root@localhost autofs]# cd /var/autofs/test
[root@localhost test]# ls
mount
[root@localhost test]# touch 123
服務器端:
[root@localhost ~]# cd /user/
[root@localhost user]# ll
total 4
-rw-r--r--. 1 root root 0 Sep 30 15:57 123
drwxr-xr-x. 2 root root 4096 Sep 30 15:36 mount
autofs 服務 默認300s (5min) 會自動取消掛載 可參考文件 /etc/sysconfig/autofs 設置
[root@localhost ~]# cat /etc/sysconfig/autofs | grep ‘TIMEOUT‘ | grep -v ‘#‘
TIMEOUT=300
三、 開機自動掛載,永久生效 (修改/etc/fstab)
開機自動掛載nfs服務 修改/etc/fstab 即可, 例如:
[root@localhost ~]# cat /etc/fstab|tail -1
192.168.190.138:/home/share /bak ext3 defaults 0 0
總結:
客戶端使用NFS服務,可以有3種方法掛載:
1. 手動掛載 (mount -t nfs 服務端IP:/共享目錄 /本地掛載點)
2. 使用自動掛載,默認5分鐘取消掛載 (/etc/auto.master、 /etc/auto.misc)
3. 開機自動掛載,永久生效 (修改/etc/fstab)
NFS 掛載 + autofs