centos防火墙设置

iptables防火墙和firewalld防火墙

背景

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

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

firewalld防火墙

说明:很多人用CentOS 7时会发现CentOS 6系列中的iptables相关命令不能用了,因为Centos 7使用firewalld代替了原来的iptables。所以iptables相关命令是不能直接使用的,这里说下开放设置端口及firewalld常用命令。

1、关闭防火墙

1
2
systemctl stop firewalld.service           
systemctl disable firewalld.service

2、开启端口

1
firewall-cmd --zone=public --add-port=80/tcp --permanent

命令含义:

1
2
3
--zone #作用域
--add-port=80/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效

3、重启防火墙

1
systemctl restart firewall-cmd 

4. 常用命令介绍

1
2
3
4
5
6
7
8
9
10
11
12
firewall-cmd --state                           ##查看防火墙状态,是否是running
firewall-cmd --reload ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones ##列出支持的zone
firewall-cmd --get-services ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent ##永久添加80端口
iptables -L -n ##查看规则,这个命令是和iptables的相同的
man firewall-cmd ##查看帮助

iptables防火墙常用命令

关闭firewalld

1. 关闭firewalld

1
2
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

2. 安装iptables

1
yum install iptables-services #安装

3. 配置防火墙文件

1
vi /etc/sysconfig/iptables #编辑防火墙配置文件

如下图所示,方框内两条语句分别开启了端口80和3306
具体添加代码:

1
2
-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 80 -j ACCEPT

4. 重启防火墙使配置生效

1
2
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动

iptables命令

1. 开启与关闭

1
2
3
4
5
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
systemctl restart iptables #重启
systemctl start iptables.service #开启iptables防火墙
systemctl stop iptables.service #关闭iptables防火墙

2. 常用操作

1
2
3
service iptables status #查看防火墙状态
yum update iptables #升级
iptables -L -n #查看防火墙现有规则!!

3. 其他

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 #查看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