Centos 7如何设置防火墙

一、概述

  不熟悉centos 7 中的firewalld怎么办啊,没关系,iptables在centos 7 中照样适用。

二、开始设置

  先看一下是否安装了iptables的相关服务

1
2
3
4
5
6
#先检查是否安装了iptables
service iptables status
#安装iptables
yum install iptables
#安装iptables-services
yum install iptables-services

  下面禁用firewalld的相关服务

1
2
3
4
#停止firewalld服务
systemctl stop firewalld
#禁用firewalld服务
systemctl mask firewalld

  名场面开始了

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
#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT
# 允许访问22端口
iptables -A INPUT -s x.x.x.x/y  -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s x.x.x.x/y  -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s x.x.x.x/y -p tcp --dport 22 -j ACCEPT       
放开整个地址段
iptables -A INPUT -s x.x.x.x/y -j ACCEPT
iptables -A INPUT -s x.x.x.x/y -j ACCEPT
# 允许访问53端口
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT --reject-with icmp-net-unreachable
iptables -A FORWARD -j REJECT --reject-with icmp-net-unreachable

  添加后,进行保存

1
service iptables save

  剩下就可以试验一下了,以上只是最基本的,进阶的操作还需要多多学习。

  下面这个文章是有关 使用iptables进行NAT转发 的,可以借鉴下。

 

iptables设置中的lo是什么意思?

iptables设置中的lo是什么意思?
如题
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
其中的lo是什么?

 

lo 是 loopback 的缩写,也就是环回的意思,linux系统默认会有一块名为 lo 的环回网络接口.而你真正的网卡一般则被linux系统识别成名为 eth0,eth1 这样的网络接口.
一般,lo接口对应的ip地址为 127.0.0.1,IP地址可通过ifconfig来查看.
当你从一台linux主机向自身发送数据包时,实际上的数据包是通过虚拟的lo接口来发送接受的,而不会通过你的物理网卡 eth0/eth1....
比如 你的主机名为 myworkstation,那么通过 ping/telnet/ssh 去访问 myworkstation,那么收发的数据包都是通过 lo接口的.同理,访问 localhost 或者 127.0.0.1 也是一样的效果.
iptables -A INPUT -i lo -j ACCEPT 添加iptables规则,允许来自于lo接口的数据包
iptables -A OUTPUT -o lo -j ACCEPT 添加iptables规则,允许向lo接口发送数据包

 

posted @ 2020-10-03 11:33  ArongH  阅读(140)  评论(0)    收藏  举报