1.软文推荐

2.软文推荐

3.软文推荐

centos7如何安装iptables防火墙?本篇文章主要介绍了centos7安装iptables防火墙的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。不仅有安装iptables防火墙的命令和说明,还有如何禁用/停止自带的firewalld服务,起跟随小编过来看看吧

使用背景:

1,CentOS7 默认的防火墙 不是iptables, 而是firewalle.

2,一些软件的端口号要放开来提供服务,如:22,80等常用端口

3,提供web服务的需要

使用步骤:

一、安装centos7,自行百度或者购买阿里云腾讯云易探云等云服务器,选择centos7即可!

二、centos7 基本配置

三、CentOS7安装iptables防火墙:

0,CentOS7默认的防火墙不是iptables,而是firewalle.

查看: 打开/etc/sysconfig/目录后,并未发现iptables文档(可能会有iptables-config文档等,别混淆了)

[root@localhost ~]# cd /etc/sysconfig/

1, 安装 iptable 与 iptable-service

#先检查是否安装了iptables

[root@localhost ~]# service iptables status

#安装iptables

[root@localhost ~]# yum install -y iptables #通常情况下是安装好了的

yum install -y iptables* (网友推荐,看下面的评论,在这里,谢谢他/她)

#升级iptables

[root@localhost ~]# yum update iptables

#安装iptables-services

[root@localhost ~]# yum install iptables-services #通常情况下没有安装

2,禁用/停止自带的firewalld服务

#停止firewalld服务

[root@localhost ~]# systemctl stop firewalld

#禁用firewalld服务

[root@localhost ~]# systemctl mask firewalld

#查看iptables现有规则

[root@localhost ~]# iptables -L -n

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

[root@localhost ~]# iptables -P INPUT ACCEPT

[root@localhost ~]# systemctl enable iptables.service # 设置防火墙开机启动

——————— 启动/关闭/重启 iptables ————————————

[root@localhost ~]# systemctl stop iptables

[root@localhost ~]# systemctl start iptables

[root@localhost ~]# systemctl restart iptables

——————————————————————————————–

# @1,手动添加端口号 (方法一)

[root@localhost ~]# vi /etc/sysconfig/iptables #配置防火墙端口

-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT-A INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT-A INPUT -m state –state NEW -m tcp -p tcp –dport 6379 -j ACCEPT

[root@localhost ~]# service iptables save #保存防火墙配置

–end

################################### 具体配置说明 ######################################

@ 2,命令 添加端口号 ( 方法二 ):

参考:https://www.linuxidc.com/Linux/2017-10/147238.htm

一、安装iptable iptable-service

1,先检查是否安装了iptables

[root@localhost ~]# service iptables status

2,安装iptables

[root@localhost ~]# yum install -y iptables

3,升级iptables

[root@localhost ~]# yum update iptables

4,安装iptables-services

[root@localhost ~]# yum install iptables-services

二、禁用/停止自带的firewalld服务

1,停止firewalld服务

[root@localhost ~]# systemctl stop firewalld

2,禁用firewalld服务

[root@localhost ~]# systemctl mask firewalld

三、设置现有规则

#查看iptables现有规则 [root@localhost ~]# iptables -L -n #先允许所有,不然有可能会杯具 [root@localhost ~]# iptables -P INPUT ACCEPT #清空所有默认规则 [root@localhost ~]# iptables -F #清空所有自定义规则 [root@localhost ~]# iptables -X #所有计数器归0 [root@localhost ~]# iptables -Z #允许来自于lo接口的数据包(本地访问) [root@localhost ~]# iptables -A INPUT -i lo -j ACCEPT #开放22端口 iptables -A INPUT -p tcp –dport 22 -j ACCEPT #开放21端口(FTP) iptables -A INPUT -p tcp –dport 21 -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 #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的 iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT #其他入站一律丢弃 iptables -P INPUT DROP #所有出站一律绿灯 iptables -P OUTPUT ACCEPT #所有转发一律丢弃 iptables -P FORWARD DROP ——————————————————————-

四、其他规则设定

#如果要添加内网ip信任(接受其所有TCP请求) iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT #过滤所有非以上规则的请求 iptables -P INPUT DROP #要封停一个IP,使用下面这条命令: iptables -I INPUT -s ***.***.***.*** -j DROP #要解封一个IP,使用下面这条命令: iptables -D INPUT -s ***.***.***.*** -j DROP

五、保存规则设定

#保存上述规则service iptables save

六、开启iptables服务

#注册iptables服务#相当于以前的chkconfig iptables onsystemctl enable iptables.service#开启服务systemctl start iptables.service#查看状态systemctl status iptables.service

—————————————————————————————————-

问题

问题:解决vsftpd在iptables开启后,无法使用被动模式的问题

1.首先在/etc/sysconfig/iptables-config中修改或者添加以下内容

#添加以下内容,注意顺序不能调换 IPTABLES_MODULES=”ip_conntrack_ftp” IPTABLES_MODULES=”ip_nat_ftp”

2.重新设置iptables设置

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

以下为完整设置脚本

#!/bin/shiptables -P INPUT ACCEPTiptables -Fiptables -Xiptables -Ziptables -A INPUT -i lo -j ACCEPTiptables -A INPUT -p tcp –dport 22 -j ACCEPTiptables -A INPUT -p tcp –dport 21 -j ACCEPTiptables -A INPUT -p tcp –dport 80 -j ACCEPTiptables -A INPUT -p tcp –dport 443 -j ACCEPTiptables -A INPUT -p icmp –icmp-type 8 -j ACCEPTiptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPTiptables -P INPUT DROPiptables -P OUTPUT ACCEPTiptables -P FORWARD DROPservice iptables savesystemctl restart iptables.service

CentOS7

[root@localhost ~]# cat /etc/redhat-release # 查看centos版本

CentOS Linux release 7.2.1511 (Core)

1.关闭firewall

[root@localhost ~]# systemctl stop firewalld.service # 停止firewall

[root@localhost ~]# systemctl disable firewalld.service # 禁止firewall开机启动

2.安装iptables

[root@localhost ~]# yum install iptables-services # 安装

[root@localhost ~]# systemctl restart iptables.service # 重启防火墙使配置生效

[root@localhost ~]# systemctl enable iptables.service # 设置防火墙开机启动

[root@localhost ~]# systemctl disable iptables.service # 禁止防火墙开机启动

CentOS6

[root@localhost ~]# service iptable status –查看防火墙状态

[root@localhost ~]# servcie iptables stop –临时关闭防火墙

[root@localhost ~]# service iptables start –临时启动防火墙

[root@localhost ~]# service iptables restart –重启防火墙

[root@localhost ~]# chkconfig iptables off –永久关闭防火墙

[root@localhost ~]# chkconfig iptables on –永久开启防火墙

本文来源:yuntue.com/post/27040.html,若侵权,请联系删除。

相关文章 8

1

Linux cp 命令的15个示例 – 创建文件和目录的副本 1分钟前

在Linux系统中拷贝文件和目录是一个熟练掌握操作系统的基本指令,本片文章和大家分享一下cp的使用用法。 copy 命令是什么 正如我们在上文...

2

CUBECLOUD:香港云服务器,机房接入联通CUVIP线路,香港原生IP,继续88折优惠中(cube云官网发布 3分钟前

CubeCloud怎么样? CubeCloud家今天宣布香港机房的套餐接入了联通cuvip,原来香港机房的线路为电信CN2 GIA回程,联通和移动走普通直连线路,现...

3

Linux 中批量移动、复制、追加和重命名文件 4分钟前

你可能已经知道,我们使用 mv 命令在类 Unix 操作系统中重命名或者移动文件和目录。 但是,mv 命令不支持一次重命名多个文件。 不用担心...

4

企鹅小屋:韩国CN2、日本CN2独立服务器特价优惠550月,大带宽,不限流量 7分钟前

企鹅小屋小屋成立的时间虽然不长,不过商家的信誉做的比较好,技术也比较不错,现在的线路也比较全面了,机房有中国香港、日本东京...

5

【真实测评】浩航互联日本大阪机房VPS测评数据 9分钟前

今天浩航互联上线了日本大阪机房的机器,目前通过本博客专用优惠码购买可以获得7折优惠,博主拿到了测试机器,给大家做一个测评数据...

6

【不限流量VPS】5每月 2G内存 40G SSD 100M不限流量 俄勒冈州 Mivocloud 11分钟前

Mivocloud是一家来至自摩尔多瓦共和国的主机销售商,主要销售虚拟主机、云服务器及独立服务器,VPS采用KVM虚拟架构,纯SSD硬盘,100M不限流...

7

宁波建设网站需要多少钱?(宁波建设信息网) 13分钟前

首先问建设网站多好少钱这个范围太广了,就像问一个手机多少钱一样,手机有很多种,很多品牌都是需要结合自身的一个需求。需要之后...

8

【黑五】RackNerd:8款特价黑五套餐,美国多地机房可选,512M套餐年付$8.89 15分钟前

RackNerd是一家去年开办的商家,商家很懂中国用户,开业就以低价的套餐打开了局面,相信很多朋友都购买过他家的套餐,商家目前正在不断...