vsftp ——- ftp服务

被动模式 生产配置 锁定家目录

禁止是否 sftp协议 匿名登录

  1. [root@prod-nfs-server01 ~]# cat /etc/passwd|grep ewftp
  2. ewftp:x:1101:1101::/data/lttsdata:/sbin/nologin
  3. [root@prod-nfs-server01 ~]#
  1. [root@prod-nfs-server01 ~]#
  2. [root@prod-nfs-server01 ~]# grep ^[^#] /etc/vsftpd/vsftpd.conf
  3. anonymous_enable=NO
  4. local_enable=YES
  5. write_enable=YES
  6. local_umask=022
  7. dirmessage_enable=YES
  8. xferlog_enable=YES
  9. connect_from_port_20=YES
  10. xferlog_std_format=YES
  11. chroot_local_user=YES
  12. listen=NO
  13. listen_ipv6=YES
  14. pam_service_name=vsftpd
  15. userlist_enable=YES
  16. tcp_wrappers=YES
  17. allow_writeable_chroot=YES
  18. pasv_enable=YES
  19. pasv_min_port=1025
  20. pasv_max_port=1035
  21. [root@prod-nfs-server01 ~]#

防火墙设置

  1. firewall-cmd --zone=public --add-port=21/tcp --permanent
  2. firewall-cmd --zone=public --add-port=1025-1035/tcp --permanent
  3. firewall-cmd --reload

安装ftp服务器端:

  1. [root@riyimei ~]# yum install -y vsftpd
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.aliyun.com
  5. * extras: mirrors.aliyun.com
  6. * updates: mirrors.aliyun.com
  7. base | 3.6 kB 00:00:00
  8. epel | 4.7 kB 00:00:00
  9. extras | 2.9 kB 00:00:00
  10. updates | 2.9 kB 00:00:00
  11. (1/7): epel/x86_64/group_gz | 95 kB 00:00:00
  12. (2/7): base/7/x86_64/group_gz | 153 kB 00:00:00
  13. (3/7): extras/7/x86_64/primary_db | 205 kB 00:00:00
  14. (4/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00
  15. (5/7): updates/7/x86_64/primary_db | 3.0 MB 00:00:01
  16. (6/7): base/7/x86_64/primary_db | 6.1 MB 00:00:01
  17. (7/7): epel/x86_64/primary_db | 6.9 MB 00:00:01
  18. Resolving Dependencies
  19. --> Running transaction check
  20. ---> Package vsftpd.x86_64 0:3.0.2-27.el7 will be installed
  21. --> Finished Dependency Resolution
  22. Dependencies Resolved
  23. =======================================================================================================================================
  24. Package Arch Version Repository Size
  25. =======================================================================================================================================
  26. Installing:
  27. vsftpd x86_64 3.0.2-27.el7 base 172 k
  28. Transaction Summary
  29. =======================================================================================================================================
  30. Install 1 Package
  31. Total download size: 172 k
  32. Installed size: 353 k
  33. Downloading packages:
  34. vsftpd-3.0.2-27.el7.x86_64.rpm | 172 kB 00:00:00
  35. Running transaction check
  36. Running transaction test
  37. Transaction test succeeded
  38. Running transaction
  39. Installing : vsftpd-3.0.2-27.el7.x86_64 1/1
  40. Verifying : vsftpd-3.0.2-27.el7.x86_64 1/1
  41. Installed:
  42. vsftpd.x86_64 0:3.0.2-27.el7
  43. Complete!
  44. [root@riyimei ~]#

创建虚拟用户

useradd -s /sbin/nologin virftp

查看配置文件位置

  1. [root@riyimei ~]# rpm -qa vsftpd
  2. vsftpd-3.0.2-27.el7.x86_64
  3. [root@riyimei ~]# rpm -ql vsftpd
  4. /etc/logrotate.d/vsftpd
  5. /etc/pam.d/vsftpd
  6. /etc/vsftpd
  7. /etc/vsftpd/ftpusers
  8. /etc/vsftpd/user_list
  9. /etc/vsftpd/vsftpd.conf
  10. /etc/vsftpd/vsftpd_conf_migrate.sh
  11. /usr/lib/systemd/system-generators/vsftpd-generator
  12. /usr/lib/systemd/system/vsftpd.service
  13. /usr/lib/systemd/system/vsftpd.target
  14. /usr/lib/systemd/system/vsftpd@.service
  15. /usr/sbin/vsftpd
  16. /usr/share/doc/vsftpd-3.0.2
  17. /usr/share/doc/vsftpd-3.0.2/AUDIT
  18. /usr/share/doc/vsftpd-3.0.2/BENCHMARKS
  19. /usr/share/doc/vsftpd-3.0.2/BUGS
  20. /usr/share/doc/vsftpd-3.0.2/COPYING
  21. /usr/share/doc/vsftpd-3.0.2/Changelog
  22. /usr/share/doc/vsftpd-3.0.2/EXAMPLE
  23. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE
  24. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README
  25. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README.configuration
  26. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf
  27. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd
  28. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD
  29. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README
  30. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration
  31. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf
  32. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG
  33. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README
  34. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README.configuration
  35. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/hosts.allow
  36. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/README
  37. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS
  38. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS/README
  39. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS
  40. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README
  41. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README.configuration
  42. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/logins.txt
  43. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf
  44. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam
  45. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2
  46. /usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2/README
  47. /usr/share/doc/vsftpd-3.0.2/FAQ
  48. /usr/share/doc/vsftpd-3.0.2/INSTALL
  49. /usr/share/doc/vsftpd-3.0.2/LICENSE
  50. /usr/share/doc/vsftpd-3.0.2/README
  51. /usr/share/doc/vsftpd-3.0.2/README.security
  52. /usr/share/doc/vsftpd-3.0.2/REWARD
  53. /usr/share/doc/vsftpd-3.0.2/SECURITY
  54. /usr/share/doc/vsftpd-3.0.2/SECURITY/DESIGN
  55. /usr/share/doc/vsftpd-3.0.2/SECURITY/IMPLEMENTATION
  56. /usr/share/doc/vsftpd-3.0.2/SECURITY/OVERVIEW
  57. /usr/share/doc/vsftpd-3.0.2/SECURITY/TRUST
  58. /usr/share/doc/vsftpd-3.0.2/SIZE
  59. /usr/share/doc/vsftpd-3.0.2/SPEED
  60. /usr/share/doc/vsftpd-3.0.2/TODO
  61. /usr/share/doc/vsftpd-3.0.2/TUNING
  62. /usr/share/doc/vsftpd-3.0.2/vsftpd.xinetd
  63. /usr/share/man/man5/vsftpd.conf.5.gz
  64. /usr/share/man/man8/vsftpd.8.gz
  65. /var/ftp
  66. /var/ftp/pub
  67. [root@riyimei ~]#

编辑虚拟用户密码文件并授权
vim /etc/vsftpd/vsftpd_login
chmod 600 /etc/vsftpd/vsftpd_login

  1. [root@riyimei ~]# cat /etc/vsftpd/vsftpd_login
  2. liwm
  3. AAbb0101
  4. riyimei
  5. !Q2w3e4r
  6. [root@riyimei ~]#

把虚拟用户的密码文件转为为数据库文件

db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db

  1. [root@riyimei ~]# db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db
  2. [root@riyimei ~]# ll -l /etc/vsftpd/
  3. total 36
  4. -rw------- 1 root root 125 Apr 1 12:55 ftpusers
  5. -rw------- 1 root root 361 Apr 1 12:55 user_list
  6. -rw------- 1 root root 5116 Apr 1 12:55 vsftpd.conf
  7. -rwxr--r-- 1 root root 338 Apr 1 12:55 vsftpd_conf_migrate.sh
  8. -rw------- 1 root root 31 Jul 26 22:18 vsftpd_login
  9. -rw-r--r-- 1 root root 12288 Jul 26 22:19 vsftpd_login.db
  10. [root@riyimei ~]#

创建虚拟用户的目录和配置文件

mkdir /etc/vsftpd/vsftpd_user_conf

  1. [root@riyimei ~]# mkdir /etc/vsftpd/vsftpd_user_conf
  2. [root@riyimei ~]# cd /etc/vsftpd/vsftpd_user_conf
  3. [root@riyimei vsftpd_user_conf]# vim liwm
  4. [root@riyimei vsftpd_user_conf]# cat liwm
  5. local_root=/home/virftp/liwm //为虚拟用户testuser1的家目录(读取文件所在处)
  6. anonymous_enable=NO // 是否允许匿名用户
  7. write_enable=YES //是否可行
  8. local_umask=022 //umask的值
  9. anon_upload_enable=NO //是否允许匿名用户上传文件
  10. anon_mkdir_write_enable=NO //是否允许匿名用户可写、创建用户
  11. idle_session_timeout=600 //连接空闲超时时间,超时就重新登录
  12. data_connection_timeout=120 //传输文件超时时间
  13. max_clients=10 //最大客户端数
  14. [root@riyimei vsftpd_user_conf]#

创建虚拟用户家目录并授权

  1. [root@riyimei vsftpd_user_conf]# mkdir /home/virftp/liwm
  2. [root@riyimei vsftpd_user_conf]# chown -R virftp:virftp /home/virftp/
  3. [root@riyimei vsftpd_user_conf]#

修改认证配置文件

  1. auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
  2. account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
  1. [root@riyimei vsftpd_user_conf]# vim /etc/pam.d/vsftpd
  2. [root@riyimei vsftpd_user_conf]# cat /etc/pam.d/vsftpd
  3. #%PAM-1.0
  4. auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
  5. account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
  6. session optional pam_keyinit.so force revoke
  7. auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
  8. auth required pam_shells.so
  9. auth include password-auth
  10. account include password-auth
  11. session required pam_loginuid.so
  12. session include password-auth
  13. [root@riyimei vsftpd_user_conf]#

修改全局配置文件

/etc/vsftpd/vsftpd.conf

  1. [root@riyimei vsftpd_user_conf]# vim /etc/vsftpd/vsftpd.conf
  2. [root@riyimei vsftpd_user_conf]# grep ^[^#] /etc/vsftpd/vsftpd.conf
  3. anonymous_enable=NO
  4. local_enable=YES
  5. write_enable=YES
  6. local_umask=022
  7. anon_upload_enable=NO
  8. anon_mkdir_write_enable=NO
  9. dirmessage_enable=YES
  10. xferlog_enable=YES
  11. connect_from_port_20=YES
  12. xferlog_std_format=YES
  13. listen=NO
  14. listen_ipv6=YES
  15. pam_service_name=vsftpd
  16. userlist_enable=YES
  17. tcp_wrappers=YES
  18. chroot_local_user=YES
  19. guest_enable=YES //是否允许虚拟用户映射
  20. guest_username=virftp //这个就是虚拟用户映射到的系统用户virftp
  21. virtual_use_local_privs=YES //
  22. user_config_dir=/etc/vsftpd/vsftpd_user_conf //创建的虚拟用户的目录
  23. allow_writeable_chroot=YES //
  24. [root@riyimei vsftpd_user_conf]#
  1. anonymous_enable=NO
  2. local_enable=YES
  3. write_enable=YES
  4. local_umask=022
  5. anon_upload_enable=NO
  6. anon_mkdir_write_enable=NO
  7. dirmessage_enable=YES
  8. xferlog_enable=YES
  9. connect_from_port_20=YES
  10. xferlog_std_format=YES
  11. listen=NO
  12. listen_ipv6=YES
  13. pam_service_name=vsftpd
  14. userlist_enable=YES
  15. tcp_wrappers=YES
  16. chroot_local_user=YES
  17. guest_enable=YES
  18. guest_username=virftp
  19. virtual_use_local_privs=YES
  20. user_config_dir=/etc/vsftpd/vsftpd_user_conf
  21. allow_writeable_chroot=YES

启动服务

  1. [root@riyimei vsftpd_user_conf]# vim /etc/vsftpd/vsftpd.conf
  2. [root@riyimei vsftpd_user_conf]# systemctl restart vsftpd
  3. [root@riyimei vsftpd_user_conf]# systemctl status vsftpd
  4. vsftpd.service - Vsftpd ftp daemon
  5. Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
  6. Active: active (running) since Sun 2020-07-26 22:42:36 CST; 1min 2s ago
  7. Process: 2683 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
  8. Main PID: 2684 (vsftpd)
  9. CGroup: /system.slice/vsftpd.service
  10. └─2684 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
  11. Jul 26 22:42:36 riyimei systemd[1]: Starting Vsftpd ftp daemon...
  12. Jul 26 22:42:36 riyimei systemd[1]: Started Vsftpd ftp daemon.
  13. [root@riyimei vsftpd_user_conf]#
  14. [root@riyimei vsftpd_user_conf]# netstat -lntup
  15. Active Internet connections (only servers)
  16. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  17. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1061/sshd
  18. tcp6 0 0 :::33060 :::* LISTEN 1091/mysqld
  19. tcp6 0 0 :::3306 :::* LISTEN 1091/mysqld
  20. tcp6 0 0 :::21 :::* LISTEN 2684/vsftpd
  21. tcp6 0 0 :::22 :::* LISTEN 1061/sshd
  22. [root@riyimei vsftpd_user_conf]#

安装客户端
yum install -y lftp

  1. [root@riyimei ~]# yum install -y lftp
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.aliyun.com
  5. * extras: mirrors.aliyun.com
  6. * updates: mirrors.aliyun.com
  7. Resolving Dependencies
  8. --> Running transaction check
  9. ---> Package lftp.x86_64 0:4.4.8-12.el7_8.1 will be installed
  10. --> Processing Dependency: libgnutls.so.28(GNUTLS_1_4)(64bit) for package: lftp-4.4.8-12.el7_8.1.x86_64
  11. --> Processing Dependency: libgnutls.so.28()(64bit) for package: lftp-4.4.8-12.el7_8.1.x86_64
  12. --> Running transaction check
  13. ---> Package gnutls.x86_64 0:3.3.29-9.el7_6 will be installed
  14. --> Processing Dependency: trousers >= 0.3.11.2 for package: gnutls-3.3.29-9.el7_6.x86_64
  15. --> Processing Dependency: libnettle.so.4()(64bit) for package: gnutls-3.3.29-9.el7_6.x86_64
  16. --> Processing Dependency: libhogweed.so.2()(64bit) for package: gnutls-3.3.29-9.el7_6.x86_64
  17. --> Running transaction check
  18. ---> Package nettle.x86_64 0:2.7.1-8.el7 will be installed
  19. ---> Package trousers.x86_64 0:0.3.14-2.el7 will be installed
  20. --> Finished Dependency Resolution
  21. Dependencies Resolved
  22. =======================================================================================================================================
  23. Package Arch Version Repository Size
  24. =======================================================================================================================================
  25. Installing:
  26. lftp x86_64 4.4.8-12.el7_8.1 updates 752 k
  27. Installing for dependencies:
  28. gnutls x86_64 3.3.29-9.el7_6 base 680 k
  29. nettle x86_64 2.7.1-8.el7 base 327 k
  30. trousers x86_64 0.3.14-2.el7 base 289 k
  31. Transaction Summary
  32. =======================================================================================================================================
  33. Install 1 Package (+3 Dependent packages)
  34. Total download size: 2.0 M
  35. Installed size: 5.9 M
  36. Downloading packages:
  37. (1/4): gnutls-3.3.29-9.el7_6.x86_64.rpm | 680 kB 00:00:00
  38. (2/4): nettle-2.7.1-8.el7.x86_64.rpm | 327 kB 00:00:00
  39. (3/4): trousers-0.3.14-2.el7.x86_64.rpm | 289 kB 00:00:00
  40. (4/4): lftp-4.4.8-12.el7_8.1.x86_64.rpm | 752 kB 00:00:00
  41. ---------------------------------------------------------------------------------------------------------------------------------------
  42. Total 3.4 MB/s | 2.0 MB 00:00:00
  43. Running transaction check
  44. Running transaction test
  45. Transaction test succeeded
  46. Running transaction
  47. Installing : nettle-2.7.1-8.el7.x86_64 1/4
  48. Installing : trousers-0.3.14-2.el7.x86_64 2/4
  49. Installing : gnutls-3.3.29-9.el7_6.x86_64 3/4
  50. Installing : lftp-4.4.8-12.el7_8.1.x86_64 4/4
  51. Verifying : trousers-0.3.14-2.el7.x86_64 1/4
  52. Verifying : lftp-4.4.8-12.el7_8.1.x86_64 2/4
  53. Verifying : gnutls-3.3.29-9.el7_6.x86_64 3/4
  54. Verifying : nettle-2.7.1-8.el7.x86_64 4/4
  55. Installed:
  56. lftp.x86_64 0:4.4.8-12.el7_8.1
  57. Dependency Installed:
  58. gnutls.x86_64 0:3.3.29-9.el7_6 nettle.x86_64 0:2.7.1-8.el7 trousers.x86_64 0:0.3.14-2.el7
  59. Complete!
  60. [root@riyimei ~]#

客户端命令

  1. lftp liwm@127.0.0.1:/> help
  2. !<shell-command> (commands) alias [<name> [<value>]] attach [PID]
  3. bookmark [SUBCMD] cache [SUBCMD] cat [-b] <files> cd <rdir>
  4. chmod [OPTS] mode file... close [-a] [re]cls [opts] [path/][pattern]
  5. debug [<level>|off] [-o <file>] du [options] <dirs> exit [<code>|bg]
  6. get [OPTS] <rfile> [-o <lfile>] glob [OPTS] <cmd> <args> help [<cmd>]
  7. history -w file|-r file|-c|-l [cnt] jobs [-v] [<job_no...>] kill all|<job_no> lcd <ldir>
  8. lftp [OPTS] <site> ln [-s] <file1> <file2> ls [<args>] mget [OPTS] <files>
  9. mirror [OPTS] [remote [local]] mkdir [-p] <dirs> module name [args] more <files>
  10. mput [OPTS] <files> mrm <files> mv <file1> <file2> [re]nlist [<args>]
  11. open [OPTS] <site> pget [OPTS] <rfile> [-o <lfile>] put [OPTS] <lfile> [-o <rfile>] pwd [-p]
  12. queue [OPTS] [<cmd>] quote <cmd> repeat [OPTS] [delay] [command]
  13. rm [-r] [-f] <files> rmdir [-f] <dirs> scache [<session_no>]
  14. set [OPT] [<var> [<val>]] site <site-cmd> source <file>
  15. torrent [-O <dir>] <file|URL>... user <user|URL> [<pass>] wait [<jobno>] zcat <files>
  16. zmore <files>
  17. lftp liwm@127.0.0.1:/>
  1. [root@FTPSERVER ~]# grep ^[^#] /etc/vsftpd/vsftpd.conf
  2. anonymous_enable=YES
  3. local_enable=YES
  4. write_enable=YES
  5. local_umask=000
  6. dirmessage_enable=YES
  7. xferlog_enable=YES
  8. connect_from_port_20=YES
  9. xferlog_std_format=YES
  10. chroot_local_user=YES
  11. chroot_list_enable=YES
  12. listen=YES
  13. pam_service_name=vsftpd
  14. userlist_enable=YES
  15. tcp_wrappers=YES