vsftp 安装

Red hat linux AS4, vsftpd-2.0.4

目标:
用源码方式安装vsftp,禁止匿名用户使用FTP;新增系统用户ftpdemo,并允许ftpdemo使用FTP,ftpdemo在自己的ftp目录可以上传、下载、删除,除了自己的目录外,ftpdemo看不到其他目录。

网 上相关的文章很多,而且都说比较简单,但是我在安装过程并不太顺利,几经周转,终于让我碰对了。回顾一下,把这简单的事情搞复杂的关键是vsftp安装完 后,用的是默认的FTP随xinetd一起启动,当时觉得这个没什么关系,就一直没改,后来一直配置不成功,实在找不出问题,就把启动方式改成了独立启 动,再配置,居然可以正常使用FTP了,所以建议各位安装完vsftp后最好修改成独立启动方式,否则下面的配置可能不生效。

安装

我用的是源代码安装
vsftpd需要使用nobody来作为运行者,一般已经存在
#useradd nobody

安装时需要/usr/share/empty/作为临时目录,一般已经存在
#mkdir /usr/share/empty/

解压
#tar zxvf vsftpd-2.0.4.tar.gz
#cd vsftpd-2.0.4

编译
#make
#make install

如果make install没有安装文件,你可能需要手动执行下面的命令
#cp vsftpd /usr/local/sbin/vsftpd
#cp vsftpd.conf.5 /usr/local/man/man5
#cp vsftpd.8 /usr/local/man/man8
#cp vsftpd.conf /etc
#cp vsftpd.xinetd /etc/xinetd.d/vsftpd    //这个在安装时候的xinetd.d目录下有
重启xinetd
#/etc/rc.d/init.d/xinetd restart
安装完毕
使本地用户能登录FTP。按照上面的源码安装配置我们的FTP还不能让本地用户登
录,因为缺少一个认证PAM文件,在源码目录下有一个RedHat/vsftpd.pam认证文
件,把它复制到/etc/pam.d/ftp。
#cp RedHat/vsftpd.pam /etc/pam.d/ftp
http://home.phpchina.com/space.php?uid=52440&do=blog&id=69273
用这种方式启动 否则一直报
500 OOPS: could not bind listening IPv4 socket
2) 启动
#service vsftpd start 启动

#service vsftpd stop 停止

#service vsftpd restart 重启
3) 配置
配置
设置为独立启动模式
安装完成后,vsftp是默认随xinetd一起启动,最好改成独立启动(我就是吃了这个亏,但是觉得没事,懒得改,导致后来配置总不成功,浪费了好多时间)。打开/etc/xinetd.d/vsftpd文件,把disable=no改成yes,
#vi /etc/xinetd.d/vsftpd

再在/etc/vsftpd.conf文件的最后添加listen=yes,
#vi /etc/vsftpd.conf

这样就改成了STANDALONE独立模式。

现在我们手动启动一下vsftp
因为xinetd已经启动了vsftpd,而vsftpd设置成了stardalone模式,
所以,先停止xinetd服务,  
#service xinetd stop
或者
#/etc/init.d/xinetd stop

然后后台执行启动ftp服务
#/usr/local/sbin/vsftpd &

(记得把xinetd服务启动/etc/init.d/xinetd start)


配置本地用户登录

现在的vsftp已经可以使用了,但是只允许匿名用户登录(默认配置),这和我们的目标还有点距离,我们要让本地用户ftpdemo可以FTP登录。

关闭匿名用户登录
出于安全性的考虑,我们不允许匿名用户使用ftp,修改/etc/vsftpd.conf
#vi /etc/vsftpd.conf

找到anonymous_enable项,取消注释,并把值改成no。重启vsftp看看,这下匿名用户不能登录了。

让本地用户登录,先修改配置
local_enable=YES
write_enable=YES
local_umask=022

使本地用户能登录FTP。按照上面的源码安装配置我们的FTP还不能让本地用户登
录,因为缺少一个认证PAM文件,在源码目录下有一个RedHat/vsftpd.pam认证文
件,把它复制到/etc/pam.d/ftp。
#cp RedHat/vsftpd.pam /etc/pam.d/ftp
测试一下,假设有一个本地用户test,登录FTP:
#ftp 127.0.0.1
Connected to 127.0.0.1 (127.0.0.1).
220 (vsFTPd 1.2.1)
Name (127.0.0.1:root): test
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
看来情况很好:)

创建 ftp用户
新建系统用户ftpdemo
#useradd -s /sbin/nologin -d /var/vhosts/www.gzfyw.com ftpdemo
-s /sbin/nologin 表示用户只能FTP登录,不能登录系统
-d /var/vhosts/www.gzfyw.com 表示用户主目录,即登录后的默认目录,根据实际情况修改成你需要的路径
如果上面的这条命令拷贝后执行出错的话,建议你手动敲入,我就碰到过这种情况

安全起见,马上给ftpdemo设置密码
#passwd ftpdemo

修改ftp目录的所属者
#chown -R ftpdemo:ftpdemo /var/vhosts/www.gzfyw.com

如果你都执行正确的话,那现在ftpdemo可以FTP登录了。开心啊。。。

限制用户权限
再用一会,你又会发现有另外一个问题,ftpdemo可以返回到ftp主目录的上级目录,这太不安全了,要把ftp用户禁止在自己的目录里,不让他们乱跑,接下来做这个事情,
如果需要将用户限制在自己的home文件夹中
在/etc/vsftpd.conf中,找到修改或者添加如下两行
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list
然后修改/创建/etc/vsftpd.chroot_list文件,在里面加入你想要限制的用户的用户名,比如你的用户名ftpdemo
kill掉vsftp进程,再重启vsftp,看到了吗,这下ftpdemo看不到ftp主目录外的其他目录,不能到处乱跑了,呵呵。

最后一次,把vsftp添加到开机启动项
打开/etc/rc.local,在文件尾增加一行
/usr/local/sbin/vsftpd &
另外:我使用chkconfig vsftpd on总添加不成功

补充:
登录后,发现FTP客户端的时间和服务器的时间不一样
原因是vsftpd默认使用GMT作为其时间,所以可能会导致与系统时间不一致(系统时间不是GMT)
解决方案:在vsftpd.conf文件中增加一行
use_localtime=yes

相关命令:
重启xinetd服务
/etc/init.d/xinetd restart
查看启动的ftp进程id
ps -ef | grep ftp
杀掉ftp进程
kill 123
在后台启动vsftp
/usr/local/sbin/vsftpd &
查看自启动项
chkconfig --list
新增用户
useradd -s /sbin/nologin -d /var/vhosts/www.gzfyw.com ftpdemo
passwd ftpdemo
chown -R ftpdemo:ftpdemo /var/vhosts/www.gzfyw.com
vi /etc/vsftpd.chroot_list

附:/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.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# 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=022
#
# 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=YES
#
# 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 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

listen=yes
use_localtime=yes
原文地址:https://www.cnblogs.com/danghuijian/p/4400467.html