vsftpd架设(配置pam模块)

Vsftpd 是很安全的ftp软件

VSFTPD的目录结构

/usr/sbin/vsftpd: VSFTPD的可执行文件

/etc/rc.d/init.d/vsftpd:启动脚本

/etc/vsftpd/vsftpd.conf:主配置文件

/etc/pam.d/vsftpd:PAM认证文件

/etc/vsftpd.ftpusers:禁用使用VSFTPD的用户列表文件

/etc/vsftpd.user_list:禁止或允许使用VSFTPD的用户列表文件

/var/ftp:匿名用户主目录

一,安装vsftpd

 sudo aptitude install vsftpd

二,配置

注意,等号“=”两边不能有空格

1,备份主配置表

sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.backup

然后打开此配置表,

sudo gedit /etc/vsftpd.conf

 ctrl+f 找到

anonymous_enable=YES 

# 是否允许匿名FTP访问。

local_enable=YES

# 是否允许本地用户访问,这里虚拟用户也是本地用户

write_enable=YES 

#是否允许所有用户都上传文件

local_umask=027

# vsftpd的默认umask是077,即,屏蔽除了创建者以外所有的用户的权限,如果觉得这样没必要,可以设置为022,这样其他用户可读。

anon_upload_enable=YES

#是否允许匿名用户上传

ftpd_banner=Welcome to blah FTP service

# 用户连接后看到的欢迎信息

pam_service_name=vsftpd

# PAM 服务名称,这里的设置决定PAM将为vsftpd使用配置文件

#/etc/pam.d/vsftpd

除了local_enable和ftpd_banner 之外,其他设置都很好地适应我们的需求,因此不需改动。

 

虚拟用户和真实用户?

真实用户,是在这台机器上登录的用户,比如安装系统时的用户。

虚拟用户,是创立的用户,只能登录ftp,而不能登录系统,不是系统的用户。这样比较安全。

为了使用虚拟用户,还需要加入以下设置:

 

guest_enable=YES

 

guest_username=ftp

 

user_config_dir=/etc/vsftpd_user_conf

这样使虚拟用户在系统中具有系统用户使用ftp的权限。系统用户ftp无法在终端登录,他是FTP服务目录的拥有者,最后一行为今后服务,是配置用户的目录。

4,配置PAM模块

   由于安全考虑,不希望vsftpd共享本地系统的用户认证信息,而采用自己独立的用户认证数据库来认证虚拟用户。这样,虚拟用户和真是的用户不必采用相同的用户名和口令。

   和linux下面大多数需要用户认证的程序一样,vsftpd也采用PAM作为后端,可插拔的认证模块来集成各种不同的认证方式。

   在这里,可以通过修改vsftpdPAM配置文件

/etc/pam.d/vsftpd来决定vsftpd使用何种认证方式,

可以是本地系统的真实用户认证(模块pam_unix),也可以是独立的用户认证数据库(模块pam_userdb,也可以是网络上的LDAP数据库(模块pam_ldap)等。所有这些模块都存放在/lib/security/目录(对AMD64则是/lib64/security/)下。

      这里采用pam_userdb模块,该模块采用独立的Berkeley DB 格式(Berkeley DB 是 Unix平台最常见的数据持久化格式,有各种版本,这里假设系统采用4.6版本)的用户认证数据库。为了建立Berkeley Db式的数据库,需要安装db4.6-util 软件包。

$ sudo aptitude install db4.6-util 

建立一个db 格式数据库的一般方式是先编辑一个文本文件,将键值和对应的数据写在相邻的行中。比如下面,我建立logins.txt的文档,内容如下:

upo 

magic

longxibendi

1234

根据pam_userdb 模块的约定,键值就是用户名,对应的数据则是口令。所以,这个文本文档中的奇数行为用户名,用户名的下一行就是其对应的口令。

下面将文档logins.txt转换为berkeley Db格式,并使他对应用户为不可读:

sudo db4.6_load -T -t hash -f logins.txt /etc/vsftpd_login.db

sudo chmod 600 /etc/vsftpd_login.db

rm logins.txt #这是删除logins.txt文档

 

然后编辑 /etc/pam.d/vsftpd

注释掉或者删除掉原有的所有内容,

加入这几行:

# /etc/pam.d/vsftpd

 

auth required /lib/security/pam_userdb.so db=/etc/vsftpd_login

 

account required /lib/security/pam_userdb.so db=/etc/vsftpd_login

PAM采用相应的认证模块和刚刚建立的用户数据库。

重新启动 vsftpd之后,虚拟用户设置就生效了。

sudo invoke-rc.d vsftpd restart

至此,用户upo 或者 longxibendi 已经可以用相应的用户名和口令登录FTP了。 

5,分用户设置

上面设置了两个用户分别是upolongxibendi

接下来相让upo 可以上传文件,但是不希望longxibendi也有同样的权限 ,借助vsftpd提供的分用户 设置机制,可以容易的做到这一点。

  上文,已经在/etc/vsftpd.conf 中加入了

user_config_dir 设置,现在只需建立相应的目录,并在该目录下增加用户upo的配置文件即可:

代码:

sudo mkdir /etc/vsftpd_user_conf 

#建立文件夹,在/etc/下,名字是#vsftpd_user_conf

#这样才可以与上面的配置相对应。

代码:

sudo sh -c “echo 'write_enable=YES' > /etc/vsftpd_user_conf/upo”

将 内容write_enable=YES 

/etc/vsftpd_user_conf/upo

而,write_enable=YES的意思是允许写操作

代码:

sudo sh -c “echo 'anno_upload_enable=YES' >> / /etc/vsftpd_user_conf/upo

按行,也就是为什么要用‘>> /’

# anno_upload_enable=YES写 

/etc/vsftpd_user_conf/upo 

# anno_upload_enable=YES 允许用户上传

代码:

sudo sh -c “echo 'anno_mkdir_write_enable=YES' >> / /etc/vsftpd_user_conf/upo”

也是写入...内容到...

# anno_mkdir_write_enable=YES 意思是

允许建立文件夹

如果允许用户upo删除和重命名文件(不建议)

执行:

sudo sh -c “echo 'anon_other_write_enable=YES' >> / /etc/vsftpd_user_conf/upo”

然后运行下面的命令,让vsftpd读入新配置

代码:

sudo invoke-rc.d vsftpd reload

现在用户upo 可以上传文件了,作为站点管理员,可能希望文件只能被上传到限定的目录,所以运行下面:

sudo mkdir /home/ftp/incoming

建立文件夹incoming /home/ftp/

sudo chown ftp:nogroup incoming

sudo chmod 770 /home/ftp/incoming 

sudo chmod -w /home/ftp

上面的命令是配置文家夹的属性

    这样,目录/home/ftpftp用户,也就是隐藏在所有虚拟用户背后的真实用户不可写了,因此上传到/home/ftp不会成功。

    注意,上面的例子中屏蔽了一般用户对/home/ftp/incoming 的访问权限,这样做是为了利用vsftpd的一个副作用。

     在默认的情况下,虚拟用户只能读取,或者下载对于任何用户都可读的文件和目录。上面的设置使虚拟用户无法列出目录 /home/ftp/incoming 下的文件,通常这是一个不错的特性。另一方面,通常,会希望upo用户,也可以看到自己上传的文件,为此可以运行如下命令:

代码:

sudo sh -c “echo 'anno_world_readable_only=NO' >> / etc/vsftpd_user_conf/upo”

... 写入...文档

# anno_world_readable_only=NO 意思是

取消用户的只读限制,不加这句,用户不能查看自

己上传的文件。

代码:

sudo killall -l vsftpd

sudo /etc/init.d/vsftpd restart

现在用户upo 可以看到/home/ftp/incoming 的内容了,完美主义者,可在/etc/vsftpd_user_conf/upo

中加入下面两行

local_umask=027 

以彻底化副作用。

 

6,注意:

1),上面的添加文档内容的命令,可以用gedit 来完成。

2),配置表中的''号两边不能有空格

3),上面的关于/etc/vsftpd_user_conf/upo并没有配置下载权限,实际上默认是可以下载文件的。包括,我们并没有配置用户longxibendi的权限,但他也可以下载文件。

 

7,下面是我的配置表:

第一个/etc/vsftpd.conf

 

# Example config file /etc/vsftpd.conf

 

#

 

# The default compiled in settings are fairly paranoid. This sample file

 

# loosens things up a bit, to make the ftp daemon more usable.

 

# Please see vsftpd.conf.5 for all compiled in defaults.

 

#

 

# READ THIS: This example file is NOT an exhaustive list of vsftpd options.

 

# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's

 

# capabilities.

 

#

 

#

 

# Run standalone?  vsftpd can run either from an inetd or as a standalone

 

# daemon started from an initscript.

 

listen=YES

 

#

 

# Run standalone with IPv6?

 

# Like the listen parameter, except vsftpd will listen on an IPv6 socket

 

# instead of an IPv4 one. This parameter and the listen parameter are mutually

 

# exclusive.

 

#listen_ipv6=YES

 

#

 

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).

 

anonymous_enable=YES

 

#

 

# Uncomment this to allow local users to log in.

 

local_enable=YES

 

#

 

# Uncomment this to enable any form of FTP write command.

 

write_enable=YES

 

#

 

# Default umask for local users is 077. You may wish to change this to 022,

 

# if your users expect that (022 is used by most other ftpd's)

 

local_umask=027

 

#

 

# Uncomment this to allow the anonymous FTP user to upload files. This only

 

# has an effect if the above global write enable is activated. Also, you will

 

# obviously need to create a directory writable by the FTP user.

 

anon_upload_enable=YES

 

#

 

# Uncomment this if you want the anonymous FTP user to be able to create

 

# new directories.

 

#anon_mkdir_write_enable=YES

 

#

 

# Activate directory messages - messages given to remote users when they

 

# go into a certain directory.

 

dirmessage_enable=YES

 

#

 

# Activate logging of uploads/downloads.

 

xferlog_enable=YES

 

#

 

# Make sure PORT transfer connections originate from port 20 (ftp-data).

 

connect_from_port_20=YES

 

#

 

# If you want, you can arrange for uploaded anonymous files to be owned by

 

# a different user. Note! Using "root" for uploaded files is not

 

# recommended!

 

#chown_uploads=YES

 

#chown_username=whoever

 

#

 

# You may override where the log file goes if you like. The default is shown

 

# below.

 

xferlog_file=/var/log/vsftpd.log

 

#

 

# If you want, you can have your log file in standard ftpd xferlog format

 

xferlog_std_format=NO

 

#

 

# You may change the default value for timing out an idle session.

 

idle_session_timeout=600

 

#

 

# You may change the default value for timing out a data connection.

 

data_connection_timeout=120

 

#

 

# It is recommended that you define on your system a unique user which the

 

# ftp server can use as a totally isolated and unprivileged user.

 

#nopriv_user=ftpsecure

 

#

 

# Enable this and the server will recognise asynchronous ABOR requests. Not

 

# recommended for security (the code is non-trivial). Not enabling it,

 

# however, may confuse older FTP clients.

 

#async_abor_enable=YES

 

#

 

# By default the server will pretend to allow ASCII mode but in fact ignore

 

# the request. Turn on the below options to have the server actually do ASCII

 

# mangling on files when in ASCII mode.

 

# Beware that on some FTP servers, ASCII support allows a denial of service

 

# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd

 

# predicted this attack and has always been safe, reporting the size of the

 

# raw file.

 

# ASCII mangling is a horrible feature of the protocol.

 

ascii_upload_enable=YES

 

ascii_download_enable=YES

 

#

 

# You may fully customise the login banner string:

 

ftpd_banner=Welcome to blah FTP service.

 

#

 

# You may specify a file of disallowed anonymous e-mail addresses. Apparently

 

# useful for combatting certain DoS attacks.

 

#deny_email_enable=YES

 

# (default follows)

 

#banned_email_file=/etc/vsftpd.banned_emails

 

#

 

# You may restrict local users to their home directories.  See the FAQ for

 

# the possible risks in this before using chroot_local_user or

 

# chroot_list_enable below.

 

chroot_local_user=NO

 

#

 

# You may specify an explicit list of local users to chroot() to their home

 

# directory. If chroot_local_user is YES, then this list becomes a list of

 

# users to NOT chroot().

 

#chroot_list_enable=YES

 

# (default follows)

 

#chroot_list_file=/etc/vsftpd.chroot_list

 

#

 

# You may activate the "-R" option to the builtin ls. This is disabled by

 

# default to avoid remote users being able to cause excessive I/O on large

 

# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume

 

# the presence of the "-R" option, so there is a strong case for enabling it.

 

ls_recurse_enable=YES

 

#

 

#

 

# Debian customization

 

#

 

# Some of vsftpd's settings don't fit the Debian filesystem layout by

 

# default.  These settings are more Debian-friendly.

 

#

 

# This option should be the name of a directory which is empty.  Also, the

 

# directory should not be writable by the ftp user. This directory is used

 

# as a secure chroot() jail at times vsftpd does not require filesystem

 

# access.

 

secure_chroot_dir=/var/run/vsftpd

 

#

 

# This string is the name of the PAM service vsftpd will use.

 

pam_service_name=vsftpd

 

#

 

# This option specifies the location of the RSA certificate to use for SSL

 

# encrypted connections.

 

rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem

 

# This option specifies the location of the RSA key to use for SSL

 

# encrypted connections.

 

rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

 

 

 

guest_enable=YES

 

guest_username=ftp

 

user_config_dir=/etc/vsftpd_user_conf

 

第二个 /etc/pam.d/vsftpd

#  # Standard behaviour for ftpd(8).

 

#  auth required pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

 

 

 

# Note: vsftpd handles anonymous logins on its own.  Do not enable

 

# pam_ftp.so.

 

 

 

# Standard blurb.

 

# @include common-account

 

# @include common-session

 

 

 

# @include common-auth

 

# auth required pam_shells.so

 

上面的行,是原有的。

 

 

 

# /etc/pam.d/vsftpd

 

auth required /lib/security/pam_userdb.so db=/etc/vsftpd_login

 

account required /lib/security/pam_userdb.so db=/etc/vsftpd_login

 

第三个 logins.txt

upo

 

magic

 

longxibendi

 

1234

第四个 /etc/vsftpd_user_conf/upo

write_enable=YES

 

anon_upload_enable=YES

 

anon_mkdir_write_enable=YES

 

anon_world_readable_only=NO

 

anon_other_write_enable=YES

 

#上面这行是是否允许删除和重命名操作

 

local_umask=027

 

 

 

 

 

 

 

 

 

 

 

 

参考资料:

Ubuntu实战技巧精粹》 何晓龙 编著 人邮出版社

Ubuntu linux从入门到精通》 郝铃,李晓 编著

http://forum.ubuntu.org.cn/viewtopic.php?f=54&t=117505

http://doc.linuxpk.com/4233.html(推荐)

 

原文地址:https://www.cnblogs.com/little-white/p/3757941.html