12.2 NFS(网络文件系统),12.2nfs
12.2 NFS(网络文件系统)
如果大家觉得Samba服务程序的配置太麻烦,而且恰巧需要共享文件的主机都是Linux系统,刘遄老师非常推荐大家在客户端部署NFS服务来共享文件。NFS(网络文件系统)服务可以将远程Linux系统上的文件共享资源挂载到本地主机的目录上,从而使得本地主机(Linux客户端)基于TCP/IP协议,像使用本地主机上的资源那样读写远程Linux系统上的共享文件。
由于RHEL 7系统中默认已经安装了NFS服务,外加NFS服务的配置步骤也很简单,因此刘遄老师在授课时会将NFS戏谑为Need For Speed。接下来,我们准备配置NFS服务。首先请使用Yum软件仓库检查自己的RHEL 7系统中是否已经安装了NFS软件包:
[root@linuxprobe ~]# yum install nfs-utils
Loaded plugins: langpacks, product-id, subscription-manager
(1/2): rhel7/group_gz | 134 kB 00:00
(2/2): rhel7/primary_db | 3.4 MB 00:00
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
第1步:为了检验NFS服务配置的效果,我们需要使用两台Linux主机(一台充当NFS服务器,一台充当NFS客户端),并按照表12-6来设置它们所使用的IP地址。
表12-6 两台Linux主机所使用的操作系统以及IP地址
主机名称 | 操作系统 | IP地址 |
NFS服务器 | RHEL 7 | 192.168.10.10 |
NFS客户端 | RHEL 7 | 192.168.10.20 |
另外,不要忘记清空NFS服务器上面iptables防火墙的默认策略,以免默认的防火墙策略禁止正常的NFS共享服务。
[root@linuxprobe ~]# iptables -F
[root@linuxprobe ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
第2步:在NFS服务器上建立用于NFS文件共享的目录,并设置足够的权限确保其他人也有写入权限。
[root@linuxprobe ~]# mkdir /nfsfile
[root@linuxprobe ~]# chmod -Rf 777 /nfsfile
[root@linuxprobe ~]# echo "welcome to linuxprobe.com" > /nfsfile/readme
第3步:NFS服务程序的配置文件为/etc/exports,默认情况下里面没有任何内容。我们可以按照“共享目录的路径 允许访问的NFS客户端(共享权限参数)”的格式,定义要共享的目录与相应的权限。
例如,如果想要把/nfsfile目录共享给192.168.10.0/24网段内的所有主机,让这些主机都拥有读写权限,在将数据写入到NFS服务器的硬盘中后才会结束操作,最大限度保证数据不丢失,以及把来访客户端root管理员映射为本地的匿名用户等,则可以按照下面命令中的格式,将表12-7中的参数写到NFS服务程序的配置文件中。
表12-7 用于配置NFS服务程序配置文件的参数
参数 | 作用 |
ro | 只读 |
rw | 读写 |
root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 |
no_root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 |
all_squash | 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 |
sync | 同时将数据写入到内存与硬盘中,保证不丢失数据 |
async | 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 |
请注意,NFS客户端地址与权限之间没有空格。
[root@linuxprobe ~]# vim /etc/exports
/nfsfile 192.168.10.*(rw,sync,root_squash)
第4步:启动和启用NFS服务程序。由于在使用NFS服务进行文件共享之前,需要使用RPC(Remote Procedure Call,远程过程调用)服务将NFS服务器的IP地址和端口号等信息发送给客户端。因此,在启动NFS服务之前,还需要顺带重启并启用rpcbind服务程序,并将这两个服务一并加入开机启动项中。
[root@linuxprobe ~]# systemctl restart rpcbind
[root@linuxprobe ~]# systemctl enable rpcbind
[root@linuxprobe ~]# systemctl start nfs-server
[root@linuxprobe ~]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.
target.wants/nfs-server.service'
NFS客户端的配置步骤也十分简单。先使用showmount命令(以及必要的参数,见表12-8)查询NFS服务器的远程共享信息,其输出格式为“共享的目录名称 允许使用客户端地址”。
表12-8 showmount命令中可用的参数以及作用
参数 | 作用 |
-e | 显示NFS服务器的共享列表 |
-a | 显示本机挂载的文件资源的情况 |
-v | 显示版本号 |
[root@linuxprobe ~]# showmount -e 192.168.10.10
Export list for 192.168.10.10:
/nfsfile 192.168.10.*
然后在NFS客户端创建一个挂载目录。使用mount命令并结合-t参数,指定要挂载的文件系统的类型,并在命令后面写上服务器的IP地址、服务器上的共享目录以及要挂载到本地系统(即客户端)的目录。
[root@linuxprobe ~]# mkdir /nfsfile
[root@linuxprobe ~]# mount -t nfs 192.168.10.10:/nfsfile /nfsfile
挂载成功后就应该能够顺利地看到在执行前面的操作时写入的文件内容了。如果希望NFS文件共享服务能一直有效,则需要将其写入到fstab文件中:
[root@linuxprobe ~]# cat /nfsfile/readme
welcome to linuxprobe.com
[root@linuxprobe ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 4 19:26:23 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root / xfs defaults 1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2
/dev/mapper /rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0
192.168.10.10:/nfsfile /nfsfile nfs defaults 0 0
12.3 autofs自动挂载服务 |
无论是Samba服务还是NFS服务,都要把挂载信息写入到/etc/fstab中,这样远程共享资源就会自动随服务器开机而进行挂载。虽然这很方便,但是如果挂载的远程资源太多,则会给网络带宽和服务器的硬件资源带来很大负载。如果在资源挂载后长期不使用,也会造成服务器硬件资源的浪费。可能会有读者说,“可以在每次使用之前执行mount命令进行手动挂载”。这是一个不错的选择,但是每次都需要先挂载再使用,您不觉得麻烦吗?
autofs自动挂载服务可以帮我们解决这一问题。与mount命令不同,autofs服务程序是一种Linux系统守护进程,当检测到用户视图访问一个尚未挂载的文件系统时,将自动挂载该文件系统。换句话说,我们将挂载信息填入/etc/fstab文件后,系统在每次开机时都自动将其挂载,而autofs服务程序则是在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器的硬件资源。
[root@linuxprobe ~]# yum install autofs
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use
subscription-manager to register.
rhel | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-40.el7 will be installed
--> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-
40.el7.x86_64
--> Running transaction check
---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================
Package Arch Version Repository Size
===============================================================================
Installing:
autofs x86_64 1:5.0.7-40.el7 rhel 550 k
Installing for dependencies:
hesiod x86_64 3.2.1-3.el7 rhel 30 k
Transaction Summary
===============================================================================
Install 1 Package (+1 Dependent package)
Total download size: 579 k
Installed size: 3.6 M
Is this ok [y/d/N]: y
Downloading packages:
-------------------------------------------------------------------------------
Total 9.4 MB/s | 579 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : hesiod-3.2.1-3.el7.x86_64 1/2
Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2
Verifying : hesiod-3.2.1-3.el7.x86_64 1/2
Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2
Installed:
autofs.x86_64 1:5.0.7-40.el7
Dependency Installed:
hesiod.x86_64 0:3.2.1-3.el7
Complete!
处于生产环境中的Linux服务器,一般会同时管理许多设备的挂载操作。如果把这些设备挂载信息都写入到autofs服务的主配置文件中,无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,因此在autofs服务程序的主配置文件中需要按照“挂载目录 子配置文件”的格式进行填写。挂载目录是设备挂载位置的上一级目录。例如,光盘设备一般挂载到/media/cdrom目录中,那么挂载目录写成/media即可。对应的子配置文件则是对这个挂载目录内的挂载设备信息作进一步的说明。子配置文件需要用户自行定义,文件名字没有严格要求,但后缀建议以.misc结束。具体的配置参数如第7行的加粗字所示。
[root@linuxprobe ~]# vim /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).
#
/media /etc/iso.misc
/misc /etc/auto.misc
#
# 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.
#
/net -hosts
#
# Include /etc/auto.master.d/*.autofs
#
+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
在子配置文件中,应按照“挂载目录 挂载文件类型及权限 :设备名称”的格式进行填写。例如,要把光盘设备挂载到/media/iso目录中,可将挂载目录写为iso,而-fstype为文件系统格式参数,iso9660为光盘设备格式,ro、nosuid及nodev为光盘设备具体的权限参数,/dev/cdrom则是定义要挂载的设备名称。配置完成后再顺手将autofs服务程序启动并加入到系统启动项中:
[root@linuxprobe ~]# vim /etc/iso.misc
iso -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
[root@linuxprobe ~]# systemctl start autofs
[root@linuxprobe ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.
target.wants/autofs.service'
接下来将发生一件非常有趣的事情。我们先查看当前的光盘设备挂载情况,确认光盘设备没有被挂载上,而且/media目录中根本就没有iso子目录。但是,我们却可以使用cd命令切换到这个iso子目录中,而且光盘设备会被立即自动挂载上。我们也就能顺利查看光盘内的内容了。
[root@linuxprobe ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
[root@linuxprobe ~]# cd /media
[root@linuxprobe media]# ls
[root@linuxprobe media]# cd iso
[root@linuxprobe iso]# ls -l
total 812
dr-xr-xr-x. 4 root root 2048 May 7 2017 addons
dr-xr-xr-x. 3 root root 2048 May 7 2017 EFI
-r--r--r--. 1 root root 8266 Apr 4 2017 EULA
-r--r--r--. 1 root root 18092 Mar 6 2012 GPL
dr-xr-xr-x. 3 root root 2048 May 7 2017 images
dr-xr-xr-x. 2 root root 2048 May 7 2017 isolinux
dr-xr-xr-x. 2 root root 2048 May 7 2017 LiveOS
-r--r--r--. 1 root root 108 May 7 2017 media.repo
dr-xr-xr-x. 2 root root 774144 May 7 2017 Packages
dr-xr-xr-x. 24 root root 6144 May 7 2017 release-notes
dr-xr-xr-x. 2 root root 4096 May 7 2017 repodata
-r--r--r--. 1 root root 3375 Apr 1 2017 RPM-GPG-KEY-redhat-beta
-r--r--r--. 1 root root 3211 Apr 1 2017 RPM-GPG-KEY-redhat-release
-r--r--r--. 1 root root 1568 May 7 2017 TRANS.TBL
[root@linuxprobe ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/cdrom 3.5G 3.5G 0 100% /media/iso
/dev/sda1 497M 119M 379M 24% /boot