目录
sudo apt-get install vsftpd
安装过程中会自动创建一个叫做ftp的用户组。
安装成功之后输入以下指令重启ftp服务,理论上到此为止客户端就可以访问ftp服务了:
sudo /etc/init.d/vsftpd restart
在Windows随便打开一个文件窗口,或者打开浏览器,地址栏输入ftp://你的服务器IP地址,比如我的服务器应该输入ftp://192.168.0.187,会提示输入用户名和密码,这时即使不输入用户名和密码也可以访问服务器文件(当然输入Ubuntu服务器上的用户名和密码也可访问服务器文件);
以上的访问方式太过简单,我们把这种访问方式称为“匿名用户访问方式”,为了让ftp文件访问不这么随便,我们尝试采取两步防护措施(这种方式其实后来证明没卵用,最后再说):
将来客户端通过输入这个用户的用户名和密码访问服务器文件,当然这一步并不是必需的,你想用现有的用户也行,新建的用户最好位于ftp用户组下,假如我新建的用户名叫做ftpuser(你可以起其它名字,不一样也行):
sudo mkdir /home/ftpuser #ftpuser是我自己起的用户名,你可以换成你喜欢的名字
sudo adduser -d /home/ftpuser -g ftp -s /bin/bash ftpuser
这样就新建了一个叫做ftpuser的用户,并且指定这个用户的用户组为ftp,当然你不想指定用户组也行,去掉-g ftp就行了。
sudo passwd ftpuser # ftpuser是我起的用户名,你要换成你的,跟上边创建用户时一致
输入以上命令后就会提示你设置密码。
sudo chown ftpuser:ftp /home/ftpuser
其中ftpuser是用户名,冒号后边的ftp是用户组,空格后边的/home/ftpuser是用户的home目录
先备份原文件
sudo mv /etc/vsftpd.conf /etc/vsftpd.conf.bak
然后新建一个配置文件
sudo vi /etc/vsftpd.conf
粘贴以下内容:
listen=NO
listen_ipv6=YES
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO # 访问限制:不允许以匿名方式访问ftp服务
# Uncomment this to allow local users to log in.
local_enable=YES #允许使用本地用户名和密码登录ftp服务器
# 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
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# 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
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
# 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.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
# You may fully customise the login banner string:
ftpd_banner=Welcome to FTP service.
# 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().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES # 允许让本地用户成为访问限制的例外
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list # 在/etc/vsftpd.chroot_list文件中指定可以用来访问ftp的用户名
local_root=/my_share_dir # 客户端登录后看到的第一个文件夹,你应该改成你希望用户看到的目录
force_dot_files=YES # 设置客户端能不能看到以点开头的隐藏文件,YES表示客户端将来可以看到以点开头命名的目录,默认为NO
# This option should be the name of a directory which is empty. Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
# pam_service_name=vsftpd
pam_service_name=ftp
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES # 是否以utf-8编码形式向客户端提供文件,建议设为YES,否则将来客户端看到的以中文命名的目录会变成乱码
其中重要的内容我已经在注释中说明了,这里补充几点:
/etc/vsftpd.chroot_list文件
这个文件具体叫什么名字,在哪个路径下,取决于你上面/etc/vsftpd.conf中配置的路径,并不是固定的。
这个文件中只写用户名即可,啥都不用写,例如我们上面新建的Ubuntu用户名叫做ftpuser,只需要写一个ftpuser即可,然后保存退出,再啥都不用写,例如我的文件全部内容是:
ftpuser
以上内容要保证用户名拼写正确,别写错,我就因为把ftpuser错写成fptuser导致调试了两个小时没试出来,太坑了。
在Windows中访问ftp看似简单,实则有坑,最简单的方式的是在浏览器或任意一个文件夹中输入ftp://你的服务器IP地址 就可以访问,例如ftp://192.168.0.187,用户名和密码输我们上面新建的用户名和密码。但是这种方式访问ftp有些文件无法传输,会报错,比如我传普通的txt和mp4文件都没问题,但是传mkv文件就不行了,所以就要用到第三方软件来访问。
我用的第三方软件是FileZilla,网上有免费下载的,还有中文版,打开软件之后点“文件->站点管理器”,在打开的窗口中点“New site”,按提示输入服务器IP地址,要以ftp://开头,ftp://这个前缀不能省,用户名和密码输我们上面新建的用户名和密码,端口号可以不填,在“字符集”选项卡中选“强制UTF-8”,否则中文目录名会显示乱码,点“连接”即可;
下载ES文件浏览器,选“我的网络”,点右上角三个点,选“新建->ftp”,服务器一栏填入ftp服务器地址,不需要加ftp://前缀,端口号默认21不用改,模式“被动”也不用改,用户名和密码输入我们上边新建的用户名和密码,例如我的用户叫做ftpuser,编码选“Unicode(UTF-8)”,点确定即可。
此时我们可以看到,通过安卓手机上的ES文件浏览器可以看到ftp服务器上所有用户的所有目录,这尼玛上边设置的安全性限制都白设了呀。
Ubuntu Server 16.04.1 LTS 64位使用vsftpd搭建ftp服务器
Linux(Ubuntu 16.04)搭建ftp服务器(最简单版)