服务器配置教案 Help

7-1 搭建FTP服务

介绍

FTP: (File Transfer Protocol)文件传输协议是在计算机网络的客户端和服务器间传输文件的应用层协议。
与NFS等的区别,NFS是访问文件(file access),FTP是传送文件(file transfer)。

FTP历史悠久始于1971年,运行在20和21两个端口。端口20用于在客户端和服务器之间传输数据流,而端口21用于传输控制流,并且是命令通向ftp服务器的进口。 虽然这个过程很像第三章文件共享,但文件共享的场景时管理员准备好文件分发给其他人1对多。
FTP的场景时开发者自己把项目代码和资料上传到服务器1对1。

主动模式和被动模式

FTP有两种使用模式:主动和被动。主动模式要求客户端和服务器端同时打开并且监听一个端口以建立连接。在这种情况下,客户端由于安装了防火墙会产生一些问题。
所以,创立了被动模式。被动模式只要求服务器端产生一个监听相应端口的进程,这样就可以绕过客户端安装了防火墙的问题。

安全性

FTP不是一项安全的协议,并且具有许多安全漏洞。通过FTP传输的流量不会被加密,所有传输通过明文进行的。
任何能够在网络上执行数据包捕获( 嗅探 )的人都可以读取用户名、密码、命令内容和数据。 其派生品FTPS、SSH能进行加密数据传输。

准备

服务端CentOS虚拟机网卡设置为桥接 ,开机。

安装

软件名: vsftpd

(可选)检测是否已安装

yum list installed | grep vsftpd

(跳过)在线安装

yum install vsftpd

离线安装

  1. 老师下发离线安装包到学生机桌面。宿主机中复制,虚拟机中图形化文件管理器中粘贴,即下载安装包到/root目录下。

  2. 解压缩解包

tar -zxvf vsftpd-installer.tar.gz
  1. 安装

rpm -iv --nodeps --force ./vsftpd-installer/*.rpm

配置介绍

实验目的

默认状态下直接可以启动配置,我们定义几条实验目的来继续:

  • 能够以zhangsan用户登录

  • 登录后显示欢迎信息"Welcome"

  • 权限为能在 /home/zhangsan 目录中进行文件上传

  • 禁止匿名用户登录

配置文件介绍

  • 主配置:/etc/vsftpd/vsftpd.conf

  • 用户管理:/etc/vsftpd/ftpusers

  • 用户管理:/etc/vsftpd/user_list

阅读主配置

# 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. # 允许linux系统本地用户登录,如root、zhangsan 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) # 反掩码,777-022=755,即rwxr-xr-x,即创建文件的权限为用户自己可读写执行、其他人可进入文件夹可查看文件列表但不能修改文件。 local_umask=022 # # 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. # When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access #anon_upload_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 # # Activate logging of uploads/downloads. xferlog_enable=YES # # Make sure PORT transfer connections originate from port 20 (ftp-data). connect_from_port_20=YES # # 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 # # You may override where the log file goes if you like. The default is shown # below. #xferlog_file=/var/log/xferlog # # If you want, you can have your log file in standard ftpd xferlog format. # Note that the default log file location is /var/log/xferlog in this case. 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 # # 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. The vsftpd.conf(5) man page explains # the behaviour when these options are disabled. # 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(). # (Warning! chroot'ing can be very dangerous. If using chroot, make sure that # the user does not have write access to the top level directory within the # chroot) #chroot_local_user=YES #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=NO # # This directive enables listening on IPv6 sockets. By default, listening # on the IPv6 "any" address (::) will accept connections from both IPv6 # and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6 # sockets. If you want that (perhaps because you want to listen on specific # addresses) then you must run two copies of vsftpd with two configuration # files. # Make sure, that one of the listen options is commented !! # 我们用的是IPv4,但此设置项目的注释说listen_ipv6=YES时ipv4和ipv6都能用,两者不能同时为yes,也就是说保持默认或把上面listen=YES把下面listen_ipv6=NO。 listen_ipv6=YES pam_service_name=vsftpd userlist_enable=YES

修改配置

  1. 添加本地用户

    1. useradd zhangsan 2. passwd zhangsan
  2. 欢迎信息
    把简单信息添加到文件末这种场景下,用>把字符串流写入到后面的文件中。

    echo "hello, zhangsan." > /etc/vsftpd/welcome

启动服务

  1. 重新启动服务
    如果服务未启动则启动相当于start,如果已启动则重启相当于先stop再start。

    systemctl start vsftpd
  2. (可选但建议)查看服务状态

    systemctl status vsftpd

    正常时的返回信息如下

    vsftpd.service - Vsftpd ftp daemon Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor pres> Active: active (running) since Wed 2024-11-06 22:04:00 EST; 7s ago Process: 16473 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited> Main PID: 16474 (vsftpd) Tasks: 1 (limit: 10796) Memory: 772.0K CGroup: /system.slice/vsftpd.service └─16474 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf Nov 06 22:04:00 bogon systemd[1]: Starting Vsftpd ftp daemon... Nov 06 22:04:00 bogon systemd[1]: Started Vsftpd ftp daemon.
  3. 关闭防火墙

    systemctl stop firewalld

客户端测试

客户端软件为ftp,(老师没有提供客户端ftp的离线安装包),想测试的可以参考书上安装客户端yum install ftp ,在命令行环境下进行ftp会话连接、用户登录、上传下载。 平时工作中普遍使用图形化软件, Windows下几款FTP工具 ,大同小异。

  1. 客户机选用机房的WindowsServer或老师提供的Windows10,与服务器处于同一网段下,因服务器用桥接 ,因此客户端也桥接 ,如果服务器用NAT,那么客户端也用NAT。

  2. 这里提供的是FileZilla ,官网可免费下载基础版。

  3. 宿主机Windows10中安装FileZilla
    安装时勾选桌面快捷方式,其它下一步。

    7 1 file zilla

  4. 客户端连接成功。
    输入服务器IP(替换成你自己的)、服务端本地用户名zhangsan、密码111111、默认端口21,然后"快速连接"。
    弹窗提示是否保存密码选"不保存",弹窗提示不支持TLS明文传输不安全点"确定"。
    软件右侧刷新出服务器的目录。
    从宿主机拖动文件(假设为开发好的代码项目)➡️到右侧服务器。🎉实验完成。

    7 1

拓展作业

  1. root用户登录时显示权限被拒绝,由于安全原因,root用户默认不允许登录。搜索网上的方法,修改配置文件,重启服务,使root用户允许登录。

  2. 尝试其它客户端如FlashFTP等。

Last modified: 25 十一月 2024