centos7开启iptables
时间: 2023-09-03 09:12:01 浏览: 93
1. 检查iptables是否已安装:
```
sudo yum list installed | grep iptables
```
如果没有安装,则执行以下命令安装:
```
sudo yum install iptables-services
```
2. 启动iptables服务:
```
sudo systemctl start iptables
```
3. 设置iptables开机自启动:
```
sudo systemctl enable iptables
```
4. 配置iptables规则:
编辑`/etc/sysconfig/iptables`文件,添加需要的规则。例如,允许SSH连接:
```
# 允许SSH连接
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
# 允许PING
-A INPUT -p icmp -j ACCEPT
# 允许HTTP和HTTPS
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
# 允许本地回环
-A INPUT -i lo -j ACCEPT
# 其他规则
...
# 默认拒绝所有
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
```
5. 重启iptables服务:
```
sudo systemctl restart iptables
```
注意:在配置iptables规则时,请确保不会阻止您的服务正常运行。最好在测试环境中进行测试,以确保规则的正确性。
阅读全文
相关推荐


















