linux服务之FTP(centos7.6)

在文章的开始,希望大家关注公众号Lind-Learn,在这里会分享各类计算机技术的知识。


1. FTP介绍

  • 引入

网络文件服务器:通过网络共享文件或文件夹,实现数据共享
NAS(Network Append storage):共享的是文件夹

  1. FTP:文件服务器
  2. samba:不同系统间的文件夹和设备共享
  3. NFS:网络文件系统
    部署思路:
  4. FTP:单纯上传下载
  5. samba:系统集成间的共享
  6. NFS:网络存储服务器
  • 介绍

FTP(File Transer Protocol)是TCP/IP协议组中的协议之一,是一个TCP协议。
功能:在服务器余客户端之间进行文件的传输。FTP就是实现两台计算机之间的拷贝。
从远处九三级拷贝文件至自己的计算机上,称之为"下载(download)"文件。
将文件从自己九三级中拷贝至远程计算机上,则称之为"上传(upload)"文件。
这个古老的协议使用的是明码传输的方式,且过去有相当多的安全危机历史。
为了更安全的使用FTP协议,我们主要采用较为安全但功能较少的vsftpd(very secure file transer protocol)这个软件。
FTP是一个C/S类型的软件,FTP监听TCP端口号为21,数据端口为20。

  • 应用场景
    • 文件服务器
    • 下载服务器
    • 上传服务器
  • 工作模式

FTP支持两种模式,一种方式叫做Standard(也就是PORT方式,主动方式),一种是Passive(也就是PASV,被动方式)。Standard模式FTP的客户端发送PORT命令道FTP服务器。Passive模式FTP的客户端发送PASV命令道FTP Server。

  • 主动模式

Port模式FTP客户端首先和FTP服务器的TCP 21端口建立连接, 通过这个通道发送命令。客户端需要接收数据的时候在这个通道上发送PORT命令。PORT命令包含了客户端用什么端口接收数据。在传送数据的时候,服务器端通过自己的TCP 20端口连接至客户端的指定端口发送数据。FTP server必须和客户端建立一个心的连接用来传送数据,vsftp默认模式。

  • 被动模式

Passive模式在建立控制通道的时候和Standard模拟类似,但建立连接后发送的不是Port命令,而是Pasv命令。FTP服务器收到Pasv命令后,随机打开一个高位端口(端口号大于1024)并且同志客户端在这个端口上传送数据的请求后,客户端连接FTP服务器对应端口,然后FTP服务器将通过这个端口进行数据的传送,这个时候FTP server不再需要建立一个新的和客户端之间的连接。

  • 访问方式:FTP服务器在默认情况下,依据使用者登录的情况而分为三种不同的身份,分别是:
    • 实体账户:本地账户
    • 来宾账户:guest
    • 匿名登录者:anonymous ftp

2. FTP服务器部署

  • 安装部署
# vsftp安装
[root@Lind ~]# yum -y install vsftpd
# vsftp开机启动
[root@Lind ~]# systemctl enable vsftpd
#启动vsftp服务
[root@Lind ~]# systemctl start vsftpd
#验证启动(三种方式都可)
[root@Lind ~]# lsof -i :21
[root@Lind ~]# systemctl is-active vsftpd
[root@Lind ~]# systemctl is-active vsftpd

3. FTP相关配置文件

  • 主配文件 /etc/vsftpd/vsftpd.conf
  • 关于下载目录 匿名用户根目下载目录 /var/ftp 本地用户 ~
  • FTP日志 /var/log/xferlog
  • 主配文件详解
# Example config file /etc/vsftpd/vsftpd.conf
# 示例配置文件 /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.
# 默认编译的设置相当保守。这个示例文件稍微放宽了一些限制,使得FTP守护进程更易用。
#
# 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.
# 请注意:此示例文件并未穷尽列出 vsftpd 的所有选项。
# 请阅读 vsftpd.conf.5 手册页以全面了解 vsftpd 的功能。
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
# 允许匿名FTP登录?(注意 - 如果注释掉下列这行,将默认允许匿名登录)。
anonymous_enable=YES
#
# 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.
# 取消注释以启用任何形式的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)
#这里022是反码(所以新建目录的权限是755 文件的权限是644)
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.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
# 取消注释以允许匿名FTP用户上传文件。
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
# 取消注释如果你希望匿名FTP用户能够创建新目录。
#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).
# 确保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_username
#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/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.
# 如果需要,可以将日志文件设置为标准ftpd xferlog格式。
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.
# 用来指定一个非特权用户,该用户用于处理FTP客户端连接时的数据传输操作
#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.
# 启用此选项,服务器将识别异步ABOR请求。不推荐出于安全原因(代码非简单)。但是,不启用它可能会让旧的FTP客户端困惑。
#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模式,但实际上忽略请求。
# 启用以下选项以在ASCII模式下对文件进行实际的ASCII处理。vsftpd.conf(5)手册页解释了禁用这些选项时的行为。
# 请注意,在某些FTP服务器上,ASCII支持允许通过命令“SIZE /big/file”进行拒绝服务攻击(DoS)。
# vsftpd预测到了这种攻击并且一直是安全的,报告原始文件的大小。
# ASCII处理是协议的一个糟糕特性。
#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.
# 你可以指定一个不允许的匿名电子邮件地址文件。显然有助于对抗某些DoS攻击。
#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到他们的主目录。
# 如果chroot_local_user为YES,则该列表成为不进行chroot的用户列表。
# (警告!chroot操作可能非常危险。如果使用chroot,请确保用户对chroot中的顶级目录没有写权限)
#chroot_local_user=YES
#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的“-R”选项。默认情况下禁用它,以避免远程用户在大型站点上造成过多的I/O。
# 但是,一些有问题的FTP客户端(如“ncftp”和“mirror”)假定存在“-R”选项,因此有很强的理由启用它。
#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”指令时,vsftpd在独立模式下运行并监听IPv4套接字。
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 !!
# 此指令启用对IPv6套接字的监听。默认情况下,监听IPv6的"any"地址(::)将接受来自IPv6和IPv4客户端的连接。
# 没有必要同时监听IPv4和IPv6套接字。如果需要那样做(例如你希望监听特定地址),
# 则必须运行两个vsftpd副本并使用两个配置文件。
# 确保其中一个监听选项被注释掉!!
listen_ipv6=YES

# 指定PAM(可插拔认证模块)服务名称为vsftpd。
pam_service_name=vsftpd

# 启用用户列表功能。
# 如果启用,vsftpd将使用/etc/vsftpd.user_list文件来允许或拒绝特定用户访问。
userlist_enable=YES

# 启用TCP Wrappers进行访问控制。
# 这允许通过/etc/hosts.allow和/etc/hosts.deny文件来控制对vsftpd的访问。
tcp_wrappers=YES

4. FTP客户端

  • FTP相关命令
    命令说明
    help打印帮助菜单
    !+linux命令执行linux命令
    lcd 目录路径切换linux当前目录
    put、mput上传、批量上传
    get、mget下载、批量下载
    ls、dir列出FTP目录内容
    mkdir、cd、delete、rmdir创建目录、进入目录、删除文件、删除目录
    pwd显示FTP当前目录
    open、close、by开启FTP连接、关闭FTP连接、退出FTP客户端

5. 虚拟用户登录

  • 引入 -> FTP安全问题

由于FTP采用的是本地用户登录,所以会直接将本地用户暴露在互联网种,如果没有相关的安全设置,FTP用户会使用该用户去登录你的计算机;
其次如果又大量的本地用户,FTP管理员还需要创建大量的本地用户,又给安全带来了不小的挑战;
解决思路:如果FTP登录的用户不是本地系统真实存在的,那么这个问题就解决了!

  • 虚拟用户指令
guest_eanble=YES // 开启虚拟用户登录功能
guest_username=ftp //设置虚拟用户ftp
  • 案例

部署一台基于虚拟用户的FTP
为了解决公司员工文件存储和下载的需求。要求部署内部FTP服务器,用户可以通过自己的账号的权限对FTP进行操作。
1)公司公共可以通过匿名下载
2)公司财务部、商务部、行政部、技术部分别拥有自己的部门文件夹,彼此之间相互隔离
3)部门之间只有主管才能拥有上传权限,普通员工只能下载
4)禁止用户查看家目录以外的数据
5)确保FTP账号安全

# 案例拆解
# 1)FTP需要开启匿名下载 		默认FTP支持匿名下载
# 2)分析针对不同部分实现目录隔离  有两种方法 1.同部门共用本地账号 2.虚拟账号
# 3)部分内部员工权限不同		   实现方法 虚拟账号
# 4)禁止用户切出				  chroot
# 5)防止本地账号泄露			 虚拟账号

#部署步骤
# 1)创建虚拟用户映射的本地帐号
# 2)配置文件开启虚拟用户验证
# 3)建立虚拟用户验证数据库
# 4)开启pam认证
# 5)设置不同部门员工的权限
# 6)测试

# 创建虚拟用户映射的本地帐号
[root@Lind ~]# useradd -s /sbin/nologin -d /var/tmp/vuser_ftp lind

# 新版vsftpd不允许用户主目录又w权限,因此子啊主目录下新建一个子目录,赋予700(意味着用户具有读写执行权限,用户组与其他用户没有任何权限)权限。所有上传、下载、删除、重命名等操作只能在子目录中进行
[root@Lind ~]# chmod 500 /var/tmp/vuser_ftp

#设置不同部门之间的跟文件夹,给予700权限
[root@Lind ~]# mkdir /var/tmp/vuser_ftp/shichang
[root@Lind ~]# mkdir /var/tmp/vuser_ftp/caiwu
[root@Lind ~]# mkdir /var/tmp/vuser_ftp/xingzheng
[root@Lind ~]# mkdir /var/tmp/vuser_ftp/jishu
[root@Lind ~]# chmod 700 /var/tmp/vuser_ftp/*

# 修改部门共享文件夹的所有者和属组为lind
# 将 /var/tmp/vuser_ftp 目录及其所有子目录和文件的所有者和所属组都更改为 lind 用户和 lind 组
[root@Lind ~]# chown lind.lind /var/tmp/vuser_ftp -R

# 在主配文件中加入
guest_enable=YES # 这个选项启用虚拟用户功能。设置为 YES 表示允许使用虚拟用户登录 FTP 服务器。
guest_username=lind # 这个选项指定虚拟用户登录时所使用的实际系统用户。所有虚拟用户在登录时都会映射到这个实际用户 lind,并且以该用户的权限进行操作。
virtual_use_local_privs=NO # 这个选项指定虚拟用户是否使用本地用户的权限:设置为 NO 表示虚拟用户不会使用本地用户的权限,而是使用更严格的权限设置。
user_config_dir=/etc/vsftpd/vconf.df/ #这个选项指定一个目录,该目录包含针对每个虚拟用户的特定配置文件。每个虚拟用户的配置文件名应与虚拟用户名相同。例如,如果有一个虚拟用户 vuser1,那么对应的配置文件路径应该是 /etc/vsftpd/vconf.df/vuser1

# 建立虚拟用户账号密码文件
[root@Lind ~]# vim /etc/vsftpd/vuser
#单行用户名 双行密码
sc_01
123
xz_01
123
js_01
123
cw_01
123
sc_02
222
xz_02
222
js_02
222
cw_02
222
# 生成账号密码数据库文件
# 命令解释
# db_load:这是一个用于创建或加载数据库文件的命令,通常与 Berkeley DB 一起使用。
# -T:表示输入数据为简单文本格式。
# -t hash:指定数据库的类型为哈希表(hash)。
# -f /etc/vsftpd/vuser:指定输入文件 /etc/vsftpd/vuser,该文件包含用户数据。
/etc/vsftpd/vuser.db:指定输出的数据库文件路径。
[root@Lind ~]# db_load -T -t hash -f /etc/vsftpd/vuser /etc/vsftpd/vuser.db
[root@Lind ~]# chmod 600 /etc/vsftpd/vuser.db

# pam虚拟用户登录验证
# 具体作用
# 1)认证用户:当用户尝试登录 FTP 服务器时,PAM 会使用 pam_userdb 模块从指定的数据库文件 /etc/vsftpd/vuser.db 中读取用户信息进行认证。如果用户名和密码匹配,则认证成功。
# 2)账户管理:在认证成功后,PAM 还会使用 pam_userdb 模块从相同的数据库文件中读取账户信息进行管理。如果账户信息有效,则账户管理成功。
[root@Lind ~]# vim /etc/pam.d/vsftpd 
#在文件首部加上这两句
auth       sufficient   /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser
account    sufficient   /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser

# 定义部分用户不能切出家目录
# 在主配文件中添加
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
allow_writeable_chroot=YES # 如果不开的话,chroot下的用户就无法写,并且该指定根目录不能有写的权限

[root@Lind vsftpd]# vim /etc/vsftpd/chroot_list
sc_01
sc_02
xz_01
xz_02
js_01
js_02
cw_01
cw_02

# 建立部门员工子配置文件,用于定义权限
[root@Lind vsftpd]# mkdir /etc/vsftpd/vconf.d/
#定义虚拟用户权限
#部门主管有管理权限
# 以财务主管(cw_01)为例
[root@Lind ~]# vim /etc/vsftpd/vconf.df/cw_01
local_root=/var/tmp/vuser_ftp/caiwu
anon_umask=077
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

# 以财务员工(cw_02)为例
[root@Lind ~]# vim /etc/vsftpd/vconf.d/cw_02
local_root=/var/tmp/vuser_ftp/caiwu
anon_world_readable_only=NO


  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值