FTP服务搭建

Bertha 。 2023-10-06 16:51 116阅读 0赞

1、检查是否已经安装了vsftp

首先查看系统中是否成功安装了vsftpd,执行下面命令(有内容则安装了ftp)

  1. rpm -qa | grep vsftpd

2、开始安装

  1. yum install vsftpd -y

3、启动FTP服务

  1. systemctl start vsftpd.service

配置

进入cd /etc/vsftpd目录

ftp的配置文件主要有三个,位于/etc/vsftpd/目录下,分别是:

  1. ftpusers 该文件用来指定那些用户不能访问ftp服务器。
  2. user_list 该文件用来指示的默认账户在默认情况下也不能访问ftp
  3. vsftpd.conf vsftpd的主配置文件

ftp配置:vi /etc/vsftpd/vsftpd.conf

  1. #禁止匿名登录FTP服务器
  2. anonymous_enable=NO
  3. #是否允许本地用户登录
  4. local_enable=YES
  5. #是否允许写入
  6. write_enable=YES
  7. #文件夹的默认权限为022
  8. local_umask=022
  9. #进入每个目录是否显示欢迎信息
  10. dirmessage_enable=YES
  11. #上传/下载文件时记录日志
  12. xferlog_enable=YES
  13. #主动模式(PORT)才有效,是否使用20端口传输数据
  14. connect_from_port_20=YES
  15. #使用标准文件日志
  16. xferlog_std_format=YES
  17. #开启ipv4监听,与listen_ipv6不能同时开启
  18. listen=NO
  19. #同时监听IPv4和IPv6的FTP请求
  20. listen_ipv6=YES
  21. #使用pam模块控制
  22. pam_service_name=vsftpd
  23. #控制用户访问,/etc/vsftpd/user_list是一个黑名单
  24. userlist_enable=YES
  25. #控制主机访问,检查/etc/hosts.allow 和/etc/hosts.deny 中的设置,来决定请求连接的主机,是否允许访问该FTP服务器。
  26. tcp_wrappers=YES

默认被动模式
PORT主动模式:pasv_enable=NO
PASV被动模式:pasv_enable=YES

创建ftp用户

这里主要需要创建的用户无法使用普通登录功能或者telnet,只允许使用ftp登录

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

为用户设置密码

  1. passwd ftpuser

530 Login incorrect.:
注释 vi /etc/pam.d/vsftpd 文件里的这一行,#auth required pam_shells.so
这个语句的意思是只有包含shell的用户才能登录,但是我们创建的用户是nologin不允许登陆的

6、防火墙设置

  1. # 方法一:关闭防火墙
  2. systemctl stop firewalld
  3. # 方法二:开放端口
  4. # 提示:防火墙iptables是centos7之前的服务,centos7开始是firewalld服务,开放端口流程请自行百度。另外注意云服务器需要在云平台上设置安全组开放端口
  5. 开启
  6. # iptables -I INPUT -i eth0 -p tcp --dport 20 -j ACCEPT
  7. # iptables -I OUTPUT -o eth0 -p tcp --sport 20 -j ACCEPT
  8. 保存
  9. # /etc/rc.d/init.d/iptables save
  10. 重启防火墙
  11. # service iptables restart

7、修改selinux,将SELINUX改为disabled

  1. SELINUX=disabled

8、设置开机启动

  1. chkconfig vsftpd on
  2. 或者
  3. systemctl enable vsftpd.service

9、重启FTP服务

  1. systemctl restart vsftpd.service
  2. 其他命令
  3. #开启
  4. service vsftpd start
  5. #停止
  6. service vsftpd stop
  7. #重启
  8. service vsftpd restart

测试代码

  1. package com.transfer.util;
  2. import org.apache.commons.net.ftp.FTPClient;
  3. import org.apache.commons.net.ftp.FTPReply;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. /**
  11. * Ftp工具
  12. * @date 2019年12月6日 上午8:56:44
  13. */
  14. public class FtpUtil {
  15. private static final Logger logger = LoggerFactory.getLogger(FtpUtil.class);
  16. private FTPClient ftpClient = null;
  17. public static void main(String[] args) {
  18. boolean flag;
  19. String ip = "192.168.111.201";
  20. int port = 21;
  21. String username = "ftpuser";
  22. String password = "123456";
  23. FtpUtil ftpUtil = new FtpUtil();
  24. ftpUtil.login(ip, port, username, password);
  25. //上传文件
  26. String remotePath = "/a/b/c";
  27. String fileName = "测试2.txt";
  28. String localPath = "F:/测试文件.txt";
  29. //flag= ftpUtil.uploadFile(remotePath, fileName, localPath);
  30. // System.out.println(flag);
  31. //下载文件
  32. remotePath = "/a/b/c";
  33. fileName = "测试2.txt";
  34. localPath = "F:/测试文件-下载.txt";
  35. // flag = ftpUtil.downloadFile(remotePath, fileName, localPath);
  36. // System.out.println(flag);
  37. //删除文件
  38. remotePath = "/a/b/c";
  39. fileName = "测试2.txt";
  40. flag = ftpUtil.deleteFile(remotePath, fileName);
  41. System.out.println(flag);
  42. }
  43. /**
  44. * 登陆
  45. * @param ip
  46. * @param port
  47. * @param username
  48. * @param password
  49. * @date 2019年12月6日 上午9:05:11
  50. */
  51. public boolean login(String ip, int port, String username, String password) {
  52. ftpClient = new FTPClient();
  53. try {
  54. logger.info("connecting...ftp服务器:" + ip + ":" + port);
  55. ftpClient.setControlEncoding("UTF-8");// 编码设置必须放在connect前不然不起作用
  56. ftpClient.setConnectTimeout(30000);//建立 Socket 连接过程中的超时处理,毫秒
  57. ftpClient.setDefaultTimeout(30000);//传输控制命令的 Socket 的 SoTimeout 的超时处理,毫秒
  58. ftpClient.setDataTimeout(30000);//传输数据的 Socket 的 SoTimeout 的超时处理,毫秒
  59. ftpClient.connect(ip, port); // 连接ftp服务器
  60. ftpClient.login(username, password); // 登录ftp服务器
  61. ftpClient.setRemoteVerificationEnabled(false);//取消服务器获取自身Ip地址和提交的host进行匹配,否则当不一致时报出以上异常。
  62. ftpClient.enterLocalPassiveMode();//被动模式,PASV模式。通知服务器打开一个数据端口,客户端将连接到这个端口进行数据传输。
  63. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  64. ftpClient.setBufferSize(1024);
  65. int replyCode = ftpClient.getReplyCode(); // 是否成功登录服务器
  66. if (FTPReply.isPositiveCompletion(replyCode)) {
  67. logger.info("连接成功...ftp服务器:" + ip + ":" + port);
  68. return true;
  69. }
  70. logger.info("连接失败...ftp服务器:" + ip + ":" + port);
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. logger.error(e.getMessage(), e);
  74. }
  75. return false;
  76. }
  77. /**
  78. * 上传文件
  79. * @param remotePath ftp服务文件路径
  80. * @param fileName ftp服务要保存的文件名
  81. * @param localPath 本地文件绝对路径
  82. * @return
  83. */
  84. public boolean uploadFile(String remotePath, String fileName, String localPath) {
  85. FileInputStream fis = null;
  86. boolean flag = false;
  87. try {
  88. logger.info("开始上传文件");
  89. fis = new FileInputStream(localPath);
  90. createDirecroty(remotePath);//ftp创建目录
  91. flag = ftpClient.storeFile(fileName, fis);
  92. ftpClient.logout();
  93. } catch (Exception e) {
  94. logger.error("上传文件失败");
  95. e.printStackTrace();
  96. logger.error(e.getMessage(), e);
  97. } finally {
  98. if (ftpClient.isConnected()) {
  99. try {
  100. ftpClient.disconnect();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. if (fis != null) {
  106. try {
  107. fis.close();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. }
  113. return flag;
  114. }
  115. /**
  116. * 下载文件
  117. * @param remotePath ftp服务文件路径
  118. * @param fileName ftp服务保存的文件名
  119. * @param localPath 本地文件绝对路径
  120. * @return
  121. */
  122. public boolean downloadFile(String remotePath, String fileName, String localPath) {
  123. FileOutputStream fos = null;
  124. boolean flag = false;
  125. try {
  126. logger.info("开始下载文件");
  127. //由于默认登录后是进入/目录(注意这里的/实际是指:/home/用户名/)
  128. if (remotePath.charAt(0) == '/') {
  129. remotePath = remotePath.substring(1);
  130. }
  131. // FTPFile[] ftpFiles = ftpClient.listFiles(remotePath);
  132. // String[] listNames = ftpClient.listNames(remotePath);
  133. // 切换FTP目录
  134. ftpClient.changeWorkingDirectory(remotePath);
  135. File localFile = new File(localPath);
  136. if (!localFile.getParentFile().exists()) {
  137. localFile.getParentFile().mkdirs();
  138. }
  139. fos = new FileOutputStream(localFile);
  140. //第一个参数: fileName 如果以/开头就是绝对路径,否则是相对路径
  141. flag = ftpClient.retrieveFile(fileName, fos);
  142. ftpClient.logout();
  143. } catch (Exception e) {
  144. logger.error("下载文件失败");
  145. e.printStackTrace();
  146. logger.error(e.getMessage(), e);
  147. } finally {
  148. if (ftpClient.isConnected()) {
  149. try {
  150. ftpClient.disconnect();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. if (fos != null) {
  156. try {
  157. fos.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. }
  163. return flag;
  164. }
  165. /**
  166. * ftp创建目录
  167. * 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
  168. * @date 2020/9/1 15:42
  169. */
  170. public boolean createDirecroty(String remote) {
  171. try {
  172. String directory = remote;
  173. if (!remote.endsWith("/")) {
  174. directory = directory + "/";
  175. }
  176. // 如果远程目录不存在,则递归创建远程服务器目录
  177. if (!directory.equals("/") && !ftpClient.changeWorkingDirectory(directory)) {
  178. int start = 0, end;
  179. if (directory.startsWith("/")) {
  180. start = 1;
  181. }
  182. end = directory.indexOf("/", start);
  183. while (true) {
  184. String subDirectory = remote.substring(start, end);
  185. // 目录不存在就创建
  186. if (!ftpClient.changeWorkingDirectory(subDirectory)) {
  187. if (ftpClient.makeDirectory(subDirectory)) {
  188. ftpClient.changeWorkingDirectory(subDirectory);
  189. }
  190. }
  191. start = end + 1;
  192. end = directory.indexOf("/", start);
  193. // 检查所有目录是否创建完毕
  194. if (end <= start) {
  195. break;
  196. }
  197. }
  198. }
  199. return true;
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. return false;
  204. }
  205. /**
  206. * 删除文件
  207. * @param remotePath ftp服务文件路径
  208. * @param fileName 要删除的文件名称
  209. * @return
  210. */
  211. public boolean deleteFile(String remotePath, String fileName) {
  212. boolean flag = false;
  213. try {
  214. logger.info("开始删除文件");
  215. //由于默认登录后是进入/目录(注意这里的/实际是指:/home/用户名/)
  216. if (remotePath.charAt(0) == '/') {
  217. remotePath = remotePath.substring(1);
  218. }
  219. // 切换FTP目录
  220. ftpClient.changeWorkingDirectory(remotePath);
  221. int dele = ftpClient.dele(fileName);
  222. ftpClient.logout();
  223. if (dele == 250) {
  224. flag = true;
  225. }
  226. } catch (Exception e) {
  227. logger.error("删除文件失败");
  228. e.printStackTrace();
  229. logger.error(e.getMessage(), e);
  230. } finally {
  231. if (ftpClient.isConnected()) {
  232. try {
  233. ftpClient.disconnect();
  234. } catch (IOException e) {
  235. e.printStackTrace();
  236. }
  237. }
  238. }
  239. return flag;
  240. }
  241. }

发表评论

表情:
评论列表 (有 0 条评论,116人围观)

还没有评论,来说两句吧...

相关阅读

    相关 FTP服务

    1、检查是否已经安装了vsftp 首先查看系统中是否成功安装了vsftpd,执行下面命令(有内容则安装了ftp) rpm -qa | grep vsftpd

    相关 17. FTP服务

    熟悉FTP的读者可能会觉得这个太简单了,直接在网上下载软件安装运行就可以了,客户端和服务器都有,但是只能满足一些简单的工作需求。如果我们通过写Python代码搭建FTP服务器和

    相关 linux中FTP服务

    FTP服务器 文件传输协议(File Transfer Protocol)是用于在网络上进行文件传输的一套标准协议,使用客户/服务器模式。专门用来传输文件的协议 工作原

    相关 linuxFTP服务

    在万维网出现以前,用户使用DOS方式传输文件,最通用的应用程序就是FTP,FTP属于应用层协议,使用TCP端口20和21进行传输 FTP有两种文件传输模式: 二进制模式,...