vsftp命令使用:文件上传及下载



sftp是ssh的一种,是一种安全的j交互式文件传输协议,可以为传输文件提供一种安全的加密方法。语法和ftp基本相同,比FTP有更高的安全性。

示例1:将本地Windows机器D:\Program Files (x86)\Oracle\rhel-server-6.6-x86_64-dvd.iso镜像文件上传至远端192.168.2.231 Linux主机的/home目录。

远程主机配置vsftpd服务
//安装vsftp服务
[root@test1 ~]# service vsftpd status
vsftpd: unrecognized service
[root@test1 ~]# yum install vsftpd
Loaded plugins: product-id, security, subscription-manager
Updating certificate-based repositories.
Unable to read consumer identity
rhel_6_iso | 4.0 kB 00:00 …
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package vsftpd.x86_64 0:2.2.2-11.el6 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================
Installing:
vsftpd x86_64 2.2.2-11.el6 rhel_6_iso 151 k

Transaction Summary
=======================================================================================================================================================================
Install 1 Package(s)

Total download size: 151 k
Installed size: 331 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Installing : vsftpd-2.2.2-11.el6.x86_64 1/1
Installed products updated.
Verifying : vsftpd-2.2.2-11.el6.x86_64 1/1

Installed:
vsftpd.x86_64 0:2.2.2-11.el6

Complete!
[root@test1 ~]# service vsftpd status
vsftpd is stopped
[root@test1 ~]#

//注释掉如下2个文件的root用户

[root@test1 ~]# vi /etc/vsftpd/user_list

# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
#root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
~
~
~
“/etc/vsftpd/user_list” 20L, 362C written
[root@test1 ~]#
[root@test1 ~]# vi /etc/vsftpd/ftpusers

# Users that are not allowed to login via ftp
#root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
~
~
~
~
~
“/etc/vsftpd/ftpusers” 15L, 126C written
[root@test1 ~]#
说明:允许root用户登录FTP SERVER。
(1)如果没有在2个文件中进行设置,则当在windows的DOS命令行窗口执行ftp命令时会提示“530 Permission denied.”登陆失败;
(2)2个文件必须都配置root用户访问才可正常使用vsftp服务。

//启动vsftp服务

[root@test1 ~]# service vsftpd start
Shutting down vsftpd: [ OK ]
Starting vsftpd for vsftpd: [ OK ]
说明:
(1)start命令启动vsftpd服务,如果修改上述与vsftp服务相关的文件,则修改文件后需要使用restart命令重启vsftp服务;
(2)status命令查看服务状态,stop命令停止服务。

windows上执行文件上传
//windows下正常执行完上传操作



这里写图片描述
//linux下查看上传文件信息
这里写图片描述
//查看vsftp端口
这里写图片描述
说明:通过netstat的输出中,可以看到vsftpd的监听端口为21.

问题及解决
问题一:windows命令窗口可以使用ftp登陆,但是不能执行ls等命令。
原因:可能主要是端口被防火墙控制(端口限制)。
错误:执行put上传命令时提示“425 Failed to establish connection.”错误。


这里写图片描述
解决:
//设置启用PASV模式连接信息
[root@test1 ~]# sudo vi /etc/vsftp.conf

pasv_enable=YES
pasv_min_port=6000
pasv_max_port=7000
~
~
~
“/etc/vsftp.conf” 3L, 54C written
[root@test1 ~]#
说明:启动PASV模式,开放6000~7000端口号作为数据传送端口。

//(可选)为了保险,将6000~7000端口写入iptables白名单中
[root@test1 ~]# sudo iptables -A INPUT -p tcp –dport 6000:7000 -j ACCEPT
说明:当前系统环境Red Hat6.3.
(1)在Ubuntu中,一般可以不用,因Ubuntu有iptables但默认是没有规则的,即不会设置端口限制。
(2)其他版本最好加上该操作。

//设置PASV模式
![这里写图片描述](http://img.blog.csdn.net/20150915163704051)
(重启vsftp服务,还是不可用)

//(可选)带有selinux的,最好关闭selinux
[root@test1 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing – SELinux security policy is enforced.
# permissive – SELinux prints warnings instead of enforcing.
# disabled – No SELinux policy is loaded.
SELINUX=disabled
# SELNUXTYPE= can take one of these two values:
# targeted – Targeted processes are protected,
# mls – Multi Level Security protection.
SELINUXTYPE=targeted

[root@test1 ~]#
说明:Ubuntu默认没有安装selinux,类似redhat、centOS的系统,修改配置文件后须重启。

(重启vsftp服务后,还是不可用)
//关闭windows防火墙
![这里写图片描述](http://img.blog.csdn.net/20150915163055793)

OK,可以上传镜像文件!
说明:如果上述操作还未能解决问题,则可考虑如下几点(1)防火墙(本地、客户机);(2)FTP目录的权限;(3)客户机是否是IPv6网络;(4)客户机的网关限制了外网ftp。

问题2:文件传输中止。
原因:远端目标主机磁盘空间满。
错误:
//windows下报错


这里写图片描述
//linux下查看磁盘信息
这里写图片描述
重新执行文件上传命令,OK!(该错误可能是因为网络等问题导致的)

总结
(1)一般,DOS命令行下默认以PORT主动模式连接,即ftp使用21端口监听 处理控制信息,再以20端口连接客户端 发送数据;
而PASV被动模式是以21端口监听,有连接请求时,随时开放一个比较大的端口号来处理数据传输。
​但是经常通过外网NAT 基本端口20的数据会被禁止掉,此时如果需对外开放ftp,最好使用PASV模式。
(2)端口使用说明
FTP使用21端口监听控制,20端口传输数据
SFTP和SSH默认都使用22端口
可以通过/etc/services配置文件查看端口信息。
可以执行netstat -anp | grep vsftpd,查看vsftp服务的监听端口。

(3)iptables规则
sudo iptables -A INPUT -p tcp –dport 6000:7000 -j ACCEPT