Linux 安装FTP

CentOs7 使用yum命令安装FTP

查看是否安装ftp

rpm -qa | grep vsftpd

如果现实ftp,则进行下面命令卸载

rpm -e  vsftpd-*

一、使用以下命令安装Ftp(在线)

#安装
yum install -y vsftpd

一、离线安装ftp(离线)

首先下载vsftp的rpm包

http://rpmfind.net/linux/rpm2html/search.php?query=vsftpd%28x86-64%29&submit=Search+...&system=&arch=

 

然后执行安装命令

rpm -ivh vsftpd-3.0.2-27.el7.x86_64.rpm 

这样就安装在/etc/vsftp下面了

 

二、配置FTP

默认安装配置文件流路径为:/etc/vsftpd/vsftpd.conf

然后我们打开该配置文件 vi vsftpd.conf

修改内容如下:

 

: set nu

 

复制代码
# 第一处 12行左右
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).--是否允许匿名访问
anonymous_enable=NO


#第二处  33行左右
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.  -- 匿名用户登录否创建新目录。
anon_mkdir_write_enable=YES


#第三处  48行左右
# If you want, you can arrange for uploaded anonymous files to be owned by
chown_uploads=YES



#第四处 修改72行   异步中断启用??
async_abor_enable=YES


#第五处 修改83行
ascii_upload_enable=YES


#第六处 修改84行
ascii_download_enable=YES


#第七处 修改87行
ftpd_banner=Welcome to blah FTP service.



#第八处 修改101行   --是否将所有用户限制在主目录,YES为启用 NO禁用.
hroot_local_user=YES

# 然后再文件的最后添加如下配置:

# 使用本地时间
use_localtime=YES
# 监听端口
listen_port=21
# 空闲回话超时时间 单位秒
idle_session_timeout=300
# 来宾启用 
guest_enable=YES
# 来宾用户名称
guest_username=vsftpd
# 用户配置文件
user_config_dir=/etc/vsftpd/uconf
# 连接超时时间  单位秒
data_connection_timeout=100
# ???
virtual_use_local_privs=YES
# pasv模式下指定端口范围 最小和最大
pasv_min_port=40000
pasv_max_port=40010
# 接受超时 次数?
accept_timeout=5
# 连接超时 单位秒
connect_timeout=100
# 允许写入根目录
allow_writeable_chroot=YES
复制代码

然后保存退出

修改后的文件如下:

复制代码
# 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.
# When SELinux is enforcing check for SE bool ftp_home_dir
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)
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 !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES


# 使用本地时间
use_localtime=YES
# 监听端口
listen_port=21
# 空闲回话超时时间
idle_session_timeout=300
# 来宾启用 
guest_enable=YES
# 来宾用户名称
guest_username=vsftpd
# 用户配置文件
user_config_dir=/etc/vsftpd/uconf
# 连接超时时间
data_connection_timeout=1
# ???
virtual_use_local_privs=YES
# pasv模式下指定端口范围 最小和最大
pasv_min_port=40000
pasv_max_port=40010
# 接受超时 次数?
accept_timeout=5
# 连接超时
connect_timeout=1
# 允许写入根目录
allow_writeable_chroot=YES
vsftp.conf
复制代码

 

 

复制代码
#是否启用匿名用户
anonymous_enable=NO
local_enable=YES
#全局设置,是否容许写入(无论是匿名用户还是本地用户,若要启用上传权限的话,就要开启它)
write_enable=YES
#本地用户上传文件的umask
local_umask=022
#控制匿名用户对文件夹的创建权限
anon_mkdir_write_enable=YES
#允许为目录配置显示信息,显示每个目录下面的message_file文件的内容
dirmessage_enable=YES
#开启日记功能
xferlog_enable=YES
#使用标准格式
xferlog_std_format=YES
dual_log_enable=YES
xferlog_file=/var/log/xferlog
#使用标准的20端口来连接ftp
connect_from_port_20=YES
#所有匿名上传的文件的所属用户将会被更改成chown_username
chown_uploads=YES
chown_username=vsftpd

ftpd_banner=Welcome to blah FTP service.
listen=NO
listen_ipv6=YES
#设置一个本地用户登录后进入到的目录
local_root=/data/ftp
#是否禁止用户离开设置的根目录
chroot_local_user=YES
pam_service_name=vsftpd
#限制了/etc/vsftpd/user_list文件里的用户不能访问
userlist_enable=YES
#开启tcp_wrappers支持
tcp_wrappers=YES
#显示目录清单时是用本地时间还是GMT时间,可以通过mdtm命令来达到一样的效果
use_localtime=YES
listen_port=21
#(用户会话空闲后600秒断)
idle_session_timeout=600
#启用虚拟用户。默认值为NO。
guest_enable=YES
guest_username=vsftpd
#定义用户配置文件的目录
user_config_dir=/etc/vsftpd/uconf
#(将数据连接空闲120秒断)
data_connection_timeout=120
virtual_use_local_privs=YES
pasv_min_port=50000
pasv_max_port=53000
#PASV请求超时
accept_timeout=60
#PROT模式连接超时
connect_timeout=100
allow_writeable_chroot=YES
vsftpd.conf 新
复制代码

记得添加:pasv_enable=YES 

 

 

 

三、建立用户文件并生成 并 生成用户数据文件

#创建编辑用户文件
vim /etc/vsftpd/ftpuser
#第一行为用户名,第二行为密码。不能使用root作为用户名 

wanghao
wanghao
db_load -T -t hash -f /etc/vsftpd/ftpuser /etc/vsftpd/ftpuser.db

#设定PAM验证文件,并指定对虚拟用户数据库文件进行读取

chmod 600 /etc/vsftpd/ftpuser.db 

四、修改 /etc/pam.d/vsftpd 文件

复制代码
# 修改前先备份 

cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak

vi /etc/pam.d/vsftpd
#先将配置文件中原有的 auth 及 account 的所有配置行均注释掉
auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/ftpuser
account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/ftpuser

# 如果系统为32位,上面改为lib
复制代码

五、新建系统用户vsftpd,用户目录为/home/vsftpd

#用户登录终端设为/bin/false(即:使之不能登录系统)
useradd vsftpd -d /home/vsftpd -s /bin/false
chown -R vsftpd:vsftpd /home/vsftpd

建立虚拟用户个人配置文件

复制代码
mkdir /etc/vsftpd/uconf
cd /etc/vsftpd/uconf

#这里建立虚拟用户wanghao配置文件
touch wanghao

#编辑wanghao用户配置文件,内容如下,其他用户类似
vi wanghao

local_root=/home/vsftpd/wanghao/
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

#建立wanghao用户根目录
mkdir -p /home/vsftpd/wanghao/
复制代码

然后我们在/home/vsftpd/wanghao 下面创建一个index.html 测试文件

六、防火墙设置

复制代码
# IPtables 的设置方式:
vi /etc/sysconfig/iptables
#编辑iptables文件,添加如下内容,开启21端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 40000:40010 -j ACCEPT


# firewall 的设置方式:
firewall-cmd --zone=public --add-service=ftp --permanent

firewall-cmd --zone=public --add-port=21/tcp --permanent
firewall-cmd --zone=public --add-port=40000-40010/tcp --permanent 
复制代码

根据自己服务器选择不同的方式。我这里使用的是Firewalls的设置方式

七、启动ftp服务器

复制代码
#设置开机启动
systemctl enable vsftpd.service

#启动
systemctl start vsftpd.service

#停止
systemctl stop vsftpd.service

#重启
systemctl restart vsftpd.service


#查看状态
systemctl status vsftpd.service





另一种
service vsftpd restart

service vsftpd status
复制代码

然后启动ftp服务器, 注:我这边启动ftp服务器后访问不了,重启了linux服务器重新打开ftp服务器即可。

ftp://192.168.200.129/   ftp:// ip

然后访问浏览器:提示需要登录账号密码

使用IE浏览器登录之后就如下所示:

 

 使用谷歌浏览器登陆之后如下图所示:

 

 时间不对是因为,我用linux服务器创建的文件夹。我的linux服务器的时间不对如下图所示:

修改时间后现在时间就正确了

修改系统时间直通车

 

八、使用FTP工具连接FTP

写好ip 端口,与用户名密码,即可登录

 

至此简单的CentOs 7安装ftp服务器已经成功。

 

九、添加新用户   未成功,只有刚初始化的时候创建了两个用户 成功,后面添加的时候未成功。TODO:后面研究

 1、建立用户文件并生成 并 生成用户数据文件

 用户文件是在一起的,所以要重新生成用户文件覆盖之前的  之前创建了wanghao,现在添加wanghao2 wanghao3

vim /etc/vsftpd/ftpuser
#第一行为用户名,第二行为密码。不能使用root作为用户名  可以看到我创建了三个用户 wanghao  wanghao2  wanghao3 
wanghao
wanghao
wanghao2
wanghao2
wanghao3
wanghao3

 

# 重新生成db文件 覆盖之前的  --更新用户
db_load -T -t hash -f /etc/vsftpd/ftpuser /etc/vsftpd/ftpuser.db

#设定PAM验证文件,并指定对虚拟用户数据库文件进行读取

chmod 600 /etc/vsftpd/ftpuser.db 

 建立虚拟用户个人配置文件

复制代码
# 因为上面的已经创建了uconf文件夹所以新添加用户配置直接进入
cd /etc/vsftpd/uconf

touch wanghao2 wanghao3

# 依次编辑 新添加的文件
local_root=/home/vsftpd/wanghao2/
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES


#建立新用户用户根目录
mkdir -p /home/vsftpd/wanghao2/

mkdir -p /home/vsftpd/wanghao3/
复制代码

然后我们依次在wanghao里面放个index.html,在wanghao2里面放index22222.html 在wanghao3里面放index333333.html 以便进入测试

然后重启ftp

 

#启动
systemctl start vsftpd.service

#停止
systemctl stop vsftpd.service

#重启
systemctl restart vsftpd.service

 

 

 

 

 

 

 

 

 

 


 

以下我没有遇到

10、使用ftp工具连接测试

这个时候,使用ftp的工具连接时,我们发现是可以连接的。传输文件的时候,会发现文件上传和下载都会出现

500、503 、200等问题。这个时候,可以进行以下操作:

方式一、关闭SELINUX

复制代码
复制代码
#打开SELINUX配置文件
vim /etc/selinux/config


#修改配置参数
#注释  
SELINUX=enforcing

#增加  
SELINUX=disabled


#修改完成后,需要重启!
复制代码
复制代码

方式二、修改SELINUX

复制代码
复制代码
setenforce 0 #暂时让SELinux进入Permissive模式


#列出与ftp相关的设置
getsebool -a|grep ftp


#以下是显示出来的权限,off是关闭权限,on是打开权限。不同的机器显示的可能不一样。我看了我的显示的,和网上其他教程就不太一样
ftp_home_dir --> off
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
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off


#将包含有 ftp_home_dir 和 ftpd_full_access 相关的都设置为 1

setsebool -P ftp_home_dir 1
setsebool -P allow_ftpd_anon_write 1 setsebool -P ftp_home_dir 1 setenforce 1 #进入Enforcing模式
复制代码
复制代码

方式三、 SELINUX不对vsftp不做任何限制

setsebool -P ftpd_connect_all_unreserved 1

 

这个时候再使用工具连接,你发现,就可以正常的上传和下载文件了。

 

如果还是有问题尝试给我们用户的ftp目录,设置一下操作权限

chmod -R 775 /home/vsftpd/leo

 下面是网友遇到的错误总结:

问题一:

LZ:我很负责任的说一句:我非常认真的按照上文做到了第10条后,Windows和IE都可开启FTP,登陆系统用户还是FTP临时用户都是无法登陆进去!/home/vsftpd/leo权限改为777 也是一样,DOS登陆开始很愉快的进入!输入完用户名 报 331错误,登陆密码后报530错误,用ftp工具登陆直接500错误!
另外:
vsftpd.conf内的
guest_enable=YES guest_username=vsftpd 若不注解开放的话MS-DOS和Centos7本机登陆都是500错误
我系统的内核版本是:Kernel 3.10.0-862.el7.x86_64 on an x86_64
还有很重要的一点:关于IPtables 的设置方式:
系统中没有 /etc/sysconfig/iptables文件 只有 iptables-config
所以我也是单条命令执行的!
关于:SELINUX 我是直接关了执行SELINUX=disabled 也一样没用
没有别的我也不知哪里出问题了所以请赐教如何配置才能访问了!

回答一:

谢谢!问题已解决!一点小小的教训仅供参考,之前一直500和530错误是应为我把第6条的当做Centos7中的新添加user用户了!这条用户命令用户是不能随意修改的,所以连不上了,LZ整段信息已经很详细了!修正一点小细节,IPTable还是要逐条手工添加!所以可以在第8条中注明一下,没有IPTable的处理方法,关于第6条一定要重点强调一下,【我就是被上面第3条的leo用户给误导了!导致第6条的vsftpd用户擅自做了改动,所以无法登陆!】
另外在补充一点,在需要添加第二个ftp虚拟用户时在第3条中添加完后,一定要重复做第4条和第7条,其中第7条的mkdir /etc/vsftpd/vconf这个命令可以不写,应为已经有目录了!
还有权限用户目录权限一定要是777 我试过775 无法写入可以读!
最后说一下SELINUX 直接用第10条第一种方法即可,没什么必要搞得更美国情报局似的严格,一、犯不上,二、费神!
PS:终结!LZ文本基本完整,操作时需要仔细阅读#字注解!非常感谢,虚拟机搞瘫了4次才悟道上述一点心得!互相学习!若还有更好的优化请指教!

 

感谢 @寒@鹏 总结

useradd vsftpd -d /home/vsftpd (这个目录你可以改) -s /bin/false
chown -R vsftpd:vsftpd /home/vsftpd (这个目录你可以改)
---------------------------------------------------------------------------------------------
useradd vsftpd 这是不能动的 ,本来以为是 FTP登录的用户名但不是哦
----------------------------------------------------------------------------------------------

mkdir /etc/vsftpd/vconf
cd /etc/vsftpd/vconf
-------------------------------------------------------------------------------------------

#这里建立虚拟用户leo配置文件
touch leo(这里才是登录用户)

#编辑leo用户配置文件,内容如下,其他用户类似
vi leo

local_root=/home/vsftpd/leo/ (对于的FTP目录,随便改和上面一致)
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

#建立leo用户根目录
mkdir -p /home/vsftpd/leo/ (对于的FTP目录,随便改和上面一致)


搞半天 我把 useradd vsftpd -d /home/vsftpd 弄成 useradd 登录用户 -d /home/vsftpd 结果没有结果根本不行

我做完这就可以了,防火墙提前弄好的

 

 

/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

db_load -T -t hash -f /etc/vsftpd/vuser_list /etc/vsftpd/vuser.db

自己遇到的问题汇总

问题一,安装成功后其他服务器wget失败,可以登录并下载文件 ,如下图所示:

 

 

1
2
3
4
5
6
7
8
9
10
--2020-09-28 18:08:36--  ftp://jwztftphd:*password*@121.41.39.238:2121/test
  (try: 2) => ‘test.1’
Connecting to 121.41.39.238:2121... connected.
Logging in as jwztftphd ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD not needed.
==> SIZE test ... 12
==> PASV ... done.    ==> RETR test ...
Error in server response, closing control connection.
Retrying.

 抓包并追踪后发现这个错误:

 

 

425 Security: Bad IP connecting.

 

1.#vim /etc/vsftpd/vsftpd.conf
2.添加:pasv_promiscuous=YES
3.保存后退出
4.重启vsftpd #service vsftpd restart

参考链接:https://blog.csdn.net/qq_15127715/article/details/69055099 

 

参考文档:

https://www.cnblogs.com/leoxuan/p/8329998.html

 

 

 

问题二 出现500 OOPS: could not bind listening IPv4 socket的原因和解决办法

  因为xinetd已经启动了vsftpd,而vsftpd设置成了stardalone模式,所以,先停止xinetd服务, service xinetd stop,然后再重启vsftpd,启动后再重新连接。

 

posted @   苦心明  阅读(2749)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示