欢迎光临!
若无相欠,怎会相见

以CentOS 7 搭建FTP服务器

序言

因为工作需要,需要用到FTP服务器,因此就用 CentOS 7 安装 vsftpd 软件作为 FTP 服务器。但是在搭建过程中还是遇见各种坑,经过一天的各种尝试,我把我自己搭建好的配置文件发出来,以供参考。

搭建过程

安装 vsftpd

vsftpd 作为 FTP 的服务端。在CentOS 7中只需执行命令安装即可 yum install vsftpd .

安装客户端 ftp

同上,执行命令安装 yum install ftp ,提供ftp命令。

修改配置

vsftpd 的配置文件是在 /etc/vsftpd 目录下的 vsftpd.conf 文件。

刚开始,因为各种权限、参数问题,快把我搞炸了,有时候能登陆,但不能上传下载;能上传的时候,又不能下载;能上传能下载的时候,但是他第一个能上传,第二个就不能了,间隔一个。。。。

我也按照一些书上的步骤进行操作,结果还是不行。由于我也是初学者,我就把折腾好的配置拿出来,也不要问我为什么,我特么也想知道,我用这个配置能后成功搭建起正常 FTP 服务器。

# 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=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=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=/var/log/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_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 whith two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES

pam_service_name=vsftpd.vu
userlist_enable=YES
tcp_wrappers=YES
local_root=/var/ftp
allow_writeable_chroot=YES

创建vsftpd.vu文件

在 /etc/pam.d 目录下执行 vi vsftpd.vu ,创建 vsftpd.vu 文件。

auth       required       pam_userdb.so     db=/etc/vsftpd/vuser
account    required       pam_userdb.so     db=/etc/vsftpd/vuser

创建vuser.db

进入 vsftpd 目录下,创建 vuser.list 文件,添加文本如下,奇数行为账户名,偶数行为密码。

test
test

然后执行如下命令:db_load -T -t hash -f vuser.list vuser.db ,使用 db_load 命令用 hash 算法将用户名密码的明文信息文件装换成数据库文件,降低数据库文件权限,然后把明文信息文件删除。

[root@localhost vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@localhost vsftpd]# file vuser.db 
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@localhost vsftpd]# chmod 600 vuser.db
[root@localhost vsftpd]# rm -f vuser.list

创建 chroot_list 文件

在 /etc/vsftpd 目录下创建 chroot_list 文件,位于 chroot_list 文件中的用户将不能切换出ftp的主目录。例如:ftp主目录是 /var/ftp/ ,如果test用户在chroot_list中,他就不能切换到 /var/ 目录,但是如果不在,是可以切换到的,亲自试验过。

root
test

创建本地用户

创建用户test,修改密码为test。

[root@localhost vsftpd]# adduser -d /var/ftp/ -g ftp -s /sbin/nologin test
adduser:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
正在创建信箱文件: 文件已存在
[root@localhost vsftpd]# passwd test
更改用户 test 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost vsftpd]# 

SELinux放行FTP

设置SELinux域允许策略。

[root@localhost vsftpd]# getsebool -a | grep ftp
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> on
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off

我这是已经设置好的,ftpd_full_access --> on 。如果为off,请执行 setsebool -P ftpd_full_access=on 来修改。

防火墙放行FTP

[root@localhost ~]# firewall-cmd --zone=trusted --add-service=ftp
success
[root@localhost ~]# firewall-cmd --permanent --zone=trusted --add-service=ftp
success
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# firewall-cmd --zone=trusted --query-service=ftp  # 查询ftp是不是在trusted域放行
yes
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=ftp # 添加FTP public域放行
success
[root@localhost ~]# firewall-c
firewall-cmd firewall-config  
[root@localhost ~]# firewall-cmd --reload 
success

测试

经过本人的测试,可以上传下载,已经符合我的要求。

结语

经过摸索,以上述步骤搭建,是可以成功的,但是我也只是摸索出来的,不确定是否有问题,请见谅。

如有错误,敬请指出,感谢指正!      — 2018-12-06 11:46:12  于苏州

版权声明:  本站原创文章,于 2018-12-06,由 Darker 发表。
转载请注明:原文地址:《以CentOS 7 搭建FTP服务器 | 飘零博客》
本站所有文章如果没有特殊声明均为原创。本文作者 : Darker
复制或转载请以超链接形式注明:飘零博客    
原文地址:《以CentOS 7 搭建FTP服务器 | 飘零博客》
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
//

欢迎光临