FTP部署

1. 简介

文件传输协议(File Transfer Protocol,FTP)是用于在网络上进行文件传输的一套标准协议,它工作在 OSI 模型的第七层, TCP 模型的第四层, 即应用层, 使用 TCP 传输, 客户在和服务器建立连接前要经过一个“三次握手”的过程, 保证客户与服务器之间的连接是可靠的, 而且是面向连接, 为数据传输提供可靠保证。
FTP允许用户以文件操作的方式(如文件的增、删、改、查、传送等)与另一主机相互通信。然而, 用户并不真正登录到自己想要存取的计算机上面而成为完全用户, 可用FTP程序访问远程资源, 实现用户往返传输文件、目录管理以及访问电子邮件等等, 即使双方计算机可能配有不同的操作系统和文件存储方式。

1.1 FTP的应用

FTP的应用.jpg

1.2 FTP传输文件的过程

默认情况下FTP协议使用TCP端口中的 20 和 21 这两个端口,其中20用于传输数据,21用于传输控制信息。
ftp传输文件的过程.jpg

1.3 FTP传输模式FTP传输模式.jpg

2. 安装

安装环境:ubuntu 20.04
apt update
apt install -y vsftpd
vsftpd -v查看版本

3. 修改配置文件

vim /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=NO   
是否允许监听,如果设置为YES,则vsftpd将以独立模式运行,由vsftpd自己监听和处理IPv4端口的连接;
#
# 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.
listen_ipv6=YES   
设定是否支持IPV6。如要同时监听IPv4和IPv6端口,则必须运行两套vsftpd,采用两套配置文件,同时确保其中有一个监听选项是被注释掉的;
#
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO
是否允许匿名登录FTP服务器,默认设置为YES允许,如不允许匿名访问则设置为NO;
#
# Uncomment this to allow local users to log in.
local_enable=YES
是否允许本地用户登录访问FTP服务器,默认设置为YES允许;
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
是否允许用户修改文件权限,默认设置为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   
FTP上本地的文件权限
#
# 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
是否激活目录欢迎信息功能,用户第一次进入目录的提示信息;
#
# If enabled, vsftpd will display directory listings with the time
# in  your  local  time  zone.  The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES
使用本地时间
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
一个存有详细的上传下载信息的日志,默认情况该日志文件为/var/log/vsftpd.log,可以通过下面的xferlog_file选项对其进行设定
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
是否设定FTP服务器将启用FTP数据端口的连接请求,20端口用于传输数据;
#
# 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.
# 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.
# 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=YES
#
# 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=YES
指定用户列表文件中的用户是否允许切换到上级目录
chroot_list_enable=YES
用户登录FTP服务器后是否具有访问自己目录以外的其他文件的权限;
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
被列入此文件的用户,在登录后将不能切换到自己目录以外的其他目录,有利于FTP服务器的安全管理和隐私保护。此文件需自己建立;
#
# 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
#
# Customization
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# 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/empty
#
# 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
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
#utf8_filesystem=YES
local_root=/home/ftp/ftpfile
上传下载的目录

4. 创建用户及用户列表

useradd -m zhangsan
passwd zhangsan给用户张三设置密码
vim /etc/vsftpd.chroot_list创建配置用户列表

root
ftpuser
test
zhangsan

/etc/init.d/vsftpd restart重启服务,重新加载 vsftpd.conf 配置文件

5. 创建目录和测试文件

mkdir -p /home/123
touch test.txt在123目录下创建测试文件
echo "HelloWorld" > test.txt写入内容

6. 登录测试

使用本机或另一台能互通的机器进行登录访问测试,如果是另一台机器需要安装ftp客户端(apt install ftp)
ftp 192.168.152.139
输入用户名和密码即可登录成功。
1663658351801.jpg

知识拓展
FTP服务常见状态码:

1XX:信息125:数据连接打开
2YY:成功类状态200:命令OK/230:登录成功
3XX:补充类331:用户名OK
4XX:客户端错误425:不能打开数据连接
5XX:服务器错误500:不能登录

7. FTP基础使用、常用命令

7.1 连接

ftp ip连接ftp服务器

7.2 修改文件传输类型

bin设置文件传输类型为二进制传输类型
一般默认为ascii传输类型,但是使用ascii模式传输类似于可执行文件时,会造成传输的文件内容不对。因此建议在上传或者下载文件之前,执行 bin 命令将文件传输类型设置为二进制传输类型。

7.3 下载

get下载FTP服务器中的文件。下载时注意切换到文件的所在目录。
get filename [newname]下载单个文件
filename为下载的FTP服务器上的文件名,newname为保存在本都计算机上时使用的名字,如果不指定newname,文件将以原名保存。
mget aa cc批量下载

get /usr/your/1.htm 1.htm   #将ftp服务器中/usr/your目录中的1.html下载到本地

#下载/usr/your/下的所有文件
cd /usr/your/
prompt off   #去除下载确定提示
mget *.*

7.4 上传

put aa.txt单个上传
mput aa cc批量上传
注意:上传文件都来自于本地的当前目录下

put 1.html /usr/your/2.html   #将本地的1.html上传到ftp服务器/usr/your/目录下,并改名为2.html

#将本地当前目录下所有html文件上传到服务器/usr/your/下
cd /usr/your
mput *.html

7.5 退出

bye/exit结束FTP客户端与服务器的连接

8. FAQ

8.1 下载报错550

550 无法打开文件
**分析:**这种报错通常是因为没有切换到指定的目录,ftp在修改配置文件时会设置用户允许访问的目录权限,所以会出现无法打开文件的问题。
下载报错.png
**解决:**首先 pwd 查看下当前所在的位置,之后 cd 到想要下载的文件的目录下再进行下载,即可成功。
切换到指定目录.jpg

8.2 上传报错553

553 无法创建文件
分析:在查询资料后发现这种报错有多种可能,有可能是 selinux 对 vsftpd 有拦截,也有可能是磁盘空间不足造成的,在排查之后发现这两种问题都不存在,那就可能是用户文件权限的问题了。
上传报错.jpg
**解决:**在部署 ftp 服务端的机器,找到需要上传文件的目录,添加权限。
chmod 777 123
再次上传,即可成功。
解决:权限问题.jpg

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值