Linux巩固篇011-Linux vsftpd 服务传输文件

简介: 纸上得来终觉浅,绝知此事要躬行

前言

身为一个三年的运维工程师,从开发转测开再转运维,都是不断学习的过程,未必开发才是最优秀的,找到适合自己的职业不断深耕,你也会在自己的行业大放光彩,本系列依照《Linux就该这么学》系列随书学习练习操作,将一些课本上不顺畅的地方,全部以最简方式免费开源展示给大家,资源大家可以自行百度,也希望大家多关注刘遄老师的第二版关于centos8的丛书,学习最前沿的Linux相关技术。

常用命令汇总

文件传输协议

FTP 是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用 20、21 号端口,其中端口 20(数据端口)用于进行数据传输,端口 21(命令端口)用于接受客户端 发出的相关 FTP 命令与参数。

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

vsftpd服务部署

1.安装 vsftpd 服务

[root@www ~]# yum install vsftpd

2.清空防火墙规则(或者直接关闭防火墙:systemctl stop iptables

[root@www ~]# iptables -F

[root@www ~]# service iptables save

3.修改vsftpd配置文件

[root@www ~]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak

[root@www ~]# grep -v "#" /etc/vsftpd/vsftpd.conf_bak > /etc/vsftpd/vsftpd.conf

[root@www ~]# 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

参数解析:

image.png

vsftpd 服务程序

匿名开放模式:是一种最不安全的认证模式,任何人都可以无需密码验证而直接登录 到 FTP 服务器。

本地用户模式:是通过 Linux 系统本地的账户密码信息进行认证的模式

虚拟用户模式:是这三种模式中最安全的一种认证模式,它需要为 FTP 服务单独 建立用户数据库文件,虚拟出用来进行口令验证的账户信息,而这些账户信息在 服务器系统中实际上是不存在的,仅供 FTP 服务程序进行认证使用。

我们首先手 动安装这个 ftp 客户端工具,以便在后续实验中查看结果(安装在客户端,另外一台机器上)。

[root@www ~]# yum install ftp

匿名开放模式

image.png

1.修改配置文件

[root@www ~]# vim /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

2.重启vsftpd服务

[root@www ~]# systemctl restart vsftpd

[root@www ~]# systemctl enable vsftpd

Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

3.修改服务端权限

[root@www ~]# ls -ld /var/ftp/pub

drwxr-xr-x 2 root root 6 Jun  9  2021 /var/ftp/pub

[root@www ~]# chown -Rf ftp /var/ftp/pub

4.在客户端访问服务端

[root@localhost ~]# ftp 192.168.227.130

Connected to 192.168.227.130 (192.168.227.130).

220 (vsFTPd 3.0.2)

Name (192.168.227.130: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> mkdir files

257 "/pub/files" created

ftp> rename files database

350 Ready for RNTO.

250 Rename successful.

ftp> rmdir database

250 Remove directory operation successful.

ftp> exit

221 Goodbye.

本地用户模式

image.png

1.修改配置文件

[root@www ~]# vim /etc/vsftpd/vsftpd.conf

删掉上边配的五项,然后把下边的从YES替换成NO如下配置:

anonymous_enable=NO

2.重启vsftpd服务

[root@www ~]# systemctl restart vsftpd

3.查看被禁止名单,只要在这两个表里的均无法通过远程访问

[root@www ~]# cat /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

[root@www ~]# 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

4.测试

[root@localhost ~]# ftp 192.168.227.130

Connected to 192.168.227.130 (192.168.227.130).

220 (vsFTPd 3.0.2)

Name (192.168.227.130:root): root

530 Permission denied.

Login failed.

虚拟用户模式

1.创建用于进行 FTP 认证的用户数据库文件

[root@www ~]# cd /etc/vsftpd/

[root@www vsftpd]# vim vuser.list

做两个用户,密码均为 redhat

zhangsan

redhat

lisi

redhat

用 db_load 命令用哈希(hash)算法将原始的明文信息文件转换成数据库文件,再删除相关明文信息文件

[root@www vsftpd]# db_load -T -t hash -f vuser.list vuser.db

[root@www vsftpd]# file vuser.db

vuser.db: Berkeley DB (Hash, version 9, native byte-order)

[root@www vsftpd]# chmod 600 vuser.db

[root@www vsftpd]# rm -f vuser.list

2.创建 vsftpd 服务程序用于存储文件的根目录以及虚拟用户映射的系统本地用户

[root@www vsftpd]# useradd -d /var/ftproot -s /sbin/nologin virtual

[root@www vsftpd]# ls -ld /var/ftproot/

drwx------ 3 virtual virtual 78 Jun 19 23:40 /var/ftproot/

[root@www vsftpd]# chmod -Rf 755 /var/ftproot/

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

PAM 是一组安全机制的模块,系统管理员可以用来轻易地调整服务程序的认 证方式,而不必对应用程序进行任何修改。PAM 采取了分层设计(应用程序层、应用接口层、 鉴别模块层)的思想,其结构如图

image.png

[root@www vsftpd]# vim /etc/pam.d/vsftpd.vu

auth required pam_userdb.so db=/etc/vsftpd/vuser  

account required pam_userdb.so db=/etc/vsftpd/vuser

4.在 vsftpd 服务程序的主配置文件中通过 pam_service_name 参数将 PAM 认证 文件的名称修改为 vsftpd.vu

image.png

[root@www vsftpd]# vim /etc/vsftpd/vsftpd.conf

 1 anonymous_enable=NO

 2 local_enable=YES

 3 write_enable=YES

 4 guest_username=virtual

 5 allow_writeable_chroot=YES

 6 write_enable=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 tcp_wrappers=YES

5.为虚拟用户设置不同的权限

允许张三上传、创建、 修改、查看、删除文件,只允许李四查看文件

[root@www vsftpd]# mkdir /etc/vsftpd/vusers_dir/

[root@www vsftpd]# cd /etc/vsftpd/vusers_dir/

[root@www vusers_dir]# touch lisi

[root@www vusers_dir]# vim zhangsan

anon_upload_enable=YES  

anon_mkdir_write_enable=YES  

anon_other_write_enable=YES

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

[root@www vusers_dir]# vim /etc/vsftpd/vsftpd.conf

在结尾添加相关路径

17 user_config_dir=/etc/vsftpd/vusers_dir

重启vsftpd服务

[root@www vusers_dir]# systemctl restart vsftpd

6.客户端测试服务端

(小插曲,我通过客户端访问的时候一直报错,不知道哪的原因查了半天告诉我是有空格,在配置文件一顿删,弄完了还报错,一直排查,发现是zhangsan下边的配置文件里的空格,这个和主配置文件不一样是单独给zhangsan设置的在/etc/vsftpd/vusers_dir目录下,真服了)

zhangsan(加了权限)

[root@localhost ~]# ftp 192.168.227.130

Connected to 192.168.227.130 (192.168.227.130).

220 (vsFTPd 3.0.2)

Name (192.168.227.130:root): zhangsan

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> mkdir files

257 "/files" created

ftp> rename files database

350 Ready for RNTO.

250 Rename successful.

ftp> rmdir database

250 Remove directory operation successful.

ftp> exit

221 Goodbye.

李四(无任何权限)

Connected to 192.168.227.130 (192.168.227.130).

220 (vsFTPd 3.0.2)

Name (192.168.227.130:root): lisi

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> mkdir files

550 Permission denied.

ftp> exit

221 Goodbye.

简单文件传输协议

TFTP 在传输文件时采用的是 UDP 协议,占用的端口号为 69,因此 文件的传输过程也不像FTP 协议那样可靠。但是,因为 TFTP 不需要客户端的权限认证, 也就减少了无谓的系统和网络带宽消耗,因此在传输琐碎(trivial)不大的文件时,效率 更高。

[root@www vusers_dir]# yum -y install tftp-server tftp

(尽量yum的时候加-y不用判断,直接下载)

1.修改tftp配置文件,改为disable=no,这个主要是加一个日志记录+网络服务

[root@www ~]# vim /etc/xinetd.d/tftp

# default: off

# description: The tftp server serves files using the trivial file transfer \

#       protocol.  The tftp protocol is often used to boot diskless \

#       workstations, download configuration files to network-aware printers, \

#       and to start the installation process for some operating systems.

service tftp

{

       socket_type             = dgram

       protocol                = udp

       wait                    = yes

       user                    = root

       server                  = /usr/sbin/in.tftpd

       server_args             = -s /var/lib/tftpboot

       disable                 = no

       per_source              = 11

       cps                     = 100 2

       flags                   = IPv4

}

2.重启+防火墙开端口,如果防火墙闭了就不用后边两条

[root@www ~]# systemctl restart tftp

[root@www ~]# systemctl enable tftp

Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.

[root@www ~]# firewall-cmd --permanent --add-port=69/udp  

[root@www ~]# firewall-cmd --reload

tftp常用命令及作用

image.png

3.本机自测(在其他目录下通过tftp把tftp根目录文件挪到当前目录下)

[root@www ~]# echo "i love linux" > /var/lib/tftpboot/readme.txt

[root@www ~]# tftp 192.168.227.130

tftp> get readme.txt

tftp> quit

[root@www ~]# ll

-rw-r--r--  1 root root    0 Jun 20 01:15 readme.txt

4.客户端连服务端

[root@localhost ~]# tftp 192.168.227.130

tftp> get readme.txt

tftp>quit

[root@localhost ~]# ll

-rw-r--r--  1 root root    0 Jun 20 01:23 readme.txt

结语

简问简答

1.简述 FTP 协议的功能作用以及所占用的端口号。

答:FTP 是一种在互联网中进行文件传输的协议,默认使用 20、21 号端口,其中端口 20 (数据端口)用于进行数据传输,端口 21(命令端口)用于接受客户端发起的相关 FTP 命 令与参数。

2.vsftpd 服务程序提供的三种用户认证模式各自有什么特点?

答:匿名开放模式是任何人都可以无需密码认证即可直接登录到 FTP 服务器的验证方式; 本地用户模式是通过系统本地的账户密码信息登录到 FTP 服务器的认证方式;虚拟用户 模式是通过创建独立的 FTP 用户数据库文件来进行认证并登录到 FTP 服务器的认证方式, 相较来说它也是最安全的认证模式。

3.使用匿名开放模式登录到一台用 vsftpd 服务程序部署的 FTP 服务器上时,默认的 FTP根 目录是什么?

答:使用匿名开放模式登录后的 FTP 根目录是/var/ftp 目录,该目录内默认还会有一个名 为 pub 的子目录。

4.简述 PAM 的功能作用。

答:PAM 是一组安全机制的模块(插件),系统管理员可以用来轻易地调整服务程序的认证方式,而不必对应用程序进行过多修改。

5.使用虚拟用户模式登录 FTP 服务器的所有用户的权限都是一样的吗?

答:不一定,可以通过分别定义用户权限文件来为每一位用户设置不同的权限。

6.TFTP 协议与 FTP 协议有什么不同?

答:TFTP 协议提供不复杂、开销不大的文件传输服务(可将其当作 FTP 协议的简化版本)

如果想根据教程实践的朋友们可以通过阿里云ecs服务器免费试用和低价购买,入口如下

入口一:新人免费试用

入口二:大学生免费试用

入口三:低价服务器购买

入口四:低价服务器购买2

入口五:建站特惠购买

目录
相关文章
|
2天前
|
人工智能 Linux 开发工具
linux 对文件内容的查看、归档 及 vim基本操作
linux 对文件内容的查看、归档 及 vim基本操作
|
9天前
|
存储 数据挖掘 Linux
在 Linux 中查找目录中最大文件用什么,你知道吗
【5月更文挑战第23天】在 Linux 中查找目录中最大文件,可以使用 `du` 命令结合 `sort`,`find` 命令搭配 `xargs` 和 `sort`,或编写 Python 脚本。例如:`du -a /path | sort -nr | head -n 1` 或 `find /path -type f -print0 | xargs -0 du -h | sort -nr | head -n 1`。这些方法适用于服务器管理、数据分析和文件清理等场景,注意文件权限、目录深度和文件系统类型可能影响结果。
26 1
|
11天前
|
Linux
linux 如何删除文件中的空格和换行符号并保存到新文件中
linux 如何删除文件中的空格和换行符号并保存到新文件中
17 0
|
11天前
|
Linux C语言 Windows
linux基本指令总结--文件和目录(一)
linux基本指令总结--文件和目录(一)
|
16天前
|
Linux 网络安全
linux/服务器使用scp将一个服务器文件转移到另一个服务器上
linux/服务器使用scp将一个服务器文件转移到另一个服务器上
48 3
|
17天前
|
Linux Go
linux ls -la文件信息含义
linux ls -la文件信息含义
21 1
|
17天前
|
Ubuntu Linux Shell
mc实现目录同步并封装成Linux服务形式
mc实现目录同步并封装成Linux服务形式
262 1
|
10天前
|
Shell Linux 程序员
【Linux】Shell 命令以及运行原理
【Linux】Shell 命令以及运行原理
|
2天前
|
Linux 应用服务中间件 nginx
linux小技巧: 可以补全命令 别名永久有效
linux小技巧: 可以补全命令 别名永久有效
|
2天前
|
缓存 关系型数据库 MySQL
linux 基本知识与命令
linux 基本知识与命令