分类
Linux

Pureftpd完全配置

这篇文档描述了在CentOS 6.2下安装Pureftpd服务器,包括使用MySQL的虚拟用户,磁盘配额,带宽管理,TLS加密会话和集成病毒检查功能。

在文档开始之前,假设你已经安装好了基本的CentOS 6.2操作系统,且网络正常,安装并配置好了MySQL数据库。如果没有,下面简单说明一下。

分类
Linux

CentOS 5.5安装Pure-ftpd

最近想更换一个FTP软件,看到Pure-FTPd不错,对客户端编码支持比较好,尤其是现在我同事大多使用WINDOWS,如果使用VSFTPD,上传上去的文档之类的全部是乱码,而且Pure-Ftpd还可以很方便的打开TLS认证,于是乎先卸载了原本安装的VSFTPD软件,先yum search pure,发现有Pure-FTPD,嘿嘿,直接YUM安装即可。

yum install pure-ftpd  

安装完成之后,就可以进行配置了。下面是我的配置文件,提供给大家看看。

 vi /etc/pure-ftpd/pure-ftpd.conf 
############################################################ 
# # # Configuration file for pure-ftpd wrappers # # # ############################################################ 
# If you want to run Pure-FTPd with this configuration 
# instead of command-line options, please run the 
# following command : 
# # /usr/sbin/pure-config.pl /etc/pure-ftpd/pure-ftpd.conf 
# # Please don't forget to have a look at documentation at 
# http://www.pureftpd.org/documentation.shtml for a complete list of 
# options. 
# Cage in every user in his home directory 将所有用户锁定于其home目录 
ChrootEveryone yes 

# If the previous option is set to "no", members of the following group 
# won't be caged. Others will be. If you don't want chroot()ing anyone, 
# just comment out ChrootEveryone and TrustedGID.设置一个信任的组ID,这样就可以不被锁定于home目录了。 
# TrustedGID 100 
# Turn on compatibility hacks for broken clients 可以兼容IE浏览器 
BrokenClientsCompatibility no 

# Maximum number of simultaneous users 服务器可以同时登录的最大客户端数目 
MaxClientsNumber 50 

# Fork in background 是否以守护进程运行 
Daemonize yes 

# Maximum number of sim clients with the same IP address同一IP最大连接数,默认是8 
MaxClientsPerIP 4 

# If you want to log all client commands, set this to "yes". 
# This directive can be duplicated to also log server responses.记录所有的客户端命令到日志里面。 
VerboseLog no 

# List dot-files even when the client doesn't send "-a". 显示隐藏文件。 
DisplayDotFiles yes  
# Don't allow authenticated users - have a public anonymous FTP only. 禁止匿名登录 
AnonymousOnly no 

# Disallow anonymous connections. Only allow authenticated users. 禁止匿名连接,只允许认证用户。 
NoAnonymous yes 

# Syslog facility (auth, authpriv, daemon, ftp, security, user, local*) 
# The default facility is "ftp". "none" disables logging. SyslogFacility ftp 
# Display fortune cookies 
# FortunesFile /usr/share/fortune/zippy 
# Don't resolve host names in log files. Logs are less verbose, but 
# it uses less bandwidth. Set this to "yes" on very busy servers or 
# if you don't have a working DNS. 
DontResolve yes 

# Maximum idle time in minutes (default = 15 minutes) 最大空闲时间 
MaxIdleTime 5 

# LDAP configuration file (see README.LDAP) 
# LDAPConfigFile /etc/pure-ftpd/pureftpd-ldap.conf 
# MySQL configuration file (see README.MySQL) 
# MySQLConfigFile /etc/pure-ftpd/pureftpd-mysql.conf 
# Postgres configuration file (see README.PGSQL) 
# PGSQLConfigFile /etc/pure-ftpd/pureftpd-pgsql.conf 
# PureDB user database (see README.Virtual-Users) 
# PureDB /etc/pure-ftpd/pureftpd.pdb # Path to pure-authd socket (see README.Authentication-Modules) 
# ExtAuth /var/run/ftpd.sock 
# If you want to enable PAM authentication, uncomment the following line 
PAMAuthentication yes 

# If you want simple Unix (/etc/passwd) authentication, uncomment this 
UnixAuthentication yes 

# Please note that LDAPConfigFile, MySQLConfigFile, PAMAuthentication and 
# UnixAuthentication can be used only once, but they can be combined 
# together. For instance, if you use MySQLConfigFile, then UnixAuthentication, 
# the SQL server will be asked. If the SQL authentication fails because the 
# user wasn't found, another try 
# will be done with /etc/passwd and 
# /etc/shadow. If the SQL authentication fails because the password was wrong, 
# the authentication chain stops here. Authentication methods are chained in 
# the order they are given. 

# 'ls' recursion limits. The first argument is the maximum number of 
# files to be displayed. The second one is the max subdirectories depth 
LimitRecursion 2000 8 

#Are anonymous users allowed to create new directories ? 
AnonymousCanCreateDirs no 

# If the system is more loaded than the following value, 
# anonymous users aren't allowed to download. 
MaxLoad 4 

# Port range for passive connections replies. - for firewalling. PASSIVE端口范围 
PassivePortRange 45000 50000 

# Force an IP address in PASV/EPSV/SPSV replies. - for NAT. 
# Symbolic host names are also accepted for gateways with dynamic IP 
# addresses. 
#如果FTP服务器在内网,而PASSIVE的模式是被动连接,所以要指定一个公网IP。如果不是通过NAT出公网,就不需要了。 
ForcePassiveIP x.x.x.x 

# Upload/download ratio for anonymous users. 
# AnonymousRatio 1 10 

# Upload/download ratio for all users. 
# This directive superscedes the previous one. 
# UserRatio 1 10 

# Disallow downloading of files owned by "ftp", ie. 
# files that were uploaded but not validated by a local admin. 
AntiWarez yes 

# IP address/port to listen to (default=all IP and port 21). 
# FTP服务绑定的IP和端口,我发现设置为127.0.0.1后,不能连接,设置为0.0.0.0后就可以,这个大家注意一下。 
# Bind 127.0.0.1,21 

# Maximum bandwidth for anonymous users in KB/s 
# AnonymousBandwidth 8 

# Maximum bandwidth for *all* users (including anonymous) in KB/s 
# Use AnonymousBandwidth *or* UserBandwidth, both makes no sense. 
# UserBandwidth 8 

# File creation mask. <umask files="" for="">:</umask><umask dirs="" for=""> . 
# 177:077 if you feel paranoid. 
Umask 133:022 

# Minimum UID for an authenticated user to log in. 登录用户的UID如果低于500,是无法登录的。 
MinUID 500 

# Do not use the /etc/ftpusers file to disable accounts. We're already 
# using MinUID to block users with uid :<max anonymous="" sessions=""> 
# For instance, 3:20 means that the same authenticated user can have 3 active 
# sessions max. And there are 20 anonymous sessions max. 
# PerUserLimits 3:20 

# When a file is uploaded and there is already a previous version of the file 
# with the same name, the old file will neither get removed nor truncated. 
# Upload will take place in a temporary file and once the upload is complete, 
# the switch to the new version will be atomic. For instance, when a large PHP 
# script is being uploaded, the web server will still serve the old version and 
# immediatly switch to the new one as soon as the full file will have been 
# transfered. This option is incompatible with virtual quotas. 
# NoTruncate yes 

# This option can accept three values : 
# 0 : disable SSL/TLS encryption layer (default). 关闭SSL/TLS加密层,也就是只支持普通的明文连接。 
# 1 : accept both traditional and encrypted sessions. 支持明文连接和加密连接。 
# 2 : refuse connections that don't use SSL/TLS security mechanisms, 
# including anonymous sessions.只允许SSL/TLS加密连接。 
# Do _not_ uncomment this blindly. Be sure that : 
# 1) Your server has been compiled with SSL/TLS support (--with-tls), 
# 2) A valid certificate is in place, 
# 3) Only compatible clients will log in. TLS 2 

# Listen only to IPv4 addresses in standalone mode (ie. disable IPv6) 
# By default, both IPv4 and IPv6 are enabled. 只监听IPV4地址。 
IPV4Only yes 

# Listen only to IPv6 addresses in standalone mode (ie. disable IPv4) 
# By default, both IPv4 and IPv6 are enabled. 
IPV6Only no 

# UTF-8 support for file names (RFC 2640) 
# Define charset of the server filesystem and optionnally the default charset 
# for remote clients if they don't use UTF-8. 
# Works only if pure-ftpd has been compiled with --with-rfc2640 
# 客户端编码 
#FileSystemCharset utf-8 
ClientCharset gbk  

编辑完成后,就可以启动服务了。

chkconfig --levels 235 pure-ftpd on  
service pure-ftpd start  

接着就可以在iptables里面增加相应的规则,比如运行21端口和PASSIVE端口范围。

iptables -A INPUT -p tcp -m state --state NEW --dport 21 -j ACCEPT iptables -A INPUT -p tcp -m multiport --dports 45000:50000 -j ACCEPT  

如果在内网,还需要在网关上添加端口映射。

iptables -t nat -A PREROUTING -d 202.123.212.113 -p tcp -m tcp --dport 2020 -j DNAT --to-destination 192.168.10.3:2020  
iptables -t nat -A PREROUTING -d 202.123.212.113 -p tcp -m multiport --dports 43000:45000 -j DNAT --to-destination 192.168.10.3  

附:证书生成方法

mkdir -p /etc/ssl/private/  
openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -keyout /etc/ssl/private/pure-ftpd.pem -out /etc/ssl/private/pure-ftpd.pem  
chmod 600 /etc/ssl/private/pure-ftpd.pem  
分类
Linux

Ubuntu Pure-FTPd服务安装配置+虚拟用户

Pure-FTPd FTP server:
Pure-FTPd is a fast, production-quality, standards-conformant FTP server based upon Troll-FTPd. Features include chrooted home directories, virtual domains, built-in ‘ls’, anti-warez system, configurable ports for passive downloads, FXP protocol, bandwidth throttling, ratios, fortune files, Apache-like log files, fast standalone mode, atomic uploads, text / HTML / XML real-time status report, virtual users, virtual quotas, privilege separation, SSL/TLS and more.

一、安装Pure-FTPd

$ sudo apt-get install pure-ftpd
$ sudo apt-get install pure-ftpd-mysql

二、配置Pure-FTPd

1.添加用户和组

$ sudo groupadd -g 2001 ftpgroup
$ sudo useradd -u 2001 -s /bin/false -d /dev/null -c "Pure-FTPd User" -g ftpgroup ftpuser

在上面创建用户的命令中,指定将/bin/false作为shell,使得该用户无法登录到服务器的shell环境,从而避免了很多安全问题;而将其HOME目录设置为/dev/null,也提高操作系统的安全。

2.Chroot 设置

为了安全起见,我们要将每个FTP用户限制在Chroot环境中。为此,我们可以在其配置目录/etc/pure-ftpd/conf下面创建一个名为ChrootEveryone的文件,并将内容设置为yes

$ sudo sh -c "echo 'yes' > /etc/pure-ftpd/conf/ChrootEveryone"

这样设置后,每个FTP用户自己的HOME目录就成了他的root目录,他无法达到上层目录中,也就无法浏览其它目录下的文件了。

3.手工创建用户目录

下面的设置,告诉Pure-FTPd不要为用户自动创建HOME目录,我们将手工创建,后面我将会介绍web管理工具创建用户和HOME目录:

$ sudo sh -c "echo 'No' > /etc/pure-ftpd/conf/CreateHomeDir"

4.为Pure-FTPd 创建MySQL 数据库

$ mysql -u root -p
mysql> CREATE DATABASE ftpusers;  

然后,创建一个名为ftpadmin的MySQL用户,密码为ftpadminPassword,并将数据库ftpusers的相关权限赋给该用户:

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON ftpusers.* TO  
 'ftpadmin'@'localhost' IDENTIFIED BY 'ftpadminPassword';  
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON ftpusers.* TO  
 'ftpadmin'@'localhost.localdomain' IDENTIFIED BY 'ftpadminPassword';  
mysql> FLUSH PRIVILEGES;  

然后,我们在该数据库中,创建一个users表和admin表,其SQL语句如下:

mysql> USE ftpusers;  
mysql> CREATE TABLE IF NOT EXISTS `users` (  
 `User` varchar(16) NOT NULL default '',  
 `Password` varchar(32) NOT NULL default '',  
 `Uid` int(11) NOT NULL,  
 `Gid` int(11) NOT NULL,  
 `Dir` varchar(128) NOT NULL default '',  
 `QuotaFiles` int(10) NOT NULL default '500',  
 `QuotaSize` int(10) NOT NULL default '30',  
 `ULBandwidth` int(10) NOT NULL default '80',  
 `DLBandwidth` int(10) NOT NULL default '80',  
 `Ipaddress` varchar(15) NOT NULL default '*',  
 `Comment` tinytext,  
 `Status` enum('0','1') NOT NULL default '1',  
 `ULRatio` smallint(5) NOT NULL default '1',  
 `DLRatio` smallint(5) NOT NULL default '1',  
 PRIMARY KEY (`User`),  
 UNIQUE KEY `User` (`User`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

mysql> CREATE TABLE IF NOT EXISTS `admin` (  
 `Username` varchar(35) NOT NULL default '',  
 `Password` char(32) NOT NULL default '',  
 PRIMARY KEY (`Username`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

mysql> quit  

5.配置Pure-FTPd 的mysql.conf

$ sudo cp /etc/pure-ftpd/db/mysql.conf /etc/pure-ftpd/db/mysql.conf_orig
$ sudo vi /etc/pure-ftpd/db/mysql.conf

内容为:

MYSQLServer 127.0.0.1  
 MYSQLSocket /var/run/mysqld/mysqld.sock  
 MYSQLUser ftpadmin  
 MYSQLPassword ftpadminPassword  
 MYSQLDatabase ftpusers  
 MYSQLCrypt md5  
 MYSQLGetPW SELECT Password FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MYSQLGetUID SELECT Uid FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MYSQLGetGID SELECT Gid FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MYSQLGetDir SELECT Dir FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MySQLGetQTAFS SELECT QuotaFiles FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MySQLGetQTASZ SELECT QuotaSize FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MySQLGetRatioUL SELECT ULRatio FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MySQLGetRatioDL SELECT DLRatio FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MySQLGetBandwidthUL SELECT ULBandwidth FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")  
 MySQLGetBandwidthDL SELECT DLBandwidth FROM users WHERE User="\L" AND Status="1" AND (Ipaddress = "*" OR Ipaddress LIKE "\R")
$ sudo chmod g=o= /etc/pure-ftpd/db/mysql.conf
$ sudo /etc/init.d/pure-ftpd-mysql restart

三、实现FTP用户的Web管理

1、安装User manager for PureFTPd

$ cd /var/www
$ sudo wget http://machiel.generaal.net/files/pureftpd/ftp_v2.1.tar.gz
$ sudo tar xvf ftp_v2.1.tar.gz

2、配置User manager for PureFTPd

我们来修改/var/www/ftp/config.php

$ sudo vi /var/www/ftp/config.php
 $LANG = “Chinese”;  
 $LocationImages = “images”;  
 $DBHost = “127.0.0.1″;  
 $DBLogin = “ftpadmin”;  
 $DBPassword = “ftpadminPassword”;  
 $DBDatabase = “ftpusers”;  
 $FTPAddress = “ubox.mytest.com:21″;  
 $DEFUserID = “2001″;  
 $DEFGroupID = “2001″;  
 $UsersFile = “/etc/passwd”;  
 $GroupFile = “/etc/group”;  
 $StyleSheet = “style/default.css.php”;  
 $EnableQuota = 1;  
 $EnableRatio = 1;  
 […]

上述配置中,MySQL数据库的用户名、密码、数据库名及FTPAddress,请按自己的实际情况修改。最后两行设置,默认启用Quota和速率限制。

3、设置User manager for PureFTPd 管理员

$ mysql -u root -p
mysql> USE ftpusers;  
mysql> INSERT INTO `admin` (`Username`, `Password`) VALUES  
 ('ftpadmin', MD5('ftpadminPassword'));
mysql> quit  

4、登录user manager进行用户管理

打开http://localhost/ftp/index.php输入ftpadmin的密码,进行管理。