linux 简单记录10--使用 vsftpd 服务传输文件(三种模式--匿名模式,本地用户模式,虚拟账号模式),ftp

使用 vsftpd 服务传输文件

FTP 是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用 20、 21号端口,其中端口 20(数据端口)用于进行数据传输,端口 21(命令端口)用于接受客户端
发出的相关 FTP 命令与参数。
FTP 服务器是按照 FTP 协议在互联网上提供文件存储和访问服务的主机, FTP 客户端则是向服务器发送连接请求,以建立数据传输链路的主机。
两种工作模式:
主动模式:FTP服务器主动向客户端发起连接请求
被动模式:FTP服务器等待客户端发起连接请求(FTP的默认工作模式)
vsftpd(very secure ftp daemon,非常安全的 FTP 守护进程)是一款运行在 Linux 操作系统上的 FTP 服务程序,不仅完全开源而且免费,此外,还具有很高的安全性、传输速度,以
及支持虚拟用户验证等其他 FTP 服务程序不具备的特点。

[root@iscsi ~]# yum install vsftpd
主配置文件 /etc/vsftpd/vsftpd.conf
[root@iscsi vsftpd]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak
[root@iscsi vsftpd]# grep -v "#" /etc/vsftpd/vsftpd.conf_bak > /etc/vsftpd/vsftpd.conf
[root@iscsi vsftpd]# 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
vsftpd 服务程序常用的参数以及作用
listen=[YES|NO] 是否以独立运行的方式监听服务
listen_address=IP 地址 设置要监听的 IP 地址
listen_port=21 设置 FTP 服务的监听端口
download_enable=[YES|NO] 是否允许下载文件
userlist_enable=[YES|NO]
userlist_deny=[YES|NO] 设置用户列表为“允许”还是“禁止”操作
max_clients=0 最大客户端连接数, 0 为不限制
max_per_ip=0 同一 IP 地址的最大连接数, 0 为不限制
anonymous_enable=[YES|NO] 是否允许匿名用户访问
anon_upload_enable=[YES|NO] 是否允许匿名用户上传文件
anon_umask=022 匿名用户上传文件的 umask 值
anon_root=/var/ftp 匿名用户的 FTP 根目录
anon_mkdir_write_enable=[YES|NO] 是否允许匿名用户创建目录
anon_other_write_enable=[YES|NO] 是否开放匿名用户的其他写入权限(包括重命名、删除等操作权限)
anon_max_rate=0 匿名用户的最大传输速率(字节/秒), 0 为不限制
local_enable=[YES|NO] 是否允许本地用户登录 FTP
local_umask=022 本地用户上传文件的 umask 值
local_root=/var/ftp 本地用户的 FTP 根目录
chroot_local_user=[YES|NO] 是否将用户权限禁锢在 FTP 目录,以确保安全
local_max_rate=0 本地用户最大传输速率(字节/秒), 0 为不限制

vsftpd 作为更加安全的文件传输的服务程序,允许用户以三种认证模式登录到 FTP 服务器上。
匿名开放模式:是一种最不安全的认证模式,任何人都可以无需密码验证而直接登录到 FTP 服务器。
本地用户模式:是通过 Linux 系统本地的账户密码信息进行认证的模式,相较于匿名开放模式更安全,而且配置起来也很简单。
虚拟用户模式:是这三种模式中最安全的一种认证模式,它需要为 FTP 服务单独
建立用户数据库文件,虚拟出用来进行口令验证的账户信息,而这些账户信息在
服务器系统中实际上是不存在的,仅供 FTP 服务程序进行认证使用。

[root@iscsi vsftpd]# yum install ftp -y
[root@iscsi vsftpd]# rpm -qa|grep ftp
vsftpd-3.0.2-27.el7.x86_64
ftp-0.17-67.el7.x86_64

1 匿名模式

可以向匿名用户开放的权限参数以及作用

anonymous_enable=YES 允许匿名访问模式
anon_umask=022 匿名用户上传文件的 umask 值
anon_upload_enable=YES 允许匿名用户上传文件
anon_mkdir_write_enable=YES 允许匿名用户创建目录
anon_other_write_enable=YES 允许匿名用户修改目录名称或删除目录
[root@iscsi ~]# vim /etc/vsftpd/vsftpd.conf
[root@iscsi ~]# systemctl restart vsftpd
[root@iscsi ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@iscsi ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_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@iscsi ~]# ftp 10.15.7.20
Connected to 10.15.7.20 (10.15.7.20).
220 (vsFTPd 3.0.2)
Name (10.15.7.20: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 yhqfile ##创建文件夹失败
550 Create directory operation failed.
[root@iscsi pub]# ls -ld /var/ftp/pub/ ##修改目录权限
drwxr-xr-x 2 root root 6 Apr  1 12:55 /var/ftp/pub/
[root@iscsi pub]# chown -Rf ftp /var/ftp/pub/
[root@iscsi pub]# ls -ld /var/ftp/pub/
drwxr-xr-x 2 ftp root 6 Apr  1 12:55 /var/ftp/pub/
再次创建文件夹,成功
ftp> cd pub
250 Directory successfully changed.
ftp> mkdir files
257 "/pub/files" created

2 本地用户模式
本地用户模式使用的权限参数以及作用

anonymous_enable=NO 禁止匿名访问模式
local_enable=YES 允许本地用户模式
write_enable=YES 设置可写权限
local_umask=022 本地用户模式创建文件的 umask 值
userlist_enable=YES 启用“禁止用户名单”,名单文件为 ftpusers 和 user_list
userlist_deny=YES 开启用户作用名单文件功能
[root@iscsi ~]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_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
userlist_enable=YES
userlist_deny=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@iscsi ~]# systemctl restart vsftpd
[root@iscsi ~]# systemctl is-enabled named.service ##查看服务是否开机自启动
disabled
[root@iscsi ~]# systemctl is-enabled vsftpd.service 
enabled
[root@iscsi ~]# systemctl list-units --type service --all ##显示所有的服务状态
[root@iscsi ~]# systemctl list-unit-files |grep enabled ##查看启动成功的服务列表
systemctl list-dependencies --after name.service ##列出在指定服务之前启动的服务(依赖)
systemctl list-dependencies --before name.service ##列出在指定服务之后启动的服务(被依赖)
[root@iscsi ~]# systemctl status vsftpd.service 
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled) ##Loaded:关于服务是否已经加载的信息,文件的绝对路径以及是否被启用的注释
   Active: active (running) since Mon 2020-07-13 09:51:48 CST; 9min ago ##Active:服务是否正在运行,然后是启动时间信息
  Process: 86994 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) #Process:进程额外信息。
 Main PID: 86997 (vsftpd) #Main PID:服务主进程pid。
   CGroup: /system.slice/vsftpd.service #CGroup:Control Groups额外信息。
           └─86997 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

Jul 13 09:51:48 iscsi systemd[1]: Starting Vsftpd ftp daemon...
Jul 13 09:51:48 iscsi systemd[1]: Started Vsftpd ftp daemon.
[root@iscsi ~]# ll /usr/lib/systemd/system/named* ##查看服务路径
-rw-r--r-- 1 root root 1103 Jun  1 23:26 /usr/lib/systemd/system/named-chroot.service
-rw-r--r-- 1 root root  385 Jun  1 23:26 /usr/lib/systemd/system/named-chroot-setup.service
-rw-r--r-- 1 root root  824 Jun  1 23:26 /usr/lib/systemd/system/named.service
-rw-r--r-- 1 root root  121 Jun  1 23:26 /usr/lib/systemd/system/named-setup-rndc.service
[root@iscsi ~]# more /usr/lib/systemd/system/named.service 
[Unit]
Description=Berkeley Internet Name Domain (DNS)
Wants=nss-lookup.target
Wants=named-setup-rndc.service
Before=nss-lookup.target
After=network.target
After=named-setup-rndc.service

[Service]
Type=forking
Environment=NAMEDCONF=/etc/named.conf
EnvironmentFile=-/etc/sysconfig/named
Environment=KRB5_KTNAME=/etc/named.keytab
PIDFile=/run/named/named.pid

ExecStartPre=/bin/bash -c 'if [ ! "$DISABLE_ZONE_CHECKING" == "yes" ]; then /usr/sbin/named-checkconf -z "$NAMEDCONF"; else echo "Checking of zone files is disabled"; fi'
ExecStart=/usr/sbin/named -u named -c ${NAMEDCONF} $OPTIONS

ExecReload=/bin/sh -c '/usr/sbin/rndc reload > /dev/null 2>&1 || /bin/kill -HUP $MAINPID'

ExecStop=/bin/sh -c '/usr/sbin/rndc stop > /dev/null 2>&1 || /bin/kill -TERM $MAINPID'

PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@iscsi ~]# ftp 10.15.7.20
Connected to 10.15.7.20 (10.15.7.20).
220 (vsFTPd 3.0.2)
Name (10.15.7.20:root): root
530 Permission denied.
Login failed.
ftp> ls
530 Please login with USER and PASS.
Passive mode refused.
ftp> exit
221 Goodbye.
[root@iscsi ~]# more /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
[root@iscsi ~]# more /etc/vsftpd/ftpusers 
# Users that are not allowed to login via ftp
root
bin
[root@iscsi ~]# id hong
uid=1006(hong) gid=1006(hong) groups=1006(hong)
[root@iscsi ~]# passwd hong
Changing password for user hong.
[root@iscsi ~]# ftp 10.15.7.20
Connected to 10.15.7.20 (10.15.7.20).
220 (vsFTPd 3.0.2)
Name (10.15.7.20:root): hong
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir files
257 "/home/hong/files" created
ftp> rename files database
350 Ready for RNTO.
250 Rename successful.
ftp> ls
227 Entering Passive Mode (10,15,7,20,131,49).
150 Here comes the directory listing.
drwxr-xr-x    2 1006     1006            6 Jul 13 02:45 database
226 Directory send OK.
ftp> cd database
250 Directory successfully changed.
[root@iscsi ~]# ll /home/hong/
total 0
drwxr-xr-x 2 hong hong 6 Jul 13 10:45 database

3 虚拟用户模式
--1 创建ftp认证的用户数据库文件,奇数行为账户名,偶数行为密码

[root@iscsi ~]# cd /etc/vsftpd/
[root@iscsi vsftpd]# vim vuser.list
yang
12345
tang
54321
[root@iscsi vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@iscsi vsftpd]# file vuser.db
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@iscsi vsftpd]# chmod 600 vuser.db
[root@iscsi vsftpd]# rm -rf vuser.list

--2 创建 vsftpd 服务程序用于存储文件的根目录以及虚拟用户映射的系统本地用户。 FTP 服务用于存储文件的根目录指的是,当虚拟用户登录后所访问的默认位置。

[root@iscsi vsftpd]# useradd -d /var/ftproot -s /sbin/nologin virtual
[root@iscsi vsftpd]# ls -ld /var/ftproot/
drwx------ 3 virtual virtual 78 Jul 13 10:53 /var/ftproot/
[root@iscsi vsftpd]# chmod -Rf 755 /var/ftproot/

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

PAM(可插拔认证模块)是一种认证机制,通过一些动态链接库和统一的 API 把系统提供的服务与认证方式分开,使得系统管理员可以根据需求灵活调整服务程序的不同认证方式。
[root@iscsi 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,
PAM 作为应用程序层与鉴别模块层的连接纽带,可以让应用程序根据需求灵活地在自身插入所需的鉴别功能模块。
利用 PAM 文件进行认证时使用的参数以及作用

anonymous_enable=NO 禁止匿名开放模式
local_enable=YES 允许本地用户模式
guest_enable=YES 开启虚拟用户模式
guest_username=virtual 指定虚拟用户账户
pam_service_name=vsftpd.vu 指定 PAM 文件
allow_writeable_chroot=YES 允许对禁锢的 FTP 根目录执行写入操作,而且不拒绝用户的登录请求
[root@iscsi vsftpd]# vim /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
guest_enable=YES
guest_username=virtual
allow_writeable_chroot=YES

pam_service_name=vsftpd.vu
userlist_enable=YES
tcp_wrappers=YES

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

[root@iscsi vsftpd]# mkdir /etc/vsftpd/vusers_dir/
[root@iscsi vsftpd]# cd /etc/vsftpd/vusers_dir/
[root@iscsi vusers_dir]# touch yang
[root@iscsi vusers_dir]# vim tang
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
[root@iscsi vusers_dir]# vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vusers_dir
[root@iscsi vusers_dir]# ftp 10.15.7.20
Connected to 10.15.7.20 (10.15.7.20).
220 (vsFTPd 3.0.2)
Name (10.15.7.20:root): tang
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> cd files
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (10,15,7,20,60,177).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/files"
ftp> exit
221 Goodbye.
[root@iscsi vusers_dir]# ftp 10.15.7.20
Connected to 10.15.7.20 (10.15.7.20).
220 (vsFTPd 3.0.2)
Name (10.15.7.20:root): yang
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir files ##创建失败,没有权限
550 Create directory operation failed.
ftp> exit
221 Goodbye.
[root@iscsi vsftpd]# ll
total 36
-rw------- 1 root root   125 Apr  1 12:55 ftpusers
-rw------- 1 root root   361 Apr  1 12:55 user_list
-rw-r--r-- 1 root root   489 Jul 13 11:00 vsftpd.conf
-rw------- 1 root root  5116 Apr  1 12:55 vsftpd.conf_bak
-rwxr--r-- 1 root root   338 Apr  1 12:55 vsftpd_conf_migrate.sh
-rw------- 1 root root 12288 Jul 13 10:52 vuser.db
drwxr-xr-x 2 root root    30 Jul 13 10:59 vusers_dir
[root@iscsi ~]# ll /var/ftproot/
total 0
drwxr-xr-x 2 virtual virtual 6 Jul 13 11:01 files
[root@iscsi vusers_dir]# vim tang
local_root=/tmp/tang_ftp/ ##修改tang用户的ftp文件目录
write_enable=YES
[root@iscsi vusers_dir]# systemctl restart vsftpd
[root@iscsi vsftpd]# mkdir /tmp/tang_ftp/
[root@iscsi vsftpd]# cd /tmp/tang_ftp/
[root@iscsi tang_ftp]# ll
total 0
[root@iscsi tang_ftp]# touch 1.txt
[root@iscsi tang_ftp]# touch 2.txt 3.txt
[root@iscsi tang_ftp]# ftp 10.15.7.20
Connected to 10.15.7.20 (10.15.7.20).
220 (vsFTPd 3.0.2)
Name (10.15.7.20:root): tang
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (10,15,7,20,26,239).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 Jul 13 03:13 1.txt
-rw-r--r--    1 0        0               0 Jul 13 03:14 2.txt
-rw-r--r--    1 0        0               0 Jul 13 03:14 3.txt
226 Directory send OK.

简单文件传输协议
简单文件传输协议(Trivial File Transfer Protocol, TFTP)是一种基于 UDP 协议在客户端和服务器之间进行简单文件传输的协议。

[root@iscsi ~]# yum install tftp-server tftp -y
在centos7中,tftp默认用xinetd服务程序来管理
[root@iscsi ~]# vim /etc/xinetd.d/tftp
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
}
# systemctl restart xinetd
# systemctl enable xinetd
# firewall-cmd --permanent --add-port=69/udp
# firewall-cmd --reload
[root@iscsi ~]# systemctl restart xinetd
Failed to restart xinetd.service: Unit not found.
[root@iscsi ~]# ll /usr/lib/systemd/system/xin*
ls: cannot access /usr/lib/systemd/system/xin*: No such file or directory
[root@iscsi ~]# /usr/sbin/in.tftpd restart
Terminated
[root@iscsi ~]# systemctl list-unit-files --type=service xinetd
UNIT FILE STATE

0 unit files listed.
[root@iscsi ~]# rpm -qa|grep ftp
vsftpd-3.0.2-27.el7.x86_64
tftp-server-5.2-22.el7.x86_64
ftp-0.17-67.el7.x86_64
tftp-5.2-22.el7.x86_64
[root@iscsi ~]# yum -y install xinetd
[root@iscsi ~]# systemctl restart xinetd
[root@iscsi ~]# systemctl status xinetd
● xinetd.service - Xinetd A Powerful Replacement For Inetd
   Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-07-13 11:44:56 CST; 15s ago
  Process: 90012 ExecStart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid $EXTRAOPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 90013 (xinetd)
   CGroup: /system.slice/xinetd.service
           └─90013 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid

tftp 命令中可用的参数以及作用
? 帮助信息
put 上传文件
get 下载文件
verbose 显示详细的处理信息
status 显示当前的状态信息
binary 使用二进制进行传输
ascii 使用 ASCII 码进行传输
timeout 设置重传的超时时间
quit 退出
[root@iscsi ~]# echo "i love tang" > /var/lib/tftpboot/readme.txt
[root@iscsi ~]# tftp 10.15.7.20
tftp> get readme.txt
tftp> quit
[root@iscsi ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  original-ks.cfg  readme.txt
[root@iscsi ~]# ll readme.txt 
-rw-r--r-- 1 root root 12 Jul 13 17:43 readme.txt
[root@iscsi ~]# cat readme.txt 
i love tang
原文地址:https://www.cnblogs.com/yhq1314/p/13345944.html