欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

linux下vsftpd的安装及配置使用详细步骤(推荐)

程序员文章站 2023-11-11 15:05:22
vsftpd 是“very secure ftp daemon”的缩写,安全性是它的一个最大的特点。 vsftpd 是一个 unix 类操作系统上运行的服务器的名字,它可...

vsftpd 是“very secure ftp daemon”的缩写,安全性是它的一个最大的特点。

vsftpd 是一个 unix 类操作系统上运行的服务器的名字,它可以运行在诸如 linux、bsd、solaris、 hp-unix等系统上面,是一个完全免费的、开放源代码的ftp服务器软件,支持很多其他的 ftp 服务器所不支持的特征。

比如:非常高的安全性需求、带宽限制、良好的可伸缩性、可创建虚拟用户、支持ipv6、速率高等。

vsftpd是一款在linux发行版中最受推崇的ftp服务器程序。特点是小巧轻快,安全易用。

1.1  安装

命令:

yum install vsftpd -y

配置vsftpd

安装完之后我们要对它进行配置,才能正常使用。

编辑vsftpd的配置文件

 vi /etc/vsftpd/vsftpd.conf

在配置文件中找到“anonymous_enable=yes”,将"yes"改为"no",将匿名登录禁用。

添加开机自动启动,

chkconfig vsftpd on

不确定是否已经加入了开机启动项可以运行chkconfig –list进行查看

1.2  创建用户

命令:useradd ftpuser

指定密码:passwd ftpuser

此用户就是用来登录ftp服务器用的。

这样一个用户建完,可以用这个登录,记得用普通登录不要用匿名了。登录后默认的路径为 /home/ftpuser.

----------------------------------------------------------------------------------------------------------------------------------

设置ftp用户的账号,例如账号为“ftpuser1”,目录为/home/ftpuser1,且设置不允许通过ssh登录。

useradd -d /home/ftpuser -s /sbin/nologin ftpuser 

设置账号对应的密码,例如密码为“ftpuser”

passwd ftpuser

1.3  开启selinux权限

vsftpd是一个ftp服务器程序,然后selinux是centos的防火墙组件。由于 vsftpd 默认被 selinux 拦截,所以会遇到的ftp以下的问题:

1.226 transfer done (but failed to open directory).(传输完成,但是打开路径失败)

2.550 failed to change directory(更改路径失败)

3.
553 could not create file.

4.

或者干脆在发送了list命令以后,服务器没响应,超时断开。

遇到这样的问题,通常是vsftpd 没有足够的权限,很有可能是被selinux阻止了。

查看命令:getsebool -a | grep ftp

getsebool -a | grep ftpd
#以下是显示出来的权限,off是关闭权限,on是打开权限
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftpd_connect_db --> off
ftpd_use_passive_mode --> off
ftp_home_dir --> off

其中,ftp_home_dir和allow_ftpd_full_access必须为on 才能使vsftpd 具有访问ftp根目录,以及文件传输等权限。

      设置命令:setsebool -p xxxxxx on

[root@bogon ~]# setsebool -p allow_ftpd_full_access on
[root@bogon ~]# setsebool -p ftp_home_dir on

1.4  设置或关闭防火墙

因为ftp默认的端口为21,而centos默认是没有开启的,所以要修改iptables文件

设置:vi /etc/sysconfig/iptables

在行上面有22 -j accept 下面另起一行输入跟那行差不多的,只是把22换成21,然后:wq保存。

还要运行下,重启iptables

重启:service iptables restart

关闭防火墙:service iptables stop

禁用防火墙重启:chkconfig iptables off

1.5  启动vsftpd

命令:service vsftpd start

  java客户端(代码)调用

package com.jonychen.util;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstream;
import java.net.socketexception;
import java.util.date;
import java.util.uuid;
import org.apache.commons.net.ftp.ftp;
import org.apache.commons.net.ftp.ftpclient;
public class ftpuploadutil {
  private static threadlocal<ftpclient> threadlocal = new threadlocal<>();
  private static threadlocal<string> threadlocalhost = new threadlocal<>();
  public static void init(string host,int port,string username,string password) throws socketexception, ioexception {
    ftpclient client = threadlocal.get();
    if(client==null) {
      client = new ftpclient();
      //1.连接
      client.connect(host,port);
      //2.登录
      client.login(username,password);
      threadlocal.set(client);
      threadlocalhost.set(host);
    }
  }
  public static string upload(inputstream local,string filename,string path) throws socketexception, ioexception {
    string datepath = dateutil.date2str(new date(),"/yyyy/mm/dd/");
    //路径添加日期
    path+=datepath;
    ftpclient client = threadlocal.get();
    string host = threadlocalhost.get();
    //3.指定文件上传路径(路径不存在返回false)
    boolean exists = client.changeworkingdirectory(path);
    if(!exists) {
      string patharray[] = path.split("/");
      string temp = "/";
      for(string p:patharray) {
        temp+=(p+"/");
        //4.如果文件路径不存在,则创建(一次只能创建一级目录)
        client.makedirectory(temp);
      }
      //重新指定文件上传路径
      client.changeworkingdirectory(path);
    }
    //5.指定文件类型
    client.setfiletype(ftp.binary_file_type);
    //获取后缀
    string suffix = filename.substring(filename.lastindexof("."));
    string uuid = uuid.randomuuid().tostring();
    //6.执行上传
    client.storefile(uuid+suffix, local);
    //7.退出
    client.logout();
    //8.断开连接
    client.disconnect();
    threadlocalhost.remove();
    threadlocal.remove();
    return "http://"+host+"/jonychen"+datepath+uuid+suffix;
  }
  public static void main(string[] args) throws socketexception, ioexception {
    inputstream local = new fileinputstream("d:\\documents\\pictures\\01.png");
    init("192.168.178.161", 21, "ftpuser", "111111");
     //上传路径
    string res = upload(local, "code.png","/home/ftpuser/ego");
    system.out.println(res);
  }
}

总结

以上所述是小编给大家介绍的linux下vsftpd的安装及配置使用详细步骤,希望对大家有所帮助