阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

在CentOS 6.3上搭建FTP服务器vsftpd

110次阅读
没有评论

共计 10583 个字符,预计需要花费 27 分钟才能阅读完成。

vsftpd 是 Linux 平台下非常著名的一个 ftp 软件。vsftpd 是 very secure ftp daemon 的缩写。

一、安装 vsftpd
先检查系统中是否安装了 vsftpd
[root@localhost Packages]# rpm -qa | grep vsftpd
[root@localhost Packages]#

如果没安装,就继续下面安装。

把 CentOS 的第一张安装光盘插入光驱,在光盘的 Packages 目录下找到 vsftpd 安装包,然后安装。
[root@localhost Packages]# ls *vsftpd*
vsftpd-2.2.2-11.el6.x86_64.rpm
[root@localhost Packages]# rpm -ivh vsftpd-2.2.2-11.el6.x86_64.rpm
Preparing…                ########################################### [100%]
  1:vsftpd                ########################################### [100%]

二、启动 vsftpd 服务

[root@localhost Packages]# service vsftpd status
vsftpd is stopped
[root@localhost Packages]# service vsftpd start
Starting vsftpd for vsftpd:                                [OK]

vsftpd 的匿名用户的默认主目录是 /var/ftp,vsftpd 的配置文件位于 /etc/vsftpd 目录下。

三、测试在客户端访问 vsftpd 服务器的 /var/ftp 目录。

在 Windows 系统的资源管理器的“地址”栏中输入“ftp://Centos 系统的 IP 地址”,回车即显示当前 /var/ftp 目录下的内容啦。

四、vsftpd 匿名用户的上传和下载权限的配置

很多朋友配置 vsftpd 时不能使用匿名用户上传和下载(创建目录或删除、重命名文件夹),本文主要解决 vsftpd 的匿名用户权限配制问题。

配置要注意三部分,请一一仔细对照:

1. vsftpd.conf 文件的配置(vi /etc/vsftpd/vsftpd.conf)
#允许匿名用户登录 FTP(如果不允许匿名访问就设置为 NO,后面有添加 FTP 用户的详细介绍)
anonymous_enable=YES
#打开匿名用户的上传权限
anon_upload_enable=YES
#打开匿名用户创建目录的权限
anon_mkdir_write_enable=YES

# 允许匿名用户具有建立目录和上传之外的权限,如重命名、删除等。

anon_other_write_enable=YES

2. ftp 目录的权限设置
ftp 的根目录为 /var/ftp,为了安全,这个目录默认不允许设置为 777 权限,否则 ftp 将无法访问。/var/ftp/pub 是匿名用户的默认访问目录,即 vsftpd.conf 文件中没有 anon_root 设置时的默认目录。不知为何,如果用 anon_root 指定其它目录后,在终端用 ftp 命令匿名登录就失败,并提示:500 OOPS: vsftpd: refusing to run with writable anonymous root 错误,目前还没有找到具体原因和解决办法。

一般至此,便实现 vsftpd 匿名用户的上传下载了,如果还不行,就按下面说明配置一下 selinux。

3. selinux 的配置
SELinux(Security-Enhanced Linux) 是美国国家安全局(NAS)对于强制访问控制的实现,是 Linux 上最杰出的新安全子系统。NSA 是在 Linux 社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。

最简单的办法是关闭 selinux
方法 1:修改 /etc/selinux/config 文件中的 SELINUX=”” 为 disabled,然后重启。(验证通过)
方法 2:用命令 setenforce 0,无需重启。(setenforce 的格式:setenforce [Enforcing | Permissive | 1 | 0])(转述的,没试过)
方法 3:在 lilo 或者 grub 的启动参数中增加:selinux=0, 也可以关闭 selinux。(转述的,没试过)

使用 getenforce 查看当前 selinux 是否正在运行。

如果不关闭 selinux,就要设置 selinux 的 ftp 权限。(转述的,没试过)
(1)使用 getsebool -a | grep ftp 查看 ftp 相关设置状态,我们要将 allow_ftpd_anon_write 设为 on。
(2)使用 setsebool -P 进行设置。例:setsebool -P allow_ftpd_anon_write=on。
或使用 togglesebool 进行 bool 值取反,例如 togglesebool allow_ftpd_anon_write。
(3)修改 selinux 安全上下文,先介绍两个命令:
命令 1:ls -Z ps -Z id -Z # 分别可以看到文件, 进程和用户的 SELinux 属性
命令 2:#chcon 改变 SELinux 安全上下文
chcon -u  对象
-r
-t
-R 递归
–reference 源文件 目标文件          # 复制安全上下文
使用方法:
步骤 1:ls -Zd /var/ftp/upload/ 通常会看到:
drwxr-xr-x ftp root system_u:object_r:public_content_t /var/ftp/upload/
步骤 2、chcon -R -t public_content_rw_t /var/ftp/upload/
步骤 3、ls -Zd /var/ftp/upload/ 如果看到如下信息就 OK 了:
drwxr-xr-x ftp root system_u:object_r:public_content_rw_t /var/ftp/upload/
最后还是重启下 selinux 和 vsftpd 吧,不重启其实也没关系。重新登录到 ftp 上,应该就能解决问题了。
另,selinux 的图形界面 可由 system-config-selinux 命令进入。

————————————– 分割线 ————————————–

FTP 服务器的简介就不介绍了,相信大家都知道是什么东东。不了解的就 google 吧!这里用到的 FTP 服务器软件是非常著名的 vsftpd.

1. 安装 vsFTPd 软件
源码安装就不说了, 可以 google, 因为在线安装很方便的.
Fedora 下安装很简单 yum install vsftpd 即可.

2. 启动 / 重启 / 关闭 vsftpd 服务器
[root@localhost ftp]# /sbin/service vsftpd restart
Shutting down vsftpd:                                      [OK]
Starting vsftpd for vsftpd:                                [OK]
OK 表示重启成功了.
启动和关闭分别把 restart 改为 start/stop 即可.
如果是源码安装的, 到安装文件夹下找到 start.sh 和 shutdown.sh 文件, 执行它们就可以了.

3. 与 vsftpd 服务器有关的文件和文件夹
vsftpd 服务器的配置文件的是: /etc/vsftpd/vsftpd.conf

vsftpd 服务器的根目录, 即 FTP 服务器的主目录:
[root@localhost ftp]# more /etc/passwd|grep ftp
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
这样你就能看到 FTP 的服务器的目录在 /var/ftp 处
如果你想修改服务器目录的路径, 那么你只要修改 /var/ftp 到别处就行了

4. 添加 FTP 用户
有的 FTP 服务器需要用户名和密码才能登录, 就是因为设置了 FTP 用户和权限.
FTP 用户一般是不能登录系统的, 只能进入 FTP 服务器自己的目录中, 这是为了安全. 这样的用户就叫做虚拟用户了. 实际上并不是真正的虚拟用户, 只是不能登录 SHELL 了而已, 没能力登录系统.

/usr/sbin/adduser -d /opt/test_ftp -g ftp -s /sbin/nologin test
这个命令的意思是:
使用命令 (adduser) 添加 test 用户, 不能登录系统 (-s /sbin/nologin), 自己的文件夹在(-d /opt/test_ftp)), 属于组 ftp(-g ftp)
然后你需要为它设置密码:passwd test
这样就添加了一个 FTP 用户了. 下面的示例可以帮助你进入 FTP 服务器了.

[root@localhost ftp]# ftp
ftp> open 192.168.0.33
Connected to 192.168.0.33 (192.168.0.33).
220 (vsFTPd 2.0.5)
Name (192.168.0.33:gxl): test
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> quit
221 Goodbye.

在 windows 中, 只要在浏览器中输入 ftp://192.168.0.33 进入 FTP 服务器, 然后 右键 登录, 输入用户名和密码就可以登录自己的目录了.
当然你要保证自己能读写自己的目录, 就要在配置文件 vsftpd.conf 里设置一下就可以读写了.
local_enable=yes
write_enable=yes
local_umask=022

5. 匿名上传下载
修改配置文件即可 vsftpd.conf, 确定有以下几行, 没有自己添加进去就可以了.
anonymous_enable=yes
anon_upload_enable=yes
anon_mkdir_write_enable=yes
anon_umask=022

然后你可以新建一个文件夹, 修改它的权限为完全开放, 任何用户就可以登录这个文件夹, 并上传下载文件:
mkdir /var/ftp/guest
chmod 777 /var/ftp/guest

6. 定制进入 FTP 服务器的欢迎信息
在 vsftpd.conf 文件中设置:
dirmessage_enable=yes
然后进入用户目录建立一个.message 文件, 输入欢迎信息即可(我这里写入的是 Welcome to gxlinux’s FTP!):
[root@localhost test_ftp]# ftp 192.168.0.33
Connected to 192.168.0.33 (192.168.0.33).
220 (vsFTPd 2.0.5)
Name (192.168.0.33:gxl): test
331 Please specify the password.
Password:
230-Welcome to gxlinux’s FTP!
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

7. 实现虚拟路径
将某个目录挂载到 FTP 服务器下供用户使用, 这就叫做虚拟路径.
比如将 gxl 用户的目录挂载到 FTP 服务器中, 供 FTP 服务器的用户使用, 使用如下命令即可:
[root@localhost opt]# mount –bind /home/gxl /var/ftp/pub #使用挂载命令
[root@localhost opt]# ls /var/ftp/pub
LumaQQ  Screenshot.png 桌面

8. 打开 vsFTPd 的日志功能
添加下面一行到 vsftpd.conf 文件中, 一般情况下该文件中有这一行, 只要把前面的注释符号 #去掉即可, 没有的话就添加, 或者修改:
xferlog_file=/var/log/vsftpd.log

9. 限制链接数, 以及每个 IP 最大的链接数
修改配置文件中, 例如 vsftp 最大支持链接数 100 个, 每个 IP 能支持 5 个链接:
max_client=100
max_per=5

10. 限制传输速度
修改配置文件中, 例如让匿名用户和 vsftd 上的用户 (即虚拟用户) 都以 80KB=1024*80=81920 的速度下载
anon_max_rate=81920
local_max_rate=81920

11. 将用户 (一般指虚拟用户) 限制在自家目录
修改配置文件中, 这样用户就只能访问自己家的目录了:
chroot_local_user=yes
如果只想某些用户仅能访问自己的目录, 其它用户不做这个限制, 那么就需要在 chroot_list 文件 (此文件一般是在 /etc/vsftpd/ 中) 中添加此用户.
编辑此文件, 比如将 test 用户添加到此文件中, 那么将其写入即可. 一般的话, 一个用户占一行.
[root@localhost vsftpd]# cat chroot_list
test

12. 绑定某个 IP 到 vsFTPd
有时候要限制某些 IP 访问服务器, 只允许某些 IP 访问, 例如只允许 192.168.0.33 访问这个 FTP, 同样修改配置文件:
listen_address=192.168.0.33

 

 

=======================================================================

自己配置的 vsftpd.conf(主动模式和被动模式都支持(已验证),还可以输出日志文件)

 

[root@localhost vsftpd]# pwd
/etc/vsftpd
[root@localhost vsftpd]# cat vsftpd.conf
# Example config file /etc/vsftpd/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
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
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
#
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES
# WARNING – changing this filename affects /etc/logrotate.d/vsftpd.log
# 指定日志目录(还没研究这个日志和后面的日志有何区别,呵呵):
xferlog_file=/ftptest/xferlog
#
# Switches between logging into vsftpd_log_file and xferlog_file files.
# NO writes to vsftpd_log_file, YES to xferlog_file
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_local_user=YES
#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
#
# When “listen” directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
#
# other…

anon_other_write_enable=YES
#anon_root=/var/ftp/pub
dual_log_enable=YES
# 指定日志目录:
vsftpd_log_file=/ftptest/vsftpd.log   
# 开启主动模式:
port_enable=YES
# 开启被动模式(默认开启被动模式,想关闭被动模式需要设置 NO):
pasv_enable=YES
# 指定被动模式端口范围:
pasv_min_port=50000
pasv_max_port=60000

[root@localhost vsftpd]#

玩转 vsftpd 服务器的四大高级配置:http://www.linuxidc.com/Linux/2013-09/90565.htm

vsFTPd 配置教程:http://www.linuxidc.com/Linux/2013-09/90562.htm

Ubuntu 实用简单的 FTP 架设 http://www.linuxidc.com/Linux/2012-02/55346.htm

Ubuntu 上架设 FTP 服务器和 Apache 服务器 http://www.linuxidc.com/Linux/2011-04/35295.htm

Ubuntu 13.04 安装 LAMP\vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

RHEL6 平台下 SeLinux 和 vsftpd 的匿名上传的简单案例 http://www.linuxidc.com/Linux/2013-04/82300.htm

Linux 系统 vsftpd 源码安装 http://www.linuxidc.com/Linux/2013-03/81475.htm

openSUSE 13.2/13.1 下安装配置 FTP 服务器 vsftpd  http://www.linuxidc.com/Linux/2014-12/110070.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133315.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计10583字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中