
Linux上搭建FTP服务器
FTP简介
一般来讲,人们将计算机联网的首要目的就是获取资料,而文件传输是一种非常重要的获取资料的方式。
FTP是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用20、21号端口,其中端口20用于进行数据传输,端口21用于接受客户端发出的相关FTP命令与参数。
FTP服务器普遍部署于内网中,具有容易搭建、方便管理的特点,有些FTP客户端工具还可以支持文件的多点下载以及断点续传技术。
FTP服务器是按照FTP协议在互联网上提供文件存储和访问服务的主机,FTP客户端则是向服务器发送连接请求,以建立数据传输链路的主机。
vsftpd 简介
vsftpd (Very Secure FTP Daemon )
由于FTP、HTTP、Telnet等协议的数据都是使用明文进行传输的,因此从设计上就是不可靠的。
为了满足以密文方式传输文件的需求,发明了vsftpd服务程序。
vsftpd(very secure ftp daemon,非常安全的FTP守护进程)是一款运行在Linux操作系统上的FTP服务程序,不仅完全开源而且免费。此外,它还具有很高的安全性、传输速度,以及支持虚拟用户验证等其他FTP服务程序不具备的特点。
vsftpd作为更加安全的文件传输协议服务程序,允许用户以3种认证模式登录FTP服务器。
匿名开放模式
:是最不安全的一种认证模式,任何人都可以无须密码验证而直接登录到FTP服务器。本地用户模式
:是通过Linux系统本地的账户密码信息进行认证的模式,相较于匿名开放模式更安全,而且配置起来也很简单。虚拟用户模式
:更安全的一种认证模式,它需要为FTP服务单独建立用户数据库文件,虚拟出用来进行密码验证的账户信息,而这些账户信息在服务器系统中实际上是不存在的,仅供FTP服务程序进行认证使用。
开始搭建FTP服务器
一、准备工作
环境
确认Linux系统运行稳定,检查网络连接顺畅
系统权限的设置--以root用户身份登录
检查21端口
FTP默认使用21端口,需要在安装前确保该端口没有被占用
二、安装vsftpd
vsftpd 安装
sudo apt install vsftpd
vsftpd服务将在安装过程完成后自动启动。通过打印服务状态进行验证:
sudo systemctl status vsftpd
输出如下所示,表明vsftpd服务处于活动状态并正在运行:
* vsftpd.service - vsftpd FTP server
Loaded: loaded (/lib/systemd/system/vsftpd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-10-15 03:38:52 PDT; 10min ago
Main PID: 2616 (vsftpd)
Tasks: 1 (limit: 2319)
CGroup: /system.slice/vsftpd.service
`-2616 /usr/sbin/vsftpd /etc/vsftpd.conf
三、FTP目录和用户创建
创建FTP服务的目录和Ubuntu系统用户
1.创建用户主目录(ZZZ):
sudo mkdir /home/ZZZ
2.添加用户:
sudo useradd ZZZ
3.设置用户密码
sudo passwd Hbuhubj789.
4.设置目录权限:
sudo chmod -R 777 /home/ZZZ
Linux chmod命令
Linux chmod(英文全拼:change mode)命令是控制用户对文件的权限的命令
Linux的文件调用权限分为三级
u – 所有人的权限
g – 所有组的权限
o – 其他人的权限
在Linux中的文件权限有3种类型:
r – 读
w – 写
x – 执行
只有root用户或具有sudo特权的普通用户才能更改文件或目录权限
使用chmod命令设置文件和目录权限
1)使用数字符号分配权限
sudo chmod [选项]数字值文件名
sudo chmod 744 linuxmi.txt #所有者具有读取,写入和执行权限,该组具有读取和执行权限,而其他用户仅具有读取权限
数值可以为3或4个数字。但是,在大多数情况下,使用3个数字。读取,写入和执行权限采用以下值:
读取权限=> 4
写权限=> 2
执行权限=> 1
权限值的总和,即在三个段中的每一个中的读取,写入和执行,都占给定文件或目录的完整权限。
我们得到的值为754,作为给定文件的文件权限的数值。
2)递归分配目录权限
为目录分配权限时,使用-R标志以递归方式为其目录和子文件夹分配权限
$ chmod 数字值 -R 目录名
如上述设置的目录权限:
sudo chmod -R 777 /home/ZZZ
及为/home/ZZZ目录和子文件夹赋权其u:rwx = 7;g:rwx = 7;o:rwx = 7
3)使用文本表示法指定权限
在此方法中,chmod命令采用标志或符号,这些标志或符号代表语法中的所有者,组,其他或所有用户(u,g和o)。
语法:
$ chmod [选项] [ugoa] [– + =] [r,w,x]文件
第一组参数[ugoa]指定权限将更改的用户类别
u:用户
g:组
o:其他
a:全部(包括以上所有内容)
如果省略了这个集合,那么默认选项是a选项。
第二组选项[– + =]
– :该标志从指定的用户中删除文件许可权。
+ :这会将权限添加/添加到指定用户。
= :这将为指定的用户分配不同的权限,并删除该用户段的先前权限。
例:
sudo chmod ug+x linuxmi.txt
上面的命令将执行权限添加到文件的所有者和组
四、配置vsftpd
vsftpd的主要配置文件位于/etc/vsftpd.conf
,打开这个文件,我们就可以对一些关键参数进行修改
备份配置文件
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak
打开vsftpd配置文件:
sudo vim /etc/vsftpd.conf
进入编辑模式:按下i键或者a键
保存和退出:按下Esc键退出编辑模式,然后输入:wq命令保存更改
修改后的配置文件:
# 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 #设置vsftpd服务器是否以standalone模式运行
#
# 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).
anonymous_enable=YES #匿名访问允许
anon_root=/home/ftp #匿名用户的FTP根目录
#
# 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 #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 #vsftpd使用本机时间作为vsftpd时间;默认为显示格林威治时间(GMT);
#
# Activate logging of uploads/downloads.
# Activate logging of uploads/downloads.
xferlog_enable=YES #ftp 日志记录允许,记录于 、var/log/xferlog
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES #启动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 #设置是否启用chroot_list_file配置项指定的用户列表文件
# (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
#
# 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 #支持PAM模块的管理
#
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
#rsa_cert_file=/etc/ssl/private/vsftpd.pem
#rsa_private_key_file=/etc/ssl/private/vsftpd.pem
#ssl_enable=YES
userlist_enable=YES
userlist_deny=NO #user_list 只允许列表以外的用户登录,并且必需是以命令行的方式;
##当 userlist_enable=NO 时 user_list列表不生效,列表内和列表外的用户都可登录;
##当 userlist_enable=YES,userlist_deny=YES 时user_list列表内的用户不可以登录,列表外用户可登录;
userlist_file=/etc/vsftpd.user_list
#local_root=/home
seccomp_sandbox=NO
allow_writeable_chroot=YES #允许对禁锢的FTP根目录执行写入操作,而且不拒绝用户的登录请求
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
#utf8_filesystem=YES
3.编辑/ect/vsftpd/chroot_list文件,将ZZZ的账户名添加进去,保存退出
vi /etc/vsftpd/chroot_list
#将ZZZ加进去 多个user换行添加
ZZZ
4.重启并查看状态
service vsftpd restart
service vsftpd status
FTP协议有下面两种工作模式:
•主动模式:FTP服务器主动向客户端发起连接请求
•被动模式:FTP服务器等待客户端发起连接请求(默认工作模式)
在配置文件中加入如下开启
#被动模式 pasv_enable=YES pasv_min_port=30000 pasv_max_port=35000
五、测试FTP连接
测试FTP服务器是否正常运行
1.本地测试
1.打开FileZilla输入主机ip、用户名、密码、端口号
2.点击快速连接,出现如下信息连接成功
2.同网段测试
1.打开WinScp,输入主机ip、用户名、密码、端口号
2.点击登录
3.出现如下信息,能够进行文件传送,连接成功
参考文章:
如何在Linux系统中安装并启动FTP服务:详细步骤与注意事项 – Linux命令大全(手册)
如何在Ubuntu 22.04上搭建FTP服务器 - sll917-cn - 博客园
Linux chmod命令教程:轻松学会控制文件权限(附常用语法参数及N条实例及扩展)_linux给文件权限chmod命令-CSDN博客