文件系统 FTP Ubuntu 安装入门介绍

打印 上一主题 下一主题

主题 849|帖子 849|积分 2547

FTP

环境: Ubuntu 14.04
blog zh_CN
ubuntu14.04


  • Install
  1. 全新安装:apt-get install vsftpd
  2. 重新安装:apt-get --reinstall install vsftpd
  3. 卸载并清除配置文件:apt-get --purge remove vsftpd
复制代码

  • Start & Restart
  1. $   service vsftpd start
  2. $   service vsftpd restart
复制代码
注意:
网上文章很多有提及/etc/init.d/vsftpd start 之类的启动方式。但是这个目录下我不存在 vsftpd. 这个目录确实有: /etc/init/vsftpd.conf
  1. vsftpd 已經進化為 Upstart job
  2. 設定檔放在
  3. /etc/init/vsftpd.conf
复制代码

  • Create ftp user
1.此用户只是用来使用ftp服务的
2.此用户不可登录服务器
3.此用户不能访问ftp指定文件夹之外的文件
(1) 创建一个用户ftp0
  1. useradd -d /home/ftp0 -m ftp0
复制代码
(2) 修改ftp0的密码
  1. passwd ftp0
复制代码

  • Config /etc/vsftpd.conf
  1. anonymous_enable=NO         # 不允许匿名访问
  2. write_enable=YES            # 允许写
  3. local_enable=YES            # 允许本地主机访问
  4. chroot_local_user=YES       # 只能访问自身的目录,此处有坑,需加上下面一行
复制代码
报错误信息:
  1. “500 OOPS: vsftpd: refusing to run with writable root inside chroot()”
复制代码
从2.3.5之后,vsftpd加强了安全检查,假如用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!假如检查发现还有写权限,就会报该错误。
(1) 启用了chroot的话,根目录要设置为不可写
  1. chmod a-w /home/ftp0
复制代码
(2) 大概添加一句话
  1. allow_writeable_chroot=YES #允许写自身的目录
复制代码
可是添加这句话可能会导致服务重启失败。。。
无奈之下。。。chroot_local_user=YES这句话临时不加。

  • 让用户不能登录
  1. $   usermod -s /sbin/nologin ftp0
复制代码
after all these, restart the ftp service:
  1. # service vsftpd restart
  2. vsftpd stop/waiting
  3. vsftpd start/pre-start, process 10305
  4. # service vsftpd status
  5. vsftpd start/running, process 10305
复制代码

  • Test
  1. # ftp
  2. ftp> open 192.168.2.108
  3. Connected to 192.168.2.108.
  4. 220 (vsFTPd 3.0.2)
  5. Name (192.168.2.108:hbb): ftp0
  6. 331 Please specify the password.
  7. Password:
  8. 530 Login incorrect.
  9. Login failed.
复制代码
必要 vi /etc/shells, 最后一行添加:
  1. /sbin/nologin
复制代码
重新测试:
  1. # ftp
  2. ftp> open 192.168.2.108
  3. Connected to 192.168.2.108.
  4. 220 (vsFTPd 3.0.2)
  5. Name (192.168.2.108:hbb): ftp0
  6. 331 Please specify the password.
  7. Password:
  8. 230 Login successful.
  9. Remote system type is UNIX.
  10. Using binary mode to transfer files.
复制代码
21 端口检察:
  1. netstat -npltu | grep 21
  2. tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      11398/vsftpd
复制代码
Ftp Java code


  • Java 测试代码
  1. package com.ryo.ftp;
  2. import org.apache.commons.net.ftp.FTPClient;
  3. import org.apache.commons.net.ftp.FTPReply;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. /**
  7. * @author houbinbin
  8. * @on 17/1/1
  9. */
  10. public class FTPTest {
  11.     private FTPClient ftp;
  12.     /**
  13.      * @param path     上传到ftp服务器哪个路径下
  14.      * @param addr     地址
  15.      * @param port     端口号
  16.      * @param username 用户名
  17.      * @param password 密码
  18.      * @return
  19.      * @throws Exception
  20.      */
  21.     private boolean connect(String path, String addr, int port, String username, String password) throws Exception {
  22.         boolean result = false;
  23.         ftp = new FTPClient();
  24.         int reply;
  25.         ftp.connect(addr, port);
  26.         ftp.login(username, password);
  27.         ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  28.         reply = ftp.getReplyCode();
  29.         if (!FTPReply.isPositiveCompletion(reply)) {
  30.             ftp.disconnect();
  31.             return result;
  32.         }
  33.         ftp.changeWorkingDirectory(path);
  34.         result = true;
  35.         return result;
  36.     }
  37.     /**
  38.      * @param file 上传的文件或文件夹
  39.      * @throws Exception
  40.      */
  41.     private void upload(File file) throws Exception {
  42.         if (file.isDirectory()) {
  43.             ftp.makeDirectory(file.getName());
  44.             ftp.changeWorkingDirectory(file.getName());
  45.             String[] files = file.list();
  46.             for (int i = 0; i < files.length; i++) {
  47.                 File file1 = new File(file.getPath() + "\" + files[i]);
  48.                 if (file1.isDirectory()) {
  49.                     upload(file1);
  50.                     ftp.changeToParentDirectory();
  51.                 } else {
  52.                     File file2 = new File(file.getPath() + "\" + files[i]);
  53.                     FileInputStream input = new FileInputStream(file2);
  54.                     ftp.storeFile(file2.getName(), input);
  55.                     input.close();
  56.                 }
  57.             }
  58.         } else {
  59.             File file2 = new File(file.getPath());
  60.             FileInputStream input = new FileInputStream(file2);
  61.             ftp.storeFile(file2.getName(), input);
  62.             input.close();
  63.         }
  64.     }
  65.     //测试即使指定其他用户的文件夹,还要没有写的权限也无法上传。
  66.     public static void main(String[] args) throws Exception {
  67.         FTPTest t = new FTPTest();
  68.         t.connect("/home/ftp0/", "192.168.2.108", 21, "ftp0", "123456");
  69.         File file = new File("/Users/houbinbin/Downloads/ftptest.txt");
  70.         t.upload(file);
  71.     }
  72. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表