FTP服务器搭建

Posted 坏坏-5

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FTP服务器搭建相关的知识,希望对你有一定的参考价值。


环境说明

主机系统系统版本IP地址
服务端LinuxRHEL 7.0192.168.43.128/24
客户端LinuxRHEL 7.4192.168.43.15/24
客户端Windows 11Windows 11192.168.43.1/24

服务器配置

  • 安装FTP服务软件包
[root@Server ~]# yum -y install vsftpd.x86_64
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Base                                                  | 4.1 kB     00:00     
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installed
--> Finished Dependency Resolution

...

Running transaction
  Installing : vsftpd-3.0.2-22.el7.x86_64                                1/1 
  Verifying  : vsftpd-3.0.2-22.el7.x86_64                                1/1 

Installed:
  vsftpd.x86_64 0:3.0.2-22.el7                                               

Complete!
[root@Server ~]# 
  • 设置FTP服务开机自启动,并设置防火墙放行规则
[root@Server ~]# systemctl enable vsftpd
ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@Server ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcp dhcpv6-client dns http samba ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@Server ~]# firewall-cmd --permanent --add-service="ftp"
success
[root@Server ~]# firewall-cmd --reload 
success
[root@Server ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcp dhcpv6-client dns ftp http samba ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@Server ~]#

FTP配置文件说明

  • 配置文件
[root@Server ~]# cd /etc/vsftpd/
[root@Server vsftpd]# ls
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh
ftpusers         # 禁止登录的FTP用户
user_list        # 允许登录的FTP用户
vsftpd.conf      # FTP主配置文件
  • 主配置文件说明
[root@Server ~]# vim /etc/vsftpd/vsftpd.conf
# 控制是否允许匿名用户登录,YES允许,NO不允许,默认值为YES
anonymous_enable=YES|NO

# 设定匿名用户的根目录,即匿名用户登入后,被定位到此目录下。主配置文件中默认无此项,默认值为/var/ftp/
anon_root=

# 是否允许本地用户登录FTP服务器。默认为NO
local_enable=YES

# 是否对登录用户开启写权限。属全局性设置。默认NO
write_enable=YES

# 定义匿名用户的账户名称,默认值为ftp
ftp_username=ftp

# 设置本地用户新增文档的umask,默认为022,对应的权限为755
local_umask=022

# 登入FTP服务器的欢迎语
ftpd_banner=Welcome to Bad’s FTP.

# 设置用户登录时,将要显示输出的文件。该设置项将覆盖ftpd_banner的设置
dirmessage_enable=YES

# 设置是否显示目录消息。若设置为YES,则当用户进入特定目录(比如/var/ftp/linux)时,将显示该目录中的
# 由message_file配置项指定的文件(.message)中的内容
message_file=.message

# 指定FTP数据传输连接使用20端口
connect_from_port_20=YES

# 是否启用上传/下载日志记录
xferlog_enable=YES

# 设置日志文件名及路径。需启用xferlog_enable选项
xferlog_file=var/log/vsftpd.log

# 日志文件是否使用标准的xferlog日志文件格式
xferlog_std_format=YES

# 设置vsftpd服务器是否以standalone模式运行。以standalone模式运行是一种较好的方式,此时listen必须设置为YES,
# 此为默认值,建议不要更改。很多与服务器运行相关的配置命令,需要此运行模式才有效。若设置为NO,则vsftpd不是
# 以独立的服务运行,要受xinetd服务的管理控制,功能上会受限制
listen=NO

# 设置是否侦听IPv6的地址
listen_ipv6=YES

# 设置在PAM所使用的名称,默认值为vsftpd
pam_service_name=vsftpd

# 决定/etc/vsftpd/user_list文件是否启用生效。YES则生效,NO不生效
userlist_enable=YES

# 决定/etc/vsftpd/user_list文件中的用户是允许访问还是不允许访问。若设置为YES,则/etc/vsftpd/user_list文件中
# 的用户将不允许访问FTP服务器;若设置为NO,则只有vsftpd.user_list文件中的用户,才能访问FTP服务器
userlist_deny=NO

# 设置vsftpd服务器是否与tcp wrapper相结合,进行主机的访问控制。默认设置为YES,vsftpd服务器会检查/etc/hosts.allow
# 和/etc/hosts.deny中的设置,以决定请求连接的主机是否允许访问该FTP服务器
tcp_wrappers=YES

# 最大允许连接的IP数量
max_per_ip=2

# 最大传输速率为500kbit/s
local_max_rate=500

服务端配置

  • 创建FTP登录的用户,并设置密码
[root@Server vsftpd]# useradd ftp1
[root@Server vsftpd]# useradd ftp2
[root@Server vsftpd]# echo "123" | passwd --stdin ftp1
Changing password for user ftp1.
passwd: all authentication tokens updated successfully.
[root@Server vsftpd]# echo "123" | passwd --stdin ftp2
Changing password for user ftp2.
passwd: all authentication tokens updated successfully.
  • 修改允许用户登入的文件和禁止用户登入的文件
    • 在文件的最后一行加入用户名
[root@Server vsftpd]# vim ftpusers
ftp1
[root@Server vsftpd]# vim user_list
ftp2
  • 重新启动FTP服务
[root@Server vsftpd]# systemctl restart vsftpd.service

客户端测试

  • FTP常用命令
1. open:与服务器相连接;
2. send(put):上传文件;
3. get:下载文件;
4. mget:下载多个文件;
5. cd:切换目录;
6. dir:查看当前目录下的文件;
7. del:删除文件;
8. bye:中断与服务器的连接

Windows客户端测试

Linux客户端测试

  • 在服务端配置,禁止Linux客户端(192.168.43.15)访问FTP服务器
    • 也可以配置允许哪些IP的主机访问
[root@Server vsftpd]# vim /etc/hosts.deny 
[root@Server vsftpd]# tail -1 /etc/hosts.deny
vsftpd:192.168.43.15/255.255.255.255
[root@Server vsftpd]# 
  • 在Linux客户端再次访问,则无法访问

以上内容均属原创,如有不详或错误,敬请指出。

以上是关于FTP服务器搭建的主要内容,如果未能解决你的问题,请参考以下文章

java Ftp上传创建多层文件的代码片段

python 搭建ftp服务器

vsftpd搭建ftp服务器

Windows搭建FTP服务器,JAVA实现读写功能

ftp服务器搭建(windows)

Centos7安装搭建FTP服务器