40 openEuler搭建FTP服务器-配置vsftpd40.1 vsftpd配置文件介绍40.2 默认配置说明40.3 配置本地时间40.3.1 概述40.3.2 设置方法40.4 配置欢迎信息40.5 配置系统帐号登录权限40.6 验证FTP服务是否搭建成功40.7 配置防火墙
40 openEuler搭建FTP服务器-配置vsftpd
40.1 vsftpd配置文件介绍
用户可以通过修改vsftpd的配置文件,控制用户权限等。vsftpd的主要配置文件和含义如表1所示,用户可以根据需求修改配置文件的内容。更多的配置参数含义可以通过man查看。
表 1 vsftpd配置文件介绍
配置文件 | 含义 |
---|---|
/etc/vsftpd/vsftpd.conf | vsftpd进程的主配置文件,配置内容格式为“参数=参数值”,且参数和参数值不能为空。vsftpd.conf 的详细介绍可以使用如下命令查看:man 5 vsftpd.conf |
/etc/pam.d/vsftpd | PAM(Pluggable Authentication Modules)认证文件,主要用于身份认证和限制一些用户的操作。 |
/etc/vsftpd/ftpusers | 禁用使用vsftpd的用户列表文件。默认情况下,系统帐号也在该文件中,因此系统帐号默认无法使用vsftpd。 |
/etc/vsftpd/user_list | 禁止或允许登录vsftpd服务器的用户列表文件。该文件是否生效,取决于主配置文件vsftpd.conf中的如下参数:userlist_enable:是否启用userlist机制,YES为启用,此时userlist_deny配置有效,NO为禁用。userlist_deny:是否禁止user_list中的用户登录,YES为禁止名单中的用户登录,NO为允许命令中的用户登录。例如userlist_enable=YES,userlist_deny=YES,则user_list中的用户都无法登录。 |
/etc/vsftpd/chroot_list | 是否限制在主目录下的用户列表。该文件默认不存在,需要手动建立。它是主配置文件vsftpd.conf中参数chroot_list_file的参数值。其作用是限制还是允许,取决于主配置文件vsftpd.conf中的如下参数:chroot_local_user:是否将所有用户限制在主目录,YES为启用,NO禁用。chroot_list_enable:是否启用限制用户的名单,YES为启用,NO禁用。例如chroot_local_user=YES,chroot_list_enable=YES,且指定chroot_list_file=/etc/vsftpd/chroot_list时,表示所有用户被限制在其主目录下,而chroot_list中的用户不受限制。 |
/usr/sbin/vsftpd | vsftpd的唯一执行文件。 |
/var/ftp/ | 匿名用户登录的默认根目录,与ftp帐户的用户主目录有关。 |
40.2 默认配置说明
[图片上传失败...(image-7a1e68-1678976002475)] 说明: 文档中的配置内容仅供参考,请用户根据实际情况(例如安全加固需要)进行修改。
openEuler系统中 ,vsftpd默认不开放匿名用户,使用vim命令查看主配置文件,其内容如下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># 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</pre>
其中各参数含义如表2所示。
表 2 参数说明
参数 | 含义 |
---|---|
anonymous_enable | 是否允许匿名用户登录,YES为允许匿名登录,NO为不允许。 |
local_enable | 是否允许本地用户登入,YES 为允许本地用户登入,NO为不允许。 |
write_enable | 是否允许登录用户有写权限,YES为启用上传写入功能,NO为禁用。 |
local_umask | 本地用户新增档案时的umask值。 |
dirmessage_enable | 当用户进入某个目录时,是否显示该目录需要注意的内容,YES为显示注意内容,NO为不显示。 |
xferlog_enable | 是否记录使用者上传与下载文件的操作,YES为记录操作,NO为不记录。 |
connect_from_port_20 | Port模式进行数据传输是否使用端口20,YES为使用端口20,NO为不使用端口20。 |
xferlog_std_format | 传输日志文件是否以标准xferlog格式书写,YES为使用该格式书写,NO为不使用。 |
listen | 设置vsftpd是否以stand alone的方式启动,YES为使用stand alone方式启动,NO为不使用该方式。 |
pam_service_name | 支持PAM模块的管理,配置值为服务名称,例如vsftpd。 |
userlist_enable | 是否支持/etc/vsftpd/user_list文件内的账号登录控制,YES为支持,NO为不支持。 |
tcp_wrappers | 是否支持TCP Wrappers的防火墙机制,YES为支持,NO为不支持。 |
listen_ipv6 | 是否侦听IPv6的FTP请求,YES为侦听,NO为不侦听。listen和listen_ipv6不能同时开启。 |
40.3 配置本地时间
40.3.1 概述
openEuler系统中,vsftpd默认使用GMT时间(格林尼治时间),可能和本地时间不一致,例如GMT时间比北京时间晚8小时,请用户改为本地时间,否则服务器和客户端时间不一致,在上传下载文件时可能引起错误。
40.3.2 设置方法
在root权限下设置vsftpd时间为本地时间的操作步骤如下:
- 打开配置文件vsftpd.conf,将参数use_localtime的参数值改为YES。命令如下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n89" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># vim /etc/vsftpd/vsftpd.conf</pre>
配置内容如下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n91" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">use_localtime=YES</pre>
- 重启vsftpd服务。
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n95" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># systemctl restart vsftpd</pre>
- 设置vsftpd服务开机启动。
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n99" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># systemctl enable vsftpd</pre>
40.4 配置欢迎信息
正常使用vsftpd服务,需要存在欢迎信息文件。在root权限下设置vsftp的欢迎信息welcome.txt文件的操作步骤如下:
- 打开配置文件vsftpd.conf,加入欢迎信息文件配置内容后保存退出。
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n105" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># vim /etc/vsftpd/vsftpd.conf</pre>
需要加入的配置行如下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n107" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">banner_file=/etc/vsftpd/welcome.txt</pre>
- 建立欢迎信息。即打开welcome.txt文件,写入欢迎信息后保存退出。
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="bash" cid="n111" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># vim /etc/vsftpd/welcome.txt</pre>
欢迎信息举例如下:
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="bash" cid="n113" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Welcome to this FTP server!</pre>
40.5 配置系统帐号登录权限
一般情况下,用户需要限制部分帐号的登录权限。用户可根据需要进行配置。
限制系统帐号登录的文件有两个,默认如下:
/etc/vsftpd/ftpusers:受/etc/pam.d/vsftpd文件的设置影响,由PAM模块掌管。
/etc/vsftpd/user_list:由vsftpd.conf的userlist_file设置,由vsftpd主动提供。
两个文件的必须同时存在且内容相同,请参考/etc/passwd文件,将UID小于500的帐号写入这两个文件,每一行代表一个帐号。
如果用户需要限制系统帐号登录,需要在root权限下将对应帐号添加到/etc/vsftpd/ftpusers和/etc/vsftpd/user_list。
打开user_list可以查看当前文件中包含的帐号信息,命令和回显如下:
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="bash" cid="n125" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># vim /etc/vsftpd/user_list
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody</pre>
40.6 验证FTP服务是否搭建成功
可以使用openEuler自带的FTP客户端进行验证。命令和回显如下,根据提示输入用户名(用户为系统中存在的用户)和密码。如果显示Login successful,即说明FTP服务器搭建成功。
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="bash" cid="n129" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220-Welcome to this FTP server!
220
Name (localhost:root): USERNAME
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> bye
221 Goodbye.</pre>
[图片上传失败...(image-662736-1678976002475)] 说明: 如果没有ftp命令,可以在root权限下执行dnf install ftp命令安装后再使用ftp命令。
40.7 配置防火墙
如果要将FTP开放给Internet使用,需要在root权限下对防火墙和SElinux进行设置。
<pre class="md-fences mock-cm md-end-block" spellcheck="false" lang="bash" cid="n135" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"># firewall-cmd --add-service=ftp --permanent
success
firewall-cmd --reload
success
setsebool -P ftpd_full_access on</pre>
👍 点赞,你的认可是我创作的动力!
⭐️ 收藏,你的青睐是我努力的方向!
✏️ 评论,你的意见是我进步的财富!