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 [email protected]
  Password: 
  lftp [email protected]:~> ls    
  -rw-r--r--    1 1018     1019            0 Jul 16 01:42 zyshan.txt
  lftp [email protected]:/> 

可以查看該狀態下能夠運行的命令

  lftp [email protected]:/> ?
      !<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 [email protected]:/> get zyshan.txt
  lftp [email protected]:/> 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 [email protected]
  Password: 
  lftp [email protected]:~> 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 [email protected]:/> 

可以更改一下屬主屬組,屬主映射成uid,屬組映射系統組的名字。

  lftp [email protected]:/> quit
  [root@zyshanlinux-001 ~]# chown pure-ftp:pure-ftp /data/ftp/123.txt
  [root@zyshanlinux-001 ~]# lftp [email protected]
  Password: 
  lftp [email protected]:~> 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
  

擴展 :

發佈了57 篇原創文章 · 獲贊 22 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章