CentOS配置iptables防火墙

编程入门 行业动态 更新时间:2024-10-13 20:13:44

CentOS配置iptables<a href=https://www.elefans.com/category/jswz/34/1771117.html style=防火墙"/>

CentOS配置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* updates: mirrors.kernel
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

设置参考的是这位仁兄的写的文章:.html 觉得写得蛮清晰的,我比较懒,哈哈。

更多推荐

CentOS配置iptables防火墙

本文发布于:2024-03-12 04:01:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1730649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:防火墙   CentOS   iptables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!