今天在51cto论坛Linux区看到的问题,比较感兴趣,就测试一下。

问题来源:http://bbs.51cto.com/thread-919310-1.html

需求:将ftp服务 设置为只允许 早上8点 到 晚上 10 点 访问,其余时间不能访问。  
以下测试基本CentOS 5.5,vsftpd-2.0.5.关闭iptables、selinux.
方法一:通过xinetd启动vsftpd,控制访问时间段
1)安装vsftp软件和xinetd守护进程
 
  
  1. [root@server ~]# yum -y install vsftpd xinetd 
如果不能使用yum,那就挂载光盘进行安装
 
  
  1. [root@server ~]# chkconfig vsftpd on 
  2. [root@server ~]# chkconfig xinetd on 

2)修改/etc/vsftpd/vsftpd.conf使vsftpd不用独立运行方式启动
 
  
  1. [root@server ~]# vim /etc/vsftpd/vsftpd.conf 
  2.     listen=YES 
  3.  
  4. 改为: 
  5.     listen=NO 

3)配置使用vsftp通过xinetd启动
 
  
  1. [root@server ~]# cp /usr/share/doc/vsftpd-2.0.5/vsftpd.xinetd /etc/xinetd.d/vsftpd 
  2. [root@server ~]# vim /etc/xinetd.d/vsftpd  
  3. service ftp 
  4.         socket_type             = stream 
  5.         wait                    = no 
  6.         user                    = root 
  7.         server                  = /usr/sbin/vsftpd 
  8.         server_args             = /etc/vsftpd/vsftpd.conf 
  9.         nice                    = 10 
  10.         disable                 = no 
  11.         access_times            = 09:00-22:00 
  12.         flags                   = IPv4 

将disable的值改成no,允许vsftpd启动,access_times定义可以访问的时间
4)重启服务测试登录
 
  
  1. [root@server ~]# /etc/init.d/xinetd restart 
  2. [root@client ~]# lftp 192.168.209.128 
  3. lftp 192.168.209.128:~> ls               
  4. drwxr-xr-x    2 0        0            4096 Dec 16  2009 pub 
  5. drwxrwxrwx   10 0        0            4096 Feb 28 02:13 upload 

当前时间为19:00,属于ftp允许访问的时间范围内,现在访问时间段修改为09:00-18:00
 
  
  1. service ftp 
  2.         socket_type             = stream 
  3.         wait                    = no 
  4.         user                    = root 
  5.         server                  = /usr/sbin/vsftpd 
  6.         server_args             = /etc/vsftpd/vsftpd.conf 
  7.         nice                    = 10 
  8.         disable                 = no 
  9.         access_times            = 09:00-18:00 
  10.         flags                   = IPv4 
  11. [root@server ~]# /etc/init.d/xinetd restart 
  12.  
  13. [root@client ~]# lftp 192.168.209.128 
  14. lftp 192.168.209.128:~> ls 
  15. `ls' at 0 [Delaying before reconnect: 20] 
 
无法访问了,看看访问的日记会不会有提示:
 
  
  1. [root@server ~]# tail -2 /var/log/messages 
  2. Feb 29 19:24:17 server xinetd[8851]: FAIL: ftp time from=192.168.209.129 
  3. Feb 29 19:24:53 server xinetd[8851]: FAIL: ftp time from=192.168.209.129 
PS:通过xinetd启动的进程,还可以使用only_from控制访问来源IP。
 
方法二:通过crontab来根据ftp开放时间控制进程启动
 
  
  1. 00 8 * * * /etc/init.d/vsftpd start 
  2. 00 22 * * * /etc/init.d/vsftpd stop  

这种方法使用的是vsftpd独立运行方式。
 
所要求的目的达到了。。。。
以上内容,纯属个人测试笔记,如有不错,多多指教。。。。