Centos7的iptables应用

avatar 2021年3月29日18:08:01 评论 750 次浏览

CentOS7默认的防火墙不是iptables,而是firewalle.所以,我们需要重新安装一下,安装后需要启动才能生效,iptables的作用就是限制网络的进出,防止网络攻击以及不正常的请求等等,一般在服务器上都是使用的内网所以限制不是很严格,但是在公网上的服务必须要做iptables。

[root@www.wulaoer.org ~]# systemctl status iptables
Unit iptables.service could not be found.
[root@www.wulaoer.org ~]# yum install -y iptables #安装iptables
[root@www.wulaoer.org ~]# yum update iptables   #升级iptables
[root@www.wulaoer.org ~]# yum install iptables-services #安装iptables-services
[root@www.wulaoer.org ~]# systemctl start iptables.service
[root@www.wulaoer.org ~]# systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: active (exited) since 五 2021-03-19 10:45:56 CST; 10s ago
  Process: 22722 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 22722 (code=exited, status=0/SUCCESS)

3月 19 10:45:56 www.wulaoer.org systemd[1]: Starting IPv4 firewall with iptables...
3月 19 10:45:56 www.wulaoer.org iptables.init[22722]: iptables: Applying firewall rules: [  确定  ]
3月 19 10:45:56 www.wulaoer.org systemd[1]: Started IPv4 firewall with iptables.

上面已经安装好了,也启动了,下面我们就看看iptables的使用吧,不过使用之前我们需要先了解一下iptables的语法,我们先看一下基本的语法

[root@www.wulaoer.org ~]# iptables -L -n    #查看iptables现有规则
[root@www.wulaoer.org ~]# iptables -P INPUT ACCEPT  #先允许所有,不然有可能会杯具
[root@www.wulaoer.org ~]# iptables -F   #清空所有默认规则
[root@www.wulaoer.org ~]# iptables -X	#清空所有自定义规则
[root@www.wulaoer.org ~]# iptables -Z	#所有计数器归0
#允许来自于lo接口的数据包(本地访问)
[root@www.wulaoer.org ~]# iptables -A INPUT -i lo -j ACCEPT
[root@www.wulaoer.org ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT     #开放22端口
[root@www.wulaoer.org ~]# iptables -A INPUT -p tcp --dport 21 -j ACCEPT     #开放21端口(FTP)
[root@www.wulaoer.org ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT     #开放80端口(HTTP)
[root@www.wulaoer.org ~]# iptables -A INPUT -p tcp --dport 443 -j ACCEPT    #开放443端口(HTTPS)
[root@www.wulaoer.org ~]# iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT  #允许ping
[root@www.wulaoer.org ~]# iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT   #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
[root@www.wulaoer.org ~]# iptables -P INPUT DROP  #其他入站一律丢弃
[root@www.wulaoer.org ~]# iptables -P OUTPUT ACCEPT   #所有出站一律绿灯
[root@www.wulaoer.org ~]# iptables -P FORWARD DROP    #所有转发一律丢弃
其他规则设定
[root@www.wulaoer.org ~]# iptables -A INPUT -p tcp -s 10.0.0.1 -j ACCEPT   #如果要添加内网ip信任(接受其所有TCP请求)
[root@www.wulaoer.org ~]# iptables -P INPUT DROP  #过滤所有非以上规则的请求
[root@www.wulaoer.org ~]# iptables -I INPUT -s ***.***.***.*** -j DROP   #要封停一个IP
[root@www.wulaoer.org ~]# iptables -D INPUT -s ***.***.***.*** -j DROP   #要解封一个IP
保存规则设定
[root@www.wulaoer.org ~]# service iptables save     #保存上述规则

以上是使用命令操作,我们也可以在iptables的配置文件中根据自己的需要进行修改,在/etc/sysconfig/iptables-config文件中修改

#添加以下内容,注意顺序不能调换
IPTABLES_MODULES="ip_conntrack_ftp"
IPTABLES_MODULES="ip_nat_ftp"
2.重新设置iptables设置
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

下面是使用脚本添加iptables的规则,和上面一样,

以下为完整设置脚本
#!/bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service

最后,这里要记住,添加规则后一定要保存规则后,重启一下iptables,否则不生效,切记,切记,切记!!!!!!!

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: