linux防火墙-iptables(上)

简介:

FILTER表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[root@server01 ~] # iptables -t filter -nvL ##查看filter表,主要用于过滤包
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
  pkts bytes target  prot opt  in  out   source     destination
   116  8692 ACCEPT  all  --  *    *  0.0.0.0 /0  0.0.0.0 /0  state RELATED,ESTABLISHED
     0     0 ACCEPT  icmp --  *    *  0.0.0.0 /0  0.0.0.0 /0
     0     0 ACCEPT  all  --  lo   *  0.0.0.0 /0  0.0.0.0 /0
     0     0 ACCEPT  tcp  --  *    *  0.0.0.0 /0  0.0.0.0 /0  state NEW tcp dpt:22
     4   478 REJECT  all  --  *    *  0.0.0.0 /0  0.0.0.0 /0  reject-with icmp-host-prohibited
 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  pkts bytes target  prot opt  in  out  source     destination
     0     0 REJECT  all  --  *    * 0.0.0.0 /0  0.0.0.0 /0   reject-with icmp-host-prohibited
 
Chain OUTPUT (policy ACCEPT 68 packets, 9944 bytes)
  pkts bytes target     prot opt  in      out      source                destination
[root@server01 ~] # iptables -Z  ##清零计数器
[root@server01 ~] # iptables -nvL --line-numbers ##显示行号
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num  pkts bytes target  prot opt  in   out  source      destination
1       6  432  ACCEPT  all  --  *   *   0.0.0.0 /0   0.0.0.0 /0   state RELATED,ESTABLISHED
2       0     0 ACCEPT  icmp --  *   *   0.0.0.0 /0   0.0.0.0 /0
......
  [root@server01 ~] # iptables -F  ##清空规则
[root@server01 ~] # iptables -nvL ##查看iptables规则
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
  pkts bytes target     prot opt  in      out      source                destination 
 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  pkts bytes target     prot opt  in      out      source                destination 
 
Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
  pkts bytes target     prot opt  in      out      source                destination 
[root@server01 ~] # service iptables save  ##保存规则
iptables: Saving firewall rules to  /etc/sysconfig/iptables :[  确定  ]
 
##三种动作:DROP、REJECT、ACCEPT,链默认规则是ACCEPT。
[root@server01 ~] # iptables -A INPUT -s 192.168.111.1 -p tcp --sport 1234 -d 192.168.137.100 --dport 80 -j DROP  ##在下面增加
[root@server01 ~] # iptables -I INPUT -s 192.168.111.2 -p tcp --sport 1234 -d 192.168.137.100 --dport 80 -j DROP  ##在上面增加
[root@server01 ~] # iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
  pkts bytes target prot opt  in  out  source          destination
     0     0 DROP   tcp  --  *    * 192.168.111.2  192.168.137.100  tcp spt:1234 dpt:80
     ......
     0     0 DROP   tcp  --  *    *  192.168.111.1 192.168.137.100  tcp spt:1234 dpt:80
[root@server01 ~] # iptables -D INPUT 1  ##删除INPUT第一行
[root@server01 ~] # iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt  in  out  source      destination
1      353 28859 ACCEPT all  --  *  *   0.0.0.0 /0   0.0.0.0 /0     state RELATED,ESTABLISHED
......
[root@server01 ~] # iptables -I INPUT -s 100.100.100.0/24 -i ens33 -j ACCEPT
[root@server01 ~] # iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt  in      out      source                destination
1        0     0 ACCEPT     all  --  ens33  *       100.100.100.0 /24      0.0.0.0 /0
.......
[root@server01 ~] # iptables -D INPUT -s 100.100.100.0/24 -i ens33 -j ACCEPT
[root@server01 ~] # iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target prot opt  in  out  source      destination
1      626 50787 ACCEPT all  --  *  *   0.0.0.0 /0   0.0.0.0 /0    state RELATED,ESTABLISHED
......
[root@server01 ~] # iptables-save  > 1.ipt    ##将规则重定向到文件中,备份用
[root@server01 ~] # iptables-restore < 1.ipt  ##恢复规则
[root@server01 ~] # service iptables restart  ##重启iptables服务
Redirecting to  /bin/systemctl  restart  iptables.service


在虚拟机网络模式为NAT的情况下,也可以实现物理机和虚机的单向访问:                  

iptables -I INPUT -p icmp --icmp-type 0 -j DROP      // 只有物理机可以ping通虚机

iptables -I INPUT -p icmp --icmp-type 8 -j DROP     // 只有虚机可以ping通物理机 


iptables -P INPUT DROP 将filter表INPUT链的默认规则改成DROP(不要随意更改,会导致无法管理)











本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1946913,如需转载请自行联系原作者


相关文章
|
1月前
|
网络协议 安全 Linux
linux配置防火墙 Centos7下 添加 端口白名单
linux配置防火墙 Centos7下 添加 端口白名单
73 0
|
2月前
|
存储 安全 网络协议
使用 firewall-cmd 管理 Linux 防火墙端口
本文将介绍如何使用 firewall-cmd 工具在 Linux 系统中进行简单端口管理,包括开放、查询、关闭等操作。通过实例展示相关命令的用法,希望能对大家有所帮助。
148 0
|
18天前
|
Linux 网络安全 数据安全/隐私保护
Linux 如何关闭防火墙(开启管理员权限)
Linux 如何关闭防火墙(开启管理员权限)
10 0
|
26天前
|
Linux 网络安全
linux如何关闭防火墙
记住,关闭防火墙可能会降低系统安全性,请在真正需要的情况下关闭,并确保你在网络环境中采取其他安全措施。
20 0
|
2月前
|
Linux 网络安全
Linux(CentOS6.5)开放端口,配置防火墙
Linux(CentOS6.5)开放端口,配置防火墙
23 0
|
4月前
|
Linux 网络安全 网络架构
百度搜索:蓝易云【强大的iptables:解锁Linux网络安全的神器】
总结: iptables是Linux系统中的强大防火墙工具,通过管理网络流量和实施安全策略来保护系统的网络安全。通过配置iptables规则,可以防止未经授权的访问、过滤恶意流量、实现端口转发和网络地址转换等功能。合理使用iptables,可以加强系统的网络安全性和灵活性。
33 0
|
5月前
|
网络协议 Linux 网络安全
linux服务器防火墙的开启及关闭
linux服务器防火墙的开启及关闭
261 1
|
5月前
|
网络协议 Linux 网络安全
Centos7中如何打开和关闭防火墙??CentOS 7以上默认使用firewall作为防火墙改为iptables
Centos7中如何打开和关闭防火墙??CentOS 7以上默认使用firewall作为防火墙改为iptables
|
5月前
|
运维 网络协议 安全
小白带你学习linux的防火墙
小白带你学习linux的防火墙
152 1
|
7月前
|
安全 Linux 网络安全
Linux一些防火墙实战知识
本文介绍了如何在Linux中设置防火墙和开放端口,以提高服务器的安全性。首先,使用firewalld作为防火墙软件包,并确保firewalld服务正在运行。然后,通过添加服务来定义允许的服务端口,可以使用firewall-cmd命令查看当前已定义的服务,并使用firewall-cmd命令添加服务。添加规则后,需要重新加载firewalld配置以使更改生效。在某些情况下,需要打开特定的端口,例如HTTP端口80和HTTPS端口443。可以使用firewall-cmd命令打开端口,并将规则添加到相应的区域。。。确实都是工作中日常中会用到的一些命令