linux iptables:安全应用,防火墙

iptables:安全应用,防火墙
windows和linux都有防火墙,企业的边缘会部署防火墙保证企业内部的局域网是安全的。针对个人电脑会有防火墙保证系统是安全的。

防火墙是唯一通道。

防火墙分类(这里的分类只是一个简单的分类,还有很多种):
1.包过滤防火墙,速度快,但是只检查包头,不检查数据区,不建立连接状态表,安全性能低。
2.应用代理防火墙。安全高,性能高。只检查包头,不检查数据区,建立连接状态表。

防火墙还可以分为硬件和软件防火墙。硬件防火墙就是一个硬件设备,软件防火墙比如windows的360和linux的开源的iptables。

iptables是linux自带的小型的防火墙。

table有4张表,filter过滤表,写的是防火墙的策略。nat表做网络相关的。mangle和raw是内核部分,不用管。
command:过滤表的策略。不允许哪个网卡进去或者出来,
match:匹配条件。匹配数据包来做。
target:匹配的数据包进行操作。就2种可能性,通过和不通过。如果没问题就通过,有问题就不通过。
表
filter表:针对本机,itables的默认表有:input,forward,output链。
nat表:转换数据包的源地址和目的地址。包括:prerouting,postrouting,out链。

https://www.linuxidc.com/Linux/2017-02/140556.htm
以下是centos的:
(ubuntu没有这些命令)
service iptables stop
service iptables start
service iptables restart
root@ubuntu:/# iptables -L  (查看iptables防火墙的默认策略)
Chain INPUT (policy DROP)
target     prot opt source               destination
ufw-before-logging-input  all  --  anywhere             anywhere
ufw-before-input  all  --  anywhere             anywhere
ufw-after-input  all  --  anywhere             anywhere
ufw-after-logging-input  all  --  anywhere             anywhere
ufw-reject-input  all  --  anywhere             anywhere
ufw-track-input  all  --  anywhere             anywhere
Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-ISOLATION  all  --  anywhere             anywhere
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere    
以下是centos的:
vim sysconfig/iptables :防火墙主配置文件,这里面存储的都是防火墙的策略,如果要添加策略,建议通过命令行来添加,而不是修改这个文件来添加。
*filter  : 表示是一个过滤表
:INPUT  ACCEPT  [0:0]   :这3个链都是开的
:FORWARD  ACCEPT  [0:0]
:OUTPUT  ACCEPT  [0:0]
: RH_Firewall-1-INPUT - [0:0]   : RH_Firewall-1-INPUT是防火墙当前策略的一个名字
-A INPUT -j RH_Firewall-1-INPUT
-A FORWARD -j RH_Firewall-1-INPUT
-A RH_Firewall-1-INPUT -p lo -j ACCEPT  (lo同意)
-A RH_Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT  (icmp同意)
-A RH_Firewall-1-INPUT -p 50 -j ACCEPT  (50.51端口可以访问)
-A RH_Firewall-1-INPUT -p 51 -j ACCEPT
-A RH_Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0251 -j ACCEPT  (udp可以访问)
写策略:
查看规则-L:iptables -L ,iptables  -L -n ,iptables -L -n --line-number
修改默认规则-P:iptables -P INPUT DROP (备注:为链设置默认的target可用的是DROP和ACCEPT,作为最后一条规则被执行)
追加规则-A:作为最后一条规则,iptables -A INPUT -p tcp --dport 22 -j ACCEPT
删除规则-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP
修改规则-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT
清空规则-F:iptables -F ,iptables -F INPUT ,iptables -t nat -F
root@ubuntu:/# iptables -L  (默认策略,和iptables配置文件的信息是一致的)
Chain INPUT (policy DROP)
target     prot opt source   destination
ufw-before-logging-input  all  --  anywhere  anywhere
ufw-before-input  all  --  anywhere  anywhere
ufw-after-input  all  --  anywhere   anywhere            

root@ubuntu:/# iptables -L -n (会把目标信息以地址的形式显示出来)

iptables -L -n --line-number  (显示行号,删除规则是基于行号删除的)
INPUT属于进入的概念,iptables -P INPUT DROP,-P是修改规则,规则只有3种inout,output,forward, -P表示针对inout或output或forward进行调整。vim sysconfig/iptables文件里面的:INPUT  ACCEPT  [0:0] :FORWARD  ACCEPT  [0:0]  : OUTPUT  ACCEPT  [0:0] 默认都是ACCEPT的,iptables -P  INPUT  DROP,iptables-save(修改了要保存),这样INPUT就是DROP了。  
root@ubuntu:/# iptables -L
Chain INPUT (policy DROP)  :input链,都进不来
target     prot opt source               destination
ufw-before-logging-input  all  --  anywhere             anywhere    

Chain FORWARD (policy DROP) :forward链,都不能跳转
target     prot opt source               destination
DOCKER-ISOLATION  all  --  anywhere             anywhere
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ufw-before-logging-forward  all  --  anywhere             anywhere 

Chain OUTPUT (policy ACCEPT) :output链,都出得去
target     prot opt source               destination
ufw-before-logging-output  all  --  anywhere             anywhere
ufw-before-output  all  --  anywhere             anywhere     

Chain DOCKER (1 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere            

Chain ufw-after-input (1 references)
target     prot opt source               destination
ufw-skip-to-policy-input  udp  --  anywhere  anywhere   udp dpt:netbios-ns
ufw-skip-to-policy-input  udp  --  anywhere   anywhere  udp dpt:netbios-dgm
ufw-skip-to-policy-input  tcp  --  anywhere    anywhere   tcp dpt:netbios-ssn
Chain是链,input和output是输入和输出,input链是从外面进来到主机这里来,针对进来的信息做什么策略,output链是从主机出去,针对出去的做什么策略,forward是指当要访问资源的时候必须通过我这台计算机转向,A要访问C必须经过B主机,就要通过B主机转向,B直接重定向到C上面去。

input和output是针对当前主机而言的,forward是针对其他电脑的。 iptables -L是打印当前3个链的策略信息。INPUT是DROP表示任何人访问这台电脑都是禁止的,OUTPUT是DROP表示当前电脑不能访问外部。
root@ubuntu:/# iptables -P  INPUT  ACCEPT  (-P修改input链)
root@ubuntu:/# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ufw-before-logging-input  all  --  anywhere             anywhere
ufw-before-input  all  --  anywhere             anywhere            
追加规则:要往input链或者output链或者forward链里面来追加规则。
root@ubuntu:/# iptables -A INPUT -p tcp --dport 22 -j ACCEPT (-p表示要使用tcp相关的协议,)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ufw-before-logging-input  all  --  anywhere             anywhere
7    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh  (通过tcp协议在任意地方连接ssh都是同意)
修改规则-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT
root@ubuntu:/# iptables -R INPUT  7   -s  192.168.0.1     -j  DROP (7表示input链里面的第7行,-s表示不是原来的任意地址了,192.168.0.1进来的要拒绝)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)
num  target     prot opt source               destination
7    DROP       all  --  192.168.0.1          anywhere 
删除规则-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP
root@ubuntu:/# iptables -D INPUT 7  (根据行号删除)
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy DROP)   :就没有7了
num  target     prot opt source               destination  
root@ubuntu:/#  iptables -A INPUT -p  SFTP --dport 22 -j ACCEPT
root@ubuntu:/#  iptables -A INPUT -p  FTP  --dport 21 -j ACCEPT
root@ubuntu:/# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
7    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
8    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ftp

清空规则-F:iptables -F (清空所有链),iptables -F INPUT (清空input链),iptables -t nat -F
匹配条件:是从最大的范围到最小范围划分的。计算机上网是要通过网卡上网的,网卡连接的是网线,出入接口是从网卡出去进来,对应的是input和output链。源地址和目的地址是不允许某个ip进来或者访问某个ip。

-A追加规则,output链,将从网卡eth0出去和进来的都拒绝。
root@ubuntu:/# iptables -A OUTPUT -o ens33 -j DROP
root@ubuntu:/# iptables -L --line-number
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
7    DROP       all  --  anywhere             anywhere
root@ubuntu:/# ping www.baidu.com (出不去了)(删除后就可以ping了)
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted

root@ubuntu:/# iptables -A INPUT -s 192.168.2.114 -j DROP
root@ubuntu:/# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  192.168.2.114        anywhere

root@ubuntu:/# iptables  -A  OUTPUT  -d  www.baidu.com  -j  DROP
root@ubuntu:/# iptables -L
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
7    DROP       all  --  anywhere             163.177.151.110
8  DROP  all  --  anywhere  163.177.151.109    [email protected]:/# ping www.baidu.com
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
root@ubuntu:/# iptables -D OUTPUT 7
root@ubuntu:/# iptables -D OUTPUT 8
iptables: Index of deletion too big.
root@ubuntu:/# iptables -D OUTPUT 7
root@ubuntu:/# ping www.baidu.com
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
64 bytes from 163.177.151.110: icmp_seq=1 ttl=128 time=13.7 ms

-A追加一个规则,来自于192.168.0.1网段的,tcp协议的22端口的,都禁止。

按照ip地址加端口加协议用的比较多。禁网卡用的少。

iptables的匹配规则:

不建议修改配置文件:iptables命令方式改的规则,如果不保存到配置文件,则重启后就没有了,service iptables save就是将命令行方式改的规则保存到配置文件。

linux:如果既有配置文件又有命令参数,如果通过命令方式执行的都是临时的,通过配置文件都是永久的。

原文地址:https://www.cnblogs.com/yaowen/p/8452408.html

时间: 2024-07-31 05:12:10

linux iptables:安全应用,防火墙的相关文章

linux iptables 防火墙设置

1.启动指令:service iptables start 2.重启指令:service iptables restart 3.关闭指令:service iptables stop http://www.linuxidc.com/Linux/2012-03/56066.htm 开启: chkconfig iptables on 关闭: chkconfig iptables off 即时生效,重启后失效: 开启: service iptables start 关闭: service iptable

CentOS Linux iptables 防火墙

快速安装,配置,启动,检查 - 关闭 5002 - 5011 端口开放所有其它 yum install iptables iptables -F iptables -X iptables -Z iptables -A INPUT -i lo -j ACCEPTiptables -A INPUT -p tcp --dport 5002:5011 -j DROP iptables -A INPUT -j ACCEPT iptables -A OUTPUT -j ACCEPT iptables -A

Linux 下的(防火墙)iptables

Linux上的常用的包过滤防火墙叫netfilter,是集成在内核上的,是使用iptables命令对它进行配置管理. 防火墙在做信息包过滤的时候,遵循一套规则,这些规则是存放在专用的信息过滤表中,而这些表都集成在Linux的内核中. netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集. iptables 组件是一种工具,也称为用户空间(userspace),它使插入.修改和除去信息包过滤表中的规则

Linux iptables防火墙添加删除端口

一.  Linux 防火墙的启动和关闭1.1 启动命令[[email protected] ~]# service iptables stopFlushing firewall rules:                                   [  OK  ]Setting chains to policy ACCEPT: filter nat                [  OK  ]Unloading iptables modules:                  

Linux系统中的防火墙的实现:iptables/netfilter

防火墙:包括软件防火墙(基于iptables/netfilter的包过滤防火墙)和硬件防火墙,在主机或网络边缘对经由防火墙的报文以一定条件进行检测过滤的一系列组件. Linux系统中的防火墙的实现:利用iptables/netfilter既可以实现主机防火墙(安全服务范围仅限于当前某台主机),又可以实现网络防火墙(安全服务范围为当前局域网).netfilter:Linux系统内核中防火墙的框架,防火墙功能实现的主体:iptables:为netfilter编写数据传输的匹配规则的用户空间中的应用程

保证Linux系统安全之使用iptables工具管理防火墙

随着企业中使用Linux系统的增加,保证Linux系统安全成为运维人员的必备技能之一. Linux系统本身有很强大的防护措施:防火墙.那么如何管理防火墙就成为重中之重. 随着CentOS 7.CentOS 8的出现,越来越多的人喜欢使用firewalld工具来管理防火墙.因为它不仅可以通过命令行设置,也可以通过图形化设置.关于使用firewalld工具管理Linux防火墙,可参考保证Linux系统安全之使用firewalld工具管理防火墙 下面我们主要介绍iptables工具管理防火墙. 本人第

Linux下开启关闭防火墙

一.Linux下开启/关闭防火墙命令 1) 永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后复原 开启: /etc/init.d/iptables start 关闭: /etc/init.d/iptables stop 需要说明的是对于Linux下的其它服务都可以用以上命令执行开启和关闭操作. 在当开启了防火墙时,做如下设置,开启相关端口, 修改/etc/sysconfig/iptable

Linux开启和关闭防火墙

一.即时生效,重启后失效: 1.启动:service iptables start 2.关闭:service iptables stop Linux开启和关闭防火墙,布布扣,bubuko.com

第7章 Iptables与Firewalld防火墙。

第7章 Iptables与Firewalld防火墙. Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00 104:01 204:13 304:12 402:47 502:57 605:39 702:52 803:55 903:59 1003:59 1102:56 1203:44 1303:19 1403:08 章节简述: 红帽RHEL7系统已经用firewalld服务替代了iptables服务,新的防火墙管理命令firewall

Linux iptables:场景实战一

<Linux iptables:规则原理和基础>和<Linux iptables:规则组成>介绍了iptables的基础及iptables规则的组成,本篇通过实际操作进行iptables应用场景的实际演示. 防火墙设置策略 防火墙的设置策略一般分为两种,一种叫“通”策略,一种叫“堵”策略: 通策略,默认所有数据包是不允许通过的,对于允许的数据包定义规则. 堵策略则是,默认所有数据包是全部允许通过的,对于要拒绝的数据包定义规则. 一般来说服务器的防火墙设置都是采用第一种策略,安全性更