(linux基础19)linux搭建本地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软件。

一、ftp的安装和启用

1.准备工作

配置好软件仓库,可以看前面软件仓库搭建的文章。
进入要安装ftp服务的主机,关闭selinux(下一部分学,为了ftp实验进行顺利,这里关闭)或者输入setenfore 0
在这里插入图片描述在这里插入图片描述

2.安装与启动

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

3.防火墙设置

之前都是关闭防火墙,但是不安全,为了安全,需要设置下防火墙。
1.命令设置
在这里插入图片描述
2.使用图形命令设置防火墙
在这里插入图片描述

打开防火墙图形控制界面
在这里插入图片描述
option中选择reload Firewalld

在这里插入图片描述
在这里插入图片描述

4.登陆ftp服务器

命令行登陆:

lftp  192.168.1.100  (匿名登陆)
lftp  192.168.1.100 -u  username (用户登陆)   

浏览器登陆: ftp://192.168.1.100(ip地址,这里是装ftp服务的主机)

命令行进入ftp服务后一般用到的命令:

put       上传
get       下载
rm        删除
ls        列出文件/文件夹
bye/quit  退出ftp服务器

二、ftp基本安全部署示例

确认selinux关闭、防火墙允许ftp

1.ftp服务的基本信息与常报错id解析

软件安传包: vsftpd
默认发布目录: /var/ftp
协议端口: 21/tcp
服务配置文件: /etc/vsftp/vsftp.conf

报错ID解析
500文件系统权限过大
530用户认证失败
550服务本身功能未开放
553本地文件系统权限过小

实验一 530用户认证失败
1、修改本地用户不可以登陆
2、重启vsftp服务
3、测试本地用户登陆情况
在这里插入图片描述

在这里插入图片描述实验二 550服务本身未开放
1、修改配置文件为本地用户可以登陆,但是不可以写
2、重启服务
3、写测试
在这里插入图片描述在这里插入图片描述

2.主配置文件文本

# 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
#
# 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=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.
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=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_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

三、匿名用户读写实验

操作步骤

  • 允许匿名登陆
  • 修改配置文件相关参数
  • 设置完需重启vsftp服务
    注:对于配置文件中没有写的功能,可以使用 man 5 vsftp.conf 来查询功能

1.匿名用户上传(写)

vim /etc/vsftpd/vsftpd.conf
write_enable=YES
anon_upload_enable=YES  #不开启,执行上传命令报错550
chgrp ftp /var/ftp/pub
chmod 775  /var/ftp/pub

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.匿名用户建立目录(写)

anon_mkdir_write_enable=YES|NO
在这里插入图片描述在这里插入图片描述

3.匿名用户下载(读)

上一个实验已经可以上传文件了,但是下载还不可以
在这里插入图片描述这里可以使用 man 5 vsftp.conf 来查询此功能,下载是个 读 得过程,可以搜索read相关

在这里插入图片描述在这里插入图片描述在这里插入图片描述下载成功,
在这里插入图片描述

4.匿名用户删除(写)

anon_other_write_enable
在这里插入图片描述设置功能前删除报错
在这里插入图片描述设置好功能后,匿名用户可以删除文件/文件夹

在这里插入图片描述
在这里插入图片描述

四、关于匿名用户的其他设定参数

1.匿名用户家目录修改

anon_root=/direcotry

在这里插入图片描述匿名用户默认登陆到/westos

在这里插入图片描述当我把家目录修改为/var/ftp/pub时登陆为什么报错?
答:ftp家目录不可以有写权限,为了安全
在这里插入图片描述
在这里插入图片描述

2. 匿名用户上传文件默认权限修改

anon_umask=xxx
在这里插入图片描述

在这里插入图片描述

这里改为755,为啥文件是644?
答:文件权限不包含可执行权限,自动减去
在这里插入图片描述

3.对匿名用户的上传下载限速(要保证vip特权)

anon_max_rate=速度 单位为字节,1024字节=1k
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

4.限制最大连接客户(保证服务器质量)

max_clients=x

5. 匿名用户使用的用户身份修改

chown_uploads=YES
chown_username=haojin
chown_upload_mode=0644

五、本地用户的设定

本地用户登陆方法:

lftp IP地址 -u 用户名 

1.修改本地用户家目录

默认家目录为本地目录,若要修改目录,在配置文件里添加local_root=/目录,和匿名用户修改雷同。
在这里插入图片描述

在这里插入图片描述

2.锁定本地用户目录

本地用户登陆ftp后,虽然是默认目录,但是还可以访问根目录,这样是很不安全的,所以要锁定本地用户的目录,不让其访问其他目录。
chroot_local_user=YES

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.黑名单

1.永久黑名单
黑名单目录:/etc/vsftpd/ftpusers
在这里插入图片描述
在这里插入图片描述

2.临时黑白名单

黑名单配置文件需要添加/修改:
chroot_local_users=NO
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

白名单配置文件需要添加/修改:
chroot_local_users=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

vim /etc/vsftpd/user_list    ##用户临时黑名单

#用户白名单设置
userlist_deny=NO #写入配置文件
/etc/vsftpd/user_list      ##参数设定,此文件变成用户白名单,只在名单中出现的用户可以登陆ftp

六、虚拟用户的认证设定

1.在/etc/vsftpd/ 下创建vuser_list 文件(认证文件)文件名称随意。

在这里插入图片描述

2.对该认证文件进行加密

加密工具:db_load

在这里插入图片描述

3.生成认证策略(pam) ,名字随意,但是文件位置必须在/etc/pam.d下。

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
文件意思重要
在这里插入图片描述
4.修改主配置文件

pam_service_name=ftpusers    ##将pam认证的文件改为自己写的ftpusers
guest_enable=YES             ##允许使用虚拟用户
guest_username=ftp           ##规定虚拟用户登陆的真实用户(相当于指定代理)

在这里插入图片描述在这里插入图片描述

七、虚拟用户的家目录设定及授权独立

之前给虚拟用户指定的用户是ftp,所以登陆的目录是匿名用户的家目录;这里学会修改虚拟用户的登陆家目录。

1.建立一个给虚拟用户使用的目录,在/var下

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.修改主配置文件

在这里插入图片描述登陆成功
在这里插入图片描述

3.对不同虚拟用户的授权操作(独立配置文件)

1)操作步骤

  • man 5 vsftpd 搜索config
  • 主配置文件中写入:user_config_dir=/etc/vsftpd/user_config
  • user_config 目录是不存在的需要自己建立,目录名称可以自定义,并且优先级要高于主配置文件中直接设置
  • 配置每一个用户的授权项,如给user2建立一个上传功能,vim /etc/vsftpd/user_config/user2

2)实验截图

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
独立配置文件:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述测试,user1不可以上传,user2可以上传。
在这里插入图片描述
到这里ftp服务的实验就做完了!

努力-奋斗
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值