阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS7 搭建vsftpd详细教程

145次阅读
没有评论

共计 4209 个字符,预计需要花费 11 分钟才能阅读完成。

绪论

vsftp 被公认目前最好的 ftp 之一,所以,搭建它还是很有意义的,有了它,我们可以让虚拟机与主机更加方便的通信。root 用户,在默认的情况下,是不允许做为 ftp 用户登录的,但是我们可以通过一些配置,来解禁。下面正是这些操作的详细讲解,适当的时候并贴上相应的脚本。

1. 安装 vsftp

用 yum 命令查找到了我们想要的 vsftpd,开始安装。

[root@localhost ftp]# yum search vsftpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
============================= N/S matched: vsftpd ==============================
vsftpd-sysvinit.x86_64 : SysV initscript for vsftpd daemon
vsftpd.x86_64 : Very Secure Ftp Daemon

安装:yuminstall -y vsftpd

2. 防火墙设置

如果防火墙把 ftp 的端口给拦截了,我们是无法访问到 ftp 服务的。我们可以选择把 ftp 的端口给关闭,但是个人不推荐。防火墙还是开着吧,虽然麻烦了一点。我比较喜欢用到哪个端口用开启哪个端口。

当然如果不想麻烦,也提供 CentOS7 中关闭防火墙的方法:

# 关闭防火墙(重启后不生效)
systemctl stop firewalld.service

# 禁用防火墙(永久关闭)
systemctl disable firewalld.service

如果不关闭防火墙的话,我们可以把 ftp 服务添加了防火墙外:

# 开放 ftp 服务添加到防火墙外
firewall-cmd --permanent --add-service=ftp

# 使其生效
firewall-cmd --reload

# 重启防火墙
systemctl restart firewalld.service

3. 设置 SELinux

为什么要设置 SELinux?

SELinux(Security-Enhanced Linux)是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux 历史上最杰出的新安全子系统。NSA 是在 Linux 社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。

SELinux 旨在提高 Linux 系统的安全性,提供强健的安全保证,可防御未知攻击。

大部分情况下,我们访问 ftp 的时候会被 SELinux 拦截,当然如果你没也设置 SELiunx 也能正常访问,此步可以跳过。

大家通常的作法是关闭 SELiunx,这样做会引起其它安全问题,嫌麻烦的可以直接关闭:

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled # 增加

:wq! # 保存退出

setenforce 0 # 让 SELinux 进入 Permissive 模式(宽容模式)

SELiunx 一共有三种模式:

  1. enforcing(强制模式):开始限制 domain/type
  2. permissive(宽容模式):仅会有警告信息
  3. disabled(关闭):关闭 SELinux

设置 SELiunx:

[root@localhost ~]# /usr/sbin/sestatus -v     #查看 SELinux 状态
SELinux status:                 enabled    # 启用
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
setenforce 0 # 暂时让 SELinux 进入 Permissive 模式

这个时候我们尝试访问一下 ftp 目录,发现能够正常访问。我们查看一下权限:

[root@localhost ~]# getsebool -a | grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off

ftp_home_dir 和 allow_ftpd_full_access 必须为 on 才能使 vsftpd 具有访问 ftp 根目录,以及文件传输等权限。

setsebool -P tftp_home_dir 1
setsebool -P allow_ftpd_full_access 1

让我们再回到强制模式:

setenforce 1 # 进入 Enforcing 模式

如果还是不行的话,可能是我们的目录没有权限:

chmod -R 777 /usr/yong.cao/ftp   #ftp 的访问路径

4. 配置 vsftpd

用 vim /etc/vsftpd/vsftpd.conf 打开后修改或者新增:

anonymous_enable=NO    #不允许匿名访问

write_enable=YES    #写权限

local_root=/usr/yong.cao/ftp   #这里是我自定义的 ftp 目录

chroot_local_user=YES    #这个是限制 ftp 用户只能在自己目录,如果 ftp 可以跳到其它任意目录是比较危险的,建议限制

allow_writeable_chroot=YES   #2.3.5之后,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了,需新增此配置

5. 激活 root 用户

处于安全考虑,root 用户是不能作为 ftp 的登录账户的,如果在没有限制访问目录的情况下,root 用户可以在 ftp 里访问任一文件,所以默认是不能登录 ftp 的。当然,你也可以为新建一些用户,专门作为 ftp 用户,个人习惯,用虚拟机的时候我一般直接用 root 用户登录 ftp。

我们需要注释或者删除 /etc/vsftpd/ftpusers 和 /etc/vsftpd/user_list 中的 root:

vi /etc/vsftpd/ftpusers
# Users that are not allowed to login via ftp
#root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
--------------------------------------------------------------------
vi /etc/vsftpd/user_list
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
#root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody

6. 重启服务

CentOS7 中是用这个命令:

systemctl restart vsftpd.service

如果是其它系统:

service restart vsftpd

在 Windows 上登录:

CentOS7 搭建 vsftpd 详细教程

更多 Vsftpd 相关教程见以下内容

Linux CentOS 6.5 下搭建 vsftpd ftp 服务器  http://www.linuxidc.com/Linux/2016-07/133319.htm

CentOS 安装 vsftpd-3.0.2 及安全配置  http://www.linuxidc.com/Linux/2016-12/138040.htm

openSUSE 13.2/13.1 下安装配置 FTP 服务器 vsftpd  http://www.linuxidc.com/Linux/2014-12/110070.htm

CentOS7 基于虚拟用户的 vsftpd  http://www.linuxidc.com/Linux/2016-11/137150.htm

Ubuntu 14.04 配置 vsftpd 实现 FTP 服务器 – 通过 FTP 连接 AWS  http://www.linuxidc.com/Linux/2016-08/133933.htm

FTP 协议详解与 vsftpd 在 Linux 上的安装配置 http://www.linuxidc.com/Linux/2016-09/134831.htm

CentOS 6.7 安装 vsftpd 服务器  http://www.linuxidc.com/Linux/2017-04/142612.htm

CentOS 7.3 安装配置 Vsftpd 文件服务器  http://www.linuxidc.com/Linux/2017-06/144886.htm

Vsftpd3.0–FTP 服务器搭建之本地用户篇  http://www.linuxidc.com/Linux/2017-04/142699.htm

CentOS7 基于虚拟用户的 vsftpd  http://www.linuxidc.com/Linux/2016-11/137150.htm

Ubuntu 16.04 下 vsftpd 安装配置实例  http://www.linuxidc.com/Linux/2017-06/144807.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149909.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4209字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中