虽然在嵌入式系统中,我们可以通过串口的Xmodem模式传送小的文件,但是毕竟不符合我们的使用习惯,并且串口传输速率太低,不能满足我们大部分的需求,要传输文件的时候,我们还是使用网络较为熟悉,今天我将我的ftp的笔记整理一下,分享给大家。
1.何为FTP
FTP(File Transfer Protocol,文件传输协议)是 TCP/IP 协议组中的协议之一。 FTP 协议包括两个组成部分,其一为 FTP 服务器,其二为 FTP 客户端。其中 FTP 服务器用来存储文件,用户可以使用 FTP客户端通过 FTP 协议访问位于 FTP 服务器上的资源。由于 FTP 传输效率非常高,在网络上传输大的文件时, 经常采用该协议。
默认情况下 FTP 协议使用 TCP 端口中的 20 和 21 这两个端口,其中 20 用于传输数据, 21 用于传输控制信息。但是,是否使用 20 作为传输数据的端口与 FTP 使用的传输模式有关,如果采用主动模式,那么数据传输端口就是 20;如果采用被动模式,则具体最终使用哪个端口要服务器端和客户端协商决定。
vsftpd 则是 very secure FTP daemon 的缩写,它是 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux、 BSD、 Solaris、 HP-UNIX 等系统上面,是一个完全免费的、开放源代码的 FTP 服务器软件。
2.ftp 配置
我们今天还是使用OKMX6ULL-s2开发板来举例。
开发板默认已经启动vsftpd服务,我们先看一下配置。
我们看到关于ftp的配置有4个文件,
root@fetmx6ull-s:/etc# more vsftpd.chroot_list
root
root@fetmx6ull-s:/etc#
可以看到,我们ftp默认使用的用户名为root,
root@fetmx6ull-s:/etc# more vsftpd.ftpusers
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
root@fetmx6ull-s:/etc# more vsftpd.user_list
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd.ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
root@fetmx6ull-s:/etc#
root@fetmx6ull-s:/etc# more vsftpd.conf
# Example config file /etc/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
# run standalone
listen=YES
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftp
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that turning on ascii_download_enable enables malicious remote parties
# to consume your I/O resources, by issuing the command "SIZE /big/file" in
# ASCII mode.
# These ASCII options are split into upload and download because you may wish
# to enable ASCII uploads (to prevent uploaded scripts etc. from breaking),
# without the DoS risk of SIZE and ASCII downloads. ASCII mangling should be
# on the client anyway..
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# This string is the name of the PAM service vsftpd will use.
pam_service_name=vsftpd
#
# This option is examined if userlist_enable is activated. If you set this
# setting to NO, then users will be denied login unless they are explicitly
# listed in the file specified by userlist_file. When login is denied, the
# denial is issued before the user is asked for a password.
userlist_deny=YES
#
# If enabled, vsftpd will load a list of usernames, from the filename given by
# userlist_file. If a user tries to log in using a name in this file, they
# will be denied before they are asked for a password. This may be useful in
# preventing cleartext passwords being transmitted. See also userlist_deny.
userlist_enable=NO
#
# If enabled, vsftpd will display directory listings with the time in your
# local time zone. The default is to display GMT. The times returned by the
# MDTM FTP command are also affected by this option.
use_localtime=YES
#
# If set to YES, local users will be (by default) placed in a chroot() jail in
# their home directory after login. Warning: This option has security
# implications, especially if the users have upload permission, or shell access.
# Only enable if you know what you are doing. Note that these security implications
# are not vsftpd specific. They apply to all FTP daemons which offer to put
# local users in chroot() jails.
chroot_local_user=YES
#
allow_writeable_chroot=YES
#
tcp_wrappers=YES
从以上三个文件中,我们可以看到(这里不分析具体的配置说明),可以使用root用户进行ftp的登录,这里对ftp的root账户没有指定口令,所以要使用默认的口令,
修改口令可以使用passwd来完成。
3.连接ftp服务器
我的开发板的地址是192.168.0.232
1)第一种方法使用windows的命令行模式。
我一般喜欢使用win10中的powershell,因为这个是跨平台的,可以支持linux命令;
可以看到,我们已经连接成功了,并且可以看到ftp的根目录是 家目录的地址。
PS C:\Users\Administrator> ftp 192.168.0.232
连接到 192.168.0.232。
220 (vsFTPd 3.0.3)
200 Always in UTF8 mode.
用户(192.168.0.232:(none)): root
331 Please specify the password.
密码:
230 Login successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
JL2-24.log
USBDrv.inf
aa
auto.sh
cpu.log
cpu1.log
info.log
mem.log
memtester
mountdata.sh
net.log
run.sh
stress
sysinfo.sh
temp.log
tm.sh
tn
226 Directory send OK.
ftp: 收到 161 字节,用时 0.00秒 40.25千字节/秒。
ftp>
从嵌入式系统下载文件
ftp> get auto.sh d:\auto.sh
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for auto.sh (33 bytes).
226 Transfer complete.
ftp: 收到 33 字节,用时 0.00秒 33000.00千字节/秒。
上传文件到嵌入式系统中
ftp> put d:\auto.sh a.sh
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 33 字节,用时 0.01秒 2.20千字节/秒。
ftp>
2)使用工具软件filezilla软件
输入正确的ip地址,用户名 和密码,登录
可以看到,已经登录成功了,并且列出了ftp服务器上的根目录的文件(实际就是家目录下的文件),我们可以对比一下
具体的这个上传下载旧更简单了。不需要在这里啰嗦了。
就写到这里了。欢迎评论,共同提高。。