当前位置: 移动技术网 > 网络运营>服务器>ftp服务器 > linux下用Proftpd搭建ftp服务器及配置方法

linux下用Proftpd搭建ftp服务器及配置方法

2019年04月17日  | 移动技术网网络运营  | 我要评论
首先proftpd是一个开源软件,其官方地址是http://www.proftpd.org,目前最高版本是:1.3.1(非稳定版),稳定的版本是1.3.0,下载文件为pro

首先proftpd是一个开源软件,其官方地址是http://www.proftpd.org,目前最高版本是:1.3.1(非稳定版),稳定的版本是1.3.0,下载文件为proftpd-1.3.0a.tar.gz

  我下载的是1.3.0,上传到服务器上后按照常规的方法安装即可。

  tar -zxvf proftpd-1.3.0a.tar.gz
  cd proftpd*
  ./configure --prefix=/usr/local/proftpd
  make
  make install

  安装完成!接下来是配置。

  设置一:随机启动服务,sbin/proftpd文件复制到/etc/rc.d/rc.local文件夹中,以实现开机自动启动。
  设置二:配置文件在etc/proftpd.conf,配置文件说明如下:

  servername "proftpd default installation"
  servertype standalone
  defaultserver on

  分别表示:服务器名称,服务类型和默认服务状态!
  后面的服务端口啊什么的我就省去不说了,说最关键的权限控制部分。

  # set the user and group under which the server will run.
  user nobody
  group nogroup

  注意看上面:以什么用户和什么组来运行服务。

  更改为你现有的组和用户,这里为了管理上的方便和安全性上考虑,建议新建一个ftp组和ftp用户。
  # to cause every ftp user to be "jailed" (chrooted) into their home
  # directory, uncomment this line.
  #defaultroot ~
  是否允许用户进入用户的主目录,注意:可是适用/home替代
  # normally, we want files to be overwriteable.
  allowoverwrite on
  是否具有重写的权利
  # a basic anonymous configuration, no upload directories. if you do not
  # want anonymous users, simply delete this entire section.
  user ftp
  group ftp
  # we want clients to be able to login with "anonymous" as well as "ftp"
  useralias anonymous ftp
  # limit the maximum number of anonymous logins
  maxclients 10
  # we want 'welcome.msg' displayed at login, and '.message' displayed
  # in each newly chdired directory.
  displaylogin welcome.msg
  displayfirstchdir .message
  # limit write everywhere in the anonymous chroot
  denyall

  这部分是匿名用户的定义其实也很简单。

  启动不了,出现如下错误的解决方法

  [root@new-host sbin]# ./proftpd
  - ipv4 getaddrinfo 'new-host' error: name or service not known
  - warning: unable to determine ip address of 'new-host'
  - error: no valid servers configured
  - fatal: error processing configuration file '/usr/local/proftpd/etc/proftpd.conf'

  原因是无法绑定ip地址。

  在配置文件中增加下面这句:

  defaultaddress 192.168.8.105

  再重启服务就可以了!

  接上!原来以为这样就完事了,
可是看看才知道如果我要新增加一个ftp用户的话实际上是很麻烦的一件事,因为帐号是直接跟系统帐号想关联的,不安全也不好操作,我们的目的是要做成像虚拟主机服务提供商那样的ftp!

  接下去,需要一个模块的支持到去看看proftpd-mod-quotatab模块,注意下面这句话:

  note: mod_quotatab became part of the offical proftpd source distribution in 1.2.10rc1. if using a version of proftpd later than that, please use the mod_quotatab already included, as it will be most up-to-date.

  表示我们下载的1.3版本已经支持做了这个东西,主要编译安装的时候把mod_quotatab 开起来就可以了,于是重新编译!

  前提:确认你已经安装并且能够正常运行mysql,否则后面的工作都是没有意义的了!

  重新编译:

  ./configure --prefix=/usr/local/proftpd --with-modules=mod_sql:mod_sql_mysql:mod_quotatab:mod_quotatab_sql --with-includes=/usr/include/mysql --with-libraries=/usr/lib/mysql

  注意请根据自己的linux系统找到mysql的相应include和lib目录,以上例子中的相关路径是大多数linux系统默认的,如果你的mysql是通过源码编译安装的,则这两个目录一般在安装路径下。

  有资料说:需要修改contrib目录中mod_sql_mysql.c文件:

  vi mod_sql_mysql.c

  找到#include 这一行,将mysql.h改成你的系统中此文件所在的路径,如/usr/include/mysql/mysql.h
  可是我没有这样做也可以,真奇怪!

  然后make
  make install

  一样的切换到proftpd文件夹中操作,后面的比较复杂,我试试以大学教授的水平用简单的语言讲。
配置proftpd.conf文件

  编辑/usr/local/proftpd/etc/proftpd.conf文件:
  设置磁盘限额
  #设置磁盘限额
  quotadirectorytally on
  #设置磁盘容量显示时的单位
  quotadisplayunits "kb"
  #打开磁盘限额引擎
  quotaengine on
  #设置磁盘限额日志文件
  quotalog "/usr/local/proftpd/var/quota"
  #允许显示磁盘限额信息,ftp登录后可执行quote site quota命令查看当前磁盘使用情况
  quotashowquotas on
  指定磁盘限额模块使用的数据库信息
  在proftpd.conf文件中加入以下配置:
  sqlnamedquery get-quota-limit select "name, quota_type, per_session, limit_type, bytes_in_avail, \
  bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail from quotalimits \
  where name = '%{0}' and quota_type = '%{1}'"
  sqlnamedquery get-quota-tally select "name, quota_type, bytes_in_used, bytes_out_used, \
  bytes_xfer_used, files_in_used, files_out_used, files_xfer_used from quotatallies \
  where name = '%{0}' and quota_type = '%{1}'"
  sqlnamedquery update-quota-tally update "bytes_in_used = bytes_in_used + %{0}, \
  bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, \
  files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, \
  files_xfer_used = files_xfer_used + %{5} \
  where name = '%{6}' and quota_type = '%{7}'" quotatallies
  sqlnamedquery insert-quota-tally insert "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies
  quotalimittable sql:/get-quota-limit
  quotatallytable sql:/get-quota-tally/update-quota-tally/insert-quota-tally

  很多朋友就要问了:这些是什么东东啊?完全看不懂!我也不懂!继续照做就是了~~

  配置ftp用户为mysql数据库认证方式

  在proftpd.conf文件中加上以下配置:
  sqlconnectinfo databasename@hostname:port username password
  #databasename是为proftpd建立的mysql数据库的名字
  #hostname是mysql数据库所在的服务器的名字或ip地址
  #port是mysql服务所使用的端口
  #username是数据库的用户名
  #password是数据库用户密码
  sqlauthtypes backend plaintext
  #backend表示用户认证方式为mysql数据库的认证方式
  #plaintext表示明文认证方式,排在最前面的为最先使用的方式
  sqluserinfo ftpuser userid passwd uid gid homedir shell
  #指定ftp用户数据表的名字和其中的字段名,表名可自行定义,字段名不要改动。
  sqlgroupinfo ftpgroup groupname gid members
  #指定ftp用户组数据表的名字和其中的字段名,这个数据表是可选的,字段名不要改动。
  requirevalidshell off
  #指定是否必须为ftp用户指定一个系统shell,off表示不用指定,on表示必须指定。为了系统安全应该指定为off。
  sqlauthenticate users groups usersetfast groupsetfast
  #校验数据表
  sqlhomedirondemand on
  #如果用户主目录不存在,则系统会根据此用户在用户数据表中的homedir字段的值新建一个目录
创建ftp系统用户和组
  创建一个ftp用户和组,以后所有的其它ftp用户实际上都是通过这个系统用户进行ftp的:
  groupadd –g 2003 ftpgroup
  useradd –u 2003 –g ftpgroup –d /home ftpuser
  以上建立了一个名为ftpgroup的ftp组,组id为2003;建立了一个ftp用户,用户id为2003,并加入ftpgroup组,用户主目录为/home
  接着修改/home的属主和访问权限:
  chown –r ftpuser.ftpgroup /home
  chmod 644 /home
  644的具体含义请自查看相关文档。
  再修改proftpd.conf文件,找到user 和 group,将user指定为ftpuser,group指定为ftpgroup,替换掉原默认值,否则如果系统中没有原默认值所指定的用户和组的话,proftpd是不能成功启动的。

  建立ftp用户认证相关表
  mysql –u root
  use mysql

  添加一个mysql用户:ftp
  添加一个数据库:proftpd
  具体指令请参考mysql手册。
  建完用户和数据库后,以新建的用户登录mysql:

  mysql –u ftp
  use proftpd;
  create table ftpuser (userid text not null,passwd text not null,uid int not null,gid int not null,homedir text,shell text);
  userid是用户名,passwd是用户密码,uid是用户id,gid是用户所在组的id,homedir是用户主目录,shell是用户的系统shell。
  create table ftpgroup (groupname text not null,gid smallint not null,members text not null);
  groupname是组名,gid是组id,members是组的成员,有多个成员时,要用逗号隔开。
  以上建立了ftp用户和用户组的数据表。
  建立磁盘限额数据表
  create table quotalimits (
  name varchar(30),
  quota_type enum("user", "group", "class", "all") not null,
  per_session enum("false", "true") not null,
  limit_type enum("soft", "hard") not null,
  bytes_in_avail float not null,
  bytes_out_avail float not null,
  bytes_xfer_avail float not null,
  files_in_avail int unsigned not null,
  files_out_avail int unsigned not null,
  files_xfer_avail int unsigned not null
  );
  create table quotatallies (
  name varchar(30) not null,
  quota_type enum("user", "group", "class", "all") not null,
  bytes_in_used float not null,
  bytes_out_used float not null,
  bytes_xfer_used float not null,
  files_in_used int unsigned not null,
  files_out_used int unsigned not null,
  files_xfer_used int unsigned not null
  );

  以上quotalimits表是ftp用户的磁盘限额配置信息,quotatallies表存放的是用户磁盘限额变动的信息。
  quotatallies表不需要作修改,由程序自动记录
  下面是quotalimits 表中各字段的含意:
  quota_type 磁盘限额的鉴别
  bytes_in_avail 上传最大字节数,就是ftp用户空间容量
  bytes_out_avail 下载最大字节数
  bytes_xfer_avail 总共可传输的文件的最大字节数(上传和下载流量)
  files_in_avail 总共能上传文件的数目
  files_out_avail 能从服务器上下载文件的总数目
  files_xfer_avail 总共可传输文件的数目(上传和下载)

  数据表数据初始化

  表全部建完后,就可以创建ftp用户了,在ftpuser表中插入一条记录如下:
  insert into ftpuser (userid, passwd, uid, gid, homedir, shell) values ('shine', password(‘yourpassword'), '2003', '2003', '/home/shine', '' );
  以上建立了一个新的ftp用户,用户名为shine,密码为yourpassword,可以看到密码用mysql的系统函数password进行了加密,用户id和组id均为2003,这里随便填,我填了和系统用户ftpuser和系统组ftpgroup相同的id,不知道会不会有问题。/home/shine为用户主目录,用户shell为空。

  如果要想应用到更多的功能,且建立了组的数据表,你也要为此添加记录,不过一定要注意在members的字段多个成员一定要用逗号隔开。

  insert into ftpgroup values ('ftpgroup', 2003, 'ftpuser');
  下面再来建立初始的用户磁盘限额信息:
  将已经建立的shine帐号赋予50m空间,最多能上传500个文件,文件传输流量为512k,总共只能传输2000个文件,插入记录如下:
  insert into quotalimits (name,quota_type,per_session,limit_type,bytes_in_avail,bytes_out_avail,bytes_xfer_avail,files_in_avail,files_out_avail,files_xfer_avail)values (‘shine', ‘user', ‘true', ‘soft', ‘51200000', ‘0','512000',‘500','0','2000');
  运行proftpd
  执行以下命令启动proftpd服务:
  /usr/local/proftpd/sbin/proftpd
  开一个控制台输入:
  ftp ftphostname
  输入用户名:shine
  输入用户密码:yourpassword
  成功登录后执行:
  quote site quota
  可以看到此用户的磁盘使用情况。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网