1-31FTP服务原理及vsfptd的安装、配置--C4 - 图文

更新时间:2023-11-06 19:54:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

本篇随笔将讲解FTP服务的原理以及vsfptd这个最常用的FTP服务程序的安装与配置... 一、FTP服务原理

FTP(File Transfer Protocol)是一个非常古老并且应用十分广泛的文件传输协议,FTP协议是现今使用最为广泛的网络文件共享协议之一,我们现在也一直有在用着FTP协议来进行各种文件的传输,FTP提供了一种可靠的方式在网络上进行文件的共享。

FTP是C/S架构的服务,拥有一个服务器端和一个客户端。 FTP底层通过TCP协议作为传输协议,所以FTP协议是一种可靠的文件传输方式,FTP提供了两个端口号,20和21号端口,20号是数据接口,提供数据之间的传输,21号是命令接口,提供命令之间的传输。

FTP服务端与客户端连接一般有两种模式:主动模式(Active Mode)和被动模式(Passive Mode) ① 主动模式的原理如下图所示:

主动模式下,客户端首先会向服务器端的21号端口发出一个连接命令,请求与服务器端建立连接,此时服务器端响应回去给客户端,并要求客户端发送一个用于传送数据的端口,该端口号要 > 1023 ,此时服务器端的20号端口就会与该数据端口主动建立连接,客户端与服务器端进行数据的传送。

② 被动模式的原理如下图所示:

与主动模式不同的是,在被动模式下,客户端也是首先与服务器端的21端口建立连接,此时服务器端会开启一个 > 1023 号的数据传送端口,并返回给客户端,这个时候客户端也会开启一个 > 1023 的端口,然后客户端会主动的去跟服务器端的数据传输端口建立连接,两者之间来进行数据的传送。

主动模式与被动模式的区别:究竟是服务器端的20端口主动发起于客户端建立连接,还是服务器端开放一个随机端口,等待客户端与其主动建立连接。

在实际环境中,通常使用是被动连接模式,因为我们的服务器端都有配置防火墙,而防火墙对于内网连接外网的端口一般是放行的,而外网来连接内网的端口则一般是有限制的,所以我们如果使用主动模式连接的话,端口可能被防火墙拦截。

二、vsfptd的安装

在Linux下,我们应用最广泛的FTP服务程序是

vsftpd (Very Secure FTP Daemon),

从名字我们也可以看出,其提供了非常安全的FTP服务。vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux, BSD, Solaris, HP-UX 以及 IRIX 上面。它支持很多其他的 FTP 服务器不支持的特征。例如:①非常高的安全性需求 ②带宽限制 ③创建虚拟用户的可能性 ④高速 ... 在CentOS下默认没有安装 vsftpd 这个FTP程序,我们通过 yum install vsfptd 来进行安装

[root@xiaoluo ~]# yum install -y vsftpd

Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: ftp.nsysu.edu.tw * extras: ftp.nsysu.edu.tw * updates: ftp.twaren.net Setting up Install Process Resolving Dependencies

--> Running transaction check

---> Package vsftpd.x86_64 0:2.2.2-11.el6_4.1 will be installed --> Finished Dependency Resolution

Dependencies Resolved

================================================================================

Package Arch Version Repository Size

================================================================================ Installing:

vsftpd x86_64 2.2.2-11.el6_4.1 updates 151 k

Transaction Summary

================================================================================

Install 1 Package(s)

Total download size: 151 k Installed size: 331 k Downloading Packages:

vsftpd-2.2.2-11.el6_4.1.x86_64.rpm | 151 kB 00:01 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction

Installing : vsftpd-2.2.2-11.el6_4.1.x86_64 1/1 Verifying : vsftpd-2.2.2-11.el6_4.1.x86_64 1/1

Installed:

vsftpd.x86_64 0:2.2.2-11.el6_4.1 Complete!

安装好 vsftpd 服务程序后,看一下 vsftpd 这个程序的一些配

置文件,其所有的配置文件在 /etc/vsftpd/ 目录下。

[root@xiaoluo ~]# cd /etc/vsftpd/ [root@xiaoluo vsftpd]# ls -l total 20

-rw-------. 1 root root 125 Mar 1 18:16 ftpusers -rw-------. 1 root root 361 Mar 1 18:16 user_list -rw-------. 1 root root 4599 Mar 1 18:16 vsftpd.conf

-rwxr--r--. 1 root root 338 Mar 1 18:16 vsftpd_conf_migrate.sh

一共有四个配置文件:

(1) vsftpd.conf :是主配置文件;

(2) ftpusers是黑名单用户配置文件,通常系统用户还有根用

户都是放在这个配置文件里面的,因为这些用户的权限很大,如果使用ftp服务可能造成一些问题。

(3) user_list是用户列表文件,可以通过在主配置文件里设置

该用户是黑名单用户还是白名单拥有。

(4) vsftpd_conf_migrate.sh:是操作和配置变量。

vsftpd 程序安装好后,有 /var/ftp 文件夹,该文件夹就是ftp共享文件夹。

[root@xiaoluo vsftpd]# cd /var/ftp/ [root@xiaoluo ftp]# ls pub

这种登陆是以匿名用户登录上ftp,我们可以看到登陆进去后的目录是 /var/pub 这个共享目录 ②使用 lftp -u xiaoluo 127.0.0.1 进行登陆

[root@xiaoluo ~]# lftp -u xiaoluo 127.0.0.1 Password:

lftp xiaoluo@127.0.0.1:~> pwd ftp://xiaoluo@127.0.0.1

lftp xiaoluo@127.0.0.1:~> ls

drwxr-xr-x 2 500 500 4096 May 26 05:22 Desktop drwxr-xr-x 3 500 500 4096 May 19 16:10 Documents drwxr-xr-x 2 500 500 4096 May 13 17:36 Downloads drwxr-xr-x 2 500 500 4096 May 13 17:36 Music drwxr-xr-x 2 500 500 4096 May 13 17:36 Pictures drwxr-xr-x 2 500 500 4096 May 13 17:36 Public drwxr-xr-x 2 500 500 4096 May 13 17:36 Templates drwxr-xr-x 2 500 500 4096 May 13 17:36 Videos

也可以通过 -u 指定我们以系统用户的方式登陆ftp,系统用户登录上以后,默认登陆的文件夹就是用户的根目录.

四、用户通过ftp进行文件上传、下载操作

①正常用户的上传、下载操作

通过lftp命令还可以进行文件的上传、下载操作,分别使用 put 和 get 命令

例如现在在 xiaoluo 这个用户的家目录下创建一个 xiaoluo.txt 文件,然后我们通过 get 命令将其从ftp上下载下来

[xiaoluo@xiaoluo ~]$ touch xiaoluo.txt [xiaoluo@xiaoluo ~]$ ls

Desktop Downloads Pictures Templates xiaoluo.txt Documents Music Public Videos [xiaoluo@xiaoluo ~]$ su - Password:

[root@xiaoluo ~]# lftp -u xiaoluo 127.0.0.1 Password:

lftp xiaoluo@127.0.0.1:~> ls

drwxr-xr-x 2 500 500 4096 May 26 05:22 Desktop drwxr-xr-x 3 500 500 4096 May 19 16:10 Documents drwxr-xr-x 2 500 500 4096 May 13 17:36 Downloads drwxr-xr-x 2 500 500 4096 May 13 17:36 Music drwxr-xr-x 2 500 500 4096 May 13 17:36 Pictures drwxr-xr-x 2 500 500 4096 May 13 17:36 Public drwxr-xr-x 2 500 500 4096 May 13 17:36 Templates

drwxr-xr-x 2 500 500 4096 May 13 17:36 Videos -rw-rw-r-- 1 500 500 0 Jun 08 16:20 xiaoluo.txt lftp xiaoluo@127.0.0.1:~> get xiaoluo.txt

lftp xiaoluo@127.0.0.1:~> exit [root@xiaoluo ~]# ls

anaconda-ks.cfg Desktop install.log install.log.syslog xiaoluo.txt

通过登陆xiaoluo这个用户,然后在其主目录下创建了一个 xiaoluo.txt 的文件,然后我们切换到了 root 目录下,首先登陆我们的 lftp ,然后通过 get 命令将该文件下载下来,这时我们就可以发现root用户的家目录下就有了刚才下载下来的 xiaoluo.txt 文件的。

【注意:】当前root用户是在哪个目录下通过 lftp 登陆的,在下载文件时就会下载到该目录下。 同时也可以使用 put 命令来上传文件到ftp上

[root@xiaoluo ~]# touch root.txt [root@xiaoluo ~]# ls

anaconda-ks.cfg install.log root.txt Desktop install.log.syslog xiaoluo.txt [root@xiaoluo ~]# lftp -u xiaoluo 127.0.0.1 Password:

lftp xiaoluo@127.0.0.1:~> put root.txt

lftp xiaoluo@127.0.0.1:~> ls

drwxr-xr-x 2 500 500 4096 May 26 05:22 Desktop drwxr-xr-x 3 500 500 4096 May 19 16:10 Documents drwxr-xr-x 2 500 500 4096 May 13 17:36 Downloads drwxr-xr-x 2 500 500 4096 May 13 17:36 Music drwxr-xr-x 2 500 500 4096 May 13 17:36 Pictures drwxr-xr-x 2 500 500 4096 May 13 17:36 Public drwxr-xr-x 2 500 500 4096 May 13 17:36 Templates drwxr-xr-x 2 500 500 4096 May 13 17:36 Videos -rw-r--r-- 1 500 500 0 Jun 08 16:26 root.txt -rw-rw-r-- 1 500 500 0 Jun 08 16:20 xiaoluo.txt lftp xiaoluo@127.0.0.1:~> exit

[root@xiaoluo ~]# cd /home/xiaoluo/ [root@xiaoluo xiaoluo]# ls

Desktop Downloads Pictures root.txt Videos

Documents Music Public Templates xiaoluo.txt

我在root家目录下创建了 root.txt 这个文件,然后通过 xiaoluo 这个用户上传到了 ftp 上,这时我们切换到xiaoluo的家目录下,发现该文件已经上传上去了

【注意:】我们在使用ftp进行文件上传、下载操作时,要确保该用户对目录具有读写的权限!!!

②匿名用户上传、下载操作

匿名用户登陆ftp以后默认对共享文件夹只有读的权限,即只能从共享目录里进行文件的下载操作

[root@xiaoluo ~]# cd /var/ftp/pub/ [root@xiaoluo pub]# touch xiaoluo.txt [root@xiaoluo pub]# ls xiaoluo.txt

[root@xiaoluo pub]# cd

[root@xiaoluo ~]# lftp 127.0.0.1 lftp 127.0.0.1:~> cd pub/ lftp 127.0.0.1:/pub> ls

-rw-r--r-- 1 0 0 0 Jun 08 16:39 xiaoluo.txt lftp 127.0.0.1:/pub> get xiaoluo.txt lftp 127.0.0.1:/pub> exit [root@xiaoluo ~]# ls

anaconda-ks.cfg Desktop install.log install.log.syslog xiaoluo.txt

那么要如何通过设置来实现文件的上传操作呢? (1) 修改vsftpd.conf开启匿名用户上传功能:

anonymous_enable=YES --开启匿名用户访问(默认) anon_upload_enable=YES --匿名用户文件上传功能 anon_mkdir_write_enable=YES --匿名用户创建文件夹功能

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

# Example config file /etc/vsftpd/vsftpd.conf #

# The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more usable. # Please see vsftpd.conf.5 for all compiled in defaults. #

# READ THIS: This example file is NOT an exhaustive list of vsftpd options. # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's # capabilities.

#

# Allow anonymous FTP? (Beware - allowed by default if you comment this out). anonymous_enable=YES

# Uncomment this to allow the anonymous FTP user to upload files. This only # has an effect if the above global write enable is activated. Also, you will # obviously need to create a directory writable by the FTP user.

anon_upload_enable=YES

#

# Uncomment this if you want the anonymous FTP user to be able to create # new directories.

anon_mkdir_write_enable=YES

……

(2) 赋予匿名用户上传使用的文件夹以写的权限

[root@xiaoluo ~]# chmod 2777 /var/ftp/pub/ [root@xiaoluo ~]# ls -ld /var/ftp/pub/

drwxrwsrwx. 2 root root 4096 Jun 9 00:42 /var/ftp/pub/

此时我们重新启动我们 vsftpd 服务,就可以使用匿名用户来进行文件上传的操作了

[root@xiaoluo ~]# service vsftpd restart

Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [root@xiaoluo ~]# ls

anaconda-ks.cfg Desktop install.log install.log.syslog [root@xiaoluo ~]# touch xiaoluo.txt [root@xiaoluo ~]# ls

anaconda-ks.cfg Desktop install.log install.log.syslog xiaoluo.txt [root@xiaoluo ~]# lftp 127.0.0.1 lftp 127.0.0.1:~> cd pub/ lftp 127.0.0.1:/pub> ls

lftp 127.0.0.1:/pub> put xiaoluo.txt lftp 127.0.0.1:/pub> exit

[root@xiaoluo ~]# cd /var/ftp/pub/ [root@xiaoluo pub]# ls xiaoluo.txt

我们看到,我们在 root 家目录下创建的 xiaoluo.txt 文件已经通过 匿名用户登录上传上去了

【注意:】不能将匿名用户的主目录/var/ftp权限设置为777权限,而只能设置其子目录的权限为 777,因为vsftp认为这样是很不安全的,如果这样设置了,vsftpd将禁止访问匿名用户的主目录。

本文来源:https://www.bwwdw.com/article/wjg2.html

Top