ubuntu 公网FTP搭建,解决无法连接

2 篇文章 0 订阅

1 Ftp服务器的安装

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


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

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

2 Ftp服务器的配置

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

vsftpd.conf文件如下:


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

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


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

3 添加并设置Ftp用户


   
   
  1. sudo mkdir /home/ftpuser
  2. sudo useradd ftpuser -d /home/ftpuser -m
  3. sudo passwd ftpuser
  4. sudo chmod 777 -R /home/ftpuser
  5. sudo usermod -s /sbin/nologin ftpuser

4 开启或重启vsftp服务


   
   
  1. sudo service vsftpd stop     //停止vsftpd服务
  2. sudo service vsftpd start   //开启vsftpd服务
  3. sudo service vsftpd restart //重启vsftpd服务

5 服务器开启Ftp端口范围

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

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

 

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

6 Ftp服务器测试

  • 浏览器访问

  • windows资源管理器访问

     

         

  • windows终端cmd访问

7 遇到的各种坑

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

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


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

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值