centos7下 FTP服务器的配置,以及配置ftp支持ftps

  • FTP传输协议
  • FTP [File Transfer PRocotol]主要用于在服务器与客户端之间进行文件传输,但是采用的是明文传输,对于FTP协议实现的,比较安全的软件是vsftpd
  • FTP的用户身份
    • 1.user:具有较大的权限,和较多的命令执行;
    • 2.guest:访客用户具有中等能力的权限;
    • 3.anontmous:表示匿名用户,大多数只允许登陆,和下载,一般不允许你名用户登陆;
  • 关于日至文件记录在/var/log/messages里面;
  • 一般情况下使用伪根来确保用户在访问资源时,被限定在一个特定的目录里面,不至于访问其他目录下面的重要数据,是为了安全进行的设定;
  • FTP的工作流程
  • FTP通过命令连接来接收用于发送过来的命令,通过数据连接来发送用于请求的数据,FTP默认使用的是主动连接的方式,
  • FTP工作在主动模式的过程:
    • 1.Server工作在22端口,client通过自己随机的端口和server的21端口建立TCP连接;
    • 2.当需要获得数据时,client将自己采用的active方式通知给server,并且client打开自己的一个随机端口,
    • 3.server将通过请求的数据通过数据端口20数据发送到client的随机端口上面,完成数据的发送;
  • 总结:

    • 1.server需要两个端口,21用于接收用户的命令,并且将命令的执行结果返回;
    • 2.20端口用于在用于请求数据时打开,用于完成数据连接的请求;
    • 3.client需要用自己的第一个随机端口和serer21端口建立连接,当工作在active模式下,发起数据请求时,需要将自己生成的第二个随机端口,发送给server,server将请求的数据,通过数据端口22发送给client的第二个随机端口;
      *FTP被动连接的工作过程;
  • 1.client通过正常的TCP三次连接建立和server21端口的连接;

  • 2.在client发起数据请求时,首先将自己的工作模式PASV发送给server;
  • 3.server,首先选择随机的空闲端口打开,并且将自己的随机端口,这里不在是20,发送给client;
  • 4.Client使用自己生成的随机端口连接server发送过来的随机端口,然后完成数据传送;

  • FTP服务器的配置

  • 需要安装的软件vsftpd,安装之后会生成以下几个文件:
    这里写图片描述
  • /etc/pam.d/vsftpd:用于完成用户认证的;
  • /etc/rc.d/init.d/vsftpd:表示服务控制脚本;
  • /etc/vsftpd/vsftpd.conf:表示服务配置文件;
  • /etc/vsftpd/ftpusers /etc/vsftpd/user_list:用于控制用户登录的配置文件;
  • /var/ftp:ftp服务的根目录,不允许root之外任何用户具有写权限,一定不允许运行这个进程的用户具有写权限,如果需要可以创建新的目录具有写权限;
  • /var/ftp/pub:各个用户的公共文件目录,也就是匿名用户的家目录;
    • vsftpd在安装成功之后,就会创建默认的的用户“
      这里写图片描述
  • 接下来启动ftp服务

[root@server23 private]# /etc/init.d/vsftpd start
Starting vsftpd for vsftpd:                                [  OK  ]

#centos下使用如下命令启动

systemctl start vsftpd.service
  • 作为客户端需要安装的软件是lftp,来提供相应的客户端工具,接下来需要确保防火墙的状态是关闭的,其次需要确保selinux的状态是Permissive的;
[root@server23 private]# /etc/init.d/iptables stop 
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@server23 private]# setenforce 0 
  • 接下来尝试连接lftp服务器端
    这里写图片描述
  • 可以通过help命令来查看支持的命令一个选项
 !<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>
  • 接下来解释以下配置文件/etc/vsftpd/vsftpd.conf里面的相关信息
  • anonymous_enable=YES:表示是否允许匿名用户登录,修改为NO,匿名用户无法登陆
    这里写图片描述
  • local_enable=YES:是否启用系统用户,这里已经启用了系统用户,所以可以使用hadoop用户的帐号和密码进行登陆;

  • write_enable=YES:是否允许写权限,也就是执行STOR, DELE, RNFR,RNTO, MKD, RMD, APPE and SITE.命令来改变文件系统属性,如果这个选项被更改为NO,那么用户将无法上传文件到服务器,并且无法执行创建目录等操作;

  • anon_upload_enable=YES:表示是否允许匿名用户上传文件;

  • anon_mkdir_write_enable:用于定义用户是否具有创建目录的权限;
  • dirmessage_enable=YES:用户进入一个目录时否显示欢迎信息,如果需要显示欢迎信息,就需要在对应的目录里面创建隐藏文件.messages,然后再切换用户目录时,就会显示目录的切换信息;
  • xferlog_enable=YES:用于记录用户下载文件等信息;
  • xferlog_file=/var/log/xferlog:用于定义日志的生成位置;
  • xferlog_std_format=YES:用于定义日志的格式,这个方便某些日志访问软件对于日志进行分析;
  • chown_uploads=YES 以及 chown_username=whoever:用于文件在上传之后更改文件的权限,用于防止用户在上传之后修改这些文件;
  • idle_session_timeout=600:用于定义会话超时时间的;
  • data_connection_timeout=120:用于定义数据传输超时时间的;
  • ascii_upload_enable=YES ascii_download_enable=YES:表示使用文本方式上传和下载文件,不建议开启;
  • chroot_list_enable=YES:表示锁定某个用户在自己的家目录里面,防止用户随意切换;
  • chroot_list_file=/etc/vsftpd/chroot_list:创建用户名称的文件列表,所有里面的用户;
  • chroot_local_user=YES:直接禁锢所有的用户的家目录,这个选项是建议已用的,否则会导致用户随意访问系统目录,不安全;
  • max_clients:表示最多允许多少个Client登陆;
  • max_per_ip:表示最多允许一个IP地址登陆多少次;

  • 接下来可以实现对于某些功能的启用或者是禁用

  • FTP进行传输的数据都是明文进行传输的,甚至包括用户认证的过程,底下抓包,抓到的信息甚至包括用户的认证信息和密码;

tions [nop,nop,TS val 5306475 ecr 321537], length 13
    0x0000:  4500 0041 af5f 4000 4006 0414 ac19 17fa  E..A._@.@.......
    0x0010:  ac19 1717 bead 0015 b0ca 5746 8177 7eff  ..........WF.w~.
    0x0020:  8018 00e5 8777 0000 0101 080a 0050 f86b  .....w.......P.k
    0x0030:  0004 e801 5553 4552 2068 6164 6f6f 700d  ....USER.hadoop.
    0x0040:  0a    这段是用户名信息                                     .
10:37:55.004685 IP (tos 0x0, ttl 64, id 39314, offset 0, flags [DF], proto TCP (6), length 86)
    172.25.23.23.21 > 172.25.23.250.48813: Flags [P.], cksum 0x878c (incorrect -> 0xfe4f), seq 134:168, ack 34, win 453, options [nop,nop,TS val 321537 ecr 5306475], length 34
    0x0000:  4500 0056 9992 4000 4006 19cc ac19 1717  E..V..@.@.......
    0x0010:  ac19 17fa 0015 bead 8177 7eff b0ca 5753  .........w~...WS
    0x0020:  8018 01c5 878c 0000 0101 080a 0004 e801  ................
    0x0030:  0050 f86b 3333 3120 506c 6561 7365 2073  .P.k331.Please.s
    0x0040:  7065 6369 6679 2074 6865 2070 6173 7377  pecify.the.passw
    0x0050:  6f72 642e 0d0a                           ord...
10:37:55.004791 IP (tos 0x0, ttl 64, id 44896, offset 0, flags [DF], proto TCP (6), length 65)
    172.25.23.250.48813 > 172.25.23.23.21: Flags [P.], cksum 0x8777 (incorrect -> 0x3765), seq 34:47, ack 168, win 229, options [nop,nop,TS val 5306476 ecr 321537], length 13
    0x0000:  4500 0041 af60 4000 4006 0413 ac19 17fa  E..A.`@.@.......
    0x0010:  ac19 1717 bead 0015 b0ca 5753 8177 7f21  ..........WS.w.!
    0x0020:  8018 00e5 8777 0000 0101 080a 0050 f86c  .....w.......P.l
    0x0030:  0004 e801 5041 5353 2068 6164 6f6f 700d  ....PASS.hadoop.
这段是密码信息
  • 配置允许系统用户上传文件
write_enable=YES

这里写图片描述

  • 配置允许匿名用户上传文件
anon_upload_enable=YES
  • 打开这个权限之后,还需要对应的目录ftp具有写权限,为了安全,这里自己创建一个目录
[root@server23 vsftpd]# mkdir /var/ftp/upload 
[root@server23 vsftpd]# setfacl -m u:ftp:rwx /var/ftp/upload/
  • 接下来的上传就是成功的
[root@my Desktop]# ftp 172.25.23.23
Connected to 172.25.23.23 (172.25.23.23).
220 (vsFTPd 2.2.2)
Name (172.25.23.23:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.


ftp> cd upload
250 Directory successfully changed.
ftp> put LVS+.pdf
local: LVS+.pdf remote: LVS+.pdf
227 Entering Passive Mode (172,25,23,23,103,106).
150 Ok to send data.
226 Transfer complete.
790569 bytes sent in 0.13 secs (6058.19 Kbytes/sec)
  • 对于共享的权限=文家系统权限*文件共享权限

  • 配置匿名用户可以创建目录

anon_mkdir_write_enable=YES
  • 接下来尝试匿名用户ftp创建目录
ftp> ls
227 Entering Passive Mode (172,25,23,23,243,227).
150 Here comes the directory listing.
-rw-------    1 14       50         790569 Apr 16 03:01 LVS+.pdf
226 Directory send OK.
ftp> mkdir test
257 "/upload/test" created
150 Here comes the directory listing.
-rw-------    1 14       50         790569 Apr 16 03:01 LVS+.pdf
drwx------    2 14       50           4096 Apr 16 03:07 test
226 Directory send OK.
  • 因为upload这个目录ftp用户具有了rwx权限,所以暂时匿名用户只能够在这个目录里面创建安目录;
  • 配置匿名用户ftp具有删除文件的权限
150 Here comes the directory listing.
-rw-------    1 14       50         790569 Apr 16 03:01 LVS+.pdf
drwx------    2 14       50           4096 Apr 16 03:07 test
226 Directory send OK.
ftp> delete LVS+.pdf
550 Permission denied.
  • 然后退出重新登陆之后,就可以删除文件
227 Entering Passive Mode (172,25,23,23,148,23).
150 Here comes the directory listing.
-rw-------    1 14       50         790569 Apr 16 03:01 LVS+.pdf
drwx------    2 14       50           4096 Apr 16 03:07 test
226 Directory send OK.
ftp> delete LVS+.pdf
250 Delete operation successful.
  • 配置显示欢迎信息
  • 首先创建目录里面的欢迎文件;
[root@server23 vsftpd]# vim /var/ftp/upload/.message  /在那个目录创建,进入那个目录会显示信息
Welcome to upload directory
  • 使用匿名用户进行登陆会显示欢迎信息;
ftp> ls
227 Entering Passive Mode (172,25,23,23,46,90).
150 Here comes the directory listing.
drwxr-xr-x    3 0        0            4096 Feb 12  2013 pub
drwxrwxr-x    3 0        0            4096 Apr 16 03:13 upload
226 Directory send OK.
ftp> cd upload
250-Welcome to upload directory
250-
  • 上面使用lftp登陆并且切换用户目录的过程,是不会显示欢迎信息的

  • 打开日志功能

  • 需要打开两个选项
xferlog_enable=YES
xferlog_file=/var/log/xferlog
  • 在上传和下载文件的过程中,可以查看到详细的日志信息
    这里写图片描述

  • 锁定用户的家目录

  • 首先需要开启chroot_list_enable用于打开锁定用户家目录的功能,接下来需要chroot+list_file用于指定这个文件里面定义的用户,用于执行锁定用户家目录的功能;
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list
  • 创建文件,并且添加限定的用户
[root@server23 vsftpd]# vim /etc/vsftpd/chroot_list
hadoop
  • hadoop用户随意的切换目录就会失败
[root@my Desktop]# ftp 172.25.23.23 
Connected to 172.25.23.23 (172.25.23.23).
220 (vsFTPd 2.2.2)
Name (172.25.23.23:root): hadoop
331 Please specify the password.
Password:
230-welcome to upload directory;
230-
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> cd /etc/
550 Failed to change directory.
  • 对于不在这个目录里面的用户,是可以随便的切换目录的,可以直接使用指令chroot_local_user=YES,表示所有的用户都会被禁锢到自己的家目录,需要注释上面的开启的两行选项
  • 配置工作为独立守护进程
  • listen=YES表示配置为独立守护进程,如果需要配置为瞬时守护进程,需要在/etc/xinetd.d里面提供瞬时守护脚本;

  • 对于ftpusers里面定义的用户都是禁止访问ftp服务器的;

[root@my Desktop]# ftp 172.25.23.23 
Connected to 172.25.23.23 (172.25.23.23).
220 (vsFTPd 2.2.2)
Name (172.25.23.23:root): root
530 Permission denied.
Login failed.
ftp> 
  • 因为vsftp的用户控制是接收pam进行控制的,pam里面存在关于vfstpd的定义
[root@server23 vsftpd]# cat /etc/pam.d/vsftpd 
#%PAM-1.0
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
  • 尤其是第二行,表示对于/etc/vsftpd/ftpusers里面定义的文件都是用来进行拒绝的;
  • user_list首先是这个文件控制敏感用户能否登陆系统的,其次才是ftpusers控制的,删除这个文件之后,控制就只由ftpusers来进行控制
[root@server23 vsftpd]# /etc/init.d/vsftpd restart
Shutting down vsftpd:                                      [  OK  ]
Starting vsftpd for vsftpd:                                [  OK  ]
  • 之后尝试登陆,是可以输入密码的,但是提示登录失败;
[root@my Desktop]# ftp 172.25.23.23 
Connected to 172.25.23.23 (172.25.23.23).
220 (vsFTPd 2.2.2)
Name (172.25.23.23:root): root
331 Please specify the password.
Password:     //之前是密码都不能够进行输入,直接提示失败的;
530 Login incorrect.
Login failed.
  • 如果不希望拒绝userlist里面的用户登陆,可以通过添加指令userlist_deny=NO,表示不拒绝这个文件里面的用户登录,在这个文件里面定义的用户就是可以的,但是这个列表之外的用户就不能够使用ftp服务了,也就是说,如果定义为no,表示仅仅允许这个文件里面的用户使用ftp服务

  • 接下来期望提供vsftpd结合ssl/tls实现加密通信

  • 首先生成私钥
[root@server23 CA]# (umask 077;openssl genrsa  -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
...........................................+++
................+++
e is 65537 (0x10001)
  • 其次生成自签名证书
[root@server23 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:SS
Locality Name (eg, city) [Default City]:XX
Organization Name (eg, company) [Default Company Ltd]:TE
Organizational Unit Name (eg, section) []:TEC
Common Name (eg, your name or your server's hostname) []:ca.linux.com
Email Address []:
  • 接下来配置vsftpd
  • 首先创建目录,然后生成私钥
[root@server23 CA]# cd /etc/vsftpd/ssl/
[root@server23 ssl]# (umask 077;openssl genrsa  -out vsftpd.key 2048)Generating RSA private key, 2048 bit long modulus
...........................+++
......+++
e is 65537 (0x10001)
  • 接下来生成证书签署请求
[root@server23 ssl]# openssl req -new -key vsftpd.key -out vsftpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:SX
Locality Name (eg, city) [Default City]:XA
Organization Name (eg, company) [Default Company Ltd]:LINUX
Organizational Unit Name (eg, section) []:TECH
Common Name (eg, your name or your server's hostname) []:ftp.server23.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
  • CA签署请求
[root@server23 ssl]# openssl ca -in vsftpd.csr -out vsftpd.crt
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Apr 16 08:35:09 2018 GMT
            Not After : Apr 16 08:35:09 2019 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = SX
            organizationName          = LINUX
            organizationalUnitName    = TECH
            commonName                = ftp.server23.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                40:26:AB:BD:6C:FE:50:82:44:7D:41:A8:E7:43:7F:5E:55:7C:DC:7A
            X509v3 Authority Key Identifier: 
                keyid:B3:C1:80:F2:07:25:42:C1:82:96:FE:C7:70:F1:46:63:7B:2E:16:B1

Certificate is to be certified until Apr 16 08:35:09 2019 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
  • 编辑vsftpd.conf配置文件来使用生成的证书
# ssl or tls 
ssl_enable=YES
ssl_sslv3=YES
ssl_tlsv1=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
rsa_cert_file=/etc/vsftpd/ssl/vsftpd.crt
rsa_private_key_file=/etc/vsftpd/ssl/vsftpd.key
  • 然后需要重启vsftpd服务
[root@my Desktop]# ftp 172.25.23.23
Connected to 172.25.23.23 (172.25.23.23).
220 (vsFTPd 2.2.2)
Name (172.25.23.23:root): hadoop
530 Non-anonymous sessions must use encryption.
Login failed.
ftp> hadoop
?Invalid command
  • 上面提示的信息表示非匿名用户必须使用encryption来进行连接,这里如果需要进行连接ftp服务器,就不能够使用Linux lftp命令来进行连接了,暂时只能够使用Windows FileZilla等软件来进行连接;
发布了55 篇原创文章 · 获赞 43 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_21137441/article/details/91888233
今日推荐