FTP服务搭建与配置

FTP介绍

  • FTP是File Transfer Protocol(文件传输协议,简称文传协议)的英文简称,用于在Internet上控制文件的双向传输。

  • FTP的主要作用就是让用户连接一个远程计算机(这些计算机上运行着FTP服务器程序),并查看远程计算机中的文件,然后把文件从远程计算机复制到本地计算机,或把本地计算机的文件传送到远程计算机。

  • 小公司用的多,大企业不用FTP,因为不安全。

使用vsftpd搭建ftp服务

centos系统上自带vsftpd

安装vsftpd

  [root@zyshanlinux-001 ~]# yum install -y vsftpd

创建普通用户,目的为了让虚拟用户来映射

  [root@zyshanlinux-001 ~]# useradd -s /sbin/nologin virftp

虚拟用户的密码文件配置(可以自定义).

内容如下,奇数行为用户名,偶数行为密码,多个用户就写多行

  [root@zyshanlinux-001 ~]# vim /etc/vsftpd/vsftpd_login
  testuser1
  zyshanlinux

设置好密码,接着要配置密码文件的权限

  [root@zyshanlinux-001 ~]# chmod 600 /etc/vsftpd/vsftpd_login

接着需要把密码文件的文本文件转换为计算机识别的二进制文件

  [root@zyshanlinux-001 ~]# db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db
  [root@zyshanlinux-001 ~]# ls -l /etc/vsftpd
  total 36
  -rw-------. 1 root root   125 Aug  3  2017 ftpusers
  -rw-------. 1 root root   361 Aug  3  2017 user_list
  -rw-------. 1 root root  5030 Aug  3  2017 vsftpd.conf
  -rwxr--r--. 1 root root   338 Aug  3  2017 vsftpd_conf_migrate.sh
  -rw-------  1 root root    22 Jul 16 09:25 vsftpd_login
  -rw-r--r--  1 root root 12288 Jul 16 09:29 vsftpd_login.db  ##生成的二进制文件,db结尾

创建虚拟用户的配置文件所在的目录(自定义)

  [root@zyshanlinux-001 ~]# mkdir /etc/vsftpd/vsftpd_user_conf

跳转到该目录

  [root@zyshanlinux-001 ~]# cd !$
  cd /etc/vsftpd/vsftpd_user_conf

创建虚拟用户的配置文件,创建的文件名字需要和用户名一致。

  [root@zyshanlinux-001 vsftpd_user_conf]# vim testuser1

配置内容

  local_root=/home/virftp/testuser1  ##虚拟用户的家目录
  anonymous_enable=NO  ##是否允许匿名用户,不
  write_enable=YES  ##是否允许可写,是
  local_umask=022  ##定义新建的文件或目录的权限是什么
  anon_upload_enable=NO  ##是否允许匿名用户可上传,不
  anon_mkdir_write_enable=NO  ##是否允许匿名用户创建目录并且可写,不
  idle_session_timeout=600  ##连接超时600秒
  data_connection_timeout=120  ##数据传输超时时间120秒
  max_clients=10  ##最大的客户端数目10

创建虚拟用户的家目录,新建测试文件,对虚拟用户权限做修改

  [root@zyshanlinux-001 vsftpd_user_conf]# mkdir /home/virftp/testuser1
  [root@zyshanlinux-001 vsftpd_user_conf]# touch /home/virftp/testuser1/zyshan.txt
  [root@zyshanlinux-001 vsftpd_user_conf]# chown -R virftp:virftp /home/virftp

重要:

配置认证文件

  [root@zyshanlinux-001 vsftpd_user_conf]# vim /etc/pam.d/vsftpd
  ##配置内容
  [root@zyshanlinux-001 vsftpd_user_conf]# cat !$
  cat /etc/pam.d/vsftpd
  #%PAM-1.0
  ​
  auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
  account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
  ​
  session    optional     pam_keyinit.so    force revoke
  auth       required     pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
  auth       required     pam_shells.so
  auth       include      password-auth
  account    include      password-auth
  session    required     pam_loginuid.so
  session    include      password-auth
  ​

编辑vsftpd的主配置

  [root@zyshanlinux-001 vsftpd_user_conf]# vim /etc/vsftpd/vsftpd.conf
  ##配置内容
   将anonymous_enable=YES 改为 anonymous_enable=NO
   将#anon_upload_enable=YES 改为 anon_upload_enable=NO 
   将#anon_mkdir_write_enable=YES 改为 anon_mkdir_write_enable=NO
    再增加如下内容
  chroot_local_user=YES
  guest_enable=YES
  guest_username=virftp  ##映射到系统用户
  virtual_use_local_privs=YES  ##告诉服务使用的是虚拟用户,映射
  user_config_dir=/etc/vsftpd/vsftpd_user_conf  ##定义虚拟用户配置文件所在的路径
  allow_writeable_chroot=YES

启动vsftpd服务

  [root@zyshanlinux-001 vsftpd_user_conf]# systemctl start vsftpd
  [root@zyshanlinux-001 vsftpd_user_conf]# ps aux |grep vsftp
  root     11979  0.0  0.0  53256   576 ?        Ss   10:17   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
  root     12413  0.0  0.0 112704   956 pts/0    R+   10:17   0:00 grep --color=auto vsftp
  ##可以看看监听的端口,是21
  [root@zyshanlinux-001 vsftpd_user_conf]# netstat -lnpt
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      570/rpcbind         
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1295/nginx: master  
  tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      1116/rpc.mountd     
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1101/sshd           
  tcp        0      0 0.0.0.0:44888           0.0.0.0:*               LISTEN      1118/rpc.statd      
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1373/master         
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1295/nginx: master  
  tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
  tcp        0      0 0.0.0.0:42792           0.0.0.0:*               LISTEN      -                   
  tcp6       0      0 :::111                  :::*                    LISTEN      570/rpcbind         
  tcp6       0      0 :::20048                :::*                    LISTEN      1116/rpc.mountd     
  tcp6       0      0 :::21                   :::*                    LISTEN      11979/vsftpd        
  tcp6       0      0 :::22                   :::*                    LISTEN      1101/sshd           
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1373/master         
  tcp6       0      0 :::56347                :::*                    LISTEN      1118/rpc.statd      
  tcp6       0      0 :::36797                :::*                    LISTEN      -                   
  tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
  tcp6       0      0 :::3306                 :::*                    LISTEN      1491/mysqld 

23端口是telnet监听的,但现在几乎没人用了。

在Linux上测试:

安装lftp测试工具

  [root@zyshanlinux-001 ~]# yum install lftp

测试操作~

  [root@zyshanlinux-001 ~]# lftp testuser1@127.0.0.1
  Password: 
  lftp testuser1@127.0.0.1:~> ls    
  -rw-r--r--    1 1018     1019            0 Jul 16 01:42 zyshan.txt
  lftp testuser1@127.0.0.1:/> 

可以查看该状态下能够运行的命令

  lftp testuser1@127.0.0.1:/> ?
      !<shell-command>                     (commands)                           alias [<name> [<value>]]
      attach [PID]                         bookmark [SUBCMD]                    cache [SUBCMD]
      cat [-b] <files>                     cd <rdir>                            chmod [OPTS] mode file...
      close [-a]                           [re]cls [opts] [path/][pattern]      debug [<level>|off] [-o <file>]
      du [options] <dirs>                  exit [<code>|bg]                     get [OPTS] <rfile> [-o <lfile>]
      glob [OPTS] <cmd> <args>             help [<cmd>]
      history -w file|-r file|-c|-l [cnt]  jobs [-v] [<job_no...>]              kill all|<job_no>
      lcd <ldir>                           lftp [OPTS] <site>                   ln [-s] <file1> <file2>
      ls [<args>]                          mget [OPTS] <files>                  mirror [OPTS] [remote [local]]
      mkdir [-p] <dirs>                    module name [args]                   more <files>
      mput [OPTS] <files>                  mrm <files>                          mv <file1> <file2>
      [re]nlist [<args>]                   open [OPTS] <site>
      pget [OPTS] <rfile> [-o <lfile>]     put [OPTS] <lfile> [-o <rfile>]      pwd [-p]
      queue [OPTS] [<cmd>]                 quote <cmd>                          repeat [OPTS] [delay] [command]
      rm [-r] [-f] <files>                 rmdir [-f] <dirs>                    scache [<session_no>]
      set [OPT] [<var> [<val>]]            site <site-cmd>                      source <file>
      torrent [-O <dir>] <file|URL>...     user <user|URL> [<pass>]             wait [<jobno>]
      zcat <files>                         zmore <files>

获取Ftp上的文件,下载到当前目录的。命令quit退出

  lftp testuser1@127.0.0.1:/> get zyshan.txt
  lftp testuser1@127.0.0.1:/> quit             
  [root@zyshanlinux-001 ~]# ls -lt |head
  total 20
  -rw-r--r--  1 root root    0 Jul 16 09:42 zyshan.txt  ##新下载的测试文件
  -rw-r--r--. 1 root root    0 Jun 11 09:16 zyshanlinux-01.ssh:
  -rw-r--r--. 1 root root    0 Jun 11 09:16 192.168.106.1.wins:
  -rw-r--r--. 1 root root    0 Jun 11 09:16 239.255.255.250.ssdp:
  -rw-r--r--. 1 root root   12 Jun  6 22:19 1.txt
  -rw-r--r--. 1 root root   53 Jun  6 22:19 a.txt
  drwxr-xr-x. 2 root root   22 Jun  3 20:13 awk
  drwxr-xr-x. 2 root root   42 Jun  3 17:45 sed
  drwxr-xr-x. 2 root root   35 Jun  3 15:59 grep
  • 若不正常查看日志/var/log/messages和/var/log/secure

  • windows下安装filezilla客户端软件,进行测试

即使关闭vsftpd服务,Filezilla也可以访问sftp://root@

使用pure-ftpd搭建ftp服务

pure-ftpd比vsftpd更加轻量,更加简单。

更新源,把epel-release换回来

  [root@zyshanlinux-001 ~]# yum install -y epel-release

安装pure-ftpd

  [root@zyshanlinux-001 ~]# yum install -y pure-ftpd

编辑配置文件,找到pureftpd.pdb这行,把行首的#删除

  [root@zyshanlinux-001 ~]# vim /etc/pure-ftpd/pure-ftpd.conf
  PureDB                        /etc/pure-ftpd/pureftpd.pdb  ##指定密码配置文件

由于pure-ftpd和vsftpd都是监听21端口,所以要运行pure-ftpd,就必须停掉vsftpd,否则会出错。

  [root@zyshanlinux-001 ~]# systemctl stop vsftpd
  [root@zyshanlinux-001 ~]# systemctl start pure-ftpd
  [root@zyshanlinux-001 ~]# ps aux |grep ftp
  root      6997  0.0  0.0 202516  1200 ?        Ss   11:06   0:00 pure-ftpd (SERVER)
  root      7673  0.0  0.0 112704   960 pts/0    S+   11:06   0:00 grep --color=auto ftp
  [root@zyshanlinux-001 ~]# netstat -lntp
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      570/rpcbind         
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1295/nginx: master  
  tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      1116/rpc.mountd     
  tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      6997/pure-ftpd (SER 
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1101/sshd           
  tcp        0      0 0.0.0.0:44888           0.0.0.0:*               LISTEN      1118/rpc.statd      
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1373/master         
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1295/nginx: master  
  tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
  tcp        0      0 0.0.0.0:42792           0.0.0.0:*               LISTEN      -                   
  tcp6       0      0 :::111                  :::*                    LISTEN      570/rpcbind         
  tcp6       0      0 :::20048                :::*                    LISTEN      1116/rpc.mountd     
  tcp6       0      0 :::21                   :::*                    LISTEN      6997/pure-ftpd (SER 
  tcp6       0      0 :::22                   :::*                    LISTEN      1101/sshd           
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1373/master         
  tcp6       0      0 :::56347                :::*                    LISTEN      1118/rpc.statd      
  tcp6       0      0 :::36797                :::*                    LISTEN      -                   
  tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
  tcp6       0      0 :::3306                 :::*                    LISTEN      1491/mysqld 

创建测试目录,是给pure-ftpd用户使用

  [root@zyshanlinux-001 ~]# mkdir /data/ftp

创建普通用户,1010不唯一就是已存在了,那就创建1020

  [root@zyshanlinux-001 ~]# useradd -u 1010 pure-ftp
  useradd: UID 1010 is not unique
  [root@zyshanlinux-001 ~]# useradd -u 1020 pure-ftp

更改属主属组

  [root@zyshanlinux-001 ~]# chown -R pure-ftp:pure-ftp /data/ftp

创建用户,指定用户名ftp_usera,指定系统用户是谁(映射)-u pure-ftp,虚拟用户的家目录指定-d /data/ftp,输入密码2遍。

  [root@zyshanlinux-001 ~]# pure-pw useradd ftp_usera -u pure-ftp  -d /data/ftp
  Password: 
  Enter it again: 
  [root@zyshanlinux-001 ~]#

把密码生成系统识别的文件(二进制),不执行这步是无法登录的。

  [root@zyshanlinux-001 ~]# pure-pw mkdb

ftp_usera支持哪些用法

  [root@zyshanlinux-001 ~]# pure-pw list/userdel/usermod/passwd
  或者
  [root@zyshanlinux-001 ~]# pure-pw --help

测试

  [root@zyshanlinux-001 ~]# pure-pw useradd ftp_usera -u pure-ftp  -d /data/ftp^C
  [root@zyshanlinux-001 ~]# touch /data/ftp/123.txt
  [root@zyshanlinux-001 ~]# lftp ftp_usera@127.0.0.1
  Password: 
  lftp ftp_usera@127.0.0.1:~> ls    
  drwxr-xr-x    2 1020       pure-ftp           21 Jul 16 11:23 .
  drwxr-xr-x    2 1020       pure-ftp           21 Jul 16 11:23 ..
  -rw-r--r--    1 0          0                   0 Jul 16 11:23 123.txt
  lftp ftp_usera@127.0.0.1:/> 

可以更改一下属主属组,属主映射成uid,属组映射系统组的名字。

  lftp ftp_usera@127.0.0.1:/> quit
  [root@zyshanlinux-001 ~]# chown pure-ftp:pure-ftp /data/ftp/123.txt
  [root@zyshanlinux-001 ~]# lftp ftp_usera@127.0.0.1
  Password: 
  lftp ftp_usera@127.0.0.1:~> ls
  drwxr-xr-x    2 1020       pure-ftp           21 Jul 16 11:23 .
  drwxr-xr-x    2 1020       pure-ftp           21 Jul 16 11:23 ..
  -rw-r--r--    1 1020       pure-ftp            0 Jul 16 11:23 123.txt
  ​

扩展 :

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值