FTP文件传输协议

FTP

1. ftp简介

网络文件共享服务主流的主要有三种,分别是ftp、nfs、samba。

FTP是File Transfer Protocol(文件传输协议)的简称,用于internet上的控制文件的双向传输。

FTP也是一个应用程序,基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。

在FTP的使用当中,用户经常遇到两种概念:下载和上传

下载(Download) 上传(Upload)
从远程主机拷贝文件至自己的计算机上 将文件从自己的计算机上拷贝至远程主机上

2. ftp架构

FTP工作于应用层,监听于tcp的21号端口,是一种C/S架构的应用程序。其有多种客户端和服务端的应用程序,下面来简单介绍一下

客户端工具 服务端软件
ftp lftp,lftpget wget,curl filezilla gftp(Linux GUI) 商业软件(flashfxp,cuteftp) wu-ftpd proftpd(提供web接口的一种ftp服务端程序) pureftp vsftpd(Very Secure) ServU(windows平台的一种强大ftp服务端程序)

3. ftp数据连接模式

ftp有2种数据连接模式:命令连接和数据连接

  • 命令连接:是指文件管理类命令,始终在线的持久性连接,直到用户退出登录为止
  • 数据连接:是指数据传输,按需创建及关闭的连接

其中数据连接需要关注的有2点,一是数据传输格式,二是数据传输模式

数据传输格式有以下两种:

  • 文件传输
  • 二进制传输

数据传输模式也有2种:

  • 主动模式:由服务器端创建数据连接
  • 被动模式:由客户端创建数据连接

两种数据传输模式的建立过程:

传输模式 建立过程
主动模式 命令连接: Client(1025)–> Server(21) 客户端以一个随机端口(大于1023)来连服务器端的21号端口 数据连接: Server(20/tcp) --> Client(1025+1) 服务器端以自己的20号端口去连客户端创建命令连接时使用的随机端口+1的端口号
被动模式 命令连接: Client(1110) --> Server(21) 客户端以一个随机端口来连成服务器端的21号端口 数据连接: Client(1110+1) --> Server(随机端口) 客户端以创建命令连接的端口+1的端口号去连服务器端通过命令连接告知自己的一个随机端口号来创建数据连接

主动模式有个弊端,因为客户端的端口是随机的,客户端如果开了防火墙,
则服务器端去连客户端创建数据连接时可能会被拒绝

4. 用户认证

ftp的用户主要有三种:

  • 虚拟用户:仅用于访问某特定服务中的资源
  • 系统用户:可以登录系统的真实用户
  • 匿名用户

5. vsftpd

此处我们要说的ftp应用程序是vsftpd,这也是在公司中用得最多的一款ftp软件。

5.1 vsftpd安装

//安装
[root@localhost ~]# yum -y install vsftpd
Complete!

//启动服务
[root@localhost ~]# systemctl enable --now vsftpd
Created symlink /etc/systemd/system/multi-user.target.wants/vsftpd.service → /usr/lib/systemd/system/vsftpd.service.
[root@localhost ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
     Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; preset: disabled)
     Active: active (running) since Wed 2023-12-13 14:25:09 CST; 29s ago
   Main PID: 51417 (vsftpd)
      Tasks: 1 (limit: 10820)
     Memory: 708.0K
        CPU: 3ms

//关闭防火墙
[root@localhost ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# setenforce 0

5.2 配置匿名用户ftp

//编辑配置文件
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf 
anonymous_enable=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
[root@localhost ~]# systemctl restart vsftpd

//修改所有者为ftp
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# ls
pub
[root@localhost ftp]# chown -R ftp pub
[root@localhost ftp]# ll -d pub
drwxr-xr-x. 2 ftp root 6 Oct 30 14:29 pub

5.2.1上传(下面使用的是FileZilla软件)

在这里插入图片描述

[root@localhost ftp]# cd pub
[root@localhost pub]# ls
abc.txt
5.2.2下载
[root@localhost pub]# pwd
/var/ftp/pub
[root@localhost pub]# touch file
[root@localhost pub]# ls
abc.txt  file

在这里插入图片描述
在这里插入图片描述

5.2.3创建

在这里插入图片描述

[root@localhost pub]# pwd
/var/ftp/pub
[root@localhost pub]# ls
123  abc.txt  dir  file
5.2.4删除

在这里插入图片描述

在这里插入图片描述

[root@localhost pub]# pwd
/var/ftp/pub
[root@localhost pub]# ls
abc.txt  file

5.3配置本地(系统)用户ftp

//创建用户
[root@localhost ~]# useradd tom
[root@localhost ~]# echo '123456' | passwd --stdin tom
Changing password for user tom.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# id tom
uid=1001(tom) gid=1001(tom) groups=1001(tom)

//编辑配置文件
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf 
local_enable=YES
write_enable=YES
//这两行是禁锢所有的ftp本地用户查看其家目录,只可以看到ftp文件存放目录
chroot_local_user=YES
allow_writeable_chroot=YES   //这一行配置文件没有,要添加
[root@localhost ~]# systemctl restart vsftpd
5.3.1上传

在这里插入图片描述

[root@localhost ~]# su - tom
[tom@localhost ~]$ pwd
/home/tom
[tom@localhost ~]$ ls
笔记.txt
5.3.2下载
[tom@localhost ~]$ pwd
/home/tom
[tom@localhost ~]$ touch test
[tom@localhost ~]$ ls
test  笔记.txt

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

5.3.3创建

在这里插入图片描述

在这里插入图片描述

[tom@localhost ~]$ pwd
/home/tom
[tom@localhost ~]$ ls
test  xx  zz  笔记.txt
5.3.4删除

在这里插入图片描述

在这里插入图片描述

[tom@localhost ~]$ pwd
/home/tom
[tom@localhost ~]$ ls
test  笔记.txt

5.4配置虚拟用户ftp

//
[root@localhost ~]# cd /etc/vsftpd/
[root@localhost vsftpd]# pwd
/etc/vsftpd
[root@localhost vsftpd]# vim vu.list
[root@localhost vsftpd]# cat vu.list
jerry
123456
harry
654321

//安装db_load软件包
[root@localhost ~]# yum provides *bin/db_load
Last metadata expiration check: 1:19:40 ago on Wed 13 Dec 2023 02:40:13 PM CST.
libdb-utils-5.3.28-53.el9.x86_64 : Command line tools for managing Berkeley DB databases
Repo        : appstream
Matched from:
Other       : *bin/db_load

[root@localhost ~]# yum -y install libdb-utils
Complete!

//使用db_load将刚创建的文本格式转换为数据库文件
[root@localhost ~]# db_load -T -t hash -f /etc/vsftpd/vu.list /etc/vsftpd/vu.db
[root@localhost ~]# ls /etc/vsftpd/
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh  vu.db  vu.list
[root@localhost ~]# cat /etc/vsftpd/vu.db
��MGfEotэh^[root@localhost ~]# 
[root@localhost ~]# mv /etc/vsftpd/vu.list /opt
[root@localhost ~]# chmod 600 /etc/vsftpd/vu.db 
[root@localhost ~]# ll /etc/vsftpd/vu.db 
-rw-------. 1 root root 12288 Dec 13 16:04 /etc/vsftpd/vu.db

//添加虚拟用户,指定虚拟用户家目录,并修改家目录权限
[root@localhost ~]# useradd -d /data -s /sbin/nologin vftp
[root@localhost ~]# id vftp
uid=1002(vftp) gid=1002(vftp) groups=1002(vftp)
[root@localhost ~]# chmod 755 /data
[root@localhost ~]# ll -d /data
drwxr-xr-x. 2 vftp vftp 62 Dec 13 16:12 /data

//为虚拟用户建立PAM认证
[root@localhost ~]# cd /etc/pam.d/
[root@localhost pam.d]# ls
config-util  fingerprint-auth  other   password-auth  remote   runuser-l       sshd              su    sudo-i  system-auth   vlock     vsftpd
crond        login             passwd  postlogin      runuser  smartcard-auth  sssd-shadowutils  sudo  su-l    systemd-user  vmtoolsd
[root@localhost pam.d]# cp vsftpd vsftpd.bak
[root@localhost pam.d]# ls
config-util  fingerprint-auth  other   password-auth  remote   runuser-l       sshd              su    sudo-i  system-auth   vlock     vsftpd
crond        login             passwd  postlogin      runuser  smartcard-auth  sssd-shadowutils  sudo  su-l    systemd-user  vmtoolsd  vsftpd.bak
[root@localhost pam.d]# vim vsftpd
[root@localhost pam.d]# cat vsftpd
#%PAM-1.0
auth required pam_userdb.so db=/etc/vsftpd/vu
account required pam_userdb.so db=/etc/vsftpd/vu

//上面修改了/etc/pam.d/vsftpd里面的内容,为了更好的测试注释配置文件里面的一下内容
[root@localhost pam.d]# vim /etc/vsftpd/vsftpd.conf  
#local_enable=YES
#write_enable=YES
#chroot_local_user=YES
#allow_writeable_chroot=YES
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES

//修改vsftpd配置文件,添加虚拟用户支持
[root@localhost ~]# echo 'guest_enable=YES' >> /etc/vsftpd/vsftpd.conf
[root@localhost ~]# echo 'guest_username=vftp' >> /etc/vsftpd/vsftpd.conf
[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# tail -2 /etc/vsftpd/vsftpd.conf 
guest_enable=YES
guest_username=vftp

//为不同的虚拟用户建立独立的配置文件(默认是没有以下两行)
[root@localhost ~]# echo 'user_config_dir=/etc/vsftpd/vusers_dir' >> /etc/vsftpd/vsftpd.conf
[root@localhost ~]# echo 'allow_writeable_chroot=YES' >> /etc/vsftpd/vsftpd.conf
[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# tail -2 /etc/vsftpd//vsftpd.conf 
user_config_dir=/etc/vsftpd/vusers_dir
allow_writeable_chroot=YES

//在/etc/vsftpd/vusers_dir目录中为每个虚拟用户分别建立配置文件
//例如,若要使用虚拟用户jerry能够上传文件、创建目录,而harry只有默认的下载权限
//可以执行以下操作
[root@localhost ~]#  mkdir /etc/vsftpd/vusers_dir
[root@localhost ~]# ll /etc/vsftpd/
total 32
-rw-------. 1 root root   125 Oct 30 14:29 ftpusers
-rw-------. 1 root root   361 Oct 30 14:29 user_list
-rw-------. 1 root root  5202 Dec 13 16:27 vsftpd.conf
-rwxr--r--. 1 root root   352 Oct 30 14:29 vsftpd_conf_migrate.sh
-rw-------. 1 root root 12288 Dec 13 16:04 vu.db
drwxr-xr-x. 2 root root     6 Dec 13 16:34 vusers_dir

//设置harry用户可以上传,下载,删除,创建。jerry用户只有下载权限
[root@localhost ~]# cd /etc/vsftpd/vusers_dir/
[root@localhost vusers_dir]# ls
[root@localhost vusers_dir]# touch jerry harry
[root@localhost vusers_dir]# ls
harry  jerry
[root@localhost vusers_dir]# vim harry
[root@localhost vusers_dir]# cat harry
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=022     //可加可不加
[root@localhost vusers_dir]# cat jerry
[root@localhost ~]# systemctl restart vsftpd

5.5FTP应用工具

//安装ftp工具
[root@localhost ~]# yum provides *bin/ftp
Last metadata expiration check: 2:06:31 ago on Wed 13 Dec 2023 02:40:13 PM CST.
ftp-0.17-89.el9.x86_64 : The standard UNIX FTP (File Transfer Protocol) client
Repo        : appstream
Matched from:
Other       : *bin/ftp

[root@localhost ~]# yum -y install ftp
Complete!

//帮助文档
[root@localhost ~]# ftp 192.168.116.140
Connected to 192.168.116.140 (192.168.116.140).
220 (vsFTPd 3.0.5)
Name (192.168.116.140:root): harry
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ?
Commands may be abbreviated.  Commands are:

!               debug           mdir            sendport        site
$               dir             mget            put             size
account         disconnect      mkdir           pwd             status
append          exit            mls             quit            struct
ascii           form            mode            quote           system
bell            get             modtime         recv            sunique
binary          glob            mput            reget           tenex
bye             hash            newer           rstatus         tick
case            help            nmap            rhelp           trace
cd              idle            nlist           rename          type
cdup            image           ntrans          reset           user
chmod           lcd             open            restart         umask
close           ls              prompt          rmdir           verbose
cr              macdef          passive         runique         ?
delete          mdelete         proxy           send

//创建
ftp> mkdir abc
257 "/abc" created
ftp> ls
227 Entering Passive Mode (192,168,116,140,162,82).
150 Here comes the directory listing.
drwx------    2 1001     1001            6 Dec 13 09:57 abc
226 Directory send OK.

//上传(把本机的文件上传到ftp中)
[root@localhost ~]# ls
anaconda-ks.cfg

ftp> put anaconda-ks.cfg
local: anaconda-ks.cfg remote: anaconda-ks.cfg
227 Entering Passive Mode (192,168,116,140,110,211).
150 Ok to send data.
226 Transfer complete.
1064 bytes sent in 0.000408 secs (2607.84 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,116,140,212,49).
150 Here comes the directory listing.
drwx------    2 1001     1001            6 Dec 13 10:07 123
drwx------    2 1001     1001            6 Dec 13 09:57 abc
-rw-------    1 1001     1001         1064 Dec 13 10:18 anaconda-ks.cfg
226 Directory send OK.

//下载
ftp> get 666
local: 666 remote: 666
227 Entering Passive Mode (192,168,116,140,53,121).
150 Opening BINARY mode data connection for 666 (0 bytes).
226 Transfer complete.

[root@localhost ~]# ls
123  666  anaconda-ks.cfg

//删除
ftp> rmdir abc     /删除空目录
250 Remove directory operation successful.
ftp> rmdir 123
250 Remove directory operation successful.
ftp> ls
227 Entering Passive Mode (192,168,116,140,194,20).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 Dec 13 10:22 666
-rw-------    1 1001     1001         1064 Dec 13 10:18 anaconda-ks.cfg
226 Directory send OK.

ftp> delete anaconda-ks.cfg    /删除文件
250 Delete operation successful.
ftp> ls
227 Entering Passive Mode (192,168,116,140,48,158).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 Dec 13 10:22 666
226 Directory send OK.

//lcd用法
[root@localhost opt]# ls
777  888

ftp> put 777
local: 777 remote: 777
227 Entering Passive Mode (192,168,116,140,62,177).
150 Ok to send data.
226 Transfer complete.
ftp> ls
227 Entering Passive Mode (192,168,116,140,249,56).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 Dec 13 10:22 666
-rw-------    1 1001     1001            0 Dec 13 10:28 777
226 Directory send OK.

[root@localhost opt]# ls /data
777

ftp> get 666
local: 666 remote: 666
227 Entering Passive Mode (192,168,116,140,222,91).
150 Opening BINARY mode data connection for 666 (0 bytes).
226 Transfer complete.

[root@localhost opt]# ls
666  777  888

5.6LFTP应用工具

//安装lftp工具
[root@localhost ~]# yum provides *bin/lftp
Last metadata expiration check: 0:00:36 ago on Wed 13 Dec 2023 06:33:59 PM CST.
lftp-4.9.2-4.el9.i686 : A sophisticated file transfer program
Repo        : appstream
Matched from:
Other       : *bin/lftp

lftp-4.9.2-4.el9.x86_64 : A sophisticated file transfer program
Repo        : appstream
Matched from:
Other       : *bin/lftp

[root@localhost ~]# yum -y install lftp
Complete!

//帮助文档
[root@localhost ~]# lftp -u harry,123456 192.168.116.140
lftp [email protected]:~> help
    !<shell-command>                     (commands)                           alias [<name> [<value>]]             attach [PID]
    bookmark [SUBCMD]                    cache [SUBCMD]                       cat [-b] <files>                     cd <rdir>
    chmod [OPTS] mode file...            close [-a]                           [re]cls [opts] [path/][pattern]      debug [OPTS] [<level>|off]
    du [options] <dirs>                  edit [OPTS] <file>                   exit [<code>|bg]                     get [OPTS] <rfile> [-o <lfile>]
    glob [OPTS] <cmd> <args>             help [<cmd>]                         history -w file|-r file|-c|-l [cnt]  jobs [-v] [<job_no...>]
    kill all|<job_no>                    lcd <ldir>                           lftp [OPTS] <site>                   ln [-s] <file1> <file2>              ls [<args>]
    mget [OPTS] <files>                  mirror [OPTS] [remote [local]]       mkdir [OPTS] <dirs>                  module name [args]                   more <files>
    mput [OPTS] <files>                  mrm <files>                          mv <file1> <file2>                   mmv [OPTS] <files> <target-dir>
    [re]nlist [<args>]                   open [OPTS] <site>                   pget [OPTS] <rfile> [-o <lfile>]     put [OPTS] <lfile> [-o <rfile>]      pwd [-p]
    queue [OPTS] [<cmd>]                 quote <cmd>                          repeat [OPTS] [delay] [command]      rm [-r] [-f] <files>
    rmdir [-f] <dirs>                    scache [<session_no>]                set [OPT] [<var> [<val>]]            site <site-cmd>                      source <file>
    torrent [OPTS] <file|URL>...         user <user|URL> [<pass>]             wait [<jobno>]                       zcat <files>                         zmore <files>

//创建
lftp [email protected]:~> ls
-rw-------    1 1001     1001            0 Dec 13 10:28 777
lftp [email protected]:/> mkdir abc
mkdir ok, `abc' created
lftp [email protected]:/> ls
-rw-------    1 1001     1001            0 Dec 13 10:28 777
drwx------    2 1001     1001            6 Dec 13 10:41 abc

//上传
[root@localhost ~]# ls
123  666  anaconda-ks.cfg

lftp [email protected]:/> ls
-rw-------    1 1001     1001            0 Dec 13 10:28 777
drwx------    2 1001     1001            6 Dec 13 10:41 abc
lftp [email protected]:/> put anaconda-ks.cfg
1064 bytes transferred
lftp [email protected]:/> ls
-rw-------    1 1001     1001            0 Dec 13 10:28 777
drwx------    2 1001     1001            6 Dec 13 10:41 abc
-rw-------    1 1001     1001         1064 Dec 13 10:46 anaconda-ks.cfg

//下载

5.7wget应用工具

//安装wget
[root@localhost ~]# yum -y install wget
Complete!
[root@localhost ~]# cd test
[root@localhost test]# wget --ftp-user harry --ftp-password 123456
192.168.116.140:/vu.list
--2023-12-13 17:58:27-- ftp://192.168.116.140//vu.list
=> ‘vu.list’
Connecting to 192.168.116.140:21... connected.
Logging in as admin ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD not needed.
==> SIZE vn.list ... 21
==> PASV ... done. ==> RETR vu.list ... done.
Length: 21 (unauthoritative)
vu.list 100%
[================================================================================
=========================>] 21 --.-KB/s in 0s
2023-12-13 17:58:27 (6.04 MB/s) - ‘vu.list’ saved [21]
[root@localhost test]# ls
vu.list

-13 17:58:27-- ftp://192.168.116.140//vu.list
=> ‘vu.list’
Connecting to 192.168.116.140:21… connected.
Logging in as admin … Logged in!
> SYST … done. > PWD … done.
> TYPE I … done. > CWD not needed.
> SIZE vn.list … 21
> PASV … done. > RETR vu.list … done.
Length: 21 (unauthoritative)
vu.list 100%
[
==================================================================
=========================>] 21 --.-KB/s in 0s
2023-12-13 17:58:27 (6.04 MB/s) - ‘vu.list’ saved [21]
[root@localhost test]# ls
vu.list


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>