Linux防火墙 iptables的用法

主要了解防火墙的基本了解和iptables的使用

一、使用

(常用的参数、命令要记住)

1.iptable参数:

-A:添加一条规则,默认加在最后。
-I :添加规则默认最前
-F:清除规则
-j:加动作
-p 协议
–dport 目的端口 (拒绝别人链接我)
–sport 源端口 (拒绝我链接别人)
-s 源地址,可以是IP地址,SS也可以是网段“192.168.109.10/24”“-s 为空,表示拒绝所有
-d :目的地址
-i:网卡名(报文流入的接口)
-o :网卡名(报文流出的接口)
-m 模块

Usage(用法)
iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options] 追加
iptables -R chain rulenum rule-specification [options] 修改
iptables -D chain rulenum [options] 删除
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options] 修改默认规则
iptables -h (print this help information)

两个修改规则的用法:

①:iptables -t 表名 -R 链名 规则序号 规则原本的匹配条件 -j 动作

[root@localhost ~]# iptables -t filter -R INPUT 1 -s 192.168.1.51 -j ACCEPT

②:iptables -t 表名 -P 链名 动作

[root@localhost ~]# iptables -t filter -P FORWARD ACCEPT

2.iptables基本用法:

iptables -t 表 -A 链 匹配的条件 -j 动作

[root@localhost ~]# iptables -t filter -nL   //列出规则
[root@localhost ~]# iptables -t filter -D INPUT -p tcp --dport 80 -s 192.168.42.61 -j ACCEPT

-i用法:
增加网卡
:
在这里插入图片描述
在这里插入图片描述
-m:指明要调用的扩展模块机制
常用的扩展匹配条件:

-p tcp -m mutiport --sport用于匹配报文的源端口可以指定离散的多个端口,之间用逗号隔开
-p tcp -m mutiport --sport用于匹配报文的目标端口可以指定离散的多个端口,之间用逗号隔开

(1)容许其他服务访问本机的9000到10000端口。( -p tcp -m tcp :指定连续端口)

[root@localhost ~]# iptables -t filter -I INPUT -p tcp -m tcp --dport9000:10000 -j ACCEPT

(9)禁止访问本地21端口和80到500之间的端口。(-p tcp -m multiport :指定多端口)

[root@localhost ~]# iptables -t filter -I INPUT -p tcp -m multiport --dport 21,80:500 -j REJECT

基本用法:
-t :指定要操作的表,默认filter表
-L:列出规则,在-L后加上链名时表示查看指定链中的规则
-n:显示出IP地址
-x:显示计算器的精确值
–line-number:显示规则序号
一般用法:

 [root@localhost ~]# iptables  --line-number -t filter -nvxL  OUTPUT

规则保存:

[root@localhost sysconfig]# service iptables save  保存规则到文件/etc/sysconfig/iptables
[root@localhost sysconfig]# iptables-save  > /root/iptables  #导出iptables模板
[root@localhost ~]# iptables-restore < iptables               #导入iptables模板 

3.Iptables规则配置

(①注意:一般我们所见到的这些连接,没有特殊说明就是tcp协议,说的也就是80端口,一般80端口就是给web服务的;协议和端口一起指定)
Centos7 :(要安装iptables,因为centos7服务改成了firewall)
安装iptables-service : #yum install iptables-services
停止firewalld: #systemctl stop firewalld
禁止firewalld自动启动 #systemctl disable firewalld
启动iptables: #systemctl start iptables
将iptables设为开机启动 #systemctl enable iptables

4.iptables基本练习:

(1)将来自于192.168.42.61的所有请求drop

[root@localhost ~]# iptables -t filter -I INPUT -s 192.168.42.61 -j DROP

(2)将来自于192.168.42.61的80请求drop

[root@localhost ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 192.168.42.61 -j ACCEPT

防火墙的运行顺序是从上往下一次匹配

(3)添加多个地址

[root@localhost ~]# iptables -t filter -I INPUT -s 192.168.42.61,192.168.42.128 -j DROP

(4)删除规则

[root@localhost ~]# iptables -t filter -D INPUT 2

(5)容许192.168.42.0/24所有网段访问80端口,容许192.168.42.1访问sshd服务。其他任何请求都拒绝
“白名单” —调整默认规则,一般情况不要调整默认规则。

[root@localhost sysconfig]# iptables -t filter -I INPUT -p tcp --dport 22 -s 192.168.42.1 -j ACCEPT
[root@localhost sysconfig]# iptables -t filter -I INPUT -p tcp --dport 80 -s 192.168.42.0/24 -j ACCEPT >>>>
[root@localhost sysconfig]# iptables -t filter -A INPUT -j REJECT

(6)容许该机器访问192.168.42.0/24网段的机器上的httpd服务。

[root@localhost sysconfig]# iptables -t filter -I INPUT -p tcp --dport 22 -s 192.168.42.1 -j ACCEPT
[root@localhost sysconfig]# iptables -t filter -I INPUT -p tcp --dport 80 -s 192.168.42.0/24 -j ACCEPT
[root@localhost ~]# iptables -t filter -I INPUT 2 -p tcp --sport 80 -d 192.168.42.0/24 -j ACCEPT
[root@localhost sysconfig]# iptables -t filter -A INPUT -j REJECT
[root@localhost ~]# iptables -t filter -I INPUT 3 -p tcp --dport 80 -s 192.168.42.61 -i ens32 -j ACCEPT

(7)容许其他服务访问本机的9000到10000端口。

[root@localhost ~]# iptables -t filter -I INPUT -p tcp -m tcp --dport 9000:10000 -j ACCEPT

(8)禁止访问本地的11211(memcache)以及21(ftp)服务。

[root@localhost ~]# iptables -t filter -I INPUT -p tcp -m multiport --dport 11211,21 -j REJECT

(9)禁止访问本地21端口和80到500之间的端口。

[root@localhost ~]# iptables -t filter -I INPUT -p tcp -m multiport --dport 21,80:500 -j REJECT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值