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@wangqing ~]# yum -y install vsftpd
...
...
Total download size: 169 k
Installed size: 348 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : vsftpd-3.0.2-22.el7.x86_64                              1/1
  Verifying  : vsftpd-3.0.2-22.el7.x86_64                              1/1

Installed:
  vsftpd.x86_64 0:3.0.2-22.el7

Complete!

5.2 vsftpd配置

/etc/pam.d/vsftpd       //vsftpd用户认证配置文件
/etc/vsftpd/            //配置文件目录
/etc/vsftpd/vsftpd.conf     //主配置文件

//匿名用户(映射为ftp用户)的共享资源位置是/var/ftp
//系统用户通过ftp访问的资源位置为用户的家目录
//虚拟用户通过ftp访问的资源位置为给虚拟用户指定的映射成为的系统用户的家目录

vsftpd常见的配置参数:

参数作用
anonymous_enable=YES启用匿名用户登录
anon_upload_enable=YES允许匿名用户上传
anon_mkdir_write_enable=YES允许匿名用户创建目录,但是不能删除
anon_other_write_enable=YES允许匿名用户创建和删除目录
local_enable=YES启用本地用户登录
write_enable=YES允许本地用户有写权限
local_umask=022通过ftp上传文件的默认遮罩码
chroot_local_user=YES禁锢所有的ftp本地用户于其家目录中
chroot_list_enable=YES开启禁锢文件列表需要与chroot_list_file参数一起使用
chroot_list_file=/etc/vsftpd/chroot_list指定禁锢列表文件路径在此文件里面的用户将被禁锢在其家目录中
allow_writeable_chroot=YES允许被禁锢的用户家目录有写权限
xferlog_enable=YES是否启用传输日志,记录ftp传输过程
xferlog_std_format=YES传输日志是否使用标准格式
xferlog_file=/var/log/xferlog指定传输日志存储的位置
chown_uploads=YES是否启用改变上传文件属主的功能
chown_username=whoever指定要将上传的文件的属主改为哪个用户此用户必须在系统中存在
pam_service_name=vsftpd指定vsftpd使用/etc/pam.d下的哪个pam配置文件进行用户认证
userlist_enable=YES是否启用控制用户登录的列表文件:默认为/etc/vsftpd/user_list文件
userlist_deny=YES是否拒绝userlist指定的列表文件中存在的用户登录ftp
max_clients=#最大并发连接数
max_per_ip=#每个IP可同时发起的并发请求数
anon_max_rate匿名用户的最大传输速率,单位是“字节/秒”
local_max_rate本地用户的最大传输速率,单位是“字节/秒”
dirmessage_enable=YES启用某目录下的.message描述信息假定有一个目录为/upload,在其下创建一个文件名为.message,在文件内写入一些描述信息,则当用户切换至/upload目录下时会自动显示.message文件中的内容
message_file设置访问一个目录时获得的目录信息文件的文件名,默认是.message
idle_session_timeout=600设置默认的断开不活跃session的时间
data_connection_timeout=120设置数据传输超时时间
ftpd_banner=“Welcome to chenlf FTP service.”定制欢迎信息,登录ftp时自动显示
//虚拟用户的配置:
    //所有的虚拟用户会被统一映射为一个指定的系统帐号,访问的共享位置即为此系统帐号的家目录
    //各虚拟用户可被赋予不同的访问权限,通过匿名用户的权限控制参数进行指定

    //虚拟用户帐号的存储方式:
    1.文件:编辑文件,此文件需要被编码为hash格式。
        奇数行为用户名
        偶数行为密码
    2.关系型数据库的表中:
        通过即时查询数据库完成用户认证
        mysql库:pam要依赖于pam_mysql软件,可以通过epel源yum安装

实验

1.安装vsftpd

[root@yh ~]# yum -y install vsftpd
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-22.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决


/etc/pam.d/vsftpd       //vsftpd用户认证配置文件
/etc/vsftpd/            //配置文件目录
/etc/vsftpd/vsftpd.conf     //主配置文件

//匿名用户(映射为ftp用户)的共享资源位置是/var/ftp
//系统用户通过ftp访问的资源位置为用户的家目录
//虚拟用户通过ftp访问的资源位置为给虚拟用户指定的映射成为的系统用户的家目录

2.配置匿名用户ftp

环境,关闭防火墙

[root@yh ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   ...
[root@yh ~]# systemctl disable firewalld
[root@yh ~]# systemctl stop firewalld
[root@yh ~]# setenforce 0

1.vsftpd——安装vsftpd

[root@yh ~]# yum -y install vsftpd
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-22.el7 将被 安装
--> 解决依赖关系完成

2.vsftpd——配置匿名用户ftp,只需要改主配置文件

[root@yh ~]# 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   //创建之外的权限,删除
[root@yh ~]# systemctl start vsftpd
[root@yh ~]# ss -antl
State       Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN      0      128                       *:22                                    *:*                  
LISTEN      0      100               127.0.0.1:25                                    *:*                  
LISTEN      0      32                       :::21                                   :::*                  
LISTEN      0      128                      :::22                                   :::*                  
LISTEN      0      100                     ::1:25                                   :::* 

3.客户端登陆

完毕!
[root@yh1 ~]# yum -y install lftp
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
...
[root@yh1 ~]# lftp 192.168.19.128
lftp 192.168.19.128:~>   

3.配置虚拟用户ftp

1.vsftpd——安装vsftpd

[root@yh ~]# yum -y install vsftpd
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-22.el7 将被 安装
--> 解决依赖关系完成

2.安装依赖的程序:

[root@yh ~]# cd /etc/yum.repos.d/
[root@yh yum.repos.d]# ls
myrepo.repo  redhat.repo
[root@yh yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2019-04-17 16:00:14--  http://mirrors.163.com/.help/CentOS7-Base-163.repo
正在解析主机 mirrors.163.com (mirrors.163.com)... 59.111.0.251
正在连接 mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1572 (1.5K) [application/octet-stream]
正在保存至: “CentOS7-Base-163.repo”

100%[=================================================================>] 1,572       7.39KB/s 用时 0.2s   

2019-04-17 16:00:14 (7.39 KB/s) - 已保存 “CentOS7-Base-163.repo” [1572/1572])

[root@yh yum.repos.d]# cd
[root@yh ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yh ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yh ~]# yum clean all
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在清理软件源: base centosplus extras myrepo updates
...
root@yh ~]# yum -y install epel-release
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
...
[root@yh ~]# yum -y install vsftpd
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to
...

3.创建文本格式的用户名、密码列表,例如若要添加两个用户yh、yh1,密码分别为123、456

[root@yh ~]# echo 'yh' >> /etc/vsftpd/vu.list
[root@yh ~]# echo '123' >> /etc/vsftpd/vu.list
[root@yh ~]# echo 'yh1' >> /etc/vsftpd/vu.list
[root@yh ~]# echo '456' >> /etc/vsftpd/vu.list
[root@yh ~]# cat /etc/vsftpd/vu.list 
yh
123
yh1
456

4.安装db4工具

root@yh ~]# yum -y install db4*
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务

//将之前创建的文本格式用户名,密码文件使用db4工具转换成数据库文件
   -T表示转换,-t表示加密方式使用hash算法加密
[root@yh ~]# db_load -T -t hash -f /etc/vsftpd/vu.list /etc/vsftpd/vu.db

//提高虚拟用户账号文件的安全性,将文件权限设为600,以免数据外泄
[root@yh ~]# chmod 600 /etc/vsftpd/vu.*
[root@yh ~]# ll /etc/vsftpd/vu.*
-rw-------. 1 root root 12288 417 16:11 /etc/vsftpd/vu.db
-rw-------. 1 root root    15 417 16:07 /etc/vsftpd/vu.list
[root@yh ~]# 

//添加虚拟用户的映射账号,创建ftp跟目录,列如要将使用的ftp根目录设置为/var/ftproot(指定家目录路径可另外指定,但是不能在root目录下),\
//映射账号的名称为vftp,可以执行以下操作   ( 如果不想让用户登录系统可以用 -s /sbin/nologin.此用户就不可以登录系统。)
[root@yh ~]# useradd -d /var/ftproot -s /sbin/nologin vftp
[root@yh ~]# chmod 755 /var/ftproot/
[root@yh ~]# ll -d /var/ftproot/
drwxr-xr-x. 2 vftp vftp 62 417 16:13 /var/ftproot/

5.为虚拟用户建立PAM认证

[root@yh ~]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
[root@yh ~]# vim /etc/pam.d/vsftp     //清空并追加以下3行内容,vu是之前生成的输出文件,不能有#
#%PAM-1.0
auth required pam_userdb.so db=/etc/vsftpd/vu         db前是固定式,后路径无需带后缀
account required pam_userdb.so db=/etc/vsftpd/vu

6.修改vsftp配置文件,添加虚拟用户支持

[root@yh ~]# mkdir /etc/vsftpd/vusers_dir
[root@yh ~]# echo 'guest_enable=YES' >> /etc/vsftpd/vsftpd.conf 
[root@yh ~]# echo 'guest_username=vftp' >> /etc/vsftpd/vsftpd.conf

7.为不同的虚拟用户建立独立的配置文件

[root@yh ~]# echo 'user_config_dir=/etc/vsftpd/vusers_dir' >> /etc/vsftpd
//user_config_dir=/etc/vsftpd/vusers_dir   添加'用户配置目录'(vusers_dir默认没有,需要创建

//允许被禁锢的用户家目录有写权限'allow_writeable_chroot=YES'
[root@yh ~]# echo 'allow_writeable_chroot=YES' >> /etc/vsftpd/vsftpd.conf

[root@yh ~]# ll /etc/vsftpd/
总用量 52
-rw-------. 1 root root   125 1031 03:45 ftpusers
-rw-------. 1 root root   361 1031 03:45 user_list
-rw-------. 1 root root  5173 417 16:24 vsftpd.conf
-rwxr--r--. 1 root root   338 1031 03:45 vsftpd_conf_migrate.sh
-rw-------. 1 root root  5116 1031 03:45 vsftpd.conf.rpmnew
-rw-------. 1 root root  5146 417 14:50 vsftpd.conf.rpmsave
-rw-------. 1 root root 12288 417 16:11 vu.db
-rw-------. 1 root root    15 417 16:07 vu.list
drwxr-xr-x. 2 root root     6 417 16:20 vusers_dir
[root@yh ~]# cat /etc/vsftpd/vu.
vu.db    vu.list  
[root@yh ~]# cat /etc/vsftpd/vu.list 
yh
123
yh1
456

8.设置yh用户可上传文件,创建目录

[root@yh ~]# echo 'anon_umask=022' >> /etc/vsftpd/vusers_dir/yh
[root@yh ~]# echo 'anon_upload_enable=YES' >> /etc/vsftpd/vusers_dir/yh
[root@yh ~]# echo 'anon_mkdir_write_enable=YES' >> /etc/vsftpd/vusers_dir/yh

//设置yh1用户只有默认的下载权限,只需要创建一个名为yh1的空文件即可
[root@yh ~]# touch /etc/vsftpd/vusers_dir/yh1
//注意:虚拟用户是通过匿名访问的,所以必须开启匿名访问!

9.启动服务

[root@yh ~]# systemctl start vsftpd
[root@yh ~]# ss -antl
State       Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN      0      128                       *:22                                    *:*                  
LISTEN      0      100               127.0.0.1:25                                    *:*                  
LISTEN      0      32                       :::21                                   :::*                  
LISTEN      0      128                      :::22                                   :::*                  
LISTEN      0      100                     ::1:25                                   :::*                  
[root@yh ~]#

10.验证
在这里插入图片描述
4.配置系统用户ftp

1.vsftpd——安装vsftpd

[root@yh ~]# yum -y install vsftpd
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-22.el7 将被 安装
--> 解决依赖关系完成

2.修改主配置文件

服务器端编辑配置文件: vim /etc/vsftpd/vsftpd.conf       追加:已经有的项目不添加
 anonymous_enable=NO        //是否允许匿名访问
 local_enable=YES                 //是否允许本地登陆
 write_enable=YES                //是否允许写
 local_umask=022                 //本地用户上传遮掩吗
  userlist_deny=YES                //是否启用 '禁止登陆用户名单'
 userlist_enable=YES              //是否启用 '禁止登陆用户名单'

3.创建用户和密码

etc/vsftpd/ftpusers    /etc/vsftpd/user_list 禁止列表
 服务器端vsftpd服务为了让FTP服务更加的安全,默认禁止以root身份登入,那么创建个普通用户abc 并添加密码:
 
[root@yh ~]# useradd yh
[root@yh ~]# vim /etc/vsftpd/user_list
[root@yh ~]# echo yh |passwd --stdin yh
更改用户 yh 的密码 。
passwd:所有的身份验证令牌已经成功更新。

4.服务器端设置SELinux

[root@yh ~]# getsebool -a | grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> on
[root@yh ~]# setsebool -P ftpd_full_access=on
[root@yh ~]# systemctl start vsftpd
[root@yh ~]# ss -antl
State       Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN      0      128                       *:22                                    *:*                  
LISTEN      0      100               127.0.0.1:25                                    *:*                  
LISTEN      0      32                       :::21                                   :::*                  
LISTEN      0      128                      :::22                                   :::*                  
LISTEN      0      100                     ::1:25 

5.客户端登录ftp 192.168.19.128 创建目录, 并在服务端验证

[root@yh1 ~]# yum -y install ftp
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
---> 软件包 ftp.x86_64.0.0.17-67.el7 将被 安装
--> 解决依赖关系完成
...
[root@yh1 ~]# ftp 192.168.19.128
Connected to 192.168.19.128 (192.168.19.128).
220 (vsFTPd 3.0.2)
Name (192.168.19.128:root): yh
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir yh
257 "/home/yh/yh" created


[root@yh ~]# ls /home/yh/
yh
[root@yh ~]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值