Ubuntu16.04 安装与配置Ftp服务器(解决被动模式无法正常连接问题)

1 Ftp服务器的安装

如果之前配置过ftp服务器,但无法启动服务,那么本是配置出现了错误,那么可先完全卸载后再进行安装。

sudo apt-get install vsftpd
sudo vsftpd -v          // 检查是否安装 

如果安装会出现:vsftpd: version 3.0.3

2 Ftp服务器的配置

sudo vim /etc/vsftpd.conf   // 修改配置文件 

vsftpd.conf文件如下:

listen=NO                 //是否开启监听ipv4和ipv6数据      
listen_ipv6=YES          //是否开启监听ipv6数据
​
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO      //是否允许匿名登陆,无需密码
​
# Uncomment this to allow local users to log in.
local_enable=YES        //是否允许本地用户登录
​
# Uncomment this to enable any form of FTP write command.
#write_enable=YES        //是否允许登陆者上传文件, 如果需要上传文件可开启
​
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022         //设置本地用户默认要减免的权限
​
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES       //目录消息,能够给远程登陆的用户发送目录
#
# If enabled, vsftpd will display directory listings with the time
# in  your  local  time  zone.  The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES           //服务器所展示的目录将随着本地时间而改变
#
# Activate logging of uploads/downloads.
xferlog_enable=YES          //开启上传下载的日志记录
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES    //确认连接传输的端口号为20
​
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/vsftpd.log    //日志文件存放位置
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES          //日志文件采用标准格式
​
​
# You may fully customise the login banner string:
ftpd_banner=Welcome to FTP service.  //在使用shell时登陆那么会发送欢迎语
​
​
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
#chroot_local_user=YES        //对本地用户是否实施限制
#chroot_list_enable=YES       //开启限制白名单
# (default follows)         
#chroot_list_file=/etc/vsftpd.chroot_list        //白名单路径,若无这个文件需要自己创建
​
# This option should be the name of a directory which is empty.  Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
# pam_service_name=vsftpd
pam_service_name=ftp            //此处ubuntu的系统需要改为ftp
​
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO                 
​
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES       //编码统一为utf8编码,可以识别中文,防止乱码
​
local_root=/home/ftpuser //本地用户默认访问的目录
​
pasv_enable=YES  //Ftp服务器的工作模式,此时为被动模式, 如果设置port_enable=YES,就表示主动模式
pasv_min_port=6000  //在PASV模式下,建立数据传输所可以使用port范围的下界
pasv_max_port=7000  //在PASV模式下,建立数据传输所可以使用port范围的上界,把端口范围设在比较高的一段范围内,比如50000-60000,将有助于安全性的提高。

补充:(未出现在vsftpd.conf配置文件中的常用参数)

guest_enable=YES  
guest_username=ftp  
    guest用户名,即登陆不是匿名用户的用户,具有guest用户身份  
local_root=/var/ftp  
anon_root=/var/ftp  
    以上两个选项为本地用户和匿名用户默认访问的目录  
#pasv_enable=YES  
#port_enable=YES  
    以上两个选项是FTP服务器的工作模式,两者只能出现一个,而且另一个必须注释掉。  
use_localtime=YES  
    是否使用本机时间,若设置NO时,仅使用格林尼治时间。由于北京时间和格林尼治时间有8小时时差,所以建议设置为YES  
Idle_session_timeout=300  
    客户端若在300秒之内没有任何操作,则服务器自动断开。  
max_clinet=0  
    最大连接数量(stand-alone模式下)  
max_per_ip=0  
    每个客户端最大连接ftp服务器的连接数  
local_max_rate=0  
    本地用户登陆FTP服务器最大传输速率,单位为字节/秒  
anon_max_rate=0  
    匿名用户登陆FTP服务器最大传输速率,单位为字节/秒 
allow_writeable_chroot=YES
    解决500 OOPS: vsftpd: refusing to run with writable root inside chroot (),也可以使用命令sudo chmod a-w /home/user去除用户主目录的写权限,注意把目录替换成你自己的

3 添加并设置Ftp用户

sudo mkdir /home/ftpuser
sudo useradd ftpuser -d /home/ftpuser -m
sudo passwd ftpuser
sudo chmod 777 -R /home/ftpuser
sudo usermod -s /sbin/nologin ftpuser

4 开启或重启vsftp服务

sudo service vsftpd stop     //停止vsftpd服务
sudo service vsftpd start    //开启vsftpd服务
sudo service vsftpd restart  //重启vsftpd服务

5 服务器开启Ftp端口范围

如果设置pasv_min_portpasv_max_port,需要在服务器配置一下安全组的入口规则,否则通过浏览器的ftp请求是无法访问的。注意端口号要和自己设置的范围保持一直,图片中显示没有一致。

  • 阿里云服务器配置安全组

 

  • 腾讯云服务器配置安全组

6 Ftp服务器测试

  • 浏览器访问

  • windows资源管理器访问

     

         

  • windows终端cmd访问

7 遇到的各种坑

       有的猿友可能会遇到奇葩的问题,我个人都遇到过(浪费三天时间),同样的部署在不同类型的服务器部署,会存在被动模式死活连接不上的问题。这是因为如果购买的服务器使用的是专有网络,是不区分公网和内网,修改安全组也没有这个区分选项,但采用经典网络的是由公网和内网的区分,所以就会存在这样的差异。比如:

       分析之后发现,采用ftp被动模式登录成功,服务端映射给客户端的ip地址是私有的,所以客户端无法和服务器建立连接关系,也就意味着ftp命令无法使用,只能采取主动模式。解决方案如下:

listen=NO           ===》 修改为listen=YES    
listen_ipv6=YES     ===》 修改为listen_ipv6=NO   
​
pasv_address=111.231.34.12   // 说明一定要是公网IP,如果不添加,返回被动模式的ip是0.0.0.0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章