手把手教你如何搭建自己的文件服务器

2025-09-09 09:35:28

Linux上安装文件服务器FTP

由于FTP、HTTP、Telnet等协议的数据都是使用明文进行传输的,因此从设计上就是不可靠的。人们为了满足以密文方式传输文件的需求,发明了vsftpd服务程序。vsftpd(very secure ftp daemon,非常安全的FTP守护进程)是一款运行在Linux操作系统上的FTP服务程序,不仅完全开源而且免费。此外,它还具有很高的安全性、传输速度,以及支持虚拟用户验证等其他FTP服务程序不具备的特点。在不影响使用的前提下,管理者可以自行决定客户端是采用匿名开放、本地用户还是虚拟用户的验证方式来登录vsftpd服务器。这样即便黑客拿到了虚拟用户的账号密码,也不见得能成功登录vsftpd服务器。

安装VSFTP

下载dnf

[root@chenstudy ~]# yum install epel-release

[root@chenstudy ~]# yum install dnf

下载VSFTP

[root@chenstudy ~]# dnf install vsftpd

清除防火墙的iptables缓存

iptables防火墙管理工具默认禁止了FTP协议的端口号,因此在正式配置vsftpd服务程序之前,为了避免这些默认的防火墙策略“捣乱”,还需要清空iptables防火墙的默认策略,并把当前已经被清理的防火墙策略状态保存下来:

[root@chenstudy ~]# iptables -F

[root@chenstudy ~]# iptables-save

然后再把FTP协议添加到firewalld服务的允许列表中(前期准备工作一定要做充足):

[root@chenstudy ~]# firewall-cmd --permanent --zone=public --add-service=ftp

success

[root@chenstudy ~]# firewall-cmd --reload

success

查看vsftpd服务程序的主配置文件(/etc/vsftpd/vsftpd.conf):

[root@chenstudy ~]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak

[root@chenstudy ~]# grep -v "#" /etc/vsftpd/vsftpd.conf_bak > /etc/vsftpd/vsftpd.conf

[root@chenstudy ~]# cat /etc/vsftpd/vsftpd.conf

anonymous_enable=YES

local_enable=YES

write_enable=YES

local_umask=022

dirmessage_enable=YES

xferlog_enable=YES

connect_from_port_20=YES

xferlog_std_format=YES

listen=NO

listen_ipv6=YES

pam_service_name=vsftpd

userlist_enable=YES

tcp_wrappers=YES

[root@chenstudy ~]#

**vsftpd服务程序常用的参数以及作用**

参数

作用

listen=[YES\

NO]

是否以独立运行的方式监听服务

listen_address=IP地址

设置要监听的IP地址

listen_port=21

设置FTP服务的监听端口

download_enable=[YES\

NO]

是否允许下载文件

userlist_enable=[YES\

NO] userlist_deny=[YES\

NO]

设置用户列表为“允许”还是“禁止”操作

max_clients=0

最大客户端连接数,0为不限制

max_per_ip=0

同一IP地址的最大连接数,0为不限制

anonymous_enable=[YES\

NO]

是否允许匿名用户访问

anon_upload_enable=[YES\

NO]

是否允许匿名用户上传文件

anon_umask=022

匿名用户上传文件的umask值

anon_root=/var/ftp

匿名用户的FTP根目录

anon_mkdir_write_enable=[YES\

NO]

是否允许匿名用户创建目录

anon_other_write_enable=[YES\

NO]

是否开放匿名用户的其他写入权限(包括重命名、删除等操作权限)

anon_max_rate=0

匿名用户的最大传输速率(字节/秒),0为不限制

local_enable=[YES\

NO]

是否允许本地用户登录FTP

local_umask=022

本地用户上传文件的umask值

local_root=/var/ftp

本地用户的FTP根目录

chroot_local_user=[YES\

NO]

是否将用户权限禁锢在FTP目录,以确保安全

local_max_rate=0

本地用户最大传输速率(字节/秒),0为不限制

下载FTP

vsftpd作为更加安全的文件传输协议服务程序,允许用户以3种认证模式登录FTP服务器。

匿名开放模式:是最不安全的一种认证模式,任何人都可以无须密码验证而直接登录到FTP服务器。

本地用户模式:是通过Linux系统本地的账户密码信息进行认证的模式,相较于匿名开放模式更安全,而且配置起来也很简单。但是如果黑客破解了账户的信息,就可以畅通无阻地登录FTP服务器,从而完全控制整台服务器。

虚拟用户模式:更安全的一种认证模式,它需要为FTP服务单独建立用户数据库文件,虚拟出用来进行密码验证的账户信息,而这些账户信息在服务器系统中实际上是不存在的,仅供FTP服务程序进行认证使用。这样,即使黑客破解了账户信息也无法登录服务器,从而有效降低了破坏范围和影响。

ftp是Linux系统中以命令行界面的方式来管理FTP传输服务的客户端工具。我们首先手动安装这个ftp客户端工具:

[root@chenstudy ~]# dnf install ftp

匿名访问模式

vsftpd服务程序中,匿名开放模式是最不安全的一种认证模式。任何人都可以无须密码验证而直接登录FTP服务器。这种模式一般用来访问不重要的公开文件(在生产环境中尽量不要存放重要文件)。当然,如果采用第8章中介绍的防火墙管理工具(如TCP Wrapper服务程序)将vsftpd服务程序允许访问的主机范围设置为企业内网,也可以提供基本的安全性。

vsftpd服务程序默认关闭了匿名开放模式,我们需要做的就是开放匿名用户的上传、下载文件的权限,以及让匿名用户创建、删除、更名文件的权限。需要注意的是,针对匿名用户放开这些权限会带来潜在危险,我们只是为了在Linux系统中练习配置vsftpd服务程序而放开了这些权限,不建议在生产环境中如此行事。表11-2罗列了可以向匿名用户开放的权限参数以及作用。

​ 向匿名用户开放的权限参数以及作用

参数

作用

anonymous_enable=YES

允许匿名访问模式

anon_umask=022

匿名用户上传文件的umask值

anon_upload_enable=YES

允许匿名用户上传文件

anon_mkdir_write_enable=YES

允许匿名用户创建目录

anon_other_write_enable=YES

允许匿名用户修改目录名称或删除目录

配置vsftp配置文件:

[root@chenstudy ~]# vim /etc/vsftpd/vsftpd.conf

# 重启vsftp

[root@chenstudy ~]# systemctl restart vsftpd

# 把vsftp加入开机自启动

[root@chenstudy ~]# systemctl enable vsftpd

Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

[root@chenstudy ~]#

在linux中采用匿名访问ftp

[root@chenstudy ~]# ftp 192.168.200.130

Connected to 192.168.200.130 (192.168.200.130).

220 (vsFTPd 3.0.2)

Name (192.168.200.130:root): anonymous

331 Please specify the password.

Password: 敲回车

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> cd pub

250 Directory successfully changed.

ftp> mkdir files

550 Create directory operation failed.

ftp>

# 退出ftp客户端,修改所有者身份

[root@chenstudy ~]# ls -ld /var/ftp/pub

drwxr-xr-x. 2 root root 6 Jun 10 2021 /var/ftp/pub

[root@chenstudy ~]# chown -R ftp /var/ftp/pub

[root@chenstudy ~]# ls -ld /var/ftp/pub

drwxr-xr-x. 2 ftp root 6 Jun 10 2021 /var/ftp/pub

[root@chenstudy ~]#

系统提示“创建目录的操作失败”(Create directory operation failed),我猜应该是SELinux服务在“捣乱”

[root@chenstudy ~]# getsebool -a | grep ftp

ftpd_anon_write --> off

ftpd_connect_all_unreserved --> off

ftpd_connect_db --> off

ftpd_full_access --> off

ftpd_use_cifs --> off

ftpd_use_fusefs --> off

ftpd_use_nfs --> off

ftpd_use_passive_mode --> off

httpd_can_connect_ftp --> off

httpd_enable_ftp_server --> off

tftp_anon_write --> off

tftp_home_dir --> off

[root@chenstudy ~]# setsebool -P ftpd_full_access=on

SELinux域策略就可以顺利执行文件的创建、修改及删除等操作了:

本地用户模式

本地用户模式要更安全,而且配置起来也很简单

本地用户模式使用的权限参数以及作用

参数

作用

anonymous_enable=NO

禁止匿名访问模式

local_enable=YES

允许本地用户模式

write_enable=YES

设置可写权限

local_umask=022

本地用户模式创建文件的umask值

userlist_deny=YES

启用“禁止用户名单”,名单文件为ftpusers和user_list

userlist_enable=YES

开启用户作用名单文件功能

修改vsftp的配置文件:

[root@chenstudy ~]# vim /etc/vsftpd/vsftpd.conf

anonymous_enable=NO

local_enable=YES

write_enable=YES

local_umask=022

dirmessage_enable=YES

xferlog_enable=YES

connect_from_port_20=YES

xferlog_std_format=YES

listen=NO

listen_ipv6=YES

pam_service_name=vsftpd

userlist_enable=YES

tcp_wrappers=YES

在我们输入root管理员的密码之前,就已经被系统拒绝访问了。这是因为vsftpd服务程序所在的目录中默认存放着两个名为“用户名单”的文件(ftpusers和user_list):vsftpd服务程序目录中的这两个文件也有类似的功能—只要里面写有某位用户的名字,就不再允许这位用户登录到FTP服务器上。

[root@chenstudy ~]# cat /etc/vsftpd/user_list

# vsftpd userlist

# If userlist_deny=NO, only allow users in this file

# If userlist_deny=YES (default), never allow users in this file, and

# do not even prompt for a password.

# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers

# for users that are denied.

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

news

uucp

operator

games

nobody

[root@chenstudy ~]# cat /etc/vsftpd/ftpusers

# Users that are not allowed to login via ftp

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

news

uucp

operator

games

nobody

[root@chenstudy ~]#

我们可以使用普通用户登录vsftp服务器:

[root@chenstudy ~]# ftp 192.168.200.130

Connected to 192.168.200.130 (192.168.200.130).

220 (vsFTPd 3.0.2)

Name (192.168.200.130:root): chen

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp>