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

FTP

环境: Ubuntu 14.04

blog zh_CN
ubuntu14.04

  • Install

    全新安装:apt-get install vsftpd
    重新安装:apt-get --reinstall install vsftpd
    卸载并清除配置文件:apt-get --purge remove vsftpd

  • Start & Restart

    service vsftpd start service vsftpd restart

注意:

网上文章很多有提及/etc/init.d/vsftpd start 之类的启动方式。但是这个目录下我不存在 vsftpd. 这个目录确实有: /etc/init/vsftpd.conf

复制代码
vsftpd 已經進化為 Upstart job
設定檔放在
/etc/init/vsftpd.conf
  • Create ftp user

1.此用户只是用来使用ftp服务的 2.此用户不可登录服务器 3.此用户不能访问ftp指定文件夹之外的文件

(1) 创建一个用户ftp0

复制代码
useradd -d /home/ftp0 -m ftp0

(2) 修改ftp0的密码

复制代码
passwd ftp0
  • Config /etc/vsftpd.conf

    anonymous_enable=NO # 不允许匿名访问
    write_enable=YES # 允许写
    local_enable=YES # 允许本地主机访问
    chroot_local_user=YES # 只能访问自身的目录,此处有坑,需加上下面一行

报错误信息:

复制代码
"500 OOPS: vsftpd: refusing to run with writable root inside chroot()"

从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。

(1) 启用了chroot的话,根目录要设置为不可写

复制代码
chmod a-w /home/ftp0

(2) 或者添加一句话

复制代码
allow_writeable_chroot=YES #允许写自身的目录

可是添加这句话可能会导致服务重启失败。。。

无奈之下。。。chroot_local_user=YES这句话暂时不加。

  • 让用户不能登录

    $ usermod -s /sbin/nologin ftp0

after all these, restart the ftp service:

复制代码
# service vsftpd restart
vsftpd stop/waiting
vsftpd start/pre-start, process 10305
# service vsftpd status
vsftpd start/running, process 10305
  • Test

    ftp

    ftp> open 192.168.2.108
    Connected to 192.168.2.108.
    220 (vsFTPd 3.0.2)
    Name (192.168.2.108:hbb): ftp0
    331 Please specify the password.
    Password:
    530 Login incorrect.
    Login failed.

需要 vi /etc/shells, 最后一行添加:

复制代码
/sbin/nologin

重新测试:

复制代码
# ftp
ftp> open 192.168.2.108
Connected to 192.168.2.108.
220 (vsFTPd 3.0.2)
Name (192.168.2.108:hbb): ftp0
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

21 端口查看:

复制代码
netstat -npltu | grep 21
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      11398/vsftpd

Ftp Java code

  • Java 测试代码
java 复制代码
package com.ryo.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream;

/**
 * @author houbinbin
 * @on 17/1/1
 */
public class FTPTest {

    private FTPClient ftp;

    /**
     * @param path     上传到ftp服务器哪个路径下
     * @param addr     地址
     * @param port     端口号
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws Exception
     */
    private boolean connect(String path, String addr, int port, String username, String password) throws Exception {
        boolean result = false;
        ftp = new FTPClient();
        int reply;
        ftp.connect(addr, port);
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return result;
        }
        ftp.changeWorkingDirectory(path);
        result = true;
        return result;
    }

    /**
     * @param file 上传的文件或文件夹
     * @throws Exception
     */
    private void upload(File file) throws Exception {
        if (file.isDirectory()) {
            ftp.makeDirectory(file.getName());
            ftp.changeWorkingDirectory(file.getName());
            String[] files = file.list();
            for (int i = 0; i < files.length; i++) {
                File file1 = new File(file.getPath() + "\\" + files[i]);
                if (file1.isDirectory()) {
                    upload(file1);
                    ftp.changeToParentDirectory();
                } else {
                    File file2 = new File(file.getPath() + "\\" + files[i]);
                    FileInputStream input = new FileInputStream(file2);
                    ftp.storeFile(file2.getName(), input);
                    input.close();
                }
            }
        } else {
            File file2 = new File(file.getPath());
            FileInputStream input = new FileInputStream(file2);
            ftp.storeFile(file2.getName(), input);
            input.close();
        }
    }

    //测试即使指定其他用户的文件夹,还要没有写的权限也无法上传。
    public static void main(String[] args) throws Exception {
        FTPTest t = new FTPTest();
        t.connect("/home/ftp0/", "192.168.2.108", 21, "ftp0", "123456");
        File file = new File("/Users/houbinbin/Downloads/ftptest.txt");
        t.upload(file);
    }

}
相关推荐
柯南二号1 小时前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
桦说编程8 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen8 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研8 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员9 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋9 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国10 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~10 小时前
《设计模式》装饰模式
java·设计模式
A尘埃10 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交10 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图