一、
FTP安装配置
1. FTP服务器和客户端之间的防火墙设置策略
1) 对于工作在主动模式下的服务器:
允许FTP客户端从大于1024的端口连接FTP服务器的21端口 允许FTP服务器从21端口回应FTP客户端大于1024端口的网络连接 允许FTP服务器从20端口主动连接FTP客户端中大于1024的端口
允许FTP客户端从大于1024的端口回应来自FTP服务器的20端口的网络连接 2) 对于工作在被动模式下的服务器:
允许FTP客户端从大于1024的端口连接FTP服务器的21端口 允许FTP服务器从21端口回应FTP客户端大于1024端口的网络连接 允许FTP客户端从大于1024的端口主动连接FTP服务器中大于1024的端口 允许FTP服务器从大于1024的端口回应来自FTP客户机大于1024端口的网络连
接
3) 总结:
主动模式、被动模式是对服务端而言,服务端主动访问客户端即为主动模式,反之,
为被动模式;
如果FTP服务只是局域网内访问,则对客户端、服务端要求比较低,主动模式、
被动模式均可以,较易实现,只要关闭防火墙即可;
如果在公网使用FTP服务,则建议使用被动模式。被动模式下客户端要求比较低,
服务端需要对外开放20、21以及1024~65535的所有端口映射服务。主动模式下,对客户端要求较高,客户端需要有公网地址并开通较多端口映射服务,不易实现。
2. Linux vsftpd概述
vsftpd是Linux平台下的FTP软件,有三种访问模式:匿名访问、本地系统账户访问和虚拟用户访问,本地系统账户访问需要使用系统用户账户登录,虚拟账户访问用于大量用户使用时,可以避免需要建立大量系统用户账户,而是将多个账户映射到一个系统用户。
匿名访问时,默认访问目录路径为/var/ftp;本地系统账户访问时,默认访问目录路径为系统用户家目录,并且默认可以访问系统其它目录,可以通过使用chroot_local_user配置
将用户目录在家目录下。
3. 安装FTP服务(以CentOS 6.4为例)
检查系统是否安装vnc:#rpm –qa|grepvsftpd。 安装:#yum install -y vsftpd。
4. 配置FTP服务(以CentOS 6.4为例)
创建系统用户,设置密码:useradd -d /home/ftp4 uftp4,passwd uftp4。 进入配置文件所在目录:cd /etc/vsftpd/,该目录下默认有以下文件:
ftpusers,不允许访问的用户黑名单,可默认;
user_list,根据vsftpd.conf配置文件的userlist_enable参数来确定是否启用该
配置文件(userlist_enable=YES表示启用),并配合userlist_deny参数来确定该文件为黑名单还是白名单(userlist_deny=NO表示白名单,意思是仅名单中用户能访问,反之,即黑名单,名单中用户不能访问,其他用户可访问),为白名单时可在其中加入配置的用户; vsftpd.conf,主配置文件。 备份配置文件:cpvsftpd.conf /opt/ftp/ 编辑配置文件:# vivsftpd.conf 文件解析
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file # loosens things up a bit, to make the ftp daemon more usable. # Please see vsftpd.conf.5 for all compiled in defaults. #
# READ THIS: This example file is NOT an exhaustive list of vsftpd options. # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's # capabilities. #
# Allow anonymous FTP? (Beware - allowed by default if you comment this out). anonymous_enable=NO(允许匿名访问) #
# Uncomment this to allow local users to log in. local_enable=YES(允许本地系统用户访问) #
# Uncomment this to enable any form of FTP write command. write_enable=YES(所有用户是否可写操作)
#
# Default umask for local users is 077. You may wish to change this to 022, # if your users expect that (022 is used by most other ftpd's) local_umask=022(本地用户权限掩码,等同于755) #
# Uncomment this to allow the anonymous FTP user to upload files. This only # has an effect if the above global write enable is activated. Also, you will # obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES(是否允许匿名上传文件) pasv_min_port=2000(被动模式下最小端口) pasv_max_port=3500(被动模式下最小端口)
#pasv_enable=YES(是否启用被动模式,默认启用)
#download_enable=YES(是否允许所有用户下载,默认允许)
# Uncomment this if you want the anonymous FTP user to be able to create # new directories.
#anon_mkdir_write_enable=YES(是否允许匿名用户创建目录、写操作) #
# Activate directory messages - messages given to remote users when they # go into a certain directory.
dirmessage_enable=YES(用户进入目录是否显示消息) #
# The target log file can be vsftpd_log_file or xferlog_file. # This depends on setting xferlog_std_format parameter xferlog_enable=YES(是否启用xferlog日志) #
# Make sure PORT transfer connections originate from port 20 (ftp-data). connect_from_port_20=YES(使用主动模式连接,启用20端口) #
# If you want, you can arrange for uploaded anonymous files to be owned by # a different user. Note! Using \"root\" for uploaded files is not # recommended! #chown_uploads=YES
#chown_username=whoever #
# The name of log file when xferlog_enable=YES and xferlog_std_format=YES # WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log #xferlog_file=/var/log/xferlog #
# Switches between logging into vsftpd_log_file and xferlog_file files. # NO writes to vsftpd_log_file, YES to xferlog_file xferlog_std_format=YES #
# You may change the default value for timing out an idle session. #idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120(建立数据连接请求时超时时间,120秒) #
# It is recommended that you define on your system a unique user which the # ftp server can use as a totally isolated and unprivileged user. #nopriv_user=ftpsecure #
# Enable this and the server will recognise asynchronous ABOR requests. Not # recommended for security (the code is non-trivial). Not enabling it, # however, may confuse older FTP clients. #async_abor_enable=YES #
# By default the server will pretend to allow ASCII mode but in fact ignore # the request. Turn on the below options to have the server actually do ASCII # mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service # attack (DoS) via the command \"SIZE /big/file\" in ASCII mode. vsftpd # predicted this attack and has always been safe, reporting the size of the # raw file.
# ASCII mangling is a horrible feature of the protocol. #ascii_upload_enable=YES #ascii_download_enable=YES #
# You may fully customise the login banner string:
ftpd_banner=Welcome to blah FTP service.(建立连接后欢迎词) #
# You may specify a file of disallowed anonymous e-mail addresses. Apparently # useful for combatting certain DoS attacks. #deny_email_enable=YES # (default follows)
#banned_email_file=/etc/vsftpd/banned_emails #
# You may specify an explicit list of local users to chroot() to their home # directory. If chroot_local_user is YES, then this list becomes a list of # users to NOT chroot().
chroot_local_user=YES(是否本地系统用户目录于家目录下,默认不,与chroot_list_enable参数配套使用,构成与例外)
chroot_list_enable=YES(允许用户例外,可以访问其他目录,与下一参数配合使用,默认没有例外)
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list(例外用户名单文件,需要创建,并添加用户名单) #
# You may activate the \"-R\" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as \"ncftp\" and \"mirror\" assume # the presence of the \"-R\" option, so there is a strong case for enabling it. #ls_recurse_enable=YES #
# When \"listen\" directive is enabled, vsftpd runs in standalone mode and # listens on IPv4 sockets. This directive cannot be used in conjunction # with the listen_ipv6 directive. listen=YES
#listen_port=21(设置监听命令端口号,默认无此参数,可添加设置) # This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6 # sockets, you must run two copies of vsftpd with two configuration files. # Make sure, that one of the listen options is commented !! #listen_ipv6=YES
pam_service_name=vsftpd userlist_enable=YES(启用配置) userlist_deny=NO(白名单) tcp_wrappers=YES
chroot_list(例外不受用户)文件内容样例: uftp1(用户名)
5. 客户端连接使用
1) 通过客户端工具(如FileZilla)连接 2) 浏览器地址栏输入URL信息
3) 在Windows 命令行输入:ftp IP地址,如:ftp 192.168.162.5
ftp命令解析:
close 关闭连接、bye 退出、?帮助
连接指定端口ftp:先输入ftp,进入ftp模式后,输入“open IP port 打开指定连接(open 220.168.83.253 1021)”。
ftp命令模式来连接,不支持被动模式,所以如果不在同一局域网内并关闭防火墙,将无法获取数据。
6. 错误处理
建议用客户端工具连接来检查错误。
1) 连接时错误:500 OOPS: cannot change directory:/home/ftp4
原因是目录不存在、无权限以及通常是SELinux导致无法共享用户家目录,检查系统SELinux状态:sestatus -b | grep ftp
看到ftp_home_dir状态为off,需要修改为on:setsebool -P ftp_home_dir on(也可以通过setenforce0来暂时关闭SELinux,系统重启将失效,如果要永久关闭,需要修改/etc/sysconfig/selinux配置文件中SELINUX参数为permissive,查看SELinux状态命令:getenforce)。
2) 连接成功后能访问目录及下载,不能上传、创建目录、删除等写操作 有以下原因: i.
创建用户时指定的用户家目录不在home目录下,SELinux对其他目录控制更严格,可以通过setenforce0来暂时关闭SELinux; ii.
创建用户时指定的用户家目录是先创建好的,需要重新创建用户同时来指定创建家目录。
二、 SFTP安装配置
1. SFTP概述
SFTP使用ssh通道来安全传输文件,使用ssh端口进行通信。
2. SFTP安装配置
1) 创建使用SFTP的用户及组:
groupaddsftp_crh(组名将在配置文件中使用) useradd -d /opt/crh_ftp -G sftp_crh -s /sbin/nologincrh_ftp -d 指定用户家目录,如果无此目录将创建; -G 指定用户附加属组;
-s 指定用户模式,/sbin/nologin表示不允许用户登录ssh; 上述用户参数也可在创建用户后,通过usermod修改来设定。 设置用户密码:passwdcrh_ftp。 2) 设置目录属主及权限 修改SFTP用户家目录属主及权限 chownroot:sftp_crh /opt/crh_ftp/ chmod 755 /opt/crh_ftp/
创建文件上传目录,并设置属主及权限:
mkdir /opt/crh_ftp/upload
chowncrh_ftp:sftp_crh /opt/crh_ftp/upload/ chmod 755 /opt/crh_ftp/upload/
设置完可通过“getfacl目录名”来查看目录控制信息,结果应该为,SFTP用户家目录属主为root用户,属组为SFTP组,文件上传目录属主为SFTP用户,属组为SFTP组,目录属主用户可读写,如下:
SFTP远程连接后,可以读操作家目录,可以读写操作upload目录。 3) 修改SELinux setenforce0
暂时关闭SELinux,系统重启将失效,如果要永久关闭,需要修改/etc/sysconfig/selinux配置文件中SELINUX参数为permissive,查看SELinux状态命令:getenforce。
4) 修改ssh配置文件 vi /etc/ssh/sshd_config 修改以下内容:
Port 2000(设置ssh连接端口,默认22端口) # override default of no subsystems
#Subsystem sftp /usr/libexec/openssh/sftp-server Subsystem sftp internal-sftp
# Example of overriding settings on a per-user basis
#Match User anoncvs # # #
Match Group sftp_crh(SFTP组) X11Forwarding no AllowTcpForwarding no
ChrootDirectory %h(%h表示SFTP用户家目录) ForceCommand internal-sftp
X11Forwarding no AllowTcpForwarding no ForceCommandcvs server
3. 连接使用SFTP
# sftp -oPort=2000 crh_ftp@202.98.19.161(-oPort=远程端口号) Connecting to 202.98.19.161...
crh_ftp@202.98.19.161's password: (输入SFTP用户密码) sftp> ls(查看目录下内容) upload sftp> ls upload
upload/5800_580003_2015-10-29T191522.data upload/5800_580003_2015-10-29T191551.data upload/5800_580003_2015-10-29T1911.data
sftp> put ./1.txt upload(上传本地系统当前目录下文件至目标目录) sftp>get upload/1.txt ./(下载目标文件至本地系统当前目录下) sftp> ?(显示命令帮助)
sftp>exit (退出)
ssh指定用户及端口远程连接(默认root用户及22端口): ssh crh_ftp@192.168.30.225 -p 21
@后面的IP地址是SFTP的地址
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务