在Ubuntu系统上搭建Ftp服务器,并传输文件

2021-12-30 11:15:19 浏览数 (1)

1. 在Ubuntu上搭建Ftp服务器

1.1 安装vsftpd服务器
代码语言:javascript复制
sudo apt-get install vsftpd
1.2 配置vsftpd.conf文件

配置文件:

代码语言:javascript复制
sudo vi /etc/vsftpd.conf
代码语言:javascript复制
# 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
#
# 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
#
# Allow anonymous FTP? (Disabled by default).
#这个是设置是否允许匿名登录ftp服务器,不允许。
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.
#允许上传文件到ftp服务器
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
#
# 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
#
# 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.
# 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
# (default follows) 允许chroot_list文件中配置的用户登录此ftp服务器。
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
#
# 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
#配置ftp服务器的上传下载文件所在的目录。
local_root=/home/ftpfile
#this directory is for ftp server to save  download or upload data
1.3 配置用户

新建 /etc/vsftpd.chroot_list 文件,在里面添加登录ftp服务器的linux用户

代码语言:javascript复制
sudo vi /etc/vsftpd.chroot_list

vsftpd.chroot_list文件内容,michael是我linux的用户名:

代码语言:javascript复制
michael
1.4 配置ftp上传/下载文件的存放目录

这个目录要和vsftpd.conf中配置路径一致,比如上面配置的是local_root=/home/ftpfile,就新建/home/ftpfile目录:

代码语言:javascript复制
sudo mkdir /home/ftpfile
1.6 给存放目录配置完全访问权限
代码语言:javascript复制
#对目录授予权限
sudo chmod -R 777 /home/ftpfile
1.7 使用刚刚的配置重启ftp服务器

只能用下面的命令启动服务器,否则配置不生效

代码语言:javascript复制
sudo /etc/init.d/vsftpd restart
1.8 测试登录

查看本机IP

登录ftp服务器

代码语言:javascript复制
ftp 192.168.126.132

2. 用Telnet执行ftp交互

2.1 FTP简介

文件传输协议(File Transfer Protocol,FTP)是用于在网络上进行文件传输的一套标准协议,它工作在 OSI 模型的第七层, TCP 模型的第四层, 即应用层, 使用 TCP 传输而不是 UDP, 客户在和服务器建立连接前要经过一个“三次握手”的过程, 保证客户与服务器之间的连接是可靠的, 而且是面向连接, 为数据传输提供可靠保证。

FTP允许用户以文件操作的方式(如文件的增、删、改、查、传送等)与另一主机相互通信。然而, 用户并不真正登录到自己想要存取的计算机上面而成为完全用户, 可用FTP程序访问远程资源, 实现用户往返传输文件、目录管理以及访问电子邮件等等, 即使双方计算机可能配有不同的操作系统和文件存储方式。

2.2 FTP交互过程

1.当用户主机与远程主机开启一个FTP会话前,FTP的客户机(用户)首先在21号端口上发起一个用于控制的与服务器(远程主机)的TCP连接。

2.FTP的客户机通过该控制连接发送用户的标识和口令,也可以发送改变远程目录等命令。

3.当FTP的服务器端从该连接上收到一个文件传输的命令后(无论是到远程主机还是从远程主机到客户机),就发起一个到客户机的数据连接。FTP在该数据连接上准确地传送一个文件并关闭该连接,但此时控制链路还保持连接。如果在同一个会话期间,用户还需要传输另一个文件,FTP则打开另一个数据连接。

因而对FTP传输而言,控制连接贯穿了整个用户会话期间,但是针对会话中的每一次文件传输都需要建立一个新的数据连接(即数据连接是非持久性的)。

2.3 telnet执行ftp交互常用指令

③FTP常用命令及代码:

ABOR

中断数据连接程序

ACCT <account>

系统特权帐号

ALLO <bytes>

为服务器上的文件存储器分配字节

appe <filename>

添加文件到服务器同名文件

cdup <dir path>

改变服务器上的父目录

cwd <dir path>

改变服务器上的工作目录

DELE <filename>

删除服务器上的指定文件

HELP <command>

返回指定命令信息

LIST <name>

如果是文件名列出文件信息,如果是目录则列出文件列表

MODE <mode>

传输模式(S=流模式,B=块模式,C=压缩模式)

MKD <directory>

在服务器上建立指定目录

NLST <directory>

列出指定目录内容

NOOP

无动作,除了来自服务器上的承认

PASS <password>

系统登录密码

PASV

请求服务器等待数据连接

PORT <address>

IP 地址和两字节的端口 ID

PWD

显示当前工作目录

QUIT

从 FTP 服务器上退出登录

REIN

重新初始化登录状态连接

REST <offset>

由特定偏移量重启文件传递

RETR <filename>

从服务器上找回(复制)文件

RMD <directory>

在服务器上删除指定目录

RNFR <old path>

对旧路径重命名

RNTO <new path>

对新路径重命名

SITE <params>

由服务器提供的站点特殊参数

SMNT <pathname>

挂载指定文件结构

STAT <directory>

在当前程序或目录上返回信息

STOR <filename>

储存(复制)文件到服务器上

STOU <filename>

储存文件到服务器名称上

STRU <type>

数据结构(F=文件,R=记录,P=页面)

SYST

返回服务器使用的操作系统

TYPE <data type>

数据类型(A=ASCII,E=EBCDIC,I=binary)

USER <username>

系统登录的用户名

2.4 执行telnet-ftp实验

ubuntu系统的IP地址为192.168.126.132

首先打开命令行窗口,输入 telnet 192.168.126.132 21来建立与ftp服务器的连接:

然后使用user,pass命令登录FTP

测试一些无需传输文件的指令

使用被动(PASV)方式建立与服务器的数据连接,输入psav回车,服务器返回服务器的IP和服务器开启的端口号

服务器IP为192.168.126.132,端口号为58*256 189=15037‬

然后再打开一个命令行窗口,输入telnet 192.168.126.132 15037‬,会进入一片全黑界面,此时数据传输已经开启

在输入命令的命令行窗口输入list命令,将在数据传输的命令行窗口显示出当前目录下的文件

当一个数据连接传输完成一个文件后,这个连接就会立即关闭

尝试将文件内的信息传输到主机上,在进入传输模式后使用retr 文件名的方式

在另一边收到了信息

2.5 使用ftp命令进行文件传输

使用ftp 192.168.126.132连接ftp服务器

使用put命令传输文件上服务器,首先关闭windows防火墙,否则port方式传输不成功

2.6 使用ftp工具进行分片下载

分片传输概述:

分片传输,就是将所要传输的文件,按照一定的大小,将整个文件分隔成多个数据块(我们称之为Part)来进行分别传输,传输完之后再由服务端对所有传输的文件进行汇总整合成原始的文件。

使用split命令切割文件

使用mget命令下载多个文件

本地接收到的文件

在Windows下使用copy命令合并linux下split分割的文件:copy /b xaa xab xac xad xae Redis.zip

文件成功解压

3. 参考

[1] 在ubuntu上部署安装ftp服务器

[2] 在Windows中合并Linuxsplit命令分割的文件拔剑-浆糊的传说新浪博客

[3] windows系统下ftp上传下载和一些常用命令

ftp

0 人点赞