搭建FTP服务

FTP简介

  • FTP(File Transfer Protocol,文件传输协议) 是 TCP/IP 协议组中的协议之一。FTP协议包括两个组成部分,其一为FTP服务器,其二为FTP客户端。其中FTP服务器用来存储文件,用户可以使用FTP客户端通过FTP协议访问位于FTP服务器上的资源。

FTP端口

  • 默认情况下FTP协议使用TCP端口中的 20和21这两个端口,其中20用于传输数据,21用于传输控制信息。但是,是否使用20作为传输数据的端口与FTP使用的传输模式有关,如果采用主动模式,那么数据传输端口就是20;如果采用被动模式,则具体最终使用哪个端口要服务器端和客户端协商决定

FTP数据连接模式

  • ftp有2种数据连接模式:命令连接和数据连接
    • 命名连接:是指文件管理类命令,始终在线的持久性连接,直到用户退出登录止
    • 数据连接:是指数据传输,按需创建及关闭的连接
  • 数据传输格式有以下两种:
    • 文件传输
    • 二进制传输
  • 数据传输模式有两种:
    • 主动模式:由服务器端创建数据连接
    • 被动模式:有客户端创建数据连接
  • 两种数据传输模式的建立过程:
    • 主动模式:命令连接——客户端以一个随机端口(随机端口大于1023)来连服务器端的21号端口;数据连接——服务器端以自己的20号端口去连客户端创建命令连接时使用的随机端口+1的端口号进行连接
    • 被动模式:命令连接——客户端以一个随机端口(随机端口大于1023)来连服务器端的21号端口,服务端并告知客服端一个随机端口;数据连接——客户端以创建命令连接的端口+1的端口号去连服务器端通过命令连接告知自己的一个随机端口号来创建数据连接

vsftpd常见的配置参数

参数作用
anonymous启用匿用户登录
anon_upload_enable=YES允许匿名用户上传
anon_mkdir_write_enable=YES允许匿名用户创建目录,但不能删除(与anon_other_write_enable连用)
anon_orther_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=“INFO”制定欢迎信息,登录ftp时自动显示

搭建要求

  • 安装vsftpd服务
  • 配置匿名用户ftp
  • 配置虚拟用户ftp
  • 配置系统用户ftp
  • 安装并配置完成后要使用ftp客户端工具登录验证

搭建环境#

  • 本实验所用redhat7.0系统;
  • 防火墙、selinux已关闭;
  • 本实验所用地址如下:
主机名角色IP地址
server服务器172.16.12.131
client客户端172.16.12.132

相关文件

  • 主配置文件
  • /etc/vsftpd/vsftpd.conf
  • 用户认证配置文件
  • /etc/pam,d/vsftpd
  • 匿名用户的共享资源位置
  • /var/ftp/pub
  • 系统用户访问为用户的家目录
  • 虚拟用户访问为被映射成虚拟用户的家目录

搭建步骤

  • 安装vsftpd
yum -y install vsftpd
  • 启动vsftpd服务
systemctl start vsftpd

匿名用户

  • 配置匿名用户,利用匿名用户访问ftp
## 在客户端安装ftp命令,以匿名用户登录FTP,匿名账号为anonymous,密码直接确认 ##
[root@client ~]# yum -y install ftp

## 匿名用户默认开启,若没有开启则将NO改为YES ##
anonymous_enable=YES

## 登录172.16.12.130 ftp服务器,账号为anonymous,密码直接确认 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

## 登录之后 ,ls 可以看到,匿名用户访问的是pub目录 ##
ftp> ls
227 Entering Passive Mode (172,16,12,131,46,39).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0               6 Aug 03  2017 pub
226 Directory send OK.
ftp> 

## 进入pub目录,在ftp目录中创建目录,目录没有权限创建 ##
ftp> mkdir aaa
550 Permission denied.
ftp> 

## 要想在ftp目录中写,就必须修改配置, ##
[root@client ~]# vim /etc/vsftpd/vstpd.conf
找到这两行取消这注释,或者重新再写
anon_upload_enable=YES #允许匿名用户上传
anon_mkdir_write_enable=YES #允许匿名用户创建目录
添加一行
anon_other_write_enable=YES #允许匿名用户除创建和上传之外的权限,如删除、重命名
:wq #保存退出

## 因为匿名用户登录ftp所用的账户是ftp用户,所以将/var/ftp/pub的属主和属组改为ftp ##
chown ftp.ftp /var/ftp/pub

## 重启vsftpd服务 ##
systemctl restart vsftpd

## 再在/var/ftp/pub目录下创建目录,这时可以写 ##
ftp> mkdir test
257 "/pub/test" created
ftp> ls
227 Entering Passive Mode (172,16,12,131,53,244).
150 Here comes the directory listing.
drwx------    3 14       50             16 Sep 13 00:03 aaa
drwx------    2 14       50              6 Sep 13 00:09 test
226 Directory send OK.
ftp> 

系统用户

  • 配置系统用户,利用tom用户进行访问
## 还原匿名用户配置,注销添加的配置文件 ##
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
#anon_upload_enable=YES

## 创建tom用户,并设置密码 ##
[root@localhost ~]# useradd tom
[root@localhost ~]# echo 123456 | passwd --stdin tom
Changing password for user tom.
passwd: all authentication tokens updated successfully.

## 修改/etc/vsftpd/vsftpd.conf配置文件,开启如下参数 ##
local_user=YES
write_eanble=YES
local_umask=022

## 利用tom登录FTP服务器,账号为tom,密码为123456 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

## 查看登录到ftp服务器的家目录 ##
ftp> ls
227 Entering Passive Mode (172,16,12,131,211,38).
150 Here comes the directory listing.
drwxr-xr-x    2 1000     1000            6 Sep 13 00:59 123
226 Directory send OK.
ftp> cd ..
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (172,16,12,131,81,233).
150 Here comes the directory listing.
drwx------    3 1000     1000           69 Sep 13 00:59 tom
226 Directory send OK.
ftp> pwd
257 "/home" #可以看出利用tom账户登录ftp,ftp家目录就是tom用户的家目录
ftp> 

## 为了系统安全,将用户锁定在家目录里面,修改配置文件,取消注释或则添加一条 ##
chroot_local_user=YES #禁锢所有FTP本地用户在其家目录中

## 重新启动vsftpd服务后,再次登录,tom用户无法登录 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed. #登录失败
421 Service not available, remote server has closed connection
ftp> 

## 修改配置文件,添加一条 ##
allow_writeable_chroot=YES #允许被禁锢

## 重启vsftpd服务,再次登录tom用户 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
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 (172,16,12,131,165,161).
150 Here comes the directory listing.
drwxr-xr-x    2 1000     1000            6 Sep 13 00:59 123
226 Directory send OK.

## 利用tom用户上传文件,利用windows上传文件(linux上传文件有问题)##
ftp> put C:\Users\dell\Desktop\新建文本文档.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 463 字节,用时 0.07秒 6.61千字节/秒。
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
123
456.txt
新建文本文档.txt  #上传的文件
226 Directory send OK.
ftp: 收到 41 字节,用时 0.00秒 41000.00千字节/秒。
ftp>

## 利用tom用户进行下载,利用windows将文件下载到本地(linux上传下载有问题) ##
ftp> get 456.txt
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for 456.txt (463 bytes).
226 Transfer complete.
ftp: 收到 463 字节,用时 0.00秒 463000.00千字节/秒。
ftp>

虚拟用户

  • 配置虚拟用户,创建一个本地用户vuser,两个虚拟用户分别为tom,list。
## 注销如下两行,恢复上面改动的配置,删除用户 ##
allow_writeable_chroot=YES
chroot_local_user=YES
[root@localhost ~]# userdel -r tom

## 配置yum仓库,安装依赖的程序(本地源没有需要安装的依赖程序,利用网络源安装)并修改yum源 ##
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0   8088      0 --:--:-- --:--:-- --:--:--  8138
[root@localhost ~]# sed  -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo #修改yum源
[root@localhost ~]# yum -y install epel-release #安装依赖关系

## 创建文本格式的用户名、密码列表(创建的列表规则为奇数用户名,偶数为用户密码) ##
[root@localhost ~]# echo 'tom' >> /etc/vsftpd/user.list
[root@localhost ~]# echo '123456' >> /etc/vsftpd/user.list 
[root@localhost ~]# echo 'list' >> /etc/vsftpd/user.list 
[root@localhost ~]# echo '123456' >> /etc/vsftpd/user.list 
[root@localhost ~]# cat /etc/vsftpd/user.list 
tom
123456
list
123456

## 安装db4工具(依赖前面的epel_release包),将创建/etc/vsftpd/user.list文件使用db4工具进行转换 ##
[root@localhost ~]# yum -y install db4*
[root@localhost ~]# db_load -T -t hash -f /etc/vsftpd/user.list /etc/vsftpd/user.db

## 为了提高虚拟用户账号的安全性,将文件权限设置为600 ##
[root@localhost ~]# chmod 600 /etc/vsftpd/user.*

##添加虚拟用户的映射账号、创建ftp根目录,并将权限改为755 ##
[root@localhost ~]# useradd -d /var/vftproot -s /sbin/nologin vuser
[root@localhost ~]# ll -d /var/vftproot/
drwx------ 2 vuser vuser 59 Sep 12 23:57 /var/vftproot/
[root@localhost ~]# chmod 755 /var/vftproot/
[root@localhost ~]# ll -d /var/vftproot/
drwxr-xr-x 2 vuser vuser 59 Sep 12 23:57 /var/vftproot/

## 为虚拟用户建立PAM认证,修改/etc/pam.d/vsftpd文件,将文件里面的内容删除或者注销(留下第一行),并添加两行内容(防止出现问题,先备份一份) ##
[root@localhost ~]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
[root@localhost ~]# vim /etc/pam.d/vsftpd
#%PAM-1.0
auth    required        pam_userdb.so   db=/etc/vsftpd/user 
account required        pam_userdb.so   db=/etc/vsftpd/user

## 修改/etc/vsftpd/vsftpd.conf配置文件,添加虚拟用户支持 ##
guest_enable=YES
guest_username=vuser

## 修改/etc/vsftpd/vsftpd.conf配置文件,建立虚拟用户配置文件 ##
user_config_dir=/etc/vsftpd/vuser_dir
allow_writeable_chroot=YES

## 创建虚拟用户目录(/etc/vsftpd/vuser_dir),为两个虚拟用户创建不同的配置文件(创建两个用户配置文件) ##
[root@localhost ~]# mkdir /etc/vsftpd/vuser_dir
[root@localhost ~]# cd /etc/vsftpd/vuser_dir/
[root@localhost vuser_dir]# touch tom list

## 设置tom用户可上传文件、创建目录;设置list用户只有默认的下载权限(/etc/vsftpd/vuser_dir/list文件为空)
[root@localhost ~]# vim /etc/vsftpd/vuser_dir/tom
anon_upload_enable=YES
anon_mkdir_write_enable=YES
:wq
注:虚拟用户是通过匿名访问的,所以要开启匿名访问功能。

##重启服务 ##
[root@localhost ~]# systemctl restart vsftpd

## 用tom虚拟用户登录ftp服务器,验证是否可以上传文件,创建目录但不能删除目录 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): tom
331 Please specify the password.
Password: #密码为123456
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir test
257 "/test" created
ftp> ls
227 Entering Passive Mode (172,16,12,131,48,140).
150 Here comes the directory listing.
drwx------    2 1000     1000            6 Sep 13 04:25 test
226 Directory send OK.
ftp> rmdir test
550 Permission denied. #权限拒绝
ftp> 

## 利用list用户登录到ftp服务器,验证是否只能查看 ##
[root@client ~]# ftp 172.16.12.131
Connected to 172.16.12.131 (172.16.12.131).
220 (vsFTPd 3.0.2)
Name (172.16.12.131:root): list
331 Please specify the password.
Password: #密码123456
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,131,249,198).
150 Here comes the directory listing.
drwx------    2 1000     1000            6 Sep 13 04:25 test
226 Directory send OK.
ftp> mkdir list
550 Permission denied. #权限拒绝
ftp> 

搭建遇到的问题

  • 匿名用户登录后,不能创建目录,但是配置文件设置了匿名用户允许创建目录和删除目录,经排查,需要将匿名用户的家目录下里面的/var/ftp/pub的属主和属组修改为ftp,因为匿名用户登录ftp服务器是将用户名映射为ftp系统用户登录的,而/var/ftp/pub的属主和属组是root,而权限是755(注意,不能将/var/ftp/pub权限改为777,原因未知,修改报错)
  • 利用本地用户登录FTP服务器时,将用户禁锢在家目录后,用户就无法登录,解决办法是将allow_writeable_chroot=YES(允许被禁锢的用户家目录有写的权限)。
  • 利用本地用户登录FTP服务器进行上传个和下载操作时,linux客户端不能
    上传和下载,而windowos客户端能上传下载,原因未可知。(这两个上传下载验证时都没有修改过配置文件)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值