NFS服务可以让不同的客户端挂载使用同一个上目录,作为共享存储使用,这样可以保证不同节点的客户端数据的一致性,在集群环境中经常用到
1、简单------>容易上手,学习
2、方便------>安装快速,后期维护简单
3、可靠------>从软件层面上看,数据可靠性高
NFS服务安装成功并能使用之后,客户端挂载时也会加上一些参数进行优化
1、有关系统安全的挂载优化参数
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
检查测试环境
[root@localhost ~]# showmount -e 192.168.1.2
Export list for 192.168.1.2:
/data/bbs 192.168.1.3
[root@localhost ~]# mount -t nfs -o rw,sync,nosuid,noexec 192.168.1.2:/data/bbs /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
192.168.1.2:/data/bbs 50G 3.5G 44G 8% /mnt
[root@localhost mnt]# echo ‘echo pwd
‘>/mnt/text.sh
[root@localhost mnt]# sh /mnt/text.sh
/mnt
[root@localhost mnt]# ./text.sh
-bash: ./text.sh: Permission denied
经测试发现noexec参数只能禁止二进制程序执行,shell程序依然可执行
[root@localhost ~]# umount /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
[root@localhost ~]# grep mnt /proc/mounts
192.168.1.2:/data/bbs /mnt nfs4 rw,nosuid,noexec,relatime,vers=4,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.3,minorversion=0,local_lock=none,addr=192.168.1.2 0 0
[root@localhost ~]# mount -t nfs -o rw,sync,nosuid,noexec,noatime,rsize=65536,wsize=65536 192.168.1.2:/data/bbs /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
192.168.1.2:/data/bbs 50G 3.5G 44G 8% /mnt
[root@localhost ~]# ls /bin/touch -lsh
52K -rwxr-xr-x. 1 root root 52K Nov 22 2013 /bin/touch
[root@Centos bbs]# ls|wc -l
4999
[root@localhost mnt]# time for((i=1;i<5000;i++));do /bin/cp /bin/touch /mnt/touch$i;done
real 1m39.245s
user 0m2.570s
sys 0m11.889s
[root@localhost ~]# umount /mnt
[root@localhost ~]# mount -t nfs -o rw,sync,nosuid,noexec,rsize=65536,wsize=65536 192.168.1.2:/data/bbs /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
192.168.1.2:/data/bbs 50G 3.5G 44G 8% /mnt
[root@localhost ~]# time for((i=1;i<5000;i++));do /bin/cp /bin/touch /mnt/toucx$i;done
real 0m45.965s
user 0m2.435s
sys 0m6.907s
[root@Centos bbs]# cat /etc/exports
#####config for nfs-server 2016-8-21
/data/bbs 192.168.1.3(rw,sync,all_squash,anonuid=3000,anongid=3000)
[root@localhost ~]# umount /mnt 先卸载掉原来的挂载
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
[root@localhost ~]# /etc/init.d/autofs start
Starting automount: automount: program is already running. [ OK ]
autofs两个组件:/etc/auto.master /etc/auto.misc
[root@localhost ~]# vi /etc/auto.master
Sample auto.master file
This is an automounter map and it has the following format
key [ -mount-options-separated-by-comma ] location
For details of the format look at autofs(5).
/misc /etc/auto.misc
/mnt /etc/auto.misc --timeout 60
#
NOTE: mounts done from a hosts map will be mounted with the
"nosuid" and "nodev" options unless the "suid" and "dev"
options are explicitly given.
[root@localhost ~]# vi /etc/auto.misc
This is an automounter map and it has the following format
key [ -mount-options-separated-by-comma ] location
Details may be found in the autofs(5) manpage
#the following entries are samples to pique your imagination
#linux -ro,soft,intr ftp.example.org:/pub/linux
#boot -fstype=ext2 :/dev/hda1
#floppy -fstype=auto :/dev/fd0
#floppy -fstype=ext2 :/dev/fd0
#e2floppy -fstype=ext2 :/dev/fd0
#jaz -fstype=ext2 :/dev/sdc1
#removable -fstype=ext2 :/dev/hdd
nfsfile -fstype=nfs 192.168.1.2:/data/bbs
在/mnt下新建一个nfsfile文件夹,-fstype=nfs 192.168.1.2:/data/bbs
[root@localhost ~]# /etc/init.d/autofs restart
Stopping automount: [ OK ]
Starting automount: [ OK ]
[root@localhost ~]# /etc/init.d/autofs status
automount (pid 12307) is running...
下面测试自动挂载情况
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# cd nfsfile
[root@localhost nfsfile]# ls
[root@localhost nfsfile]# touch 123.txt
[root@localhost nfsfile]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
192.168.1.2:/data/bbs 50G 3.5G 44G 8% /mnt/nfsfile
客户访问NFS共享目录时,才会自动挂载
[root@Centos bbs]# ls -ll 服务端共享目录
total 0
-rw-r--r--. 1 nfsuser nfsuser 0 Aug 26 06:57 123.txt
到一定时间后就会自动断开(60S)
[root@localhost ~]# date
Sat Aug 27 14:01:53 SYOT 2016
[root@localhost ~]# date
Sat Aug 27 14:03:00 SYOT 2016
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp -s 192.168.1.0/24 --dprot 111 -j ACCEPT
iptables -A INPUT -i eth0 -p udp -s 192.168.1.0/24 --dprot 111 -j ACCEPT
iptables -A INPUT -i eth0 -p udp -s 192.168.1.0/24 --dprot 2049 -j ACCEPT
iptables -A INPUT -i eth0 -p udp -s 192.168.1.0/24 -j ACCEPT
原文:https://blog.51cto.com/mingongge/2555722