CentOS配置iptables防火墙

本文详细介绍如何在VPS上安装并配置iptables防火墙,包括基本的安装步骤、查看现有规则、清除规则、添加特定端口和服务的规则,以及如何保存配置使其在重启后依然生效。

这几天在virpus上买了个VPS练练手 先从基础的iptables配置折腾起吧

[root@yip ~]# service iptables status
Table: filter
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
4    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8 
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state ESTABLISHED 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination   

这是配置好的防火墙... 具体配置如下:

首先先确认iptables是安装的:

[root@yip ~]# yum install -y iptables
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.unl.edu
 * extras: centos.mirror.facebook.net
 * updates: mirrors.kernel.org
Setting up Install Process
Package iptables-1.4.7-11.el6.x86_64 already installed and latest version
Nothing to do

如果提示Nothing to do就证明已经安装好了 接下来使用iptables -L -n命令可以获取到目前防火墙设置的规则,就是最上面的代码。如果你想清楚规则,可以使用以下命令:

#首先在清除前要将policy INPUT改成ACCEPT,表示接受一切请求。
#这个一定要先做,不然清空后可能会悲剧
iptables -P INPUT ACCEPT
 
#清空默认所有规则
iptables -F
 
#清空自定义的所有规则
iptables -X
 
#计数器置0
iptables -Z

规则的具体配置如下:

#允许来自于lo接口的数据包
#如果没有此规则,你将不能通过127.0.0.1访问本地服务,例如ping 127.0.0.1
iptables -A INPUT -i lo -j ACCEPT
 
#ssh端口22
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
#FTP端口21
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
 
#web服务端口80
iptables -A INPUT -p tcp --dport 80 -j ACCEP
 
#tomcat
iptables -A INPUT -p tcp --dport xxxx -j ACCEP
 
#mysql
iptables -A INPUT -p tcp --dport xxxx -j ACCEP
 
#允许icmp包通过,也就是允许ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
 
#允许所有对外请求的返回包
#本机对外请求相当于OUTPUT,对于返回数据包必须接收啊,这相当于INPUT了
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
 
#如果要添加内网ip信任(接受其所有TCP请求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
 
#过滤所有非以上规则的请求
iptables -P INPUT DROP

最后保存规则,系统会在/etc/sysconfig/iptables下生成文件:

[root@yip ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

然后设置开机自动启动吧:

chkconfig iptables on

设置参考的是这位仁兄的写的文章:http://www.woxplife.com/articles/404.html 觉得写得蛮清晰的,我比较懒,哈哈。
### 配置 CentOS 7 上的 iptables 防火墙规则 在 CentOS 7 系统中,`iptables` 已被 `firewalld` 取代作为默认防火墙管理工具。然而,仍可通过安装 `iptables-services` 来使用传统的 `iptables` 命令进行配置。 #### 安装与启用 iptables 1. **停止并禁用 firewalld** ```bash systemctl stop firewalld systemctl disable firewalld.service ``` 2. **安装 iptables 及其服务组件** ```bash yum -y install iptables iptables-services ``` 3. **启动 iptables 并设置开机自启** ```bash systemctl start iptables systemctl enable iptables ``` 4. **保存当前配置以确保重启后生效** ```bash service iptables save ``` #### 基础配置命令 - **查看现有规则** ```bash iptables -L -n -v ``` - **清空所有规则(谨慎操作)** ```bash iptables -F ``` - **允许特定端口流量通过** 比如开放 TCP 协议的 80 端口: ```bash iptables -A INPUT -p tcp --dport 80 -j ACCEPT ``` - **限制特定 IP 地址访问** 添加对来自 192.168.10.103 的请求的限制,例如阻止 8601 端口: ```bash iptables -A INPUT -s 192.168.10.103 -p tcp --dport 8601 -j DROP ``` - **允许本地回环接口通信** ```bash iptables -A INPUT -i lo -j ACCEPT ``` - **设置默认策略** 设置默认拒绝所有传入连接,接受所有传出连接: ```bash iptables -P INPUT DROP iptables -P OUTPUT ACCEPT ``` - **保存更改** ```bash service iptables save ``` #### 示例:开放 SSH 访问 为了保证服务器的安全性同时又能远程管理,通常会开放 SSH 服务(默认为 22 端口),可以执行如下命令: ```bash iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT ``` 此命令添加了一条规则,允许新的 SSH 连接进入系统[^1]。 #### 示例:配置 SNAT 和 DNAT 对于需要网络地址转换的情况,比如将内部网络的多个设备映射到一个公网 IP 上,可以配置 SNAT 或者 DNAT 规则: - **SNAT(源地址转换)** ```bash iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE ``` - **DNAT(目标地址转换)** 将外部请求转发至内网某台主机(假设为 192.168.1.100)上的 Web 服务: ```bash iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80 ``` 以上是基本的 `iptables` 配置流程和一些常用示例。实际应用中应根据具体需求调整规则,并测试其有效性以确保网络安全性和功能性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值