Vsftp 服务器搭建

简介:

一、说明    

 vsftp提供3种远程的登录方式:

1、匿名登录方式

 就是不需要用户名,密码。就能登录到服务器电脑里面

2、本地用户方式

 需要帐户名和密码才能登录。而且,这个帐户名和密码,都是在你linux系统里面,已经有的用户。

3、虚拟用户方式

 同样需要用户名和密码才能登录。但是和上面的区别就是,这个用户名和密码,在你linux系统中是没有的(没有该用户帐号)

 

二、安装

使用yum install    vsftpd 命令安装

安装后/etc/vsftpd 目录下面一般有下面的几个文件

1、/etc/vsftpd/vsftpd.conf  主配置文件

2、/usr/sbin/vsftpd 启动脚本

3、/etc/pam.d/vsftpd PAM认证文件

4、/etc/vsftpd/user_list 禁止或允许使用vsftpd的用户列表文件。这个文件中指定的用户缺省情况(即在/etc/vsftpd /vsftpd.conf中设置userlist_deny=YES)下也不能访问FTP服务器,在设置了userlist_deny=NO时,仅允许 user_list中指定的用户访问FTP服务器。

5/etc/vsftpd/ftpusers 禁止使用vsftpd的用户列表文件。记录不允许访问FTP服务器的用户名单,管理员可以把一些对系统安全有威胁的用户账号记录在此文件中,以免用户从FTP登录后获得大于上传下载操作的权利,而对系统造成损坏。

匿名用户的登录目录下也有如下所示的几个文件

1/var/ftp 匿名用户主目录;本地用户主目录为:/home/用户主目录,即登录后进入自己家目录

2/var/ftp/pub 匿名用户的下载目录,此目录需赋权根chmod 1777 pub1为特殊权限,使上载后无法删除

日志回滚目录下,有一个设置日志目录的文件

1/etc/logrotate.d/vsftpd.log Vsftpd的日志文件

 

三、启动、重启、加为系统服务

1、service vsftpd start 启动

2、service vsftpd stop 关闭

3、service vsftpd restart 重启

4、chkconfig --add vsftpd 加为服务

5、chkconfig vsftpd on 设为开机启动

 

四、配置主配置文件

cd /etc/vsftpd,下面的是现网的配置

[root@localhost vsftpd]# grep -v "#" vsftpd.conf 
anonymous_enable=NO #不允许匿名访问

local_enable=YES#允许本地用户登录
write_enable=YES#允许本地用户对FTP服务器文件具有写权限

local_umask=022#使用ftp本地文件权限
anon_upload_enable=NO#禁止匿名用户上传文件

anon_mkdir_write_enable=NO#禁止匿名用户创建文件夹
xferlog_enable=YES#让系统自动维护上传和下载的日志文件

xferlog_file=/var/log/xferlog#日志文件路径
xferlog_std_format=YES#使用标准的日志格式
idle_session_timeout=3600#表示空闲的用户会话中断时间为600秒

data_connection_timeout=3600#设置数据连接超时时间为3600秒

ftpd_banner=Welcome to MY FTP service.#登录服务后的欢迎信息
chroot_local_user=YES#定义所有用户都被限制在主目录下

chroot_list_enable=YES#定义限制在家目录的用户名单

chroot_list_file=/etc/vsftpd/user_list#限制用户家目录文件所在的路径
listen_port=6162
#控制连接端口,系统默认是21
listen=YES#vsftpd以独立方式运行

pam_service_name=vsftpd#验证方式

userlist_enable=YES#是否抑制userlist里用户登录服务器

userlist_deny=YES#不允许user_list文件中的用户登录ftp服务器
userlist_file=/etc/vsftpd/ftpusers#user_list_file文件路径
tcp_wrappers=YES#启用 tcp_wrappers做为主机的访问控制方式
pasv_enable=YES#启用被动模式

port_enable=NO#关闭主动模式
pasv_min_port=30000#被动连接最小端口
pasv_max_port=30999#被动连接最大端口
port_promiscuous=yes#关闭port模式安全检查
pasv_addr_resolve=yes#可以在pasv模式使用主机名

listen_address=172.30.65.119#监听地址

pasv_promiscuous=yes#关闭pasv模式安全检查

reverse_lookup_enable=NO#关闭反向DNS解析,该项要注意,使用移动的网络不关闭改项可能会报错

 

五、测试服务器登录和传输文件情况

mkdir /test

useradd test -d /test -s /bin/bash

passwd test

chown test:test /test

service vsftpd restart

登录测试,如下所示

[root@192 ~]# ftp 192.168.10.102 6162

Connected to 192.168.10.102(192.168.10.102).

220 Welcome to MY FTP service.

Name (192.168.10.102:root): test

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> ls

227 Entering Passive Mode(192,168,10,102,118,20).

150 Here comes the directory listing.

-rw-r--r--    1 506     507           286 Jul 12 05:21 梅梅.txt

-rw-r--r--    1 506     507         12023 Jul 12 05:25 脚本文件.docx

226 Directory send OK.

ftp> get 梅梅.txt

local: 梅梅.txt remote: 梅梅.txt

227 Entering Passive Mode(192,168,10,102,119,139).

150 Opening BINARY mode data connection for梅梅.txt (286 bytes).

226 Transfer complete.

286 bytes received in 0.000878 secs (325.74Kbytes/sec)

ftp> ls

227 Entering Passive Mode(192,168,10,102,118,79).

150 Here comes the directory listing.

-rw-r--r--    1 506     507           286 Jul 12 05:21 梅梅.txt

-rw-r--r--    1 506     507         12023 Jul 12 05:25 脚本文件.docx

226 Directory send OK.

ftp> !dir

anaconda-ks.cfg   check_mem.sh         install.log             latin-numbers  myfile2.xml    nginx-1.10.1.tar.gz  quotas.sorted sales.sorted  test.txt  梅梅.txt

check_filehandle  example.txt       install.log.syslog  myapp.data          nginx-1.10.1 quotas                       sales         testfile            third.sh

ftp> put nginx-1.10.1.tar.gz

local: nginx-1.10.1.tar.gz remote:nginx-1.10.1.tar.gz

227 Entering Passive Mode(192,168,10,102,120,110).

150 Ok to send data.

226 Transfer complete.

909077 bytes sent in 0.0828 secs (10982.64Kbytes/sec)

 

 

六、测试用户是否可以切换出主目录

1、所有用户都被限制在其主目录下,使用chroot_list_file指定的用户列表,这些用户作为“例外”,不受限制。

chroot_local_user=YES#禁止用户切换到主目录以外的目录

chroot_list_enable=YES#启用限制用户的列表

chroot_list_file=/etc/vsftpd/user_list#限制用户列表的文件路径

[root@192 vsftpd]# grep -v "#"user_list #把test用户加入限制名单中

root

test

可以看到test用户可以切换到自己的主目录以外的目录。除了列表里的用户,别的用户不能切换到自己主目录以外的目录。如下所示:

ftp> cd /home

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode(192,168,10,102,119,22).

150 Here comes the directory listing.

drwx------    4 505     506          4096 Jul 12 05:14hello

drwx------    4 501     502          4096 Jul 10 23:15huaxia

drwx------  19 500      501          4096 Jul 11 22:04 nagios

drwx------    5 502     503          4096 Jul 12 00:05 tom

drwx------    5 503     504          4096 Jul 12 05:06vftp

226 Directory send OK.

ftp> cd /

250 Directory successfully changed.

 

2所有用户都被限制在其主目录下,不使用chroot_list_file指定的用户列表,没有例外。

chroot_local_user=YES#禁止用户切换到主目录以外的目录

chroot_list_enable=NO#不启用限制用户的列表

chroot_list_file=/etc/vsftpd/user_list#限制用户列表的文件路径

[root@192 vsftpd]# grep -v "#"user_list #test用户加入限制名单中

root

test

可以看到所有用户都不可以切换到自己的主目录以外的目录。不使用chroot_list_file指定的用户列表,没有任何例外用户。如下所示:

ftp> cd /home

550 Failed to change directory.

 

3、所有用户都不被限制在其主目录下,使用chroot_list_file指定的用户列表,这些用户作为例外,受到限制。

chroot_local_user=NO#允许用户切换到主目录以外的目录

chroot_list_enable=YES#启用限制用户的列表

chroot_list_file=/etc/vsftpd/user_list#限制用户列表的文件路径

[root@192 vsftpd]# grep -v "#"user_list #把test用户加入限制名单中

root

test

可以看到所有用户都可以切换到自己的主目录以外的目录。chroot_list_file指定的用户列表里的用户作为例外,不能切换到主目录以外的目录。如下所示:

 

[root@192 ~]# ftp 192.168.10.102 6162

Connected to 192.168.10.102(192.168.10.102).

220 Welcome to MY FTP service.

Name (192.168.10.102:root): hello#使用hello用户登录,hello用户不在user_list列表中

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> cd /home

250 Directory successfully changed.

ftp> cd /usr

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode(192,168,10,102,121,19).

150 Here comes the directory listing.

dr-xr-xr-x    2 0       0           40960 Jul 11 22:41 bin

drwxr-xr-x    2 0       0            4096 Sep 23  2011 etc

drwxr-xr-x    2 0       0            4096 Sep 23  2011 games

drwxr-xr-x  61 0        0            4096 Jul 11 22:12 include

dr-xr-xr-x  27 0        0            4096 Oct 17  2015 lib

dr-xr-xr-x 114 0        0           61440 Jul 11 22:41 lib64

drwxr-xr-x  26 0        0           12288 Jul 11 22:41 libexec

drwxr-xr-x  14 0        0            4096 Oct 17  2015 local

dr-xr-xr-x    2 0       0           12288 Jul 11 22:41sbin

drwxr-xr-x 227 0        0           12288 Oct 17  2015 share

drwxr-xr-x    4 0       0            4096 Oct 17  2015 src

lrwxrwxrwx    1 0       0              10 Oct 17  2015 tmp -> ../var/tmp

226 Directory send OK.

 

[root@192 ~]# ftp 192.168.10.102 6162

Connected to 192.168.10.102(192.168.10.102).

220 Welcome to MY FTP service.

Name (192.168.10.102:root): test#使用test用户登录

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> cd /home

550 Failed to change directory.

ftp> cd /usr

550 Failed to change directory.

ftp> cd /dev

550 Failed to change directory.

ftp> cd /opt

550 Failed to change directory.

 

4、所有用户都不被限制在其主目录下,不使用chroot_list_file指定的用户列表,没有例外。

chroot_local_user=NO#允许用户切换到主目录以外的目录

chroot_list_enable=NO#不启用限制用户的列表

chroot_list_file=/etc/vsftpd/user_list#限制用户列表的文件路径

[root@192 vsftpd]# grep -v "#"user_list #把test用户加入限制名单中

root

test

可以看到所有用户都可以切换到自己的主目录以外的目录。

[root@192 ~]# ftp 192.168.10.102 6162

Connected to 192.168.10.102(192.168.10.102).

220 Welcome to MY FTP service.

Name (192.168.10.102:root): hello#使用hello用户登录,hello用户不在user_list列表中

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> cd /opt

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode(192,168,10,102,119,108).

150 Here comes the directory listing.

drwx------    2 0       0           16384 Oct 17  2015 lost+found

drwxr-xr-x    2 0       0            4096 Nov 22  2013 rh

226 Directory send OK.

ftp> bye

221 Goodbye.

[root@192 ~]# ftp 192.168.10.102 6162

Connected to 192.168.10.102(192.168.10.102).

220 Welcome to MY FTP service.

Name (192.168.10.102:root): test#使用test用户登录

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> cd /usr

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode(192,168,10,102,118,148).

150 Here comes the directory listing.

dr-xr-xr-x    2 0       0           40960 Jul 11 22:41 bin

drwxr-xr-x    2 0       0            4096 Sep 23  2011 etc

drwxr-xr-x    2 0       0            4096 Sep 23  2011 games

drwxr-xr-x  61 0        0            4096 Jul 11 22:12 include

dr-xr-xr-x  27 0        0            4096 Oct 17 2015 lib

dr-xr-xr-x 114 0        0           61440 Jul 11 22:41 lib64

drwxr-xr-x  26 0        0           12288 Jul 11 22:41 libexec

drwxr-xr-x  14 0        0            4096 Oct 17  2015 local

dr-xr-xr-x    2 0       0           12288 Jul 11 22:41sbin

drwxr-xr-x 227 0        0           12288 Oct 17  2015 share

drwxr-xr-x    4 0       0            4096 Oct 17  2015 src

lrwxrwxrwx    1 0       0              10 Oct 17  2015 tmp -> ../var/tmp

226 Directory send OK.


七、添加虚拟用户

1、添加虚拟用户口令文件

cd /etc/vsftpd

vim vuser.txt

添加虚拟用户名和密码,一行用户名,一行密码,以此类推。奇数行为用户名,偶数行为密码。

test01#用户名

123456#密码

test02#用户名

123456#密码


2.、生成虚拟用户口令认证文件,将刚添加的vuser.txt虚拟用户口令文件转换成系统识别的口令认证文件。

Yum install db4 db4-utils

 db_load -T -t hash -f /etc/vsftpd/vuser.txt /etc/vsftpd/vuser.db#vuser.db是认证生成的数据库文件


3、编辑vsftpd的PAM认证文件

vim /etc/pam.d/vsftpd

将里面其他的都注释掉,添加下面这两行:

auth required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser#db路径不用写成/etc/vsftpd/vuser.db

account required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser#db路径不用写成/etc/vsftpd/vuser.db

注意:64位的系统要把/lib/改成/lib64这样认证时不会报错,必须把原先的认证方式注释掉,因为使用的认证模块已经改变。


4、建立本地映射用户并设置宿主目录权限

所有的FTP虚拟用户需要使用一个系统用户,这个系统用户不需要密码。

mkdir /myftp

useradd myftp -d /myftp -s /bin/false


5、配置vsftpd.conf(设置虚拟用户配置项)

grep -v "#" vsftpd.conf


anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=NO
anon_mkdir_write_enable=NO
dirmessage_enable=YES
xferlog_enable=YES
xferlog_file=/var/log/xferlog
xferlog_std_format=YES
idle_session_timeout=3600
data_connection_timeout=3600
ftpd_banner=Welcome to MY FTP service.
chroot_local_user=NO
chroot_list_enable=NO
chroot_list_file=/etc/vsftpd/user_list
listen_port=6162

listen=YES

pam_service_name=vsftpd
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd/ftpusers
tcp_wrappers=YES
port_enable=NO
pasv_enable=YES
pasv_min_port=30000
pasv_max_port=30999
port_promiscuous=yes
pasv_addr_resolve=yes
pasv_promiscuous=yes
listen_address=172.30.65.119
user_config_dir=/etc/vsftpd/vuser_conf#新加的虚拟用户配置文件存放目录


6、进入虚拟用户配置文件目录,配置虚拟用户配置文件

cd /etc/vsftpd/vuser_conf

vim test01#注意虚拟用户配置文件名必须与在vuser.txt添加的两个用户名相同

guest_enable=yes#开启虚拟用户

local_root=/myftp#虚拟用户映射的目录

guest_username=myftp#虚拟用户对应的系统用户

anon_world_readable_only=no#关闭匿名全局浏览
write_enable=yes#全局写入权限开启
anon_mkdir_write_enable=yes#允许匿名用户创建目录
anon_other_write_enable=yes#允许匿名用户具有建立目录,上传之外的权限
anon_upload_enable=yes#匿名上传开启


vim test02#注意虚拟用户配置文件名必须与在vuser.txt添加的两个用户名相同

guest_enable=yes#开启虚拟用户

local_root=/myftp/test02#虚拟用户映射的目录

guest_username=myftp#虚拟用户对应的系统用户

anon_world_readable_only=no#关闭匿名全局浏览

write_enable=yes#全局写入权限开启
anon_mkdir_write_enable=yes#允许匿名用户创建目录

anon_other_write_enable=yes#允许匿名用户具有建立目录,上传之外的权限

anon_upload_enable=yes#匿名上传开启

7、测试使用虚拟用户帐号登录

注意:一定要先把/myftp,虚拟用户映射目录的所有权限,改成虚拟用户对应的系统用户myftp,不然会报文件无法传输的错误。

chown -R myftp:myftp /myftp

[root@localhost ~]# ftp 172.30.65.119 6162
Connected to 172.30.65.119 (172.30.65.119).
220 Welcome to MY FTP service.
Name (172.30.65.119:root): test01#
使用test01登录

331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

ftp> !dir
anaconda-ks.cfg   example.txt       latin-numbers  nginx-1.10.1   quotas.sorted  testfile  梅梅.txt
check_filehandle  install.log       myapp.data     nginx-1.10.1.tar.gz  sales   test.txt
check_mem.sh   install.log.syslog  myfile2.xml    quotas    sales.sorted  third.sh

ftp> dir
227 Entering Passive Mode (172,30,65,119,118,178).
150 Here comes the directory listing.
-rw-------    1 507      508          2708 Jul 13 17:29 check_mem.sh
drwxr-xr-x    2 507      508          4096 Jul 13 17:22 test02
-rw-------    1 507      508           286 Jul 13 17:32 梅梅.txt
226 Directory send OK.

ftp> put check_mem.sh#上传正常

local: check_mem.sh remote: check_mem.sh
227 Entering Passive Mode (172,30,65,119,120,52).
150 Ok to send data.
226 Transfer complete.
2708 bytes sent in 0.03 secs (90.17 Kbytes/sec)

ftp> get 梅梅.txt#下载正常

local: 梅梅.txt remote: 梅梅.txt
227 Entering Passive Mode (172,30,65,119,118,185).
150 Opening BINARY mode data connection for 梅梅.txt (286 bytes).
226 Transfer complete.
286 bytes received in 0.000122 secs (2344.26 Kbytes/sec)

 

[root@localhost ~]# ftp 172.30.65.119 6162
Connected to 172.30.65.119 (172.30.65.119).
220 Welcome to MY FTP service.
Name (172.30.65.119:root): test02#使用test02登录

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,30,65,119,120,223).
150 Here comes the directory listing.
226 Directory send OK.

ftp> !dir
anaconda-ks.cfg   example.txt       latin-numbers  nginx-1.10.1   quotas.sorted  testfile  梅梅.txt
check_filehandle  install.log       myapp.data     nginx-1.10.1.tar.gz  sales   test.txt
check_mem.sh   install.log.syslog  myfile2.xml    quotas    sales.sorted  third.sh
ftp> pwd
257 "/"

ftp> put nginx-1.10.1.tar.gz#上传正常

local: nginx-1.10.1.tar.gz remote: nginx-1.10.1.tar.gz
227 Entering Passive Mode (172,30,65,119,121,6).
150 Ok to send data.
226 Transfer complete.
909077 bytes sent in 0.127 secs (7164.86 Kbytes/sec)

ftp> get openssh-6.8p1.tar.gz#下载正常

local: openssh-6.8p1.tar.gz remote: openssh-6.8p1.tar.gz
227 Entering Passive Mode (172,30,65,119,118,225).
150 Opening BINARY mode data connection for openssh-6.8p1.tar.gz (1475953 bytes).
226 Transfer complete.
1475953 bytes received in 0.116 secs (12705.00 Kbytes/sec)



八、注意事项

1、如主机启用了防火墙,需在防火墙中放通ftp控制端口和数据端口。

2、测试过程中,如有报错。可根据报错信息,到网上查询相应的资料。

3、关闭机器上的selinux

本文转自服务器运维博客51CTO博客,原文链接http://blog.51cto.com/shamereedwine/1828354如需转载请自行联系原作者


neijiade10000

相关文章
|
弹性计算 运维 安全
搭建自己的FTP服务器
网上较多的都是centos上搭建ftp,因为centos8已停止更新,阿里云上的ECS搭建ftp教程也是基于centos系统,我比较喜欢用Ubuntu来搭建自己的FTP服务器,个人喜好不同,勿喷。
2352 1
搭建自己的FTP服务器
|
弹性计算 数据安全/隐私保护 Windows
如何在阿里云服务器搭建FTP服务器?
如何在阿里云服务器搭建FTP服务器?
2163 0
如何在阿里云服务器搭建FTP服务器?
|
Web App开发
Day 1 搭建FTP服务器
学习创建ESC实例,如何如何连接ESC实例,安装并配置vsftpd服务
Day 1  搭建FTP服务器
|
存储 弹性计算 网络协议
搭建FTP服务器
本文是基于阿里云ECS搭建FTP服务器的记录
2404 0
搭建FTP服务器
|
数据安全/隐私保护 Apache 安全
Debian 使用VSFtpd安装配置架设FTP服务器
Vsftpd的安装: aptitude update //更新数据源列表 aptitude install vsftpd //安装vsftpd Vsftpd的文件结构: /usr/sbin/vsftpd —- VSFTPD的主程序 /etc/init.
3710 0