Linux ftp服务器和客户端解析


嵌入式Linux开发过程中需要经常涉及到文件的拷贝,那么使用简单、快捷的应该是ftp服务。ftp服务器一般分为两种:vsftpd和ftpd,下面逐一讲解一下。

ftp术语

是什么

FTP(File Transfer Protocol,文件传输协议) 是 TCP/IP 协议组中的协议之一。FTP基于TCP协议,由于较高的传输效率,经常用于文件的传输。其包含两部分:客户端和服务器;服务器一般用来存储文件供客户端下载。
FTP默认情况下使用TCP的21和20端口,其中21用于传输控制信息,20用于传输数据。是否使用20作为数据传输端口与ftp的工作方式有关,如果采用主动模式,数据传输端口为20;被动模式下,端口需要客户端和服务器端协商决定。

工作方式

FTP有两种工作方式:主动模式和被动模式。

  • 主动模式”.:Standard (也就是 PORT方式,主动方式),该模式下FTP的客户端发送PORT命令到FTP服务器。该模式下,ftp客户端首先与服务器建立TCP 21端口建立连接,用于发送控制命令,客户端在传输数据时在该通道上发送PORT命令。PORT包含了客户端用什么端口接收数据,FTP服务器通过自己的20端口连接至客户端指定的端口上,然后进行数据的传输。
  • 被动模式:Passive(也就是PASV,被动方式),该模式下FTP的客户端发送
    PASV命令到FTP服务器。与主动模式一样,先建立21端口上的连接,用于发送控制命令。客户端在传输数据时在该通道上发送Pasv命令。FTP服务器收到Pasv命令之后,随机打开一个高端端口(大于1024)并且通知客户端在这个端口上传输数据,然后客户端连接
    至服务器的该端口,然后进行数据的传输。

用户授权

  • 授权:要能够连接到FTP服务器需要该服务器授权的账号,即一个用户名和密码。之后才能登陆到FTP服务器之上,享受FTP服务;
  • 地址格式:FTP的地址格式如下:ftp://用户名:密码@FTP服务器IP或域名:FTP命令端口/路径/文件名,上面的参数除FTP服务器IP或域名为必要项外,其他都不是必须的。如以下地址都是有效FTP地址:
ftp://foolish.6600.org
ftp://list:[email protected]
ftp://list:[email protected]:2003
ftp://list:[email protected]:2003/soft/list.txt
  • 匿名:互联网中有很大一部分 FTP 服务器被称为“匿名”(Anonymous)FTP 服务器。这类服务器的目的是向公众提供文件拷贝服务,不要求用户事先在该服务器进行登记注册,也不用取得FTP服务器的授权。常用的匿名登录名有:“Anonymous”,或者用户的E-mail地址;

传输模式

FTP有两种传输模式:ASCII传输模式和二进制传输模式。

  • ASCII传输模式:如果传输的文件是纯文本内容,那么在不同操作系统之间传输文件时,ftp会自动的转换文本内容以适应不同操作系统的文本格式差异。
  • 二进制传输模式:如果传输的文件是二进制文件,例如,可执行程序、数据库文件等,那么就需要使用二进制传输模式,进行字节的逐个拷贝。
  • 注意:如果你在ASCII方式下传输二进制文件,即使不需要也仍会转译。这会使传输稍微变慢 ,也会损坏数据,使文件变得不能用。(在大多数计算机上,ASCII方式一般假设每一字符的第一有效位无意义,因为ASCII字符组合不使用它。如果你传输二进制文件,所有的位都是重要的。)如果你知道这两台机器是同样的,则二进制方式对文本文件和数据文件都是有效的。

ftp服务器

ftpd

嵌入式Linux根文件系统构建时,一般使用busybox来生成系统命令,其中包括ftpd,执行ftpd提示如下:

BusyBox v1.20.2 (2012-08-07 15:53:49 CST) multi-call binary.

Usage: ftpd [-wvS] [-t N] [-T N] [DIR]

Anonymous FTP server

ftpd should be used as an inetd service.
ftpd's line for inetd.conf:
        21 stream tcp nowait root ftpd ftpd /files/to/serve
It also can be ran from tcpsvd:
        tcpsvd -vE 0.0.0.0 21 ftpd /files/to/serve

        -w      Allow upload
        -v      Log errors to stderr. -vv: verbose log
        -S      Log errors to syslog. -SS: verbose log
        -t,-T   Idle and absolute timeouts
        DIR     Change root to this directory

  • 可以看到ftpd服务器为匿名服务器;
  • ftpd有两种启动方式:
    1. 作为inet的服务启动,其中ftpd在inetd.conf中的配置参数为:
    21 stream tcp nowait root ftpd ftpd /files/to/serve
    
    1. 通过tcpsvd启动,其配置参数如下,其中0.0.0.0 21可以在本地任务IP上建立ftp连接。/files/to/serve 表示ftp服务的根目录。
    tcpsvd -vE 0.0.0.0 21 ftpd /files/to/serve
    
    1. 注意,使用tcpsvd配置ftpd时,如果需要上传文件,需要加上-w选项。
    tcpsvd -vE 0.0.0.0 21 ftpd -w /files/to/serve
    

vsftpd

vsftpd(very security ftp daemon)顾名思义就是非常安全的ftp。其开源、轻量、安全。支持所有的Unix-like系统。

安装

  1. 安装
$ sudo apt-get install vsftpd

2.查看是否打开21端口

$ sudo netstat -npltu | grep 21
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      15601/vsftpd 

3.登录

ftp localhost

输入系统的账户、密码登录ftp服务器
4.dir显示用户根目录所有文件

dir

文件结构

1.ftp登录时,使用的哪个系统账号,登录成功后就会进入用户对应的home目录,例如,用户ftp在/etc/passwd目下的配置如下:

ftp::10:40:ftp:/var/ftp:

其中,/var/ftp为ftp的home目录,使用ftp登录vsftpd之后,会进入到/var/ftp目录。
2.配置文件

/etc/vsftpd.conf

3.查阅配置文件详细信息

man 5 vsftpd.conf

4.设定log保存位置,默认如下

xferlog_file=/var/log/vsftpd.log

运行方式

standalone
最为普遍的方法

sudo service vsftpd start

super daemon
1.需要修改vsftpd.conf

listen=NO

**注意:**这里若不改成NO,会出现如下错误:

500 OOPS: could not bind listening IPv4 socket

2.安装xinetd

sudo apt-get install xinetd
sudo vi /etc/xinetd.conf
service ftp
{
        socket_type             = stream
        wait                    = no
        user                    = root
        server                  = /usr/sbin/vsftpd
        log_on_success          += DURATION USERID
        log_on_failure          += USERID
        nice                    = 10
        disable                 = no
}

3.停止vsftpd,启动xinetd

sudo service vsftpd stop
sudo service xinetd start

查看端口

$ sudo netstat -npltu | grep 21
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      16787/xinetd  

vsftpd配置文件

vsftpd.conf
listen=<YES/NO> :设置为YES时vsftpd以独立运行方式启动,设置为NO时以xinetd方式启动(xinetd是管理守护进程的,将服务集中管理,可以减少大量服务的资源消耗)
listen_port=<port> :设置控制连接的监听端口号,默认为21
listen_address=<ip address> :将在绑定到指定IP地址运行,适合多网卡
connect_from_port_20=<YES/NO> :若为YES,则强迫FTP-DATA的数据传送使用port 20,默认YES
pasv_enable=<YES/NO> :是否使用被动模式的数据连接,如果客户机在防火墙后,请开启为YES
pasv_min_port=<n>
pasv_max_port=<m> :设置被动模式后的数据连接端口范围在n和m之间,建议为50000-60000端口
message_file=<filename> :设置使用者进入某个目录时显示的文件内容,默认为 .message
dirmessage_enable=<YES/NO> :设置使用者进入某个目录时是否显示由message_file指定的文件内容
ftpd_banner=<message> :设置用户连接服务器后的显示信息,就是欢迎信息
banner_file=<filename> :设置用户连接服务器后的显示信息存放在指定的filename文件中
connect_timeout=<n> :如果客户机连接服务器超过N秒,则强制断线,默认60
accept_timeout=<n> :当使用者以被动模式进行数据传输时,服务器发出passive port指令等待客户机超过N秒,则强制断线,默认60
accept_connection_timeout=<n> :设置空闲的数据连接在N秒后中断,默认120
data_connection_timeout=<n> : 设置空闲的用户会话在N秒后中断,默认300
max_clients=<n> : 在独立启动时限制服务器的连接数,0表示无限制
max_per_ip=<n> :在独立启动时限制客户机每IP的连接数,0表示无限制(不知道是否跟多线程下载有没干系)
local_enable=<YES/NO> :设置是否支持本地用户帐号访问
guest_enable=<YES/NO> :设置是否支持虚拟用户帐号访问
write_enable=<YES/NO> :是否开放本地用户的写权限
local_umask=<nnn> :设置本地用户上传的文件的生成掩码,默认为077
local_max_rate<n> :设置本地用户最大的传输速率,单位为bytes/sec,值为0表示不限制
local_root=<file> :设置本地用户登陆后的目录,默认为本地用户的主目录
chroot_local_user=<YES/NO> :当为YES时,所有本地用户可以执行chroot
chroot_list_enable=<YES/NO> 
chroot_list_file=<filename> :当chroot_local_user=NO 且 chroot_list_enable=YES时,只有filename文件指定的用户可以执行chroot
anonymous_enable=<YES/NO> :设置是否支持匿名用户访问
anon_max_rate=<n> :设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制
anon_world_readable_only=<YES/NO> 是否开放匿名用户的浏览权限
anon_upload_enable=<YES/NO> 设置是否允许匿名用户上传
anon_mkdir_write_enable=<YES/NO> :设置是否允许匿名用户创建目录
anon_other_write_enable=<YES/NO> :设置是否允许匿名用户其他的写权限(注意,这个在安全上比较重要,一般不建议开,不过关闭会不支持续传)
anon_umask=<nnn> :设置匿名用户上传的文件的生成掩码,默认为077
  1. 允许匿名登录
# Allow anonymous FTP? (Disabled by default)
anonymous_enable=YES
  1. 上传文件
    允许匿名上传
write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES

注意:
注意2点:

1.匿名用户就是ftp,想要匿名用户写入,必须文件夹的权限为ftp可写。

2.匿名用户的根目录不允许写,所以根目录的权限绝对不能是ftp可写和其他用户可写,如果根目录所有者为ftp的话,所有者的权限也不能写。

所以解决方法是建个单独的public文件夹用于上传文件,设置其为ftp可写或”其他用户可写“

还可建个download文件夹只用于下载,设置其他用户没有写权限便可。
  1. 重命名、删除文件
    开放重命名,删除文件等权限,不开的话没法续传。
anon_other_write_enable=YES

4.仅能上传不能下载

write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES
chown_uploads=YES
chown_username=root

上传的文件所有者被改为root,匿名用户的ftp用户就无法读取,下载了。
5.认证FTP配置
设定vsftp认证系统用户,并允许他们上传文件,编辑 /etc/vsftpd.conf:

local_enable=YES
write_enable=YES

重启vsftpd

sudo service vsftpd restart

系统用户登录ftp便进入他们的home目录。

chroot

  1. 限制所有
    限制登录用户访问其他目录,改之前登录显示的路径比如是 ~ ,改之后则是 /。
效果是像这样的。
注:我的本地用户(local user为yeshuai,home directory为/home/yeshuai)


root@ubuntu:~# ftp localhost
Connected to localhost.
220 (vsFTPd 2.3.2)
Name (localhost:yeshuai): yeshuai

331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 
ftp> pwd
'''257 "/"'''

看上边,正常情况下,输入pwd时,应该是显示/home/yeshuai.
由于我做了chroot.所以,/home/yeshuai变成 /

chroot_local_user=YES
  1. 开放所有,限制特定
    可指定一组用户限制
chroot_local_user=NO
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

随后创建列表

sudo vi  /etc/vsftpd.chroot_list

一行一个用户名 重启vsftp

sudo service vsftpd restart

3.限制所有,开放特定
上面的规则是限制 /etc/vsftpd.chroot_list 中的用户,反过来限制一切,只解禁 /etc/vsftpd.chroot_list 的用户。这样:

chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

账号登录

两种方式:

  1. /etc/ftpusers文件
    该文件内的用户一律禁止ftp连接,默认列表包括了root, daemon, nobody等。需要禁止某个用户,添加进来便是。

这个文件是由PAM模块的 /etc/pam.d/vsftpd 指定的

qii@ubuntu:~$ sudo cat /etc/pam.d/vsftpd
# Standard behaviour for ftpd(8).
auth    required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.

# Standard pam includes
@include common-account
@include common-session
@include common-auth
auth    required        pam_shells.so
  1. /etc/userlist_file文件
    vsftpd自订的列表,跟/etc/ftpusers类似,具体文件名和路径是由用户自己指定的。这边设成 /etc/vsftpd.user_list
    添加配置文件字段:
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd.user_list

3.限制一切用户,开发特定
上述2个方法都是限制列表文件中的用户,如果要反过来,限制一切用户登录,只允许列表文件中的用户,用 userlist_file,这样:

userlist_enable=YES
userlist_deny=NO

4.root登录
禁止root是为了系统安全考虑,如果非要允许root登录,将root用户配置到上述文件即可。

限制

1.限制链接数
前者为服务器最大支持连接数,后者为每个ip允许最多连接数。

max_clients=数字
max_per_ip=数字

错误提示分别是:

qii@ubuntu:~$ ftp localhost 
Connected to localhost.
421 There are too many connected users, please try later.
qii@ubuntu:~$ ftp localhost 
Connected to localhost.
421 There are too many connections from your internet address.

2.限制下载速率
单位是字节,所以需要换算。比如我想让匿名用户和vsFTP上的用户都以80KB下载,所以这个数字应该是1024x80=81920。

anon_max_rate=数字 #匿名用户下载速度
local_max_rate=数字 #普通用户下载速度

欢迎信息

dirmessage_enable=YES

然后编辑各用户home目录下的.message。

vi .message

qii@ubuntu:~$ cat .message 
欢迎来到vsftpd

qii@ubuntu:~$ ftp localhost 
Connected to localhost.
220 (vsFTPd 2.2.2)
Name (localhost:qii): qii
331 Please specify the password.
Password:
230-欢迎来到vsftpd
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

若是匿名用户,就放到/var/ftp目录下。

虚拟路径

比如我的ftp的默认目录是/srv/ftp,我想把/mnt/LinG/WinSoft文件夹,映射到/srv/ftp目录中,我就如下操作 命令:

#mount --bind [原有的目录] [新目录]

先创建文件夹

sudo mkdir /srv/ftp/WinSoft

执行mount命令

sudo mount --bind /mnt/LinG/WinSoft /srv/ftp/WinSoft

ftp客户端

Linux下ftp的客户端,一般就是ftp。终端下敲入ftp username回车,就会提示输入ftp账号和密码。

  1. 匿名登录ftp服务器。
qihua@qihua-virtual-machine:~$ ftp 192.168.200.167
Connected to 192.168.200.167.
220 Welcome to blah FTP service.
Name (192.168.200.167:qihua): ftp
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

  1. ftp支持的命令
ftp> help
Commands may be abbreviated.  Commands are:

!	    	dir	    	mdelete		qc		   site
$	    	disconnect	mdir		sendport	size
account		exit		mget		put		    status
append		form		mkdir		pwd		    struct
ascii		get	    	mls		    quit		system
bell		glob		mode		quote		sunique
binary		hash		modtime		recv		tenex
bye		    help		mput		reget		tick
case		idle		newer		rstatus		trace
cd	    	image		nmap		rhelp		type
cdup		ipany		nlist		rename		user
chmod		ipv4		ntrans		reset		umask
close		ipv6		open		restart		verbose
cr	    	lcd	    	prompt		rmdir		?
delete		ls	    	passive		runique
debug		macdef		proxy		send

  1. 查看ftp连接的状态
ftp> status
Connected to 192.168.200.167.
No proxy connection.
Connecting using address family: any.
Mode: stream; Type: binary; Form: non-print; Structure: file
Verbose: on; Bell: off; Prompting: on; Globbing: on
Store unique: off; Receive unique: off
Case: off; CR stripping: on
Quote control characters: on
Ntrans: off
Nmap: off
Hash mark printing: off; Use of PORT cmds: on
Tick counter printing: off

  • Type:binary:当前传输模式是binary模式。可以通过ascii切换到ASCII模式;
  • Use of PORT cmds:on,表示当前工作模式是主动模式
  1. pwd显示当前工作目录。例如,ftp账号登录之后,进入ftp的home目录/var/ftp/
ftp> pwd
257 "/"

  1. cd目录切换,例如切换到/var/pub目录
ftp> cd pub
250 Directory successfully changed.
ftp> pwd
257 "/pub"
ftp> 

  1. 显示目录内容,dir或者ls
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rwxrwxrwx    1 14       50       14739185 Sep 12 08:38 atmel9260.tar.gz
-r-------T    1 14       50           2219 Oct 12 06:31 build.sh
-rwxr-xr-x    1 0        0            3987 Oct 12 09:11 vsftpd.conf
226 Directory send OK.
ftp> dir
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rwxrwxrwx    1 14       50       14739185 Sep 12 08:38 atmel9260.tar.gz
-r-------T    1 14       50           2219 Oct 12 06:31 build.sh
-rwxr-xr-x    1 0        0            3987 Oct 12 09:11 vsftpd.conf
226 Directory send OK.
ftp> 

  1. 使用get命令,下载文件到本地
ftp> get vsftpd.conf
local: vsftpd.conf remote: vsftpd.conf
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for vsftpd.conf (3987 bytes).
226 Transfer complete.
3987 bytes received in 0.00 secs (5794.0 kB/s)
ftp> 

  1. 使用put命令,上传文件到ftp服务器
ftp> put test
local: test remote: test
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
5 bytes sent in 0.00 secs (14.2 kB/s)
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rwxrwxrwx    1 14       50       14739185 Sep 12 08:38 atmel9260.tar.gz
-r-------T    1 14       50           2219 Oct 12 06:31 build.sh
-r-------T    1 14       50              5 Oct 14 00:56 test
-rwxr-xr-x    1 0        0            3987 Oct 12 09:11 vsftpd.conf
226 Directory send OK.
ftp> 
  1. close关闭当前连接,quit退出ftp客户端
ftp> close
221 Goodbye.
ftp> quit

te.
5 bytes sent in 0.00 secs (14.2 kB/s)
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rwxrwxrwx 1 14 50 14739185 Sep 12 08:38 atmel9260.tar.gz
-r-------T 1 14 50 2219 Oct 12 06:31 build.sh
-r-------T 1 14 50 5 Oct 14 00:56 test
-rwxr-xr-x 1 0 0 3987 Oct 12 09:11 vsftpd.conf
226 Directory send OK.
ftp>

9. close关闭当前连接,quit退出ftp客户端

ftp> close
221 Goodbye.
ftp> quit

发布了119 篇原创文章 · 获赞 125 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/linux_embedded/article/details/102544691