firewalld使用iptables使用

1 安装firewalld

yum install firewalld firewall-config
systemctl start  firewalld # 启动
systemctl status firewalld # 或者 firewall-cmd --state 查看状态
systemctl disable firewalld # 停止
systemctl stop firewalld  # 禁用

# 关闭服务的方法
# 你也可以关闭目前还不熟悉的FirewallD防火墙,而使用iptables,命令如下:

systemctl stop firewalld
systemctl disable firewalld
yum install iptables-services
systemctl start iptables
systemctl enable iptables

2 使用firewalld

firewall-cmd --state        查看firewall的状态

systemctl status firewalld  查看firewall服务状态

# 查询端口是否开放
firewall-cmd --query-port=8080/tcp

# 开放80端口
firewall-cmd --permanent --add-port=80/tcp

# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp

查看已经开放的所有端口
firewall-cmd --list-ports


#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

用nmap 192.168.4.10 -p 8080 查看端口是否打开。

3 关闭防火墙与selinux关闭

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# systemctl disable firewalld

[root@localhost ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

4 修改防火墙文件和封ip

 vim /etc/firewalld/zones/public.xml

#防火墙修改:开放端口和封ip

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <port protocol="tcp" port="22"/>
  <port protocol="tcp" port="8810-9500"/>
  <rule family="ipv4">
    <source address="27.224.187.19"/>
    <drop/>
  </rule>
</zone>

systemctl restart firewalld

 

5 iptables使用

安装

#先检查是否安装了iptables
service iptables status
#安装iptables
yum install -y iptables
#升级iptables
yum update iptables 
#安装iptables-services
yum install iptables-services
添加
iptables -A INPUT -p tcp --dport 10050 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 10050 -j ACCEPT
删除
iptables -D INPUT -p tcp --dport 10050 -j ACCEPT
iptables -D OUTPUT -p tcp --sport 10050 -j ACCEPT
保存
service iptables save
查看filter列表
iptables -L -n 或 iptables -nL
查看nat表
iptables -nL -t nat

删除所有规则
iptables -F
iptables -X
iptables -Z

service iptables restart
或者在文件里添加
vim /etc/sysconfig/iptables

-A INPUT -p tcp -m tcp --dport 10050 -j ACCEPT

或者

vim /etc/sysconfig/iptables

# Generated by iptables-save v1.4.21 on Mon Nov 16 10:40:03 2020
*filter
:INPUT ACCEPT [3:1840]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [442:1182641]
-A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 25 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 80 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 8898 -j ACCEPT

COMMIT
# Completed on Mon Nov 16 10:40:03 2020
# Generated by iptables-save v1.4.21 on Mon Nov 16 10:40:03 2020
*nat
:PREROUTING ACCEPT [30:3476]
:INPUT ACCEPT [30:3476]
:OUTPUT ACCEPT [25:2693]
:POSTROUTING ACCEPT [25:2693]
-A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source 192.168.0.8
COMMIT
# Completed on Mon Nov 16 10:40:03 2020

 

 

设置客户端主机网关
查看网关:
route -n
设置默认网关:
route add default gw 192.168.1.1
删除默认网关:
route del default gw 192.168.1.1

开启路由功能:
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

防火墙服务的逻辑关系:
​iptables服务–>表–>链–>规则–>处理动作

查看指定表的所有规则
iptables -t filter -nL
iptables -t nat -nL
iptables -t mangle -nL
iptables -t raw -nL
filter表 包过滤功能
nat表 地址转换
mangle表 打标记 
raw表 状态跟踪

规则链之间的流向:
入站:PREROUTING–流向–>INPUT
出站:OUTPUT–流向–>POSTROUTING
转发:PREROUTING–流向–>FORWARD-流向–>POSTROUTING
INPUT链  匹配进入防火墙主机的IP包
OUTPUT链  匹配从防火墙主机出去的IP包
FORWARD链 匹配经过防火墙主机的IP包
PREPROUTING链  匹配路由前的IP包
POSTROUTING链  匹配路由后的IP包

处理动作类型:
ACCEPT  放行
DROP    丢弃
REJECT  拒绝
SNAT    源地址转换
DNAT    目的地址转换
PNAT   端口转换

SNAT:主要用于实现内网客户端访问外部主机时使用(局域网上网用)
定义在POSTROUTING链上
iptables -t nat -A postrouting -s 内部网络地址或主机地址 -j SNAT --to-source NAT服务器上的某外部地址

如:iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 172.20.1.10

MASQUERADE地址伪装(适用于PPPOE拨号上网,假设eth1是出口,#适用于共享动态ip地址上网(如adsl拨号,dhcp获取外网ip))
iptables -t nat -A postrouting -s 内部网络或主机地址 -o eth1 -j MASQUERADE

DNAT:主要用于内部服务器被外网访问(发布服务)
定义在PREROUTING链上
iptables -t nat -A PREROUTING -d NAT服务器的某外部地址 -p 某协议 --dport 某端口 -j DNAT --to-destination 内网服务器地址[:port]

如 iptables -t nat -A PREROUTING -d 10.1.249.125 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.4

 iptables -t filter -F
 iptables -t nat -F




具体分析
https://blog.csdn.net/longbei9029/article/details/53056744
https://blog.csdn.net/chengxuyuanyonghu/article/details/64441374


编写规则
 iptables -t nat -I POSTROUTING -o 外网网卡 -s 内网网段 -j SNAT --to-source 外网ip地址  #适用于外网ip地址固定场景

 iptables -t nat -I POSTROUTING -o 外网网卡 -s 内网网段 -j MASQUERADE  #适用于共享动态ip地址上网(如adsl拨号,dhcp获取外网ip)

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -d 172.18.2.0/24 -j SNAT –to-source 172.18.2.30


iptables常用选项的使用:
添加规则
-A append在链的末尾追加一条规则
-I insert在链的开头(或指定序号)插入一条规则
-R 修改规则,比较麻烦,可以先删除再添加规则
查看规则
-L列出所有的规则条目
-n以数字形式显示地址、端口信息
–line-numbers显示数字行数
-v查看数据量
-Z清空统计的数据量
删除规则
-D删除链内指定序号(或内容)的一条规则
-F删除所有规则
-X删除使用者自定链中的规则
默认策略
-P policy为指定的链设置默认规则,该值只能设置为ACCEPT或者DROP,使用REJECT会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值