基于Centos8系统在阿里云服务器搭建FTP服务的方法与常见问题解决

一、FTP介绍

  1. 网络文件共享服务主流的主要有三种,分别是ftp、nfs、samba。

FTP是File Transfer Protocol(文件传输协议)的简称,用于internet上的控制文件的双向传输。

FTP也是一个应用程序,基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。

在FTP的使用当中,用户经常遇到两种概念:下载和上传

下载(Download) 上传(Upload)
从远程主机拷贝文件至自己的计算机上 将文件从自己的计算机上拷贝至远程主机上
  1. ftp架构
    FTP工作于应用层,监听于tcp的21号端口,是一种C/S架构的应用程序。其有多种客户端和服务端的应用程序,下面来简单介绍一下
1  客户端工具					      服务端软件
2   ftp	 						
3  lftp,lftpget				      wu-ftpd
4  wget,curl					  proftpd(提供web接口的一种ftp服务端程序)
5  filezilla ,Xftp					  pureftp
6  gftp(Linux GUI)			      vsftpd(Very Secure)
7  商业软件(flashfxp,cuteftp)	  ServU(windows平台的一种强大ftp服务端程序)
  1. ftp数据连接模式

ftp有2种数据连接模式:命令连接和数据连接

命令连接:是指文件管理类命令,始终在线的持久性连接,直到用户退出登录为止
数据连接:是指数据传输,按需创建及关闭的连接

其中数据连接需要关注的有2点:

数据传输格式
数据传输模式

数据传输格式有以下两种:

文件传输
二进制传输

数据传输模式也有2种:

主动模式:由服务器端创建数据连接
被动模式:由客户端创建数据连接

两种数据传输模式的建立过程

传输模式 建立过程
主动模式 命令连接: Client(1025)–> Server(21) 客户端以一个随机端口(大于1023)来连服务器端的21号端口 数据连接: Server(20/tcp) --> Client(1025+1) 服务器端以自己的20号端口去连客户端创建命令连接时使用的随机端口+1的端口号
被动模式 命令连接: Client(1110) --> Server(21) 客户端以一个随机端口来连成服务器端的21号端口 数据连接: Client(1110+1) --> Server(随机端口) 客户端以创建命令连接的端口+1的端口号去连服务器端通过命令连接告知自己的一个随机端口号来创建数据连接

主动模式有个弊端,因为客户端的端口是随机的,客户端如果开了防火墙, 则服务器端去连客户端创建数据连接时可能会被拒绝
被动模式有个弊端,因为服务端的端口是随机的,服务端如果开了防火墙, 则客户器端去连服务端创建数据连接时可能会被拒绝

所以,最好认清关系,然后开放相应端口,由于部分端口随机,最好就是客户端关闭防火墙,采用主动连接方式

  1. 用户认证
    ftp的用户主要有三种:

虚拟用户:仅用于访问某特定服务中的资源
系统用户:可以登录系统的真实用户
匿名用户

  1. vsftpd
    此处我们要说的ftp应用程序是vsftpd,这也是在公司中用得最多的一款ftp软件。

二、Centos8安装FTP与设置

总结为三点:

1)安装ftp工具(vsftpd)

2)利用其配置文件自定义配置好ftp工具并启动

3)设置登录用户并测试

  1. 首先确认机器是否开启了FTP服务,可通过命令进行确认

注意:FTP服务会使用两个端口,20和21, 前者是数据传输端口,后者是控制端口,FTP客户端通过端口21和服务端进行连接

查看ftp服务是否开启,图是安装过得。

ps -ef|grep ftp

netstat -anp|grep 21

在这里插入图片描述

2.vsftpd 是Linux系统中比较好用的一款FTP服务端软件

  1. 运行图示命令确认本机是否已经安装了 vsftpd
rpm -qa|grep vsftpd

vsftpd-3.0.3-28.el8.x86_64
  1. 如果没有安装,运行图示命令进行安装,若是红帽系统yum命令可能要进行换源才可用,方法可百度,这里不再赘述。
yum install vsftpd -y

vsftpd配置
对ftp服务的配置都在这些文件里,其中用到最多的就是 /etc/vsftpd/vsftpd.conf (主配置文件)
使用vi/vim编辑器都可以对其配置(linux下一切皆文件)

1 /etc/pam.d/vsftpd       vsftpd用户认证配置文件
2 /etc/vsftpd/            配置文件目录
3 /etc/vsftpd/vsftpd.conf     主配置文件
4
5 匿名用户(映射为ftp用户)的共享资源位置是/var/ftp
6 系统用户通过ftp访问的资源位置为用户的家目录
7 虚拟用户通过ftp访问的资源位置为给虚拟用户指定的映射成为的系统用户的家目录

vsftpd常见的配置参数:

参数 作用
anonymous_enable=YES 启用匿名用户登录
anon_upload_enable=YES 允许匿名用户上传
anon_mkdir_write_enable=YES 允许匿名用户创建目录,但是不能删除
anon_other_write_enable=YES 允许匿名用户创建和删除目录
local_enable=YES 启用本地用户登录
write_enable=YES 允许本地用户有写权限
local_umask=022 通过ftp上传文件的默认遮罩码
chroot_local_user=YES 禁锢所有的ftp本地用户于其家目录中
chroot_list_enable=YES 开启禁锢文件列表,需要与chroot_list_file参数一起使用
chroot_list_file=/etc/vsftpd/chroot_list 指定禁锢列表文件路径,在此文件里面的用户将被禁锢在其家目录中
allow_writeable_chroot=YES 允许被禁锢的用户家目录有写权限
xferlog_enable=YES 是否启用传输日志,记录ftp传输过程
xferlog_std_format=YES 传输日志是否使用标准格式
xferlog_file=/var/log/xferlog 指定传输日志存储的位置
chown_uploads=YES 是否启用改变上传文件属主的功能
chown_username=whoever 指定要将上传的文件的属主改为哪个用户,此用户必须在系统中存在
pam_service_name=vsftpd 指定vsftpd使用/etc/pam.d下的,哪个pam配置文件进行用户认证
userlist_enable=YES 是否启用控制用户登录的列表文件:默认为/etc/vsftpd/user_list文件
userlist_deny=YES 是否拒绝userlist指定的列表文件中存在的用户登录ftp
max_clients=# 最大并发连接数
max_per_ip=# 每个IP可同时发起的并发请求数
anon_max_rate 匿名用户的最大传输速率,单位是“字节/秒”
local_max_rate 本地用户的最大传输速率,单位是“字节/秒”
dirmessage_enable=YES 启用某目录下的.message描述信息,假定有一个目录为/upload,在其下创建一个文件名为.message,在文件内写入一些描述信息,则当用户切换至/upload目录下时会自动显示.message文件中的内容
message_file 设置访问一个目录时获得的目录信息文件的文件名,默认是.message
idle_session_timeout=600 设置默认的断开不活跃session的时间
data_connection_timeout=120 设置数据传输超时时间
ftpd_banner=“Welcome to chenlf FTP service.” 定制欢迎信息,登录ftp时自动显示

接下来说下我的配置,我是将匿名用户关闭,不允许匿名登录,单独设置了一个用户(ftpuser)用于登录ftp服务器的
配置完vsftpd后需要进行启动/重启并查看状态,使用这三个个命令

service vsftpd start     启动服务
service vsftpd status    查看状态
service vsftpd restart   重启服务

图中表示已经开启服务
在这里插入图片描述
然后就是创建一个登录账户(useradd)
创建一个用户 ftpuser:

useradd ftpuser

为用户 ftpuser 设置密码:

echo 123456 | passwd ftpuser --stdin

限制该用户仅能通过 FTP 访问
限制用户 ftpuser 只能通过 FTP 访问服务器,而不能直接登录服务器:

usermod -s /sbin/nologin ftpuser

为用户分配主目录
为用户 ftpuser 创建主目录并约定:

/data/ftp 为主目录, 该目录不可上传文件

/data/ftp/pub 文件只能上传到该目录下

mkdir -p /data/ftp/pub

设置访问权限: 不设置的话后期没法进行上传下载

chmod 755 /data/ftp && chmod 777 -R /data/ftp/pub

设置为用户的主目录:用户的主目录是用户通过 FTP 登录后看到的根目录

usermod -d /data/ftp ftpuser

最后就是 访问 FTP 服务
访问 ftp://<我们的CVM IP 地址> 。

用户:ftpuser
密码:123456

四、常见问题汇总

在解决问题前,需要确定centos关闭防火墙和selinux;其次阿里云服务器还是有防火墙,需要登录阿里云服务器进行安全组管理,增加一个20/21端口的允许。
在这里插入图片描述

  1. 登不进ftp,显示错误
    答:重点检查vsftpd是否启动,配置文件是否有语法错误
sytemctl status vsftp.service    查看状态
sytemctl start vsftp.service     启动服务
sytemctl restart vsftp.service   重启服务
  1. 可以ftp上ip但是输户名密码登不进去
    VSFTPD一直提示“用户身份验证失败”:服务器被重启之后,无论是xftp还是cmd登录就一直提示“用户身份验证失败”,无论是匿名还是使用root账户或者ftpuser账户均如此,搜了下网上不少帖子,比如https://yq.aliyun.com/wenzhang/show_11618(但是它仅仅指的是java客户端访问),https://bbs.aliyun.com/read/252932.html?pos=6(它讲的是通过ssh登录,并非ftp),均未看到解决。使用命令行下ftp客户端登录,如下:

用户(192.168.230.128:(none)): ftpuser
331 Please specify the password.
密码:
530 OOPS:
远程主机关闭连接。

完整配置如下:

[root@aliyun ~]# cat /etc/vsftpd/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.
# When SELinux is enforcing check for SE bool ftp_home_dir
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
# anon_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.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
 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=ftpuser
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
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. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# 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().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=NO
# 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=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then 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
pasv_enable=YES
pasv_min_port=30000
pasv_max_port=31000

当我注释掉chroot_local_user的时候,也就是不限制用户切换到主目录之外就不出错了。找到具体的错误信息解决问题就很简单了,经查:

这个问题发生在最新的这是由于下面的更新造成的:

  • Add stronger checks for the configuration error of running with a writeable root directory inside a chroot(). This may bite people who carelessly turned on chroot_local_user but such is life.

从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。

要修复这个错误,可以用命令chmod a-w /data/ftp 去除用户主目录的写权限,注意把目录替换成你自己的。或者你可以在vsftpd的配置文件中增加下列两项中的一项:

allow_writeable_chroot=YES

在配置文件中加上上面的配置项之后,无论是xftp还是原生ftp客户端,均可以正常访问了。

还有个需要修改:把/etc/pam.d/vsftpd 中的 auth注释掉
在这里插入图片描述
在这里插入图片描述

  1. xftp上载错误 553 could not creat

1、检查文件夹是否设置了读写权限
2、关掉防火墙 (systemctl stop firewalld)
3、selinux限制了,把selinux关掉就可以了。(setenforce 0)

setsebool -a|grep ftp

vim /etc/vsftpd/vsftpd.conf

anon_upload_enable=YES

anon_mkdir_write_enable=YES

修改这个配置文件就好了,我也不知道原因

  1. Xftp可登ftp不显示文件

由于ftp连接模式port模式和pasv模式。设置ftp 链接模式为port (主动模式) ,ftp客户端一般默认使用的pasv (被动模式) 。如果不修改模式,用户链接服务器后,目录一直不能显示。所以要修改为主动模式。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41191813/article/details/109121925