vsftpd配置以及虚拟用户配置

Vsftp配置以及虚拟用户配置

1,安装

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

 

2,Vsftp启动模式

通过rpm -ql vsftpd | grep conf可以看到不同的配置文件说明,如下:

[root@rhel6 ~]# rpm -ql vsftpd | grep conf

/etc/vsftpd/vsftpd.conf

/etc/vsftpd/vsftpd_conf_migrate.sh

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

/usr/share/man/man5/vsftpd.conf.5.gz

 

2.1 守护进程模式

首先,看看官方配置文件示例中的说法:

more /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration

1,创建守护进程配置文件

找到vsftpd.xinetd

拷贝vsftpd.xinetd为/etc/xinetd.d/vsftpd:

命令如下:

查找:

[root@rhel6 ~]# rpm -ql vsftpd | grep xinetd

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd <-这个

/usr/share/doc/vsftpd-2.2.2/vsftpd.xinetd

拷贝:

[root@rhel6 ~]# cp /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd /etc/xinetd.d/vsftpd

[root@rhel6 ~]# ll /etc/xinetd.d/

total 8

-rw-r--r--. 1 root root 332 Mar 28  2014 rsync

-rw-r--r--  1 root root 533 Oct 15 11:38 vsftpd

然后修改配置/etc/xinetd.d/vsftpd,详情查看附录1

 

2,创建vsftpd配置文件

查找到官方示例配置文件

将官方示例复制为vsftpd配置文件,命令如下:

[root@rhel6 ~]# rpm -ql vsftpd | grep conf

/etc/vsftpd/vsftpd.conf

/etc/vsftpd/vsftpd_conf_migrate.sh

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

/usr/share/man/man5/vsftpd.conf.5.gz

[root@rhel6 ~]# cp /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf /etc/vsftpd/vsftpd.conf

注意,上面可以看到一个红色和两个蓝色的行,都是配置文件,红色的是用来配置xinetd模式的配置文件;第二个配置文件,可以看他所在的目录,是INTERNET_SITE_NOINETD,意思是网络站点-非inetd模式,即standalone模式,后面会讲;然后第三个,同样看配置文件所在目录,是virtual_users,表示是虚拟用户配置文件,这个后面会重点学习。

 

修改配置vsftpd配置文件,参考附录2

 

3,重启守护进程

[root@rhel6 ~]# /etc/init.d/xinetd status

xinetd is stopped

[root@rhel6 ~]# /etc/init.d/xinetd start

Starting xinetd: [  OK  ]

 

 

2.2独立模式

独立模式就是不依靠xinetd来启动,直接自己启动,主要的变化如下:

1,配置文件:
配置文件依旧是复制到/etc/vsftpd/vsftpd.conf,但是有个参数需要添加进去:listen=YES,表示有本程序来监听端口。

2,最大连接数和每个ip连接数,之前是在/etc/xinetd.d/vsftpd配置的,既然是独立模式,那么就在/etc/vsftpd/vsftpd.conf中配置:
max_clients=200

max_per_ip=4

3,监听虚拟ip:

只需要为每个虚拟ip分配一个vsftpd副本即可,并且要为每个副本指定配置文件:

在配置文件中指定监听的ip:

listen_address=192.168.1.2

vsftpd /etc/vsftpd.conf.site1 &

           

 

3,配置虚拟用户(用户主目录)

创建虚拟用户数据库

我们将使用pam_userdb对虚拟用户进行身份验证。 这需要用户名/密码文件,采用“ db”格式-一种常见的数据库格式。要创建“ db”格式的文件,请首先使用用户名和密码交替显示即奇数行是账号,偶数行是密码,账号在密码之前

这里我创建了一个账号密码文本文件:

[root@rhel6 ~]# cat /usr/local/login.txt

test

test

zhangsan

123456

这里有两个账号:testzhangsan,密码分别是test123456

然后切换到root账户,创建数据文件,命令如下:

db_load -T -t hash -f /usr/local/login.txt /etc/vsftpd_login.db

注意,如果机器上有多个版本的db_load命令,一般是用db3_load

上面的命令会创建/etc/vsftpd_login.db数据库文件,修改权限:

chmod 600 /etc/vsftpd_login.db

 

创建pam文件来使用账户数据库

先看看在示例中有没有pam例子:

[root@rhel6 ~]# rpm -ql vsftpd | grep pam

/etc/pam.d/vsftpd

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam

[root@rhel6 ~]# cat /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam

auth required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

account required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

可以看到本身就有pam文件例子,将这个示例pam文件拷贝到/etc/pam.d/下:

[root@rhel6 ~]# cp /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam /etc/pam.d/vsftpd

这里记得将内容修改一下,原本的内容如下:

auth required /lib/security/pam_userdb.so db=/etc/vsftpd_login

account required /lib/security/pam_userdb.so db=/etc/vsftpd_login

修改为:

auth required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

account required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

否则用户校验会出问题。

创建虚拟用户本地文件

3.1,创建虚拟用户

[root@rhel6 ~]# useradd -d /home/ftpsite virtual

我这里创建了一个用户是vsftpd,家目录是/home/ftpsite

3.2,然后往这里添加一些文件,并修改权限:

[root@rhel6 ~]# cp /etc/hosts /home/ftpsite

[root@rhel6 ~]# chown virtual.virtual /home/ftpsite/hosts

 

3.3创建vsftpd.conf配置文件

同样,先查看自带的虚拟用户配置文件:

[root@rhel6 ~]# rpm -ql vsftpd | grep conf

/etc/vsftpd/vsftpd.conf

/etc/vsftpd/vsftpd_conf_migrate.sh

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

/usr/share/man/man5/vsftpd.conf.5.gz

[root@rhel6 ~]# more /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

anonymous_enable=NO

local_enable=YES

write_enable=NO

anon_upload_enable=NO

anon_mkdir_write_enable=NO

anon_other_write_enable=NO

chroot_local_user=YES

guest_enable=YES

guest_username=virtual

listen=YES

listen_port=10021

pasv_min_port=30000

pasv_max_port=30999

[root@rhel6 ~]#

详细解析看附录。

直接拷贝到/etc/vsftpd下:

[root@rhel6]# cp /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf /etc/vsftpd

然后在配置文件中添加一行:pam_service_name=vsftpd;这样,才能找到pam文件用来验证用户。

然后启动vsftpd,登录测试:

[root@rhel888 vsftpd]# ftp localhost

Trying ::1...

ftp: connect to address ::1Connection refused

Trying 127.0.0.1...

Connected to localhost (127.0.0.1).

220 (vsFTPd 3.0.2)

Name (localhost:root): ddd

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 (127,0,0,1,173,146).

150 Here comes the directory listing.

226 Transfer done (but failed to open directory).

可以看到,这样做会有很多限制,首先,目录在home文件夹下,这样导致读写配置都有一些不方便,特别是在开启selinux的情况下,所以,后面再学习一种用来配置虚拟用户的方法。

4,配置虚拟用户(多用户多目录)

整个过程类似于之前配置的虚拟用户(用户主目录),但是有一些不同, 这里还是一步步来,让整个学习过程更详细:

4.1,创建虚拟用户数据库

[root@rhel6 ~]# cat /usr/local/login.txt

lanzige

123456

ddd

123456

这里有两个账号:testzhangsan,密码分别是test123456

然后切换到root账户,创建数据文件,命令如下:

db_load -T -t hash -f /usr/local/login.txt /etc/vsftpd_login.db

注意,如果机器上有多个版本的db_load命令,一般是用db3_load

上面的命令会创建/etc/vsftpd_login.db数据库文件,修改权限:

chmod 600 /etc/vsftpd_login.db

4.2,创建pam文件来使用账户数据库

先看看在示例中有没有pam例子:

[root@rhel6 ~]# rpm -ql vsftpd | grep pam

/etc/pam.d/vsftpd

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam

[root@rhel6 ~]# cat /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam

auth required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

account required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

可以看到本身就有pam文件例子,将这个示例pam文件拷贝到/etc/pam.d/下:

[root@rhel6 ~]# cp /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam /etc/pam.d/vsftpd

这里记得将内容修改一下,原本的内容如下:

auth required /lib/security/pam_userdb.so db=/etc/vsftpd_login

account required /lib/security/pam_userdb.so db=/etc/vsftpd_login

修改为:

auth required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

account required /lib64/security/pam_userdb.so db=/etc/vsftpd_login

否则用户校验会出问题。

4.3,创建配置文件

先查看自带的虚拟用户配置文件:

[root@rhel6 ~]# rpm -ql vsftpd | grep conf

/etc/vsftpd/vsftpd.conf

/etc/vsftpd/vsftpd_conf_migrate.sh

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README.configuration

/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

/usr/share/man/man5/vsftpd.conf.5.gz

[root@rhel6 ~]# more /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

anonymous_enable=NO

local_enable=YES

write_enable=NO

anon_upload_enable=NO

anon_mkdir_write_enable=NO

anon_other_write_enable=NO

chroot_local_user=YES

guest_enable=YES

guest_username=virtual

listen=YES

listen_port=10021

pasv_min_port=30000

pasv_max_port=30999

[root@rhel6 ~]#

详细解析看附录。

直接拷贝到/etc/vsftpd下:

[root@rhel6]# cp /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf /etc/vsftpd

然后在配置文件中添加一行:pam_service_name=vsftpd;这样,才能找到pam文件用来验证用户。

还需要添加一行:

user_config_dir=/etc/vsftpd/vusers_ //设定虚拟用户个人vsftp的配置文件存放路径。

在指定的文件夹下创建虚拟用户配置文件:

[root@rhel888 vusers]# cat lanzige

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES

 

[root@rhel888 vusers]# cat ddd

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES

虚拟用户配置解释:

  anon_world_readable_only=NO (表示用户可以浏览ftp目录和下载文件)

  anon_upload_enable=YES      (表示用户可以上传文件)
  anon_mkdir_write_enable=YES (表示用户具有建立目录的权限,但不能删除目录)
  anon_other_write_enable=YES (表示用户具有文件改名和删除文件的权限)

  local_root=/zz              (指定虚拟用户的登录目录)

allow_writeable_chroot=YES (用户不能跳出主目录)

这样就可以使用虚拟用户登录,但是登录的位置是virtual的家目录,由于vsftpd修改了安全策略,在登录到家目录的时候有很多限制,一般看不到文件,最好是将目录修改到其它地方。

我们这里将虚拟用户登录目录修改到其它地方,进入虚拟用户配置目录,修改虚拟用户配置文件:

[root@rhel888 vusers]# cat lanzige

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES

anon_world_readable_only=NO

local_root=/ftp/lanzige

allow_writeable_chroot=YES

 

4.4,测试

重启下vsftpd软件,就可以直接使用lanzige这个账户登录到/ftp/lanzige目录中了

 

附录:

1,守护进程的vsftpd配置文件解析:

[root@rhel6 ~]# cat /etc/xinetd.d/vsftpd

disable                 = no

socket_type             = stream

wait                    = no

上面三项表示vsftpd服务是自动启动的,使用的标准TCP接口,以及不等待,即多线程方式运行。

user                    = root

server                  = /usr/local/sbin/vsftpd

上面两项表示,服务使用/usr/local/sbin/vsftpd来处理进来的ftp请求,但是,当我们去查找的时候:

[root@rhel6 ~]# ll /usr/local/sbin/ | grep vsftp

[root@rhel6 ~]#

发现既让没有,然后使用rpm -ql vsftpd 可以看到真正所在的位置:

[root@rhel6 ~]# rpm -ql vsftpd

/etc/logrotate.d/vsftpd

/etc/pam.d/vsftpd

/etc/rc.d/init.d/vsftpd

/etc/vsftpd

/etc/vsftpd/ftpusers

/etc/vsftpd/user_list

/etc/vsftpd/vsftpd.conf

/etc/vsftpd/vsftpd_conf_migrate.sh

/usr/sbin/vsftpd

/usr/share/doc/vsftpd-2.2.2

...

...

可以看到真正在的位置是/usr/sbin/vsftpd,所以这里需要更改。

User这项表示启动的时候使用的root权限。

per_source              = 5

instances               = 200

这两项表示每个IP源最多只能有5个连接,总共最多同时有200个连接。

no_access               = 192.168.1.3

这项表示禁止连接的ip

banner_fail             = /etc/vsftpd.busy_banner

当连接被拒绝的时候,这个文件的内容将会显示给用户看

log_on_success          += PID HOST DURATION

log_on_failure          += HOST

上面两项是记录日志,将成功的日志以pid host duration这个格式记录,失败的日志只记录host,如果使用的是红帽系统,将记录在/var/log/secure中

2vsftpd配置文件解析

[root@rhel6 ~]# cat /usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf

# Access rights

anonymous_enable=YES

local_enable=NO

write_enable=NO

anon_upload_enable=NO

anon_mkdir_write_enable=NO

anon_other_write_enable=NO

# Security

anon_world_readable_only=YES

connect_from_port_20=YES

hide_ids=YES

pasv_min_port=50000

pasv_max_port=60000

# Features

xferlog_enable=YES

ls_recurse_enable=NO

ascii_download_enable=NO

async_abor_enable=YES

# Performance

one_process_model=YES

idle_session_timeout=120

data_connection_timeout=300

accept_timeout=60

connect_timeout=60

anon_max_rate=50000

可以看出,配置文件分4大块,分别是连接权限,安全,特性,性能。

连接权限:

anonymous_enable=YES               #是否允许匿名用户登录

local_enable=NO                     #是否允许本地用户登录

write_enable=NO                    #是否允许用户写入(上传修改删除)

anon_upload_enable=NO              #匿名用户的上传权限

anon_mkdir_write_enable=NO          #匿名用户的创建和删除文件夹权限

anon_other_write_enable=NO          #匿名用户的写入权限(创建和删除文件)

上面的这些配置使FTP服务器处于仅匿名模式,并且所有和上传权限被禁用。 请注意,其中大多数设置是无论如何都与默认值相同。

安全设置:

# Security

anon_world_readable_only=YES   #文件权限中,其它人的权限是可读取时,才能下载(针对匿名用户?)

connect_from_port_20=YES       #从20端口连接

hide_ids=YES                   #隐藏ID

pasv_min_port=50000           #被动模式端口号范围

pasv_max_port=60000

特性:

# Features

xferlog_enable=YES             #允许将日志记录到/var/log/vsftpd.log

ls_recurse_enable=NO          #禁用ls -R,防止ddos攻击

ascii_download_enable=NO      #禁止以ASCII模式下载

async_abor_enable=YES         #使较旧的FTP客户端可以取消正在进行的传输。

 

# Performance

one_process_model=YES         #一个连接一个进程模式

idle_session_timeout=120        #空闲会话120秒断开

data_connection_timeout=300    #数据传输超时300秒

accept_timeout=60             #PASV请求超时

connect_timeout=60            #PROT模式连接超时

anon_max_rate=50000          #每个用户最多50kbytes / sec下载速度

 

3,配置文件-虚拟用户相关解析

[root@rhel6 ~]# more /usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf

#########接入权限#########

anonymous_enable=NO

local_enable=YES

#为了安全,禁止匿名用户登录,允许本地用户登录

 

#########安全配置#########

write_enable=NO

anon_upload_enable=NO

anon_mkdir_write_enable=NO

anon_other_write_enable=NO #其它写权限,比如重命名,删除等

chroot_local_user=YES

#为了安全,不允许写,不允许匿名用户上传,创建文件夹,重命名和删除

#chroot_local_user=YES,表示用户被限制在虚拟ftp目录中, 本文件中,例子是/home/vsftpd

 

guest_enable=YES

guest_username=vsftpd

#允许使用虚拟用户,虚拟用户的用户名为vsftpd

 

#########特性#########

listen=YES

listen_port=10021

#表示软件自己监听,不适用xinetd模式,监听端口是10021

 

#########性能#########

pasv_min_port=30000

pasv_max_port=30999

#被动端口这是在30000-30999之间,这样方便与配置防火墙

posted @ 2020-11-07 15:22  NewBird001  阅读(784)  评论(0)    收藏  举报