檔案系統 FTP Ubuntu 安裝入門介紹

老马啸西风發表於2024-03-31

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);
    }

}

相關文章