Autofs(自动挂载工具):

1.autofs概述: 使用mount命令或者通过启动加载文件系统配置文件/etc/fstab进行挂载,不论使用与否都会将设备挂载到系统里,而通过自动挂载可以实现只有在访问时系统才会去挂载,一段时间不使用后系统会自动取消挂载;(自动取消挂载通过编辑/etc/sysconfig/autofs文件中的“TIMEOUT"选项) ###################################### 2.autofs工具特点: autofs工具与mount/umount的不同之处在于,它是一种守护程序。如果它检测到用户正试图访问一个尚未挂载的文件系统,它就会自动检测该文件系统。如果存在,那么Autofs会自动将其挂载。另一方面,如果它检测到某个已经挂载的文件系统在一段时间内没有被使用(默认300秒),那么Autofs会自动将其卸载。因此运行了Autofs工具后,用户就不再需要手动完成文件系统的挂载和卸载动作;####################################### 3.autofs功能: 3.1完成本地目录的自动挂载功能; 3.2完成远程目录到本地的自动挂载功能(映射远程目录到本地); ############################################## 4.autofs服务安装以及配置文件: root@shiyinyuan ~]# cat /etc/issue CentOS release 6.3 (Final) Kernel \r on an \m #系统所使用的发行版本号CentOS 6.4;

[root@shiyinyuan ~]# rpm -qa | grep autofs autofs-5.0.5-54.el6.i686 #在CentOS 6.4中默认安装了autofs软件包;

[root@shiyinyuan ~]# yum info autofs Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Installed Packages Name : autofs Arch : i686 Epoch : 1 Version : 5.0.5 Release : 54.el6 Size : 3.0 M Repo : installed From repo : anaconda-CentOS-201207051201.i386 Summary : A tool for automatically mounting and unmounting filesystems URL : http://wiki.autofs.net/ License : GPLv2+ Description : autofs is a daemon which automatically mounts filesystems when you use them, and unmounts them later when you are not using them. This can include network filesystems, CD-ROMs, floppies, and so forth.

#软件包的名称、运行平台,版本,大小,软件包的下载链接和服务说明等;

[root@shiyinyuan ~]# rpm -qc autofs | grep /etc /etc/auto.master
/etc/auto.misc /etc/auto.net /etc/auto.smb /etc/autofs_ldap_auth.conf /etc/rc.d/init.d/autofs /etc/sysconfig/autofs #查看autofs服务的相关配置文件; #################################################### 5.autofs配置文件介绍: 1./etc/auto.master该文件是自动挂载服务的主配置文件,在文件中每一行可以指定多个自动挂载的目录且每行中需要指定一个已存在的目录作为自动挂载的主目录,以及该主目录中所有自动挂载子目录的配置文件;定义了挂载的主目录以及后续的mount动作。 备注:主目录可以手工创建,也可以不创建等到autofs服务重启动后自动创建。(建议手工) 2./etc/auto.misc 子目录的配置文件;定义了挂载的子目录以及被挂载的目录(本地目录或者是远程服务器目录)。关于挂载的子目录无需手工指定,启动autofs服务后自动生成;

例子1:[root@shiyinyuan ~]# grep /general /etc/auto.master /general /etc/auto.man 返回的显示信息分为两个部分:第一部分表示挂载的主目录,第二个部分表示自动挂载子目录的配置文件,定义了mount动作;

例子2:[root@shiyinyuan ~]# grep cd /etc/auto.misc cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom 表示将/dev/cdrom 挂载到/misc/cd中;-fstype是一个可选项,用来表明所挂载的文件系统的类型和挂载选项;

3./etc/rc.d/init.d/autofs是autofs服务的启动脚本文件,同时链接到了/etc/init.d/autofs文件; 4./etc/sysconfig/autofs是autofs服务的系统配置文件(全局配置),可以修改自动卸载的时间值; ################################################## 6.案例演练 ####################自动挂载本地目录############################# 1.修改/etc/auto.master文件如下: [root@shiyinyuan ~]# grep -v ^# /etc/auto.master /misc /etc/auto.misc /general /etc/auto.man #指定了一个主目录/general /net -hosts +auto.master 2.建立自动挂载子配置文件(子配置文件名称必须和主配置文件定义的名称保持一致) 可以通过系统提供的模板配置子配置文件/etc/auto.misc进行导入操作; [root@shiyinyuan ~]# cp /etc/auto.misc /etc/auto.man [root@shiyinyuan ~]# grep boss /etc/auto.man boss -fstype=ext4 :/dev/sda6 #将本地磁盘挂载到/general/boss目录中 3.启动autofs服务 [root@shiyinyuan ~]# /etc/init.d/autofs restart [root@shiyinyuan ~]# chkconfig autofs on 4.测试 [root@shiyinyuan ~]# cd /general/ [root@shiyinyuan general]# ll total 0 ############目录是空的################# 切换到/general/boss目录,看看是否自动挂载成功; [root@shiyinyuan ~]# cd /general/ [root@shiyinyuan general]# ll total 0 [root@shiyinyuan general]# cd boss [root@shiyinyuan boss]# ll total 16 drwx------. 2 root root 16384 Jul 21 15:27 lost+found

[root@shiyinyuan boss]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 9.9G 6.1G 3.4G 65% / tmpfs tmpfs 504M 0 504M 0% /dev/shm /dev/sda1 ext4 1008M 54M 903M 6% /boot /dev/sda5 ext4 788M 17M 731M 3% /test /dev/sda6 ext4 1016M 34M 932M 4% /general/boss

测试成功!!

#####################映射远程服务器目录#######################

环境信息:网络中有FTP服务器提供匿名上传和下载服务,NFS服务器提供目录的共享服务;通过autofs功能映射到FTP服务器根目录以及NFS服务器的共享目录;(服务器IP地址10.1.210.12)

FTP服务器相关配置: 1.修改配置文件开启匿名用户上传功能: anonymous_enbale=YES 启用匿名用户 anon_upload_enable=YES 启用匿名用户上传功能 anon_mkdir_write_enbale=YES 打开匿名用户创建文件夹的功能

2.赋予匿名用户上传使用的文件夹以写权限: chmod 777 /var/ftp/pub 修改文件目录权限为777; 或者不改变权限的情况下,指定文件的所有者chown ftp /var/ftp/pub

注意:不可将匿名用户家目录(/var/ftp)设置为777权限,只能设置子目录,否则vsftpd将禁止访问匿名用户的家目录; 匿名用户的家目录: [root@tangsir ~]# cat /etc/passwd | grep ftp ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

3.启动FTP服务以及开机运行 [root@www ~]# /etc/init.d/vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [root@www ~]# chkconfig vsftpd on

VSFTPD常见问题总结: 关于防火墙问题: 在INPUT链中写入两条规则运行ftp通过; #iptables -A INPUT -p tcp --dport 20 -j ACCEPT #iptables -A INPUT -p tcp --dport 21 -j ACCEPT #service iptables save 注意要保存规则设置;

关于selinux的设置:(不解决这个问题ftp无法建立数据连接) 1,查看ftp的布尔值; getsebool -a | grep ftp 2,设置ftp服务具有写权限的布尔值,使之永久生效; setsebool -P allow_ftpd_anon_write on 3,设置上下文权限; ls -Zd /var/ftp/pub chcon -t public_content_rw_t /var/ftp/pub 4,重启,reboot

在vsftpd服务中于Selinux有关的配置: 1.setsebool -P ftp_home_dir=1 当FTP授权用户登录到FTP服务器时允许vsftpd将用户引导到用户家目录; 2.setsebool -P allow_ftpd_anon_write=1 ; chcon -R -t /var/ftp 表示允许隐名用户上传文件到vsftpd服务器上;

NFS服务器配置: 创建/etc/exports配置文件如下: /cisco 10.1.210.0/24(rw,sync)

启动服务: /etc/init.d/rpc start /etc/init.d/nfs start chkconfig nfs on

##########

本地autofs服务配置信息(本地IP地址10.1.210.1)

1.修改/etc/auto.master文件如下: [root@shiyinyuan ~]# grep -v ^# /etc/auto.master /misc /etc/auto.misc /general /etc/auto.man #指定了一个主目录/general /net -hosts +auto.master 2.建立自动挂载自配置文件(子配置文件名称必须和主配置文件定义的名称保持一致) 可以通过系统提供的模板配置子配置文件/etc/auto.misc进行导入操作; [root@shiyinyuan ~]# cp /etc/auto.misc /etc/auto.man [root@shiyinyuan ~]# grep nfs /etc/auto.man nfs -fstype=nfs,rw 10.1.210.12:/cisco [root@shiyinyuan ~]# grep pub /etc/auto.man pub -ro,soft,intr 10.1.210.12:/var/ftp/pub

#将远程目录挂载到本地/general/pub目录中; 3.启动autofs服务 [root@shiyinyuan ~]# /etc/init.d/autofs restart [root@shiyinyuan ~]# chkconfig autofs on 4.测试 [root@shiyinyuan ~]# cd /general/ [root@shiyinyuan general]# ll total 0 [root@shiyinyuan general]# cd nfs [root@shiyinyuan nfs]# ll total 10080 -rw-r--r--. 1 nobody nobody 0 Jul 25 2014 cisco -rw-r--r--. 1 nobody nobody 0 Jul 25 2014 h3c -rw-------. 1 nobody nobody 10315662 Jul 25 2014 haoren.pdf -rw-r--r--. 1 nobody nobody 0 Jul 25 2014 juniper -rw-r--r--. 1 nobody nobody 71 Jul 25 2014 success.txt -rw-r--r--. 1 nobody nobody 0 Jul 25 2014 Unix

##################测试成功#############################