首页 > 系统相关 >文件系统 FTP Ubuntu 安装入门介绍

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

时间:2024-03-31 12:35:45浏览次数:23  
标签:FTP File ftp 文件系统 vsftpd ftp0 file Ubuntu new

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 测试代码
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);
    }

}

标签:FTP,File,ftp,文件系统,vsftpd,ftp0,file,Ubuntu,new
From: https://www.cnblogs.com/houbbBlogs/p/18106576

相关文章

  • 安装并使用 Ubuntu Server 的一些注意事项
    最近,刚安装UbuntuServer22.04.4LTS版本,遇到些问题,记录其解决方案以备忘。 1)断网安装UbuntuServer系统采用USB启动盘,安装UbuntuServer时,一定要断网,因为连接互联网,安装UbuntuServer系统时,会自动下载更新,等待时间很可能会比较长,而断网会秒级安装,之后根据提示,重启......
  • ubuntu编译与安装 OpenSSL-1.0.0
    apt-getpurgeopensslrm-rf/etc/ssl#删除配置文件编译与安装OpenSSLprefix是安装目录,openssldir是配置文件目录,另外建议安装两次,shared作用是生成动态连接库。(需要同时指定prefix与openssldir,否则可能会因为找不到文件而报错)wgetftp://ftp.openssl.org/source/op......
  • [linux] ubuntu 下安装qtcreate遇到“无法加载Qt平台插件‘xcb’问题”解决方案
    [linux]ubuntu下安装qtcreate遇到“无法加载Qt平台插件‘xcb’问题”解决方案以下是遇到的三种报错情况From6.5.0,xcb-cursor0orlibxcb-cursor0isneededtoloadtheQtxcbplatformplugin.CouldnotloadtheQtplatformplugin“xcb”in“”eventhough......
  • Ubuntu下anaconda安装tensorflow-gpu遇到的问题
    创建虚拟环境并激活后```$condacreate-ntensorflowpython=3.9$condaactivatetensorflow```使用下面指令安装tensorflow时显示"Solvingenvironment:failedwithinitialfrozensolve.Retryingwithflexiblesolve."```$condainstalltensorflow==2.6.0``` 换成pip......
  • 【GitLab】Ubuntu使用宝塔安装GitLab最新社区版
    首先在Ubuntu安装宝塔面板在官网可以找到脚本一键安装安装GitLab社区版然后在宝塔面板的“软件商店”里面找到GitLab最新社区版12.8.1一键安装安装过程中可能出现以下问题:1.卡在ruby_block[waitforlogrotateservicesocket]actionrun解决办法:在Ubuntu终端中运行......
  • Ubuntu Server安装界面并VNC连接
    参考:公网环境下使用VNC远程连接Ubuntu系统桌面https://blog.csdn.net/qq_63320529/article/details/134506820apt更新一下sudoaptupdatesudoaptupgrade使用xfce界面安装lightdmsudoaptinstalllightdm启动lightdm服务sudosystemctlenablelightdm......
  • 解决ubuntu22.04的ssh问题--userauth_pubkey: key type ssh-rsa not in PubkeyAccepte
    问题在我新安装了一台ubuntu22.04的服务器时,配置远程服务器正常情况下,只要把握本机的公钥写到被远程主机对应用户的.ssh/authorized_keys文件中就好了但是,今天发现,虽然设置进去了,但是远程还是提示需要密码但是并不是所有的主机是这样的,同样是ubuntu22.04的服务器还是能够免......
  • Ubuntu中如何配置ssh,连接xshell
    这里演示Ubuntu中配置ssh服务,并且使用xshell连接Ubuntu中的root用户1.如果之前没有配置过root密码的话这里可以配置一下root的密码,如果之前配置过,直接登录进去即可。sudopasswdroot然后填写自己的登录密码,这时密码不会显示,但确实填进去了,然后配置自己的root密码,然后确定......
  • Avalonia 运行在Ubuntu20.04上,记录发布到运行的过程,已解决默认字体问题
    目录1.安装.NET8.0环境2.发布Avalonia程序3.默认字体问题解决Demo程序下载(开箱即用):https://download.csdn.net/download/rotion135/890489371.安装.NET8.0环境下载微软dotnet安装脚本:sudowgethttps://dot.net/v1/dotnet-install.sh-Odotnet-install.sh运行......
  • Ubuntu/Debian系统下实现应用程序开机自启动功能
    Ubuntu/Debian系统下实现应用程序开机自启动功能在做linux开发的过程中,有时候我们会想让自己编写的应用程序在开机的时候可以做到自动运行。这里以我自己编写的U盘升级程序为例简要介绍在ubuntu/debian系统下实现应用程序的开机自启动功能。一、开发环境ubuntu18.04、debian......