文件传输协议(File Transfer Protocol,FTP)部署


下面开始vsftpd 服务 程序三种认证模式(匿名开放模式、本地用户模式、虚拟用户模式)的配置方法

FTP 是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用 20、21 号端口,其中端口 20(数据端口)用于进行数据传输,端口 21(命令端口)用于接受客户端 发出的相关 FTP 命令与参数。FTP 服务器普遍部署于内网中,具有容易搭建、方便管理的特 点。而且有些 FTP 客户端工具还可以支持文件的多点下载以及断点续传技术。

vsftpd(very secure ftp daemon,非常安全的 FTP 守护进程)是一款运行在 Linux 操作系 统上的 FTP 服务程序,不仅完全开源而且免费,此外,还具有很高的安全性、传输速度,以 及支持虚拟用户验证等其他 FTP 服务程序不具备的特点。

安装vsftpd

yum install vsftpd -y

备份配置文件

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.back

过滤掉配置文件的注释信息

grep -v "#" /etc/vsftpd/vsftpd.conf.back > /etc/vsftpd/vsftpd.conf

配置文件内容如下

[root@k8s-master01 ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES            #是否允许匿名用户访问
local_enable=YES
write_enable=YES
local_umask=022              #本地用户上传文件的 umask 值
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

 

ftp 是 Linux 系统中以命令行界面的方式来管理 FTP 传输服务的客户端工具。我们首先手 动安装这个 ftp 客户端工具

yum install ftp -y 

 

匿名开放模式

vsftpd 服务程序默认开启了匿名开放模式,我们需要做的就是开放匿名用户的上传、下 载文件的权限,以及让匿名用户创建、删除、更名文件的权限。

修改配置文件如下

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
anno_umask=022             #匿名用户上传文件的 umask 值 anno_upload_enable=YES #允许匿名用户上传文件 anno_mkdir_write_enable=YES #允许匿名用户创建目录 anno_other_write_enable=YES #允许匿名用户修改目录名称或删除目录 

 

然后赋予/var/ftp目录下pub目录权限

chown -R  ftp  put

测试

[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): anonymous
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
250 Directory successfully changed.
ftp> mkdir app
257 "/pub/app" created
ftp> ls
227 Entering Passive Mode (10,122,138,244,127,159).
150 Here comes the directory listing.
drwx------    2 14       0               6 Sep 29 14:55 aa
drwx------    2 14       50              6 Sep 29 15:05 app
drwxr-xr-x    2 14       0               6 Sep 29 14:38 files
226 Directory send OK.
ftp> 

 

配置本地用户模式

修改配置文件

drwx------ 2 ftp ftp 6 Sep 29 23:01 cdd
[root@k8s-master01 ftp]# vim /etc/vsftpd/vsftpd.conf
[root@k8s-master01 ftp]# systemctl restart vsftpd
[root@k8s-master01 ftp]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
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

#no_anon_password=YES
#anon_root=/var/ftp
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
#anon_umask=077

重启服务

测试

[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): root
530 Permission denied.
Login failed.
ftp> exit
221 Goodbye.
[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): liutao
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.
ftp> exit
221 Goodbye.
[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): lili
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
550 Failed to change directory.
ftp> ls
227 Entering Passive Mode (10,122,138,244,182,97).
150 Here comes the directory listing.
226 Directory send OK.
ftp> ls
227 Entering Passive Mode (10,122,138,244,25,153).
150 Here comes the directory listing.
226 Directory send OK.
ftp> cd pub
550 Failed to change directory.
ftp> mkdir files
257 "/home/lili/files" created
ftp> ls
227 Entering Passive Mode (10,122,138,244,213,198).
150 Here comes the directory listing.
drwxr-xr-x    2 1014     1017            6 Sep 29 15:11 files
226 Directory send OK.
ftp> 

上面测试显示,root无法登陆,因为vsftpd 服务程序为了保证服务器的安全性而默认禁止了 root 管理员和大多数 系统用户的登录行为,这样可以有效地避免黑客通过 FTP 服务对 root 管理员密码进行暴力破 解。如果您确认在生产环境中使用 root 管理员不会对系统安全产生影响,只需按照上面的提 示删除掉 root 用户名即可。我们也可以选择 ftpusers 和 user_list 文件中没有的一个普通用户 尝试登录 FTP 服务器

[root@k8s-master01 ftp]# cat /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
[root@k8s-master01 ftp]# 

这上面的用户都不能登陆,测试的liutao用户不存在,lili用户存在。在采用本地用户模式登录 FTP 服务器后,默认访问的是该用户的家目录,也就是说,访 问的是/home/linuxprobe 目录。而且该目录的默认所有者、所属组都是该用户自己,因此不存 在写入权限不足的情况

 

下面配置虚拟用户模式

配置文件修改为:

anonymous_enable=NO
local_enable=YES
write_enable=YES
guest_enable=YES
guest_username=vsftpd
allow_writeable_chroot=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.vu
userlist_enable=YES
tcp_wrappers=YES

创建用于进行 FTP 认证的用户数据库文件,其中奇数行为账户名,偶数行为密 码。例如,我们分别创建出 zhangsan 和 lisi 两个用户,密码均为 redhat

[root@linuxprobe ~]# cd /etc/vsftpd/
[root@linuxprobe vsftpd]# vim vuser.list
zhangsan
redhat
lisi
redhat

 

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

[root@linuxprobe vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@linuxprobe vsftpd]# file vuser.db
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@linuxprobe vsftpd]# chmod 600 vuser.db
[root@linuxprobe vsftpd]# rm -f vuser.list

 

新建一个用于虚拟用户认证的 PAM 文件 vsftpd.vu,其中 PAM 文件内的“db=”参数为 使用 db_load 命令生成的账户密码数据库文件的路径,但不用写数据库文件的后缀:

[root@linuxprobe ~]# vim /etc/pam.d/vsftpd.vu
auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=/etc/vsftpd/vuser

 

现在可以在客户端测试

[root@localhost ~]# ftp 192.168.172.249
Connected to 192.168.172.249 (192.168.172.249).
220 (vsFTPd 3.0.2)
Name (192.168.172.249:root): lisi
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> ll
?Invalid command
ftp> ls
227 Entering Passive Mode (192,168,172,249,25,1).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/pub"
ftp> 

 

为虚拟用户设置不同的权限。虽然账户 zhangsan 和 lisi 都是用于 vsftpd 服务程 序认证的虚拟账户,但是我们依然想对这两人进行区别对待。比如,允许张三上传、创建、修 改、查看、删除文件,只允许李四查看文件。这可以通过 vsftpd 服务程序来实现。只需新建 一个目录,在里面分别创建两个以 zhangsan 和 lisi 命名的文件,其中在名为 zhangsan 的文件 中写入允许的相关权限(使用匿名用户的参数)

[root@linuxprobe ~]# mkdir /etc/vsftpd/vusers_dir/
[root@linuxprobe ~]# cd /etc/vsftpd/vusers_dir/
[root@linuxprobe vusers_dir]# touch lisi
[root@linuxprobe vusers_dir]# vim zhangsan
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

然后再次修改 vsftpd 主配置文件,通过添加 user_config_dir 参数来定义这两个虚拟用户 不同权限的配置文件所存放的路径。为了让修改后的参数立即生效,需要重启 vsftpd 服务程 序并将该服务添加到开机启动项中:

user_config_dir=/etc/vsftpd/vusers_dir

systemctl restart vsftpd

systemctl enable vsftpd


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM