首页 > 技术文章 > linux上搭建ftp服务器

qnight 2017-09-13 21:26 原文

摘要

vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点。vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux、BSD、Solaris、 HP-UNIX等系统上面,是一个完全免费的、开放源代码的ftp服务器软件,支持很多其他的 FTP 服务器所不支持的特征。比如:非常高的安全性需求、带宽限制、良好的可伸缩性、可创建虚拟用户、支持IPv6、速率高等。

vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序。特点是小巧轻快,安全易用。(来自百度百科)

安装vsftpd

检测是否已经安装vsftpd。

[root@localhost ~]# rpm -qa | grep vsftpd
[root@localhost ~]# 

什么都不显示就没有安装

安装vsftpd

使用yum安装
[root@localhost ~]# yum -y install vsftpd
使用rpm安装
[root@localhost local]# rpm -ivh vsftpd-3.0.2-21.el7.x86_64.rpm 

启动服务

[root@localhost local]# service vsftpd start
Redirecting to /bin/systemctl start  vsftpd.service

查看服务状态

[root@localhost local]# service vsftpd status
Redirecting to /bin/systemctl status  vsftpd.service
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2017-08-30 15:28:18 CST; 15min ago
  Process: 4295 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
 Main PID: 4296 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─4296 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

830 15:28:18 localhost.localdomain systemd[1]: Starting Vsftpd ftp daemon...
830 15:28:18 localhost.localdomain systemd[1]: Started Vsftpd ftp daemon.
Hint: Some lines were ellipsized, use -l to show in full.

Active: active (running) 启动状态,可以使用

现在我们就可以匿名访问了

vsftpd添加用户

vsftpd添加用户

FTP用户一般是不能登录系统的,只能进入FTP服务器自己的目录中,这是为了安全.这样的用户就叫做虚拟用户了.实际上并不是真正的虚拟用户,只是不能登录SHELL了而已,没能力登录系统.

添加用户命令

/usr/sbin/adduser -d /opt/test_ftp -g ftp -s /sbin/nologin test2

上面的命令是添加一个 名称为 test2的用户。

命令解析:使用命令(adduser)添加test2用户,不能登录系统(-s /sbin/nologin),自己的文件夹在(-d /opt/test_ftp)),属于组ftp(-g ftp).

为该用户设置密码

[root@localhost local]# passwd test2

现在重启服务我们就可以使用test2直接访问我们的 /opttest_ftp目录了
ftp1

给用户权限上传下载,修改

虽然用户能够进行查看,但是还没有权限上传、下载和修改

授权

[root@localhost local]# chmod 755 /opt/test_ftp

编辑配置文件

一般创建一个ftp 用户,作为管理员只希望它只能访问其自己的所属目录的,是不会让他选择其他目录的。

设置ftp用户的权限

在安装好ftp时,在 /etc/vsftpd目录下可以看到vsftpd的配置文件vsftpd.conf

[root@localhost vsftpd]# vim vsftpd.conf       
# Example config file /etc/vsftpd/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.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
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.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
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/xferlog
#
# 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 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=ftpsecure
#
# 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 on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
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().
# (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
# vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法
allow_writeable_chroot=YES 
#
# 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
#
  When "listen" directive is enabled, vsftpd runs in standalone mode and
▽ listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
# listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
anon_other_write_enable=YES
#anon_root=/opt/test_ftp
#guest_enable=YES
#guest_username=test2
virtual_use_local_privs=YES

用户访问权限及路径限制:

如果 userlist_deny=NO:只允许userlist_file文件中的用户可访问ftp;如果 userlist_deny=YESuserlist_file文件中列举的用户不能通过ftp访问系统。userlist_enable是该功能的开关。我们的系统配置如下:

userlist_enable=YES
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list

把需要登录的用户加入到/etc/vsftpd/user_list文件中,一个用户一行。
如果我们不需要把登陆的用户限制在主目录下需要一下配置:
进入主配置文件中chroot_list_enable=YES
创建用户文件/etc/vsftpd/chroot_list

vim /etc/vsftpd/chroot_list

将不需要限制的用户添加进去,我们只有一个test2,一行一个用户名
ps: 这些用户将能访问全部有权限的文件。

设置权限

[root@localhost vsftpd]# getsebool -a|grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
  pd_connect_db --> off
▽tpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off
开启 anon_write 和full_access
[root@localhost vsftpd]# setsebool allow_ftpd_anon_write  1
[root@localhost vsftpd]# setsebool allow_ftpd_full_access 1

重启ftp服务

[root@localhost vsftpd]# service vsftpd restart 

测试

ftp2

注:测试连接软件是FileZilla,windows访问 ftp://[ip]

update 20170901 使用FileZilla创建中文文件夹Linux乱码

首先出现这个问题的原因是WINDOWS采用的是GBK编码方式,而Linux采用的是UTF-8
首先设置好FileZilla的编码方式为zh_CN
在将 /etc/locale.conf 中的LANG修改为"zh_CN.UTF-8"

[root@localhost ~]# vim /etc/locale.conf 
LANG="zh_CN.UTF-8"

这个修改可以有效解决windows客户端使用ftp协议上传中文文件到Linux乱码的问题。
其实最根本的解决方法是客户端上传的文件经过ftp软件能与服务器统一编码方式,具体百度吧c#、java的都有。
博客地址

推荐阅读