Linux Centos7.4下的Ftp服务的搭建和使用

    • 服务简介
  • 一、Ftp服务的安装
    • 1.1、安装ftp服务
    • 1.2、启动ftp服务
    • 1.3、将ftp服务设置为开机启动
    • 1.4、停止ftp服务
  • 二、Ftp服务概要说明
    • 2.1、Ftp的用户分类
      • 2.1.1、匿名用户
      • 2.1.2、本地用户
        • 2.1.2.1、创建本地用户
        • 2.1.2.2、为该用户设置密码
        • 2.1.2.3、修改文件夹ftpfile的权限
        • 2.1.2.4、本地用户的基本配置项说明
      • 2.1.3、虚拟用户
        • 2.1.3.1、创建虚拟用户数据库文件
        • 2.1.3.2、创建虚拟用户的映射用户,并制定其用户家目录
        • 2.1.3.3、创建虚拟用户的PAM认证文件,添加虚拟用户支持。
        • 2.1.3.4、为虚拟用户建立独立的配置文件,启动服务并测试
    • 2.2、openssl+vsftpd加密方式
      • 2.2.1、ssh使用公钥和私钥对进行数据传输加密的概要
      • 2.2.2、ssh使用公钥和私钥对进行数据传输加密的概要
      • 2.2.3、实践
  • 三、配置文件
  • 四、问题汇总
    • 4.1、登录慢,具体表现在ftpClient.connect(props.getHost(), props.getPort());需要很长时间
    • 4.2、本地用户登录500 OOPS: vsftpd: refusing to run with writable root inside chroot()
    • 4.3、本地用户登录530 Login incorrect.

服务简介


这里需要注意的是所谓的安全,在数据传输的时候还是明文的。
主动模式(默认)

被动模式


一、Ftp服务的安装

1.1、安装ftp服务

# 安装ftp服务
yum install vsftpd
# 安装ftp客户端
yum install ftp

1.2、启动ftp服务

# 启动ftp服务
systemctl start vsftpd.service
# 查看ftp服务的状态
systemctl status vsftpd.service

如下图可见,我们的ftp服务是安装和启动成功了。

1.3、将ftp服务设置为开机启动

# 设置开机启动
systemctl enable vsftpd.service

1.4、停止ftp服务

# 停止
systemctl stop vsftpd.service
# 重启服务
systemctl restart vsftpd.service

二、Ftp服务概要说明

2.1、Ftp的用户分类

2.1.1、匿名用户


1、匿名用户是vsftp应用安装启动后直接用来访问的。作为匿名用户,他的用户名为:ftp;没有密码。在登录的时候,在提示输入密码的时候直接回车过就行。不需要输入空格。
2、匿名用户的默认权限是只有下载不能上传的。且上传权限由两部分组成(配置文件[vsftpd.conf]和文件系统)

同时,查看/etc/vsftpd下的vsftpd.conf文件可以看到配置文件中未开启匿名用户上传文件的权限

匿名用户上传得文件,匿名用户是无法下载的。只有配置了下面的anon_umask=022之后才可以。
当前文件的权限(755)=最大权限(777)-掩码(022)

2.1.2、本地用户


这里需要注意的是。ftp的用户用的是linux的用户系统,所以默认创建的本地用户的ftp的文件夹即是,创建的linux用户的家目录。(这里如果理解不了的,需要了解一下linux的用户和用户家目录的相关知识)。

2.1.2.1、创建本地用户

# 添加用户ftpuser
# /ftpfile 指创建的ftp文件夹
# -s /sbin/nologin 指当前的用户不能登录操作
useradd ftpuser -d /ftpfile -s /sbin/nologin

这里通过上面的命令行会自行创建文件夹,也可以通过下面的命令行创建文件夹。

# 创建ftpfile文件夹
mkdir ftpfile

PS: 这里需要注意的是我这里在添加本地的ftp使用的用户的时候给这个用户指定了家目录为ftpfile

2.1.2.2、为该用户设置密码

passwd ftpuser

2.1.2.3、修改文件夹ftpfile的权限

# -R 指遍历文件夹,修改该文件夹下的所有内容的权限到ftpuser的用户和用户组
chown -R ftpuser.ftpuser ftpfile/

注意:经过上面的设置,使用ftpuser用户登录到ftp服务器可以发现,我们当前的用户的默认的ftp文件夹就是之前创建的ftpfile文件夹。

2.1.2.4、本地用户的基本配置项说明



2.1.3、虚拟用户

2.1.3.1、创建虚拟用户数据库文件

原理如下图所示

下面首先创建了一个ftp的用户文件,然后把用户文件加密成一个用户db文件。

指令 含义
db_load: 加密的工具
-T: 将vsftpd.user加密成vsftpd.db
-t: 使用什么方式加密
hash: 使用hash码的方式加密
-f: 后跟要转化的原文件
注意:需要将这个用户数据库文件的权限修改为600,不然会报错。是vsftp的要求。

2.1.3.2、创建虚拟用户的映射用户,并制定其用户家目录


这里,可以不指定用户的根目录,那么用户的根目录就是当前用户的家目录。如图指定了当前用户virtual的根目录为/var/ftproot
注意:这里是创建了一个virtual用户,将该用户下的文件夹提供给上面创建的用户用。并非是直接使用virtual用户登录。

2.1.3.3、创建虚拟用户的PAM认证文件,添加虚拟用户支持。

# 虚拟用户开启
guest_enable=YES
# 虚拟用户对应的系统用户是谁
guest_username=virtual
# 虚拟用户的配置文件在哪里(dir可以修改成自己喜欢的,但是需要注意这个文件夹需要自己提前创建的)
user_config_dir=/etc/vsftpd/dir


2.1.3.4、为虚拟用户建立独立的配置文件,启动服务并测试


注意:虚拟用户默认使用的配置参数用的就是匿名用户的配置,所以需要先把vsftpd.conf主配置文件中的自己添加的一些匿名用户的相关设置注释掉。主配置文件的优先级大于子配置文件的优先级

添加匿名用户a1的配置

设置ftproot文件夹的用户权限

2.2、openssl+vsftpd加密方式

2.2.1、ssh使用公钥和私钥对进行数据传输加密的概要

2.2.2、ssh使用公钥和私钥对进行数据传输加密的概要

2.2.3、实践

校验是否支持openssl

生成密钥 ==> 生成证书 ==> 生成签名后的证书

# 生成密钥
openssl genrsa -out vsftpd.key 2048
# 生成证书
openssl req -new -key vsftpd.key -out vsftpd.csr
# 生成签名后的证书
openssl x509 -req -days 365 -sha256 -in vsftpd.csr -signkey vsftpd.key -out vsftpd.crt


# 修改当前目录
chmod 500 /etc/ssl/certs/


ssl_enable=YES
#启用ssl认证
ssl_tlsv1=YES
ssl_sslv2=YES
ssl_sslv3=YES
#开启tlsv1,sslv2,sslv3都支持
allow_anon_ssl=YES
#允许匿名用户{虚拟用户}
force_anon_logins_ssl=YES
force_anon_data_ssl=YES
#匿名登录和传输时强制使用ssl
rsa_cert_file=/etc/ssl/certs/vsftpd.crt
#rsa格式的证书
rsa_private_key_file=/etc/ssl/certs/vsftpd.key
#rsa格式的密钥

三、配置文件

# 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=yes
#
# 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=nolocal_root=/home/ftpfile
reverse_lookup_enable=NO
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
listen_port=8021
#pasv_min_port=30000
#pasv_max_port=31000
#pasv_address=192.168.32.1
#pasv_enable=NO

四、问题汇总

4.1、登录慢,具体表现在ftpClient.connect(props.getHost(), props.getPort());需要很长时间

在vsftpd.conf配置文件中增加下面的配置。

4.2、本地用户登录500 OOPS: vsftpd: refusing to run with writable root inside chroot()

注意点: 本地用户在登录的时候,出现了500 OOPS错误。

这是因为从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。在vsftp.conf文件中添加下面的配置之后,就可以解决这个问题。

allow_writeable_chroot=YES

4.3、本地用户登录530 Login incorrect.


检查下面的文件,注释调中间的这一段之后,重启服务。
vim /etc/pam.d/vsftpd

【Linux学习笔记】Linux Centos7.4下的Ftp服务的搭建和使用及加密相关推荐

  1. Linux学习笔记之CentOS7学习(一)

    CentOS7学习笔记(一):基础知识 学习Linux版本CentOS-7-x86_64-Minimal-1804.iso,目前比较新的一个版本. VMware安装镜像文件,设置网络连接方式为桥接模式 ...

  2. Linux学习笔记-Linux下读写文件

    在Linux编程需要读写文件时,有两种方式: (1)ANSIC: 使用stdio.h里的函数.fopen, fclose, fwrite, fread (2)Linux API:Linux提供了另外一 ...

  3. [Linux学习笔记] Linux历史发展与应用

    2019独角兽企业重金招聘Python工程师标准>>> UNIX发展历史: (1) 1965年,美国麻省理工学院(MIT).通用电气公司(GE)及AT&T的贝尔实验室联合开发 ...

  4. Linux学习笔记——Linux、命令 01

    操作系统的发展史 Unix 1965年之前的时候,电脑并不像现在一样普遍,它可不是一般人能碰的起的,除非是军事或者学院的研究机构,而且当时大型主机至多能提供30台终端(30个键盘.显示器),连接一台电 ...

  5. Linux学习笔记-Linux下的设备文件

    在Linux下,有一种文件的类型叫设备文件. 在/dev目录下的文件基本上都是设备文件,文件属性以c或b打头(charactor, block) 设备文件用于代表一个物理设备 例如,声卡.显卡.键盘. ...

  6. 嵌入式linux学习笔记--linux下基于imx6ullpro 的 CP2102 /CH340 驱动 以及简单的测试

    今天再次编译了linux 的内核,想起来之前一直没实现的嵌入式linux 的串口驱动,故想实验一下. 本文章会分别介绍CP2102 以及CH340两者的驱动 以及他们的简单的测试,后续可能会更新一个 ...

  7. Linux工作笔记029---Centos7.3 服务器下查看tomcat服务是否启动,重启,查看错误日志等基本操作

    JAVA技术交流QQ群:170933152 启动:一般是执行sh tomcat/bin/startup.sh  停止:一般是执行sh tomcat/bin/shutdown.sh脚本命令  查看:执行 ...

  8. linux学习笔记-- linux的 shell和linux C 程序 获取命令行参数和环境变量

    常用的linux指令基本都支持命令行参数 例如 : ls -l rm -r rm -f cp -r 那么 这些参数都是如何去被应用程序获取的呢? 学习记录一下 1. shell 脚本 1.1 shel ...

  9. linux学习笔记 linux内核6.0.2目录结构

    一.linux内核目录 arch 包含和硬件体系结构相关的代码,每种平台占一个相应的目录,如i386.arm.arm64.powerpc.mips等.Linux内核目前已经支持30种左右的体系结构.在 ...

最新文章

  1. python对象编程例子-python(十二)面向对象编程、类
  2. vue --- Vue中的路由跳转问题
  3. activiti7流程设计器_变频空调器通信电路
  4. Eclipse 下如何引用另一个项目的Java文件
  5. 设计类超实用的导航网站,一网包含1000+个行业内热门资讯灵感源!
  6. pycharm自定义代码段
  7. 一则 Oracle 和 SqlServer 语法区别 (原创)
  8. 一款轻量级android图表组件SimpleChart-Kotlin
  9. 4234最小差值生成树
  10. 防火墙——Efficient理论讲解(IPSec4)
  11. InfoPath 2007/2010 Helper Tool
  12. Java基础面试题(史上最全基础面试题,精心整理100家互联网企业面经)
  13. 微信跳一跳python_用python来玩微信跳一跳
  14. Visio中旋转文本框与箭头平行
  15. 水星USB无线网卡mw150us苹果macOS系统驱动成功
  16. Science上聚类算法论文——Clustering by fast search and find of density peaks翻译稿
  17. 服务器开机显示器没反应,老司机教你开机显示器没反应怎么办
  18. 压缩文件zip怎么查看注释呢,市场上一些破软件不好用啊,我有秘诀
  19. Windows: 根据分组的本地TCP/IP打印机的安装(1)
  20. Oracle入门--水表项目(单表查询,链接查询,左右外连接,子查询,分页查询)(3)

热门文章

  1. Scala、Java 50道编程题
  2. WORD几个自己常用的自定义键盘快捷键对应命令名(自我查阅使用)
  3. 描绘用户场景并将典型用户和用户场景描述
  4. Eclipse for PHP语法检查过于严格
  5. 年末了,送大家25本 数据分析 实体好书!包邮到家!
  6. 【软考 系统架构设计师】企业信息化战略与实施④ 企业信息化与电子商务
  7. Linux的Java开发环境搭建及部署
  8. 用css的动画制作——西游记人物运动起来
  9. 在…视域下是什么意思_视域与视阈
  10. 利用计算机进行教学的优点,信息化教学手段对改善教学方式的影响