加载中…
个人资料
iPan
iPan
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,058,509
  • 关注人气:234
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

centos7搭建FTP服务器

(2023-02-07 12:37:55)
标签:

vsftpd

linux

分类: Linux
CentOS7 Ftp搭建

1 安装vsftpd
yum -y install vsftpd
默认配置文件路径:/etc/vsftpd
rpm -ql vsftpd #看软件安装产生的文件
/etc/vsftpd # 配置文件的目录
/etc/vsftpd/ftpusers # 访问控制(黑名单)
/etc/vsftpd/user_list # 访问控制(黑名单或白名单)
/etc/vsftpd/vsftpd.conf # 配置文件
/var/ftp # 匿名用户的家目录
/var/ftp/pub # 默认上传下载的目录

2 启动vsftpd
systemctl start vsftpd.service

3 开机自启动
systemctl enable vsftpd.service

4 查看服务是否启动
netstat -natup | grep ftp

5 关闭SELinux
修改/etc/selinux/config中SELINUX=disabled
reboot重启服务器;
注意:使用setenforece 0修改模式后ftp可以正常启动,ftp客户端也能正常登录,但是登录后执行命令会遇到“425 Failed to establish connection”错误;

6 创建一个访问ftp的用户
useradd -s /sbin/nologin ftpuser
passwd ftpuser #设置密码

7 创建一个白名单配置
echo "ftpuser" > /etc/vsftpd/chroot_list

8 创建用户配置文件目录
mkdir /etc/vsftpd/userconfig
echo "local_root=/home/ftpuser" > /etc/vsftpd/userconfig/ftpuser #配置用户的根目录

9 修改配置信息
vim /etc/vsftpd/vsftpd.conf
###  一些常用配置:
### 是否允许匿名用户ftp登录,YES代表允许,NO不允许
anonymous_enable=NO
### 是否允许本地登录
local_enable=YES
### 是否有写的权利
write_enable=YES
### 日志文件位置
xferlog_file=/var/log/xferlog 采用标准的日志格式
### 监听端口
listen_port=21
listen=YES
#userlist_deny=NO的时候,在这个文件里面所有的用户都可以使用ftp服务,这个时候文件就从黑名单变成白名单;
userlist_deny=NO 
#有些服务器listen_ipv6=YES的话就无法启动成功
listen_ipv6=NO
#DNS解析关闭,提高链接速度;
reverse_lookup_enable=NO

10 修改/etc/pam.d/vsftpd
将auth required pam_shells.so修改为auth required pam_nologin.so

11 重启服务
systemctl restart vsftpd.service


启动服务命令小结
#开机启动
systemctl enable vsftpd.service
#重启服务
systemctl restart  vsftpd.service 
#启动
systemctl start  vsftpd.service 
#停止
systemctl stop  vsftpd.service 
#状态
systemctl status  vsftpd.service


注意一 虚拟机安装ftp并关闭SELinux后,可能实体机还不能访问虚拟机的端口号,需要配置防火墙或关闭防火墙;
1 开启防火墙
systemctl start firewalld
2 关闭防火墙
systemctl stop firewalld.service
3 查看防火墙开放的端口
firewall-cmd --zone=public --list-ports
4 打开防火墙指定端口,如80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
5 重新加载配置文件
firewall-cmd --reload
6 查看防火墙状态
systemctl status firewalld


附一 FTP客户端安装
yum -y install ftp

常用FTP命令
1、ftp连接服务器
ftp ip
2、查看当前路径
pwd
3、列出当前目录文件
ls / dir
4、返回上一级目录
cdup 
5、切换本地主机的目录,默认回到本地主机家目录
lcd
6、查看本机文件
!ls
7、进入目录
cd
8、显示状态
status
9、创建目录
mkdir
10、下载文件(从远端机器传文件到本机)
get
11、从远端机器传多个文件到本机
mget
12、上传文件(从本机传文件到远端机器)
put
13、从本机传多个文件到远端机器
mput
14、断开连接
quit / bye /exit

注:状态码
230 - 登录成功
200 - 执行命令成功
150 - 开启数据连接端口
250 - 目录切换成功
226 - 关闭数据连接端口


附二 配置文件详解
# 首先备份原始配置文件
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.default

vim /etc/vsftpd/vsftpd.conf
# 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
# 控制是否允许匿名用户登入,YES 为允许匿名登入,NO 为不允许。默认值为YES。
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#控制是否允许本地用户登入,YES 为允许本地用户登入,NO为不允许。默认值为YES。
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#是否允许登陆用户有写权限。属于全局设置,默认值为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
#本地用户新增档案时的umask 值。默认值为077。
# 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=NO
#如果设为YES,则允许匿名登入者有上传文件(非目录)的权限,只有在write_enable=YES时,此项才有效。
#当然,匿名用户必须要有对上层目录的写入权。默认值为NO。
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
#
anon_max_rate=0
#设置匿名登入者使用的最大传输速度,单位为B/s,0 表示不限制速度。默认值为0。
local_max_rate=0
#本地用户使用的最大传输速度,单位为B/s,0 表示不限制速度。预设值为0。
#
# 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
#指定FTP使用20端口进行数据传输,默认值为YES。
#
ftp_data_port=20
#设置在PORT方式下,FTP数据连接使用的端口,默认值为20。
#
#pasv_min_port=9001
#在PASV工作模式下,数据连接可以使用的端口范围的最小端口,0 表示任意端口。默认值为0。
#pasv_max_port=9010
#在PASV工作模式下,数据连接可以使用的端口范围的最大端口,0 表示任意端口。默认值为0。
#
pasv_enable=NO
# 关闭被动
#若设置为YES,则使用PASV工作模式;若设置为NO,则使用PORT模式。默认值为YES,即使用PASV工作模式。
pasv_address=192.168.4.39
pasv_addr_resolve=YES
max_clients=0
# 设置vsftpd允许的最大连接数,默认为0,表示不受限制
max_per_ip=0
# 设置每个IP地址允许与FTP服务器同时建立连接的数目。默认为0,不受限制。

#
# 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
#
log_ftp_protocol=YES
#如果启用此选项,所有的FTP请求和响应都会被记录到日志中,默认日志文件在/var/log/vsftpd.log。
#启用此选项时,xferlog_std_format不能被激活。这个选项有助于调试。默认值为NO。
#
# 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
allow_writeable_chroot=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=YES
#设置vsftpd服务器是否以standalone模式运行。以standalone模式运行是一种较好的方式,此时listen必须设置为YES,此为默认值。
#建议不要更改,有很多与服务器运行相关的配置命令,需要在此模式下才有效。
#若设置为NO,则vsftpd不是以独立的服务运行,要受到xinetd服务的管控,功能上会受到限制。
#
#
listen_address=192.168.4.39
# 设置在指定的IP地址上侦听用户的FTP请求。若不设置,则对服务器所绑定的所有IP地址进行侦听。只有在以standalone模式运行时才有效。 
# 对于只绑定了一个IP地址的服务器,不需要配置该项,默认情况下,配置文件中没有该配置项。
# 若服务器同时绑定了多个IP地址,则应通过该配置项,指定在哪个IP地址上提供FTP服务,即指定FTP服务器所使用的IP地址。
listen_port=21
#设置FTP服务器建立连接所监听的端口,默认值为21。
#
#setproctitle_enable=NO
#设置每个与FTP服务器的连接,是否以不同的进程表现出来。默认值为NO,此时使用ps aux |grep ftp只会有一个vsftpd的进程。
#若设置为YES,则每个连接都会有一个vsftpd的进程。
#
# 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
#是否启用vsftpd.user_list文件。
userlist_deny=NO
#决定vsftpd.user_list文件中的用户是否能够访问FTP服务器。
#若设置为YES,则vsftpd.user_list文件中的用户不允许访问FTP,若设置为NO,则只有vsftpd.user_list文件中的用户才能访问FTP。
#/etc/vsftpd/ftpusers 文件专门用于定义不允许访问FTP服务器的用户列表:
#注意:如果userlist_enable=YES,userlist_deny=NO,此时如果在vsftpd.user_list和ftpusers中都有某个用户时,那么这个用户是不能够访问FTP的,即ftpusers的优先级要高
userlist_file=/etc/vsftpd/user_list
#控制用户访问FTP的文件,里面写着用户名称。一个用户名称一行。
tcp_wrappers=NO
#设置vsftpd是否与tcp wrapper相结合来进行主机的访问控制。默认值为YES。
#如果启用,则vsftpd服务器会检查/etc/hosts.allow 和/etc/hosts.deny 中的设置,来决定请求连接的主机,是否允许访问该FTP服务器。
#这两个文件可以起到简易的防火墙功能。
#比如:若要仅允许192.168.0.1—192.168.0.254的用户可以连接FTP服务器,则在/etc/hosts.allow文件中添加以下内容:
#vsftpd:192.168.0. :allow
#all:all :deny

附三 黑/白名单配置细节
以下是两个选项的具体表现形式和两种搭配使用方式的效果:
1 Userlist_enable=YES
Ftpusers中用户允许访问
User_list中用户允许访问

2 Userlist_enable=NO
Ftpusers中用户禁止访问
User_list中用户允许访问

3 Userlist_deny=YES
Ftpusers中用户禁止访问(登录时可以看到密码输入提示,但仍无法访问)
user_list 中用户禁止访问

4 Userlist_deny=NO
ftpusers中用户禁止访问
user_list中用户允许访问

5 Userlist_enable=YES 并且 Userlist_deny=YES
Ftpusers中用户禁止访问
User_list中用户禁止访问(登录时不会出现密码提示,直接被服务器拒绝)

6 Userlist_enable=YES 并且 Userlist_deny=NO
Ftpusers中用户禁止访问
User_list中用户允许访问

附四 主动/被动模式
主动模式与被动模式区别:
1、PORT(主动)模式
所谓主动模式,指的是FTP服务器“主动”去连接客户端的数据端口来传输数据,其过程具体来说就是:
客户端通过访问服务端的21端口,然后客户端分配一个端口供ftp服务端获取数据,然后服务端通过20端口主动到客户端指定端口获取数据,
20为服务器的出向端口;此模式,防火墙只需要开放21、20端口的对外访问策略;

主动模式配置
port_enable=YES
connect_from_port_20=YES #数据端口是20即主动模式
ftp_data_port=? #如果数据传输端口不想用20把上面的YES改成NO,这里填上你想设置的端口;

2、PASV(被动)模式(默认模式)
所谓被动模式,指的是FTP服务器“被动”等待客户端来连接自己的数据端口,其过程具体是:
客户端通过访问服务端的21端口,然后客户端提交PASV命令,让服务端分配一个用于传输数据的端口,此端口范围为vsftpd.conf配置的
pasv_min_port-pasv_max_port,然后客户端通过分配的端口上传数据。(注意此模式下的FTP服务器不需要开启tcp 20端口)此模式,
防火墙需要开放21端口的对外访问策略和pasv_min_port - pasv_max_port端口范围内的访问策略(大于1024);

被动模式配置
pasv_enable=YES
pasv_min_port=60000
pasv_max_port=60100


虚拟用户的配置,用于针对一个用户做特殊配置;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有