网络服务器配置与管理教辅书 Help

7-1 搭建FTP服务

引题:第三章文件共享服务器的文件最开始从哪来的?FTP与Samba的使用场景有什么不同?

介绍

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能进行加密数据传输。

在线安装

软件名: 服务端vsftpd

  1. 换源。

    sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-* sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=https://mirrors.cloud.tencent.com|g" /etc/yum.repos.d/CentOS-* yum makecache
  2. 在线安装

    yum install vsftpd

(选做)配置介绍

实验目的

默认状态下直接可以启动服务,我们新增几项需求:

  • 允许客户端以用户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. 添加系统本地用户

    useradd zhangsan passwd zhangsan
  2. 修改关于欢迎信息的配置文件,除了使用vim/nano进行文本编辑,简单情况可以使用shell脚本>把字符串写入到后面的文件中。

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

启动服务

  1. 启动服务

    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 setenforce 0

客户端测试

FTP客户端有多款,大同小异,这里使用FileZillawindows版。

  1. 客户端选用宿主机Windows。

  2. FileZilla官网 找到FileZilla官网-下载 下载客户端免费基础版。

  3. 安装FileZilla,一直下一步,。

    7 1 file zilla

  4. 客户端尝试连接服务器。

    1. 客户端新建连接会话

    2. 输入服务器IP(替换成你自己的)

    3. 输入服务端本地用户名 zhangsan 、密码111111 。弹窗提示"是否保存密码"选"不保存"。弹窗警告"不支持TLS,明文传输不安全",点"确定"。

    4. 输入端口21 ,控制端口21(传输端口20),然后点击"快速连接"。

  5. 连接成功。
    软件右侧刷新出服务器的目录。
    从宿主机拖动任意文件(假设为开发好的代码项目)➡️到右侧服务器。
    上传进度条走完,在右侧服务器目录看到文件🎉实验完成。

    7 1

拓展作业

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

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

  3. SFTP协议

31 十月 2025