CentOS 安装 iptables 防火墙

CentOS 安装 iptables 防火墙

博主博客

CentOS 默认使用的是 firewall 作为防火墙,这里改为iptables防火墙。

一、关闭删除 firewall

# 停止 firewall
systemctl stop firewalld.service
# 取消 firewall开机启动
systemctl disable firewalld.service

二、安装 iptables

# 一键安装 iptables
yum install iptables-services -y
# 设置防火墙开机启动
systemctl enable iptables.service

三、配置

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

下面为默认配置

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

如果想增加规则, 可增加

# 开启 tcp 的 80 端口
-A INPUT -p tcp --dport 80 -j ACCEPT
# 开启 tcp 的 3306 端口
-A INPUT -p tcp --dport 3306 -j ACCEPT

最后 :wq 保存退出, 使用 systemctl restart iptables.service 重启 iptables 使配置生效。

验证防火墙规则是否生效命令:iptables -L

四、注意事项(笔者在上面踩过的坑)

4.1 只允许 IP 1.2.3.4 访问 80 端口, 其他 IP 禁止

# 注意顺序, 先禁止所有, 后对 IP 开放, 网段也一样
-A INPUT -p TCP –dport 80 -j DROP
-A INPUT -s 1.2.3.4 -p TCP –dport 80 -j ACCEPT

4.2 禁止 IP 1.2.3.4 访问, 其他 IP 允许

# 注意顺序, 这里是禁止 1.2.3.4 访问所有端口, 其他 IP 可以访问 80 端口, 网段也一样
-A INPUT -s 1.2.3.4 -j DROP
-A INPUT -p tcp --dport 80 -j ACCEPT

五、命令行操作(当作查表看就行了)

# 添加 Insert
iptables -I ...
# 删除 Delete
iptables -D ...
# 追加 Append
iptables -A ...

5.1 开启端口

# 开启 80 端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
# 只针对 IP 1.2.3.4 开启 80 端口
iptables -I INPUT -s 1.2.3.4 -p tcp --dport 80 -j ACCEPT
# 只针对 网段 1.2.3.0/24 开启 80 端口
iptables -I INPUT -s 1.2.3.0/24 -p tcp --dport 80 -j ACCEPT

5.2 禁止 IP 访问

# 禁止 1.2.3.4 IP 访问
iptables -I INPUT -s 1.2.3.4 -j DROP
# 禁止 1.2.3.0/24 网段访问
iptables -I INPUT -s 1.2.3.0/24 -j DROP

5.3 解封 IP 访问

# 解封 1.2.3.4 IP 访问
iptables -D INPUT -s 1.2.3.4 -j DROP
# 解封 1.2.3.0/24 网段访问
iptables -D INPUT -s 1.2.3.0/24 -j DROP

5.4 禁止服务器访问IP

# 禁止服务器访问 1.2.3.4 IP
iptables -I OUTPUT -d 1.2.3.4 -j DROP
# 禁止服务器访问 1.2.3.0/24 网段
iptables -I OUTPUT -d 1.2.3.0/24 -j DROP

5.5 清除所有规则

# 清空所有的规则
iptables -F

5.6 查看现在的规则

[root@nukixPC ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       195.54.161.0/24      0.0.0.0/0
20855   37M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    1    36 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
   19  1116 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
    9   540 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
   82 11736 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 22160 packets, 42M bytes)
 pkts bytes target     prot opt in     out     source               destination

单独输出可以使用

iptables -vnL INPUT
iptables -vnL FORWARD
iptables -vnL OUTPUT

5.7 保存规则

iptables save

5.8 防止攻击处理

# 处理 IP 碎片数量, 防止攻击, 允许每秒 100 个
iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
# 设置 ICMP 包过滤, 允许每秒 1 个包, 限制触发条件是 10 个包
iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

# 限制 syn 并发数为每秒 1 次
iptables -A INPUT -p tcp --syn -m limit --limit 1/s -j ACCEPT
# 限制 80 端口单个 IP 在 60 秒新建立的连接数为 10
iptables -I INPUT -p tcp --dport 80 --syn -m recent --name SYN_FLOOD --update --seconds 60 --hitcount 10 -j REJECT

5.9 进行端口映射

# 8080 端口映射为 80
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

5.10 查看端口映射

iptables -vnL -t nat
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是在 CentOS安装 iptables 防火墙的步骤: 1. 首先,使用 root 用户登录您的 CentOS 服务器。 2. 打开终端,执行以下命令安装 iptables: ``` yum install iptables-services ``` 3. 安装完成后,启动 iptables 服务: ``` systemctl start iptables ``` 4. 接着,将 iptables 服务设置为开机自启动: ``` systemctl enable iptables ``` 5. 确认 iptables 服务已经启动: ``` systemctl status iptables ``` 如果状态为 active(运行中),则表示 iptables 服务已经成功启动。 6. 配置 iptables 规则: 在 CentOS 上配置 iptables 规则有两种方式,一种是手动配置,另一种是使用防火墙配置工具。 手动配置: 编辑 /etc/sysconfig/iptables 文件,添加相应的规则。例如,开放 SSH 连接的端口: ``` -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT ``` 配置完毕后,保存文件并重启 iptables 服务使配置生效: ``` systemctl restart iptables ``` 防火墙配置工具: CentOS 提供了防火墙配置工具 firewalld,您可以使用以下命令安装: ``` yum install firewalld ``` 安装完成后,启动 firewalld 服务并设置开机自启动: ``` systemctl start firewalld systemctl enable firewalld ``` 然后,使用 firewall-cmd 命令添加防火墙规则。例如,开放 SSH 连接的端口: ``` firewall-cmd --zone=public --add-port=22/tcp --permanent ``` 配置完毕后,重新加载防火墙规则使其生效: ``` firewall-cmd --reload ``` 以上就是在 CentOS安装 iptables 防火墙的步骤,您可以根据需要进行配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nukix

如果觉得对你有帮助,欢迎打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值