FTP

FTP简介

FTP是File Transfer Protocol(文件传输协议)的缩写,用来在两台计算机之间互相传送文件。相比于HTTP,FTP协议要复杂得多。复杂的原因,是因为FTP协议要用到两个TCP连接,一个是命令链路,用来在FTP客户端与服务器之间传递命令;另一个是数据链路,用来上传或下载数据。

主动与被动模式

FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动式。

区别主要在于数据通道的建立方式:

  • 主动模式:服务器向客户端敲门,然后客户端开门
    • 在主动模式中,客户端向服务器发送一个随机端口号,服务器再用这个端口号和客户端建立数据通道。这样,服务器需要知道客户端的 IP 地址和端口号,并且能够穿透客户端的防火墙。
    • 如果通过代理上网的话,就不能用主动模式,因为服务器敲的是上网代理服务器的门,而不是敲客户端的门
    • 客户端也不是轻易就开门的,因为有防火墙阻挡,除非客户端开放大于1024的高端端口
  • 被动模式:客户端向服务器敲门,然后服务器开门
    • 在被动模式中,服务器向客户端发送一个随机端口号,客户端再用这个端口号和服务器建立数据通道。这样,客户端不需要公开自己的 IP 地址和端口号,并且只需要打开出站连接的防火墙。

ftp命令行登录

1
2
ftp ip
lftp user@site:port

常用命令

  1. 下载文件通常用get和mget这两条命令。
  2. 上传文件put和mput
  3. 断开连接bye

ftp空间

但是这个是当前目录的文件,不包括文件夹

1字节=1B,1024B=1KB

Ubuntu ftp服务器部署

vsftpd

  1. 安装

    1
    sudo apt install vsftpd # 安装
  2. 配置文件 /etc/vsftpd/vsftpd.conf

    1
    2
    3
    local_enable=YES # 是否允许本地用户访问  
    local_root=/home/kaikai_ftp/ftpdir # 自定义上传根目录
    write_enable=YES # 允许用户修改文件权限
  3. vsftpd虚拟用户

  4. 运行

    1
    2
    3
    4
    5
    6
    7
    systemctl restart vsftpd.service
    重启
    sudo service vsftpd start
    开机启动
    sudo systemctl enable vsftpd
    查看运行情况
    sudo service vsftpd status

vsftpd虚拟用户

虚拟用户

  1. 虚拟用户,只对ftp有效的用户。这些用户不可以登录Linux系统,只可以登录ftp服务器。其实就是一个本地用户映射成多个只对ftp服务器有效的虚拟用户。虚拟用户可以有自己的ftp配置文件,因此通常利用虚拟用户来对ftp系统的不同用户制定不同的权限,以达到安全控制的目的。与虚拟用户有关的设置以guest_开头。
  2. 匿名用户,也就是不需要输入密码就可登录ftp服务器的用户,这个用户名通常是ftp或anonymous; 与匿名用户有关的设置多以 anon_选项开头。
  3. 本地用户,也就是你Linux系统上可登录到系统的用户,这些用户是在系统上实实在在存在的用户。通常会有自己的home,shell等。与本地用户有关的设置多以local_开头或包含local_的选项。

●所有虚拟用户会统一映射为一个指定的系统帐号:访问共享位置,即为此系统帐号的家目录

●各虚拟用户可被赋予不同的访问权限,通过匿名用户的权限控制参数进行指定

具体命令

  1. 创建用户数据库文件
1
2
3
vim /etc/vsftpd/vusers.txt
ftp123
ftp2333
  1. 文件需要被加密编码为hash格式奇数行为用户名,偶数行为密码
1
2
3
sudo apt-get install db-util # install db_load
sudo db_load -T -t hash -f vusers.txt vusers.db #该 db_load 实用程序可用于将文本文件加载到数据库中
chmod 600 vusers.db
  1. 创建用户和访问FTP目录
1
2
3
4
5
6
7
8
9
sudo useradd -d /data/ftproot -s /sbin/nologin -r vuser
# -d, --home-dir HOME_DIR home directory of the new account
# -s, --shell SHELL login shell of the new account
# -r, --system create a system account
mkdir -pv /data/ftproot/upload #-pv 是没有父路径也会创建
setfacl -m u:vuser:rwx /data/ftproot/upload
# set file access control lists
# -m, --modify
#chmod a=rx /data/ftproot/ 如果自动创建家目录,需要改权限
  1. 创建pam配置文件

    1
    2
    3
    vim /etc/pam.d/vsftpd.db
    auth required pam_userdb.so db=/etc/vsftpd/vusers
    account required pam_userdb.so db=/etc/vsftpd/vusers
  2. 指定pam配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    vim /etc/vsftpd/vsftpd.conf

    pam_service_name=vsftpd.db
    userlist_enable=YES

    guest_enable=YES #所有系统用户都映射成guest用户
    guest_username=vuser #配合上面选项才生效,指定guest用户
    user_config_dir=/etc/vsftpd/vusers.d/ #虚拟用户设置独立的配置文件
    write_enable=YES

    anonymous_enable=NO # 匿名访问是否允许,默认不要开启
    local_enable=YES # 是否允许本地用户访问
    local_root=/home/shaojiemike/ftpdir # 自定义上传根目录

虚拟用户设置独立的配置文件

指定各用户配置文件存放的路径

1
2
vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vusers.d/

创建各个用户的配置文件存放路径,配置文件的文件名需要与用户名一致。
没有独立配置文件的虚拟用户会遵守/etc/vsftpd/vsftpd.conf这个主配置文件的权限配置。

1
2
3
4
5
6
7
8
9
mkdir /etc/vsftpd/vusers.d/
cd /etc/vsftpd/vusers.d/
vim ftp123
# 允许用户有自己的上传目录以及上传权限,添加这些参数以及值
local_root=/tmp/vutest_d
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
allow_writeable_chroot=YES

建立目录,更改目录的所有者与所属组

1
2
3
mkdir /tmp/vutest_d
chown vuser:vuser /tmp/vutest_d #还是那句话,用户能不能上传不仅与配置文件有关,还与目录是否有w权限有关,两个权限都开启才能正确上传。
chmod 755 upload

Linux-PAM 的配置文件

PAM 的各个模块一般存放在 /lib/security/ 或 /lib64/security/ 中,以动态库文件的形式存在,文件名格式一般为 pam_*.so。

PAM 的配置文件可以是 /etc/pam.conf 这一个文件,也可以是 /etc/pam.d/ 文件夹内的多个文件。如果 /etc/pam.d/ 这个文件夹存在,Linux-PAM 将自动忽略 /etc/pam.conf。

  1. /etc/pam.conf 类型的格式如下:

    1
    服务名称  工作类别  控制模式  模块路径  模块参数
  2. /etc/pam.d/ 类型的配置文件通常以每一个使用 PAM 的程序的名称来命令。比如 /etc/pam.d/su,/etc/pam.d/login 等等。还有些配置文件比较通用,经常被别的配置文件引用,也放在这个文件夹下,比如 /etc/pam.d/system-auth。这些文件的格式都保持一致:

    1
    工作类别  控制模式  模块路径  模块参数

需要进一步的研究学习

Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd’s capabilities.

遇到的问题

1
2
530 Login incorrect.
Login failed.
  1. 尝试改shell,但首先不是这个问题

    1
    2
    3
    cat /etc/shells #没有/sbin/nologin
    sudo usermod -s /bin/bash vuser
    cat /etc/passwd #shell改好了
  2. 查看报错

    我以为我是修改错文件了,但是好像没怎么简单

  3. 添加

    1
    2
    guest_enable=YES   # 开启虚拟用户
    guest_username=vuser # FTP虚拟用户对应的系统用户,即第四步添加的用户

    报错

    后来发现原因是,变量这一行不要加注释
    guest_enable=YES # 开启虚拟用户

  4. 只改pam的路径为pam.db
    报错


    这很明显是没有指定用户

实际问题

home.ustc.edu.cn

ftp 上传的内容几秒中之内被覆盖了。这是学校网站的保护机制吗?(我之前调试修改太多了?)

1
2
3
4
5
6
7
8
9
10
11
12
ftp> get index.html
local: index.html remote: index.html
200 EPRT command successful. Consider using EPSV.
150 Opening BINARY mode data connection for index.html (360991 bytes).
226 File send OK.
360991 bytes received in 0.01 secs (25.7474 MB/s)
ftp> get index.html
local: index.html remote: index.html
200 EPRT command successful. Consider using EPSV.
150 Opening BINARY mode data connection for index.html (16116 bytes).
226 File send OK.
16116 bytes received in 0.00 secs (15.3082 MB/s)

I try single command line put site/index.html index.html and after a minute get index.html get the old file.

My USTC homepage is blocked

1
2
3
4
5
6
7
8
9
10
11
ftp> ls
200 EPRT command successful. Consider using EPSV.
150 Here comes the directory listing.
drwxr-xr-x 46 0 0 4096 Oct 25 10:03 public_html.old
226 Directory send OK.
ftp> mkdir public_html
550 Create directory operation failed.
ftp> put jumpPage.html
local: jumpPage.html remote: jumpPage.html
200 EPRT command successful. Consider using EPSV.
553 Could not create file.

参考文献

https://www.jianshu.com/p/ac3e7009a764

https://blog.csdn.net/frank_ci/article/details/108847358

https://blog.csdn.net/enweitech/article/details/51330664

Author

Shaojie Tan

Posted on

2023-03-09

Updated on

2025-01-30

Licensed under