CentOS7 下的FTP配置

来源:互联网 发布:2017双十一数据 编辑:程序博客网 时间:2024/05/22 10:49

一、 防火墙设置CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙

1.  安装iptables防火墙

# 先检查是否安装了iptables(如果提示iptables:unrecognized service 则表示系统尚未安装)

service iptables status

# 安装iptables

yum install iptables

# 升级iptables

yum update iptables 

# 安装iptables-services

yum install iptables-services

2. 关闭Firewall

# 停止firewall 服务

systemctl stop firewalld.service         

# 禁用firewall 服务   

systemctl mask firewalld.service         

# 禁止firewall服务开机启动 

systemctl disable firewalld.service      

# 查看firewall服务状态

firewall-cmd --state         

3. 查看Iptables所在目录与配置文件

#安装完成后,配置文件默认位置

/etc/sysconfig/iptables

4. 设置现有规则

# 查看iptables现有规则  

iptables -L -n                  

# 先允许所有,不然有可能会杯具

iptables -P INPUT ACCEPT        

# 清空所有默认规则       

iptables -F      

# 清空所有自定义规则                                        

iptables -X         

# 所有计数器归0                                    

iptables -Z                                             

4.1 iptables 规则设置

# 允许来自于lo接口的数据包(本地访问)

iptables -A INPUT -i lo -j ACCEPT

# 允许本机对外访问

iptables -A OUTPUT -j ACCEPT

# 允许接受本机请求之后的返回数据 RELATED,是为FTP设置的

iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

# 开放22端口(SSH)

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 开放21、20端口(FTP)

iptables -A INPUT -p tcp --dport 21 -j ACCEPT

iptables -A INPUT -p tcp --dport 20 -j ACCEPT

# 开放80端口(HTTP)

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 开放443端口(HTTPS)

iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许ping

iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

# 10060到10090是Vsftpd被动模式需要的端口,可自定义一段大于1024的tcp端口

iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 10060:10090 -j ACCEPT

# 这两条的意思是在INPUT表和FORWARD表中拒绝所有其他不符合上述任何一条规则的数据包。

并且发送一条host prohibited的消息给被拒绝的主机。

iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited

iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited

注:防火墙的规则是从上到下解析的

4.2 保存规则设定

# 保存上述规则

service iptables save

# 查看iptables现有规则

iptables -L -n   

4.3 开启iptables服务 

# 最后重启防火墙使配置生效

systemctl restart iptables.service

# 设置防火墙开机启动

systemctl enable iptables.service

# 查看状态

systemctl status iptables.service

# 确定是否enabled

systemctl list-unit-files | grep iptables


二、 关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing # 注释掉

#SELINUXTYPE=targeted # 注释掉

SELINUX=disabled # 增加

:wq! # 保存退出

setenforce 0 # 使配置立即生效


三、 安装vsftpd 

# 安装vsftpd

yum install -y vsftpd 

yum -y install ftp vsftpd

# 安装vsftpd虚拟用户配置依赖包

yum install -y psmisc net-tools systemd-devel libdb-devel perl-DBI 

# 启动

systemctl start vsftpd.service

# 查看vsftpd服务的状态

systemctl status vsftpd.service

systemctl list-unit-files | grep vsftpd.service

# 设置vsftpd开机启动

systemctl enable vsftpd.service 


四、 配置

# 备份默认配置文件

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf-bak 

# 执行以下命令进行设置
sed -i "s/anonymous_enable=YES/anonymous_enable=NO/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#anon_upload_enable=YES/anon_upload_enable=NO/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#anon_mkdir_write_enable=YES/anon_mkdir_write_enable=YES/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#chown_uploads=YES/chown_uploads=NO/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#async_abor_enable=YES/async_abor_enable=YES/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#ascii_upload_enable=YES/ascii_upload_enable=YES/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#ascii_download_enable=YES/ascii_download_enable=YES/g" '/etc/vsftpd/vsftpd.conf'

sed -i "s/#ftpd_banner=Welcome to blah FTP service./ftpd_banner=Welcome to FTP service./g" '/etc/vsftpd/vsftpd.conf'

echo -e "use_localtime=YES\nlisten_port=21\nchroot_local_user=YES\nidle_session_timeout=300 \

ndata_connection_timeout=1\nguest_enable=YES\nguest_username=vsftpd \

nuser_config_dir=/etc/vsftpd/vconf\nvirtual_use_local_privs=YES \

npasv_min_port=10060\npasv_max_port=10090 \

naccept_timeout=5\nconnect_timeout=1" >> /etc/vsftpd/vsftpd.conf


五、 建立虚拟用户名单文件

touch /etc/vsftpd/virtusers

# 编辑虚拟用户名单文件:(第一行账号,第二行密码,注意:不能使用root做用户名,系统保留)

vi /etc/vsftpd/virtusers

web1

123456

web2

123456

web3

123456


六、 生成虚拟用户数据文件

db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db

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

chmod 600 /etc/vsftpd/virtusers.db 


七、 在/etc/pam.d/vsftpd文件添加以下信息

# 修改前先备份 

cp /etc/pam.d/vsftpd /etc/pam.d/vsftpdbak

# 将auth及account的所有配置行均注释掉

vi /etc/pam.d/vsftpd

auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers

account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers

注:如果系统为32位,上面改为lib,否则配置失败


八、 新建系统用户vsftpd,用户目录为/home/vsftpd, 用户登录终端设为/bin/false(即:使之不能登录系统)

useradd vsftpd -d /home/vsftpd -s /bin/false

chown -R vsftpd:vsftpd /home/vsftpd


九、 建立虚拟用户个人Vsftp的配置文件

mkdir /etc/vsftpd/vconf

cd /etc/vsftpd/vconf

touch web1 web2 web3 # 这里创建三个虚拟用户配置文件

mkdir -p /home/vsftpd/web1/

vi web1 # 编辑用户web1配置文件,其他的跟这个配置文件类似

local_root=/home/vsftpd/web1/

write_enable=YES

anon_world_readable_only=NO

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES


十、 重启vsftpd服务器

systemctl restart vsftpd.service

# 查看vsftpd服务的状态

systemctl status vsftpd.service

systemctl list-unit-files | grep vsftpd

0 0