FTP可以说是Internet上使用非常广泛的一种通讯协议了。它工作在OSI模型的第7层,是TCP/IP的一种具体应用。FTP采用基于TCP的可靠连接:监听21端口来等待控制连接请求,当连接建立后,采用20号端口来建立数据传输通道。

  FTP中使用的一些典型消息:

    125  数据连接打开,传输开始

    200  命令OK

    331  用户名OK,需要输入密码

    425  不能打开数据连接

    452  错误写文件

    500  命令无法识别

  Vsftp是Linux系统下的一套开源FTP服务器软件,具有结构简单、性能优良的特点,是一款轻量型稳定安全的FTP。

  1.安装Vsftp。(测试环境:ContOS 6.5)

  查看本机有没有vsftp安装源:

  1. ~# rpm -qa | grep svftpd

  如果有,则会显示vsftp的版本,这样就可以直接安装:

  1. ~# yum install vsftpd

  但我的上面貌似没有,只能下载源码编译了。Vsftp官网主页:http://vsftpd.beasts.org ,貌似进不去,网上找了下,可以在这里下载:站长之家 。版本是Vsftp v2.3.2 For Linux。解压编译:

  1. ~# tar xvf vsftpd-2.3.2.tar.gz
  2. ~# cd ./vsftpd-2.3.2

  在此说明一下vsftpd-2.3.2目录下的builddefs.h文件,该文件主要用来设定FTP服务器的一些安全配置:CTP_Wrappers、PAM和SSL。可以按需要选择/取消。  

  1. vim ./builddefs.h
  2. #ifndef VSF_BUILDDEFS_H
  3. #define VSF_BUILDDEFS_H
  4.  
  5. #undef VSF_BUILD_TCPWRAPPERS //对TCP包进行解析的工具,用于限制某种服务的访问权限进而达到保护系统的目的。
  6. #define VSF_BUILD_PAM //一种高效便利的用户级别的认证方式,用来加强服务器的安全性能。
  7. #undef VSF_BUILD_SSL //利用加密技术,保障在传输过程中数据不会被窃听。
  8.  
  9. #endif /* VSF_BUILDDEFS_H */

  2.配置vsftpd.conf文件

  vsftpd.conf文件是主配置文件,在/etc/vsftpd/目录下.

  1. # Allow anonymous FTP? (Beware - allowed by default if you comment this out).
  2. anonymous_enable=YES //设置是否允许匿名访问
  3. #
  4. # Uncomment this to allow local users to log in.
  5. local_enable=YES //设置是否允许本地用户登录
  6. #
  7. # Uncomment this to enable any form of FTP write command.
  8. write_enable=YES //设置是否允许写操作
  9. #
  10. # Default umask for local users is 077. You may wish to change this to 022,
  11. # if your users expect that (022 is used by most other ftpd's)
  12. local_umask=022 //本地用户操作权限
  13. #
  14. # Uncomment this to allow the anonymous FTP user to upload files. This only
  15. # has an effect if the above global write enable is activated. Also, you will
  16. # obviously need to create a directory writable by the FTP user.
  17. #anon_upload_enable=YES //设置是否允许匿名上传文件
  18. #
  19. # Uncomment this if you want the anonymous FTP user to be able to create
  20. # new directories.
  21. #anon_mkdir_write_enable=YES //设置是否允许匿名建立目录
  22. #
  23. # Activate directory messages - messages given to remote users when they
  24. # go into a certain directory.
  25. dirmessage_enable=YES //设置是否在改变目录后发送消息
  26. #
  27. # The target log file can be vsftpd_log_file or xferlog_file.
  28. # This depends on setting xferlog_std_format parameter
  29. xferlog_enable=YES //设置是否激活日志功能
  30. #
  31. # Make sure PORT transfer connections originate from port 20 (ftp-data).
  32. connect_from_port_20=YES //设置是否使用20端口传输数据(PORT模式)
  33. #
  34. # If you want, you can arrange for uploaded anonymous files to be owned by
  35. # a different user. Note! Using "root" for uploaded files is not
  36. # recommended!
  37. //修改匿名用户上传文件的所有者
  38. #chown_uploads=YES
  39. #chown_username=whoever
  40. #
  41. # The name of log file when xferlog_enable=YES and xferlog_std_format=YES
  42. # WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log
  43. #xferlog_file=/var/log/xferlog //设置日志文件保存位置
  44. #
  45. # Switches between logging into vsftpd_log_file and xferlog_file files.
  46. # NO writes to vsftpd_log_file, YES to xferlog_file
  47. xferlog_std_format=YES //设置是否使用标准文件日志
  48. #
  49. # You may change the default value for timing out an idle session.
  50. #idle_session_timeout=600 //设置会话的超时时间
  51. #
  52. # You may change the default value for timing out a data connection.
  53. #data_connection_timeout=120 //设置数据传输的超时时间
  54. #
  55. # It is recommended that you define on your system a unique user which the
  56. # ftp server can use as a totally isolated and unprivileged user.
  57. #nopriv_user=ftpsecure //设置非特权用户账户
  58. #
  59. # Enable this and the server will recognise asynchronous ABOR requests. Not
  60. # recommended for security (the code is non-trivial). Not enabling it,
  61. # however, may confuse older FTP clients.
  62. #async_abor_enable=YES //设置是否允许客户端使用sync等命令
  63. #
  64. # By default the server will pretend to allow ASCII mode but in fact ignore
  65. # the request. Turn on the below options to have the server actually do ASCII
  66. # mangling on files when in ASCII mode.
  67. # Beware that on some FTP servers, ASCII support allows a denial of service
  68. # attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
  69. # predicted this attack and has always been safe, reporting the size of the
  70. # raw file.
  71. //设置是否以ASCII形式传输文件
  72. # ASCII mangling is a horrible feature of the protocol.
  73. #ascii_upload_enable=YES
  74. #ascii_download_enable=YES
  75. #
  76. # You may fully customise the login banner string:
  77. #ftpd_banner=Welcome to blah FTP service. //设置登录后的欢迎信息
  78. #
  79. # You may specify a file of disallowed anonymous e-mail addresses. Apparently
  80. # useful for combatting certain DoS attacks.
  81. #deny_email_enable=YES //设置是否启用禁止指定匿名用户登录
  82. # (default follows)
  83. #banned_email_file=/etc/vsftpd/banned_emails //加入用户列表(前提是上面已经设置为YES)
  84. #
  85. # You may specify an explicit list of local users to chroot() to their home
  86. # directory. If chroot_local_user is YES, then this list becomes a list of
  87. # users to NOT chroot().
  88. #chroot_local_user=YES //与下面配置有关
  89. #chroot_list_enable=YES //设置是否允许本地用户离开其主目录
  90. # (default follows)
  91. #
  92. //如果使用该项,则上面的chroot_local_user=YES需设置为NO。指定不能离开主目录的用户,将用户名一个一行写入指定文件。
  93. #chroot_list_file=/etc/vsftpd/chroot_list
  94. #
  95. # You may activate the "-R" option to the builtin ls. This is disabled by
  96. # default to avoid remote users being able to cause excessive I/O on large
  97. # sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
  98. # the presence of the "-R" option, so there is a strong case for enabling it.
  99. #ls_recurse_enable=YES //
  100. #
  101. # When "listen" directive is enabled, vsftpd runs in standalone mode and
  102. # listens on IPv4 sockets. This directive cannot be used in conjunction
  103. # with the listen_ipv6 directive.
  104. listen=YES //设置是否开启IPV4监听
  105. #
  106. # This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
  107. # sockets, you must run two copies of vsftpd with two configuration files.
  108. # Make sure, that one of the listen options is commented !!
  109. #listen_ipv6=YES //设置是否开启IPV6监听
  110.  
  111. pam_service_name=vsftpd //设置访问所使用的PAM模块
  112. userlist_enable=YES //如果激活该选项,将禁止所指定文件中的用户登录
  113. tcp_wrappers=YES //设置是否使用TCP_Wrappers作为主机访问控制方式

3.配置ftpusers文件

ftpusers文件用于限定系统中那些用户能/不能使用FTP服务。可以根据实际情况添加/删除。

  1. #User that are not allowed to login via ftp
  2. root
  3. ...
  4. ...
  5. ...

4.配置user_list文件

 user_list文件指定的用户能否访问FTP服务器取决于userlist_deny选项的设置。默认为YES,即禁止user_list文件的用户访问FTP服务器,这与ftpusers文件相似。但是如果设置为NO,则完全相反,即只允许该文件列表里的用户访问该FTP服务器。

  1. # vsftpd userlist
  2. # If userlist_deny=NO,only allow users in this file
  3. # If userlist_deny=YES(default),never allow users in this file,and do not even prompt for a password.
  4. # ...
  5. # ...
  6. root
  7. ...

如果要限制指定的本地用户(即user_list文件的用户)不能访问FTP服务器,可以相应地修改vsftpd.conf文件:

  1. userlist_enable=YES
  2. userlist_deny=YES
  3. userlist_file=/etc/vsftpd/user_list

同样,如果要限制指定的本地用户(即user_list文件的用户)能够访问FTP服务器,而其他的本地用户不能访问,可以相应地修改vsftpd.conf文件:

  1. userlist_enable=YES
  2. userlist_deny=NO
  3. userlist_file=/etc/vsftpd/user_list

5.配置允许匿名用户使用FTP服务器

创建用户ftp-anon和目录/var/ftp-pub:

  1. mkdir /var/ftp-pub
  2. useradd -d /var/ftp-pub ftp-anon //-d 指定用户的主文件目录,vsftpd默认登录成功后的目录是该用户的home目录。

作为匿名访问,/var/ftp-pub不应该属于用户ftp-anon,也不应该有写权限,所以可以用以下方法修改其权限:

  1. ~# chown root.root /var/ftp-pub
  2. ~# og-w -d /var/ftp-pub

修改vsftpd.conf文件:

  1. anonymous_enable=YES //设置允许匿名访问(默认开启)
  2. //设置允许匿名上传和创建目录(非必须,慎用!)
  3. anon_upload_enable=YES
  4. anon_mkdir_write_enable=YES

6.虚拟用户使用Vsftp服务器

上面的(包括一般的)ftp访问都是通过建立系统帐号来访问服务器的,这样很不安全,如果权限配置错误将会使服务器受到威胁。但是,通过建立虚拟FTP账户(与系统帐号分离),就可以大大增强系统的安全性。虚拟FTP账户只能用于文件传输,也叫guest用户。它是把用户名/密码保存起来,再验证的。所以Vsftp需要一个系统用户的身份来读取数据(用户名/密码)文件,即guest用户,用以映射虚拟用户。

具体配置如下:

(1)生成虚拟用户口令库文件。以下为例:

  1. ~# vim login.txt
  2. zhangsan //username1
  3. hehe //passwd1
  4. lisi //username2
  5. mimanicai //passwd2
  6. ......

(2)配置生成Vsftp的认证文件

保存并退出,使用db_load命令生成口令库文件:

  1. ~# db_load -T -t hash -f login.txt /etc/vsftpd/vsftpd_login.db

修改口令库文件的权限:

  1. chmod 600 /etc/vsftpd/vsftpd_login.db

编辑虚拟用户所需的PAM配置文件

  1. ~# vim /etc/pam.d/vsftpd
  2. //加入以下两行:
  3. auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login.db
  4. account required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login.db

(3)建立虚拟用户访问的目录,并设置相应的访问权限

  1. ~# useradd -d /home/ftp virtual
  2. ~# chmod 700 /home/ftp

(4)建立配置文件

  1. ~# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
  2. ~# vim /etc/vsftpd/vsftpd.conf
  3. //配置如下:
  4. anonymous_enable=YES
  5. local_enable=YES
  6. write_enable=YES
  7. anon_upload_enable=NO
  8. anon_mkdir_write_enable=NO
  9. listen=YES
  10. guest_enable=YES
  11. guest_username=virtual

(5)重启Vsftp服务器

  1. ~# service vsftpd restart

至此,虚拟用户使用Vsftp服务器配置将完成了。可以使用login.txt里的用户帐号登录FTP服务器了。

7.改变Vsftp的端口号

在vsftpd.conf文件里加入如下,并重启vsftp。

  1. ~# listen_port=2121

8.配置Vstfp服务器的chroot

chroot就是为登录用户指定一个固定的目录,这个目录一般是用户的主目录,用户被限制在该目录下,类似与WEB服务器的虚拟目录,从而保护了系统安全。

设置指定的用户执行chroot:

  1. chroot_local_user=NO
  2. chroot_list_enable=YES
  3. chroot_list_file=/etc/vsftpd/chroot_list

相关参考文章: http://wiki.ubuntu.org.cn/Vsftpd

FTP服务器的搭建与安全配置的更多相关文章

  1. ubuntu 14.04 下FTP服务器的搭建--锁定用户目录,解决vsftpd: refusing to run with writable root inside chroot()

    FTP服务器的搭建,我要实现的需求是: 不允许匿名访问,因为我的机器不想让谁都能登录上来,随便获取文件, 需要锁定一个目录,因为在家里,我需要给媳妇下载一些电影 韩剧之类的东西,媳妇会来我机器下载,但 ...

  2. Ubuntu 14.04 下FTP服务器的搭建

    FTP服务器的搭建,我要实现的需求是: 不允许匿名访问,因为我的机器不想让谁都能登录上来,随便获取文件, 需要锁定一个目录,因为在家里,我需要给媳妇下载一些电影 韩剧之类的东西,媳妇会来我机器下载,但 ...

  3. Linux:FTP服务器的搭建

    FTP服务器的简介 系统用户 即系统本机的用户.Linux一般不会针对实体用户进行限制,因此实体用户可以针对整个文件 系统进行工作.但通常不希望他们通过FTP方式远程访问系统. 虚拟用户 只能采用FT ...

  4. 自学linux——18.FTP服务器的搭建

    Centos7下FTP服务器的搭建 一.FTP的作用 文件传输协议(File Transfer Protocol,FTP),是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用 20. ...

  5. 【FTP】FTP服务器的搭建

    记录一下FTP服务器的搭建首先打开 程序和功能>打开或关闭Windows功能 进入到Windows功能界面:勾选FTP服务器.然后再在IIS界面,新建一个网站.右键网站,选择“添加到FTP发布” ...

  6. FTP服务器的搭建与配置

    主要来源:http://www.cnblogs.com/helonghl/articles/5533857.html 1.安装FTP服务器: yum install vsftpd -y 2.启动FTP ...

  7. Ubuntu14.04下FTP服务器的搭建配置 标签: ubuntuftp服务器虚拟机 2017-06-13 15:24 161人阅读 评

    首先说明一下,我是在虚拟机中装的Ubuntu14.04,物理机是Win10,最初只是为了在两个系统间传输文件才在Ubuntu中安装了ftp服务器,从Windows端登陆其即可.最初也是按照网上的各种教 ...

  8. Linux篇---ftp服务器的搭建

    一.前述 企业中linux搭建ftp服务器还是很实用的,所以本文针对centoos7和centoos6搭建服务器教程做个总结. 二.具体 1.显示如下图则表示已安装 vsftp软件.如果未显示则需要安 ...

  9. Linux中FTP服务器的搭建

    vmware12上安装的CentOs6.8,最终实现物理机上的windows10能正常访问CentOs上的FTP服务器. 一.查看是否安装有ftp相关的安装包. # rpm -qa | grep vs ...

随机推荐

  1. [C++ Primer] 第6章: 函数

    参数传递 const形参和实参: 顶层const作用于对象本身, 和其他初始化过程一样, 当用实参初始化形参时会忽略掉顶层const, 换句话说, 形参顶层const被忽略掉了, 当形参有顶层cons ...

  2. springboot利用MockMvc测试controller控制器

    主要记录一下控制器的测试,service这些类测试相对简单些(可测试性强) API测试需求比较简单: ① 需要返回正确的http状态码 200 ② 需要返回json数据,并且不能返回未经捕获的系统异常 ...

  3. 在Linux中安装Oracle(较详细图解)

    原创 http://www.cnblogs.com/nucdy/p/5603998.html 参考视屏:链接: https://pan.baidu.com/s/1kViEZQZ  密码: z7ha ( ...

  4. 根据现有表操作基于active record的model

    指南上都是直接生成mode,然后db migrate来生成数据库,在现实场景中,很可能是反过来的 例如 测试表app_versions rails里面,建立model class AppVersion ...

  5. 简化Redis数据访问代码RedisTemplate

    ---恢复内容开始--- Redis数据结构简介: Redis可以存储键与5中数据结构类型之间的映射,这5中数据结构类型分别是;String(字符串),List(列表),Set(集合),Hash(散列 ...

  6. C++ 实例化对象 p->printX()

    一.从栈实例化对象 我们首先定义一个类,类的名字叫TV,里面包括两个成员变量,两个成员函数. class TV // 定义一个电视的类TV { public: ]; // 定义类的属性,一个数组 in ...

  7. 十、jdk工具之Jdb命令(The Java Debugger)

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  8. PyDev for eclipse 插件下载地址

    PyDev for eclipse 插件下载地址http://sourceforge.net/projects/pydev/files/pydev/python解释器以及python类库下载地址htt ...

  9. requests and BeautifulSoup

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  10. git/github基本命令

    Git与项目 git的使用,主要包括: 本地仓库的命令 远程仓库的命令 项目需求.页面.模型类的设计,及页面的使用 sudo apt-get install git 安装成功后,运行如下命令 git ...