练习实践-基础设施-文件共享-FTP服务搭建-匿名/本地用户/虚拟用户三种模式

参考来源:

在线书籍-linux就该这么学-第11章

安装vsftpdf服务

[root@centos7 home]# dnf install vsftpd
Extra Packages for Enterprise Linux 7 - x86_64                                     0.
CentOS-7 - Base - mirrors.aliyun.com                                               0.
CentOS-7 - Updates - mirrors.aliyun.com                                            0.
CentOS-7 - Extras - mirrors.aliyun.com                                             0.
依赖关系解决。
=====================================================================================
 软件包                  架构                    版本                               仓
=====================================================================================
Installing:
 vsftpd                  x86_64                  3.0.2-29.el7_9                     u

事务概要
=====================================================================================
安装  1 软件包

总下载:173 k
安装大小:353 k
确定吗?[y/N]: y
下载软件包:
vsftpd-3.0.2-29.el7_9.x86_64.rpm                                                   17
-------------------------------------------------------------------------------------
总计                                                                               17
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中      :                                                                      
  Installing  : vsftpd-3.0.2-29.el7_9.x86_64                                         
  运行脚本    : vsftpd-3.0.2-29.el7_9.x86_64                                         
  验证        : vsftpd-3.0.2-29.el7_9.x86_64                                         

已安装:
  vsftpd-3.0.2-29.el7_9.x86_64                                                       

完毕!

备份配置文件,通过grep的-v参数,将非注释内容重新写入配置文件中

[root@centos7 home]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak
[root@centos7 home]# grep -v "#" /etc/vsftpd/vsftpd.conf_bak > /etc/vsftpd/vsftpd.conf
[root@centos7 home]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@centos7 home]#

vsftpd服务程序的匿名开放认证模式实现

1.修改配置文件,添加anon开头的前五行

[root@centos7 ftp]# cat -n /etc/vsftpd/vsftpd.conf
     1	anonymous_enable=YES
     2	anon_umask=022
     3	anon_upload_enable=YES
     4	anon_mkdir_write_enable=YES
     5	anon_other_write_enable=YES
     6	local_enable=YES
     7	write_enable=YES
     8	local_umask=022
     9	dirmessage_enable=YES
    10	xferlog_enable=YES
    11	connect_from_port_20=YES
    12	xferlog_std_format=YES
    13	listen=NO
    14	listen_ipv6=YES
    15	
    16	pam_service_name=vsftpd
    17	userlist_enable=YES
    18	tcp_wrappers=YES

2.修改共享文件权限

匿名文件所在目录路径为/var/ftp/pub,设置ftp用户为目录所有者

[root@centos7 ~]# cd /var/ftp/
[root@centos7 ftp]# ls
pub
[root@centos7 ftp]# ls -l
总用量 4
drwxr-xr-x 2 root root 4096 610 2021 pub
[root@centos7 ftp]# chown -R ftp /var/ftp/pub
[root@centos7 ftp]# ls -l
总用量 4
drwxr-xr-x 3 ftp root 4096 814 16:01 pub

3.检查安全策略和防火墙状态

未开启安装安全策略和防火墙服务;

[root@centos7 ftp]# getsebool -a | grep ftp
getsebool:  SELinux is disabled
[root@centos7 ftp]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

4.启动服务并设置开机自启

#systemctl start vsftpd
#systemctl enable vsftpd
[root@centos7 ftp]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2024-08-14 15:57:39 CST; 11min ago
 Main PID: 7733 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─7733 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

814 15:57:39 centos7 systemd[1]: Starting Vsftpd ftp daemon...
814 15:57:39 centos7 systemd[1]: Started Vsftpd ftp daemon.

5.验证效果

安装linux下ftp客户端工具

ftp是Linux系统中以命令行界面的方式来管理FTP传输服务的客户端工具。

[root@centos7 home]# dnf install ftp
上次元数据过期检查:0:02:47 前,执行于 2024年08月14日 星期三 15时42分16秒。
依赖关系解决。
====================================================================================
 软件包          架构               版本                     仓库              大小
====================================================================================
Installing:
 ftp             x86_64             0.17-67.el7              base              61 k

事务概要
====================================================================================
安装  1 软件包

总下载:61 k
安装大小:96 k
确定吗?[y/N]: y
下载软件包:
ftp-0.17-67.el7.x86_64.rpm                          168 kB/s |  61 kB     00:00    
------------------------------------------------------------------------------------
总计                                                167 kB/s |  61 kB     00:00     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中      :                                                                 1/1 
  Installing  : ftp-0.17-67.el7.x86_64                                          1/1 
  验证        : ftp-0.17-67.el7.x86_64                                          1/1 

已安装:
  ftp-0.17-67.el7.x86_64                                                            

完毕!
[root@centos7 home]# 

尝试连接ftp服务器,使用匿名用户账号anonymous,密码为空,创建文件,验证效果符合预期;

[root@centos7 home]# ftp 127.0.0.1
Connected to 127.0.0.1 (127.0.0.1).
220 (vsFTPd 3.0.2)
Name (127.0.0.1:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (127,0,0,1,28,88).
150 Here comes the directory listing.
226 Directory send OK.
ftp> ls -l
227 Entering Passive Mode (127,0,0,1,34,42).
150 Here comes the directory listing.
226 Directory send OK.
ftp> mkdir files
257 "/pub/files" created
ftp> 

另一台主机上连接测试,可以正常创建文件

root@debian:/home/a# ftp 192.168.255.129
Connected to 192.168.255.129.
220 (vsFTPd 3.0.2)
Name (192.168.255.129:a): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxr-xr-x    3 14       0            4096 Aug 14 08:01 pub
226 Directory send OK.
ftp> cd pub
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxr-xr-x    2 14       50           4096 Aug 14 08:01 files
226 Directory send OK.
ftp> mkdir files2
257 "/pub/files2" created
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxr-xr-x    2 14       50           4096 Aug 14 08:01 files
drwxr-xr-x    2 14       50           4096 Aug 14 08:13 files2
226 Directory send OK.
ftp>

vsftpd服务程序的本地用户模式实现

相较于匿名开放模式,本地用户模式要更安全,而且配置起来也很简单。如果大家之前用的是匿名开放模式,现在就可以将它关了,然后开启本地用户模式。

1.修改配置文件,关闭第一行的匿名模式

[root@centos7 home]# cat -n /etc/vsftpd/vsftpd.conf
     1	anonymous_enable=NO
     2	local_enable=YES
     3	write_enable=YES
     4	local_umask=022
     5	dirmessage_enable=YES
     6	xferlog_enable=YES
     7	connect_from_port_20=YES
     8	xferlog_std_format=YES
     9	listen=NO
    10	listen_ipv6=YES
    11	
    12	pam_service_name=vsftpd
    13	userlist_enable=YES
    14	tcp_wrappers=YES

2.重启服务使配置生效

[root@centos7 home]# systemctl restart vsftpd

3.查看vsftp服务的本地用户名单

在本地用户名单上的用户,禁止登录,如果您确认在生产环境中使用root管理员不会对系统安全产生影响,只需按照上面的提示删除掉root用户名即可。也可以选择ftpusers和user_list文件中不存在的一个普通用户尝试登录FTP服务器。

Q:为什么同样是禁止用户登录的功能,却要制作两个一模一样的文件呢?

A:这个小玄机其实就在user_list文件上面。如果把上面主配置文件中userlist_deny的参数值改成NO,那么user_list列表就变成了强制白名单。它的功能与之前完全相反,只允许列表内的用户访问,拒绝其他人的访问。
bash
[root@centos7 home]# cd /etc/vsftpd/
[root@centos7 vsftpd]# tree
.
├── ftpusers
├── user_list
├── vsftpd.conf
├── vsftpd.conf_bak
└── vsftpd_conf_migrate.sh

0 directories, 5 files
[root@centos7 vsftpd]# cat ftpusers 
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@centos7 vsftpd]# cat 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
[root@centos7 vsftpd]# 

4.验证效果

通过本地普通用户user1在另一台客户端主机上登录ftp验证效果,
结果:符合预期,vsftp服务器上的普通用户可以正常登录,vsftp用户列表中存在的root用户无法登录;

root@debian:/home/a# ftp 192.168.255.129
Connected to 192.168.255.129.
220 (vsFTPd 3.0.2)
Name (192.168.255.129:a): user1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rw-r--r--    1 0        0              41 Apr 02 13:03 auth.smb
226 Directory send OK.
ftp> mkdir user1files
257 "/home/user1/user1files" created
ftp> rmdir user1files
250 Remove directory operation successful.

root@debian:/home/a# ftp 192.168.255.129
Connected to 192.168.255.129.
220 (vsFTPd 3.0.2)
Name (192.168.255.129:a): root    #root用户登录,验证是否被拒绝登录
530 Permission denied.
Login failed.
ftp> ls
530 Please login with USER and PASS.
ftp: bind: Address already in use
ftp>

vsftpd服务程序的虚拟用户模式实现

最后讲解的虚拟用户模式是这3种模式中最安全的一种认证模式,是专门创建出一个账号来登录FTP传输服务的,而且这个账号不能用于以SSH方式登录服务器。当然,因为它的安全性较之于前面两种模式有了提升,所以配置流程也会稍微复杂一些。

1.创建虚拟用户账户信息,并进行加密保存

创建用于进行FTP认证的用户数据库文件,其中奇数行为账户名,偶数行为密码。例如,分别创建a1和b2两个用户,密码为a11114,b11114

[root@centos7 vsftpd]# cat -n  vuser.list 
     1	a1
     2	a11114
     3	b2
     4	b11114
[root@centos7 vsftpd]# 

由于明文信息既不安全,也不符合让vsftpd服务程序直接加载的格式,因此需要使用db_load命令用哈希(hash)算法将原始的明文信息文件转换成数据库文件,并且降低数据库文件的权限(避免其他人看到数据库文件的内容),然后再把原始的明文信息文件删除。

[root@centos7 vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@centos7 vsftpd]# chmod 600 vuser.db
[root@centos7 vsftpd]# rm -rf vuser.list

2.创建虚拟用户模式的默认访问位置和权限

创建vsftpd服务程序用于存储文件的根目录以及用于虚拟用户映射的系统本地用户。vsftpd服务用于存储文件的根目录指的是,当虚拟用户登录后所访问的默认位置。
这里创建用户为virtual ,用户家目录为/var/ftproot,目录访问权限为755

[root@centos7 vsftpd]# useradd -d /var/ftproot -s /sbin/nologin virtual
[root@centos7 vsftpd]# ls -ld /var/ftproot/
drwx------ 2 virtual virtual 4096 814 16:43 /var/ftproot/
[root@centos7 vsftpd]# chmod -Rf 755 /var/ftproot/
[root@centos7 vsftpd]# ls -ld /var/ftproot/
drwxr-xr-x 2 virtual virtual 4096 814 16:43 /var/ftproot/

3.建立用于支持虚拟用户的PAM文件

通俗来讲,PAM是一组安全机制的模块,系统管理员可以用来轻易地调整服务程序的认证方式,而不必对应用程序进行任何修改。PAM采取了分层设计(应用程序层、应用接口层、鉴别模块层)的思想,其结构如图11-2所示。(图片来源:https://www.linuxprobe.com/wp-content/uploads/2021/03/PAM的分层设计结构.jpg)
在这里插入图片描述
新建一个用于虚拟用户认证的PAM文件vsftpd.vu,其中PAM文件内的“db=”参数为使用db_load命令生成的账户密码数据库文件的路径,但不用写数据库文件的后缀:

[root@centos7 vsftpd]# cat -n /etc/pam.d/vsftpd.vu 
     1	auth	required	pam_userdb.so db=/etc/vsftpd/vuser
     2	account	required	pam_userdb.so db=/etc/vsftpd/vuser

4.修改vsftp配置文件,和PAM进行关联

在vsftpd服务程序的主配置文件中通过pam_service_name参数将PAM认证文件的名称修改为vsftpd.vu。PAM作为应用程序层与鉴别模块层的连接纽带,可以让应用程序根据需求灵活地在自身插入所需的鉴别功能模块。当应用程序需要PAM认证时,则需要在应用程序中定义负责认证的PAM配置文件,实现所需的认证功能。

vim使用,指定行范围内(例如1到15行),删除前四个字符的实现–亲测有效
在命令行模式
:1,15normal! 4x

[root@centos7 vsftpd]# cat -n vsftpd.conf
     1	anonymous_enable=NO
     2	local_enable=YES
     3	write_enable=YES
     4	guest_enable=YES
     5	guest_username=virtual
     6	allow_writeable_chroot=YES
     7	local_umask=022
     8	dirmessage_enable=YES
     9	xferlog_enable=YES
    10	connect_from_port_20=YES
    11	xferlog_std_format=YES
    12	listen=NO
    13	listen_ipv6=YES
    14	pam_service_name=vsftpd.vu
    15	userlist_enable=YES

5.为虚拟用户配置各自的权限文件,在vsftp配置文件中关联

创建虚拟用户权限目录,配置每个用户的权限清单文件

[root@centos7 vsftpd]# pwd
/etc/vsftpd
[root@centos7 vsftpd]# mkdir vuser_dir
[root@centos7 vuser_dir]# vim b2
[root@centos7 vuser_dir]# cat -n b2
     1	anon_upload_enable=YES
     2	anon_mkdir_write_enable=YES
     3	anon_other_write_enable=YES
[root@centos7 vuser_dir]# cat -n a1
[root@centos7 vuser_dir]# 

然后再次修改vsftpd主配置文件,通过添加user_config_dir参数来定义这两个虚拟用户不同权限的配置文件所存放的路径,在最后一行

[root@centos7 vsftpd]# echo "user_config_dir=/etc/vsftpd/vuser_dir" >> /etc/vsftpd/vsftpd.conf
[root@centos7 vsftpd]# cat -n vsftpd.conf
     1	anonymous_enable=NO
     2	local_enable=YES
     3	write_enable=YES
     4	guest_enable=YES
     5	guest_username=virtual
     6	allow_writeable_chroot=YES
     7	local_umask=022
     8	dirmessage_enable=YES
     9	xferlog_enable=YES
    10	connect_from_port_20=YES
    11	xferlog_std_format=YES
    12	listen=NO
    13	listen_ipv6=YES
    14	pam_service_name=vsftpd.vu
    15	userlist_enable=YES
    16	user_config_dir=/etc/vsftpd/vuser_dir

6.重启vsftp服务使配置生效

pass

7.验证两个虚拟用户不同权限的登录效果

虚拟用户a1,正常登录无法创建文件

root@debian:/home/a# ftp 192.168.255.129
Connected to 192.168.255.129.
220 (vsFTPd 3.0.2)
Name (192.168.255.129:a): a1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
226 Directory send OK.
ftp> mkdir abcfiles
550 Permission denied.
ftp> 

虚拟用户b2,可以创建文件,文件的挂载点在步骤2虚拟用户模式的默认访问位置中

root@debian:/home/a# ftp 192.168.255.129
Connected to 192.168.255.129.
220 (vsFTPd 3.0.2)
Name (192.168.255.129:a): b2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/"
ftp> mkdir b2files
257 "/b2files" created
ftp> rmdir b2files
250 Remove directory operation successful.
ftp> mkdir b2files1
257 "/b2files1" created
ftp> 


[root@centos7 vuser_dir]# cd /var/ftproot/
[root@centos7 ftproot]# ls
b2files1
[root@centos7 ftproot]# ls -l
总用量 4
drwx------ 2 virtual virtual 4096 814 17:20 b2files1
[root@centos7 ftproot]# 

快速回顾:
在使用不同的方式登录文件传输服务器后,默认所在的位置,
在这里插入图片描述
–END提示–

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值