热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Linux下搭建FTP文件服务器

1、下载并安装vsftpd.rpmvsftp.rpm下载地址#查看系统是否安装了ftp,若安装了vsftp,使用这个命令会在屏幕上显示vsftpd的

1、下载并安装vsftpd.rpm

vsftp.rpm下载地址

#查看系统是否安装了ftp,若安装了vsftp,使用这个命令会在屏幕上显示vsftpd的版本
rpm -qa|grep vsftpd#卸载ftp
rpm -e vsftpd#安装vsftp,先cd去所在目录
rpm -ivh --nodeps --force vsftpd-2.2.2-24.el6.x86_64.rpm#设置开机启动
chkconfig vsftpd on管理vsftpd相关命令:
启动vsftpd: service vsftpd start
停止vsftpd: service vsftpd stop
重启vsftpd: service vsftpd restart#关闭sellinux
1、立即关闭
setenforce 02、重启也关闭
vi /etc/selinux/config#修改SELINUX=disabled3、查看是否关闭
getenforce

2、配置防火墙,开放21端口

vi /etc/sysconfig/iptables
在REJECT行之前添加如下代码-A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT

保存和关闭文件,重启防火墙
service iptables start

3、配置vsftpd服务器

useradd ftpUser -s /sbin/nologin -d /home/yangxi #1、创建个普通用户用于连接ftp,-d的家目录是ftp连接上默认访问的。这个禁止登录,是禁止远程连接服务器,不是禁止登录ftppasswd ftpUser #敲回车,设置密码

ftpusers 是黑名单,禁止使用vsftpd的用户列表文件。记录不允许访问FTP服务器的用户名单user_list   禁止或允许使用vsftpd的用户列表文件。这个文件中指定的用户缺省情况(即在/etc/vsftpd/vsftpd.conf中设置userlist_deny=YES)

 在 vsftpd.conf  主配置文件中

# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# 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
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES
# WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log
#xferlog_file=/var/log/xferlog
#
# Switches between logging into vsftpd_log_file and xferlog_file files.
# NO writes to vsftpd_log_file, YES to xferlog_file
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
ascii_upload_enable=YES
ascii_download_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# 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().
chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YESpam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
local_root=/var/ftp

 重启:service vsftpd restart,我这里photos是我自己加的

 


 4、Java对接

#添加依赖
commons-netcommons-net3.3ftp

public class FTPUtils {private static final Logger logger = LoggerFactory.getLogger(FTPUtils.class);public static char separator= IOUtils.DIR_SEPARATOR_UNIX;private static String ftpIp = "192.168.0.222";private static String ftpUser = "ftpUser";private static String ftpPass = "ftpUser";public static String DATA_ROOT_PATH= "";private static Integer ftpPort = 21;public static String imgtemporaryUrl= "";private static FTPClient ftpClient = null;public static String getPhotoPath(String userId) {return DATA_ROOT_PATH + separator + "photos" + separator+userId+separator;}public static boolean upload(InputStream input, String ftpFileName, String ftpDirectory) {try {if (FTPUtils.open()) {FTPUtils.put(input, ftpFileName, ftpDirectory);FTPUtils.close();}} catch (Exception e) {e.printStackTrace();return false;}return true;}/*** 链接到服务器* @return* @throws Exception*/public static boolean open() {if (ftpClient != null && ftpClient.isConnected()) {return true;}try {ftpClient = new FTPClient();// 连接ftpClient.connect(ftpIp, ftpPort);ftpClient.login(ftpUser, ftpPass);// 检测连接是否成功int reply = ftpClient.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {close();logger.error("FTP server refused connection.");}logger.info("open FTP success:" + ftpIp + ";port:" + ftpPort+ ";name:" + ftpUser + ";pwd:" + ftpPass);ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); // 设置上传模式.binally// or asciireturn true;} catch (Exception ex) {// 关闭close();logger.error(ex.getMessage(), ex);return false;}}/*** 上传文件到FTP服务器* * @param input* 本地文件目录和文件名* @param ftpFileName* 上传后的文件名* @param ftpDirectory* FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录* @throws Exception*/public static boolean put(InputStream input, String ftpFileName,String ftpDirectory) {if (!ftpClient.isConnected()) {return false;}boolean flag = false;if (ftpClient != null) {try {// 创建目录FTPUtils.mkDir(ftpDirectory);ftpClient.setBufferSize(1024);ftpClient.setControlEncoding("UTF-8");// 设置文件类型(二进制)ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);ftpClient.enterLocalPassiveMode();// 上传flag = ftpClient.storeFile(new String(ftpFileName.getBytes(),"iso-8859-1"), input);} catch (Exception e) {FTPUtils.close();logger.error(e.getMessage(), e);return false;} finally {IOUtils.closeQuietly(input);}}logger.info("success put file " + " to " + ftpDirectory + ftpFileName);return flag;}public static boolean mkDir(String ftpPath) {if (!ftpClient.isConnected()) {return false;}try {// 将路径中的斜杠统一char[] chars = ftpPath.toCharArray();StringBuffer sbStr = new StringBuffer(256);for (int i = 0; i

nginx添加路由配置:
location /photos/ {alias /var/ftp/photos/;
}访问上传图片的地址:http://192.168.0.222/photos/123456/72035b95379844a09506ab11b7f1edf3.png

 


推荐阅读
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 本文详细介绍了cisco路由器IOS损坏时的恢复方法,包括进入ROMMON模式、设置IP地址、子网掩码、默认网关以及使用TFTP服务器传输IOS文件的步骤。 ... [详细]
  • centos安装Mysql的方法及步骤详解
    本文介绍了centos安装Mysql的两种方式:rpm方式和绿色方式安装,详细介绍了安装所需的软件包以及安装过程中的注意事项,包括检查是否安装成功的方法。通过本文,读者可以了解到在centos系统上如何正确安装Mysql。 ... [详细]
  • 微软评估和规划(MAP)的工具包介绍及应用实验手册
    本文介绍了微软评估和规划(MAP)的工具包,该工具包是一个无代理工具,旨在简化和精简通过网络范围内的自动发现和评估IT基础设施在多个方案规划进程。工具包支持库存和使用用于SQL Server和Windows Server迁移评估,以及评估服务器的信息最广泛使用微软的技术。此外,工具包还提供了服务器虚拟化方案,以帮助识别未被充分利用的资源和硬件需要成功巩固服务器使用微软的Hyper - V技术规格。 ... [详细]
  • 第四讲ApacheLAMP服务器基本配置Apache的编译安装从Apache的官方网站下载源码包:http:httpd.apache.orgdownload.cgi今 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了将mysql从5.6.15升级到5.7.15的详细步骤,包括关闭访问、备份旧库、备份权限、配置文件备份、关闭旧数据库、安装二进制、替换配置文件以及启动新数据库等操作。 ... [详细]
  • 面试经验分享:华为面试四轮电话面试、一轮笔试、一轮主管视频面试、一轮hr视频面试
    最近有朋友去华为面试,面试经历包括四轮电话面试、一轮笔试、一轮主管视频面试、一轮hr视频面试。80%的人都在第一轮电话面试中失败,因为缺乏基础知识。面试问题涉及 ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
  • 如何使用PLEX播放组播、抓取信号源以及设置路由器
    本文介绍了如何使用PLEX播放组播、抓取信号源以及设置路由器。通过使用xTeve软件和M3U源,用户可以在PLEX上实现直播功能,并且可以自动匹配EPG信息和定时录制节目。同时,本文还提供了从华为itv盒子提取组播地址的方法以及如何在ASUS固件路由器上设置IPTV。在使用PLEX之前,建议先使用VLC测试是否可以正常播放UDPXY转发的iptv流。最后,本文还介绍了docker版xTeve的设置方法。 ... [详细]
author-avatar
346182773_20da31
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有