Linux基础学习-使用vsftpd服务传输文件

使用vsftpd服务传输文件

1 安装vsftpdhtml

[root@qdlinux ~]# yum install vsftpd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================
 Package                    Arch                       Version                           Repository               Size
=======================================================================================================================
Installing:
 vsftpd                     x86_64                     3.0.2-22.el7                      dvd                     169 k

Transaction Summary
=======================================================================================================================
Install  1 Package

Total download size: 169 k
Installed size: 348 k
Is this ok [y/d/N]: y
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!
[root@qdlinux ~]#

2 安装ftp lftp客户端软件linux

[root@qdlinux ~]# yum install ftp lftp -y
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
---> Package lftp.x86_64 0:4.4.8-8.el7_3.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================
 Package                  Arch                       Version                             Repository               Size
=======================================================================================================================
Installing:
 ftp                      x86_64                     0.17-67.el7                         dvd                      61 k
 lftp                     x86_64                     4.4.8-8.el7_3.2                     dvd                     752 k

Transaction Summary
=======================================================================================================================
Install  2 Packages

Total download size: 812 k
Installed size: 2.5 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  8.7 MB/s | 812 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : lftp-4.4.8-8.el7_3.2.x86_64                                                                         1/2 
  Installing : ftp-0.17-67.el7.x86_64                                                                              2/2 
  Verifying  : ftp-0.17-67.el7.x86_64                                                                              1/2 
  Verifying  : lftp-4.4.8-8.el7_3.2.x86_64                                                                         2/2 

Installed:
  ftp.x86_64 0:0.17-67.el7                                lftp.x86_64 0:4.4.8-8.el7_3.2                               

Complete!

3 启动服务并加入开机自启动算法

[root@qdlinux ~]# systemctl start vsftpd
[root@qdlinux ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

4 查看监听端口状态数据库

[root@qdlinux ~]# ss -lntup | grep vsftpd
tcp    LISTEN     0      32       :::21                   :::*                   users:(("vsftpd",pid=1411,fd=3))

使用匿名开放模式

服务启动后默认访问的目录为/var/ftp,且匿名用户没有上传、建立、重命名、删除功能.可是具备下载功能.apache

服务开启后直接访问vim

image

建立文件夹被拒绝安全

image

重命名失败服务器

image

咱们来修改配置文件让匿名用户能够上传文件,编辑配置文件vim /etc/vsftpd/vsftpd.conf,修改成以下内容tcp

anon_upload_enable=YES
anon_umask=022

如今依然没法上传文件,这是由于ftp的权限问题致使的,咱们将其修改成ftp用户权限.测试

==注意:默认匿名用户家目录/var/ftp的权限是755,这个权限是不能改变的,切记!==

[root@qdlinux var]# ll -d ftp
drwxr-xr-x 3 root root 30 Aug 13 21:32 ftp

[root@qdlinux var]# chown -Rf ftp /var/ftp/pub

[root@qdlinux var]# ll ftp
total 0
-rw-r--r-- 1 root root  0 Aug 13 21:32 1.txt
drwxr-xr-x 2 ftp  root 19 Aug 13 21:32 pub

如今已经能够上传文件了,可是只能上传到ftp/pub目录中,由于刚才给的就是这个目录的权限.如今就作到了只能上传文件和下载的目的,可是不能建立文件夹.

上传文件

image

重命名失败

image

建立文件夹被拒绝

image

[root@qdlinux var]# ll ftp/pub
total 0
-rw-r--r-- 1 ftp root 0 Aug 13 21:32 2.txt
-rw-r--r-- 1 ftp ftp  0 Aug 13 21:51 new.txt
[root@qdlinux var]#

若是须要开启匿名用户能够建立文件夹功能,修改配置文件以下所示.

anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_umask=022

如今匿名用户能够上传文件和下载文件以及建立文件夹.可是不能重命名.

若是须要开启匿名用户上传下载和建立文件夹以及重命名和删除功能,修改配置文件以下所示.

anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=022

如今匿名用户能够作到上传和下载,建立文件夹和删除文件夹和文件以及重命名.

上传文件
image

新建文件夹
image

这是匿名用户的最大权限了,通常匿名访问的话,只要作到下载功能就能够了。网站提供什么用户下载什么。


使用本地用户模式

示例演示公司内部有一台FTP和WEB服务器,FTP的功能主要用于维护网站内容,用于上传文件、建立目录、更新网页等等。公司有两个部门负责维护任务,他们分别用team1和team2帐号进行管理,要求仅容许team1和team2帐号登陆ftp服务器,单不能登陆本地系统,并将这两个帐号的根目录限制为/var/www/html,不能进去该目录之外的任何目录。

前期准备首先安装apachevsftpd,具体安装方法这里就不介绍了。

建立测试帐号team1和team2

[root@qdlinux ~]# useradd -s /sbin/nologin team1 
[root@qdlinux ~]# useradd -s /sbin/nologin team2
[root@qdlinux ~]# echo "123456" | passwd --stdin team1
Changing password for user team1.
passwd: all authentication tokens updated successfully.
[root@qdlinux ~]# echo "123456" | passwd --stdin team2
Changing password for user team2.
passwd: all authentication tokens updated successfully.

接下来修改配置文件,这里在RHEL6中和RHEL7中配置有些不一样

anonymous_enable=NO
local_enable=YES
chroot_local_user=YES
local_root=/var/www/html
allow_writeable_chroot=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

能够把不限制的帐户放到这里面,建立用户列表文件,由于此帐户会访问到其余目录.但这样作事很危险的.

[root@qdlinux ~]# touch /etc/vsftpd/chroot_list
[root@qdlinux ~]# vim /etc/vsftpd/chroot_list 
teamleader

修改apache的根目录权限/var/www/html

[root@qdlinux ~]# ll -d /var/www/html/
drwxr-xr-x 2 root root 6 May  9  2017 /var/www/html/
[root@qdlinux ~]# chmod -R o+w /var/www/html/

访问测试

image

不限制用户访问

image

不限制用户访问,用户能够随意到任何目录

image

限制用户访问,用户只能在此目录中,在安全方面相对要好不少.

image

文件上传后文件拥有者权限

image


虚拟用户模式

示例演示为了宣传公司的产品信息,计划搭建FTP服务器,为客户提供相关资料的下载,仅容许下载产品信息,禁止上传。公司的合做单位和能够在FTP服务器上上传和下载,可是不能删除数据。

建立用户进行FTP认证的用户数据库文件,其中奇数行为帐户名,偶数行为密码。编辑vim /etc/vsftpd/vuser.list

ftp
123456
vip
123456

因为明文的信息既不安全,也不符合让vsftpd服务程序直接加在的格式,所以须要使用db_load命令用哈希算法将明文信息文件转成数据库文件。在RHEL6中须要安装db4-utils,在RHEL7中libdb-utils

[root@qdlinux ~]# rpm -qf `which db_load`
libdb-utils-5.3.21-20.el7.x86_64
[root@qdlinux ~]# db_load -T -t hash -f /etc/vsftpd/vuser.list /etc/vsftpd/vuser.db
[root@qdlinux ~]# file /etc/vsftpd/vuser.db 
/etc/vsftpd/vuser.db: Berkeley DB (Hash, version 9, native byte-order)

下降数据库文件的权限避免其余人看到数据库文件的内容,而后把明文信息文件删除。

[root@qdlinux ~]# chmod 600 /etc/vsftpd/vuser.db
[root@qdlinux ~]# ll /etc/vsftpd/vuser.db
-rw------- 1 root root 12288 Aug 14 01:19 /etc/vsftpd/vuser.db

创建用于支持虚拟用户的PAM文件

vim /etc/pam.d/vsftpd.vu

auth    required        pam_userdb.so   db=/etc/vsftpd/vuser
account required        pam_userdb.so   db=/etc/vsftpd/vuser

创建用户存储文件的根目录以及虚拟用户映射的系统本地用户。

[root@qdlinux ~]# useradd -d /var/ftp/share -s /sbin/nologin ftpuser
[root@qdlinux ~]# useradd -d /var/ftp/vip -s /sbin/nologin ftpvip
[root@qdlinux ~]# chmod -R 500 /var/ftp/share/
[root@qdlinux ~]# chmod -R 700 /var/ftp/vip/

修改配置文件

anonymous_enable=NO
local_enable=YES
allow_writeable_chroot=YES
pam_service_name=vsftpd.vu
user_config_dir=/etc/vsftpd/vuserconfig
max_clients=300
max_per_ip=10

创建虚拟帐号配置文件

[root@qdlinux ~]# mkdir /etc/vsftpd/vuserconfig
[root@qdlinux ~]# touch /etc/vsftpd/vuserconfig/ftp
[root@qdlinux ~]# touch /etc/vsftpd/vuserconfig/vip

vim编辑/etc/vsftpd/vuserconfig/ftp中的内容

guest_enable=YES
guest_username=ftpuser
anon_world_readable_only=NO
anon_max_rate=50000

vim编辑/etc/vsftpd/vuserconfig/vip中的内容

guest_enable=YES
guest_username=ftpvip
anon_world_readable_only=NO
write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES
anon_max_rate=2000000

测试ftp帐户

[root@qdlinux ~]# lftp 192.168.56.15 -u ftp,123456
lftp ftp@192.168.56.15:~> ls          
drwx------    5 0        0              97 Aug 13 18:14 grub2
-r--r--r--    1 0        0        11607904 Aug 13 18:13 mariadb-server.rpm

访问限速测试

image

image

测试vip帐户

[root@qdlinux ~]# lftp 192.168.56.15 -u vip
Password: 
lftp vip@192.168.56.15:~> ls          
-rw-------    1 1004     1004      5505024 Aug 13 18:33 kali-linux-2018.2-amd64.iso
-rw-------    1 1004     1004     111017984 Aug 13 18:36 kali-linux-2018.2-amd641.iso
-r--r--r--    1 0        0        11607904 Aug 13 18:30 mariadb-server.rpm

上传和下载访问速度测试

image

image

相关文章
相关标签/搜索