Linux防火墙(SElinux、netfilter)防火墙工具iptables

Linux防火墙

SElinux防火墙

SElinux是Linux系统特有的安全机制,一般装完系统后都会手动将它关闭;

查询状态

getenforce

Enforcing:为开启状态,Permissive:为临时关闭状态,Disabled:为关闭状态;

[[email protected] ~]# getenforce
Enforcing
[[email protected] ~]#

临时关闭

setenforce 0

[[email protected] ~]# getenforce
Enforcing
[[email protected] ~]# setenforce 0
[[email protected] ~]# getenforce
Permissive
[[email protected] ~]#

永久关闭

配置文件/etc/selinux/config,修改SELINUX=enforcing为SELINUX=disabled
重启生效;

[[email protected] ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[[email protected] ~]# vim /etc/selinux/config
[[email protected] ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[[email protected] ~]#

重启查询,成功关闭

[[email protected] ~]# getenforce
Disabled
[[email protected] ~]#

netfilter防火墙

centos6 5版本使用netfilter防火墙,centos7版本使用为firewalld防火墙,都是用iptables工具;

关闭firewalld防火墙、安装iptables工具

systemctl disable firewalld    //关闭firewalld服务
systemctl stop firewalld        //禁止firewalld开机启动
yum install -y iptables-services    //安装iptables-services
systemctl enable iptables        //让iptables开机启动
systemctl start iptables            //开启iptables

查询iptables默认规则

iptables -nvL

[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
   49  3456 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
   15  1170 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 35 packets, 3216 bytes)
pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

-nvL选项表示查看规则,-F表示临时清除当前规则,-n表示不针对ip反解析主机名,-L表示列出,-v表示列出信息更加详细;
必须使用service iptables save 保存才行,防火墙规则保存在/etc/sysconfig/iptables中;

netfilter的5个表

  • filter:用于过滤包,是系统预设表,最常用的表;有INPUT、OUTPUT、FORWARD等三个链;
  • nat:主要用于网络地址转换;有PREROUTING、OUTPUT、POSTROUTING等三个链;
  • mangle:用来给数据包做标记;
  • raw:实现不追踪某些数据包;
  • security:访问控制MAC列表;

netfilter的5个链

  • PREROUTING:数据包进入路由表之前;
  • INPUT:通过路由表后目的地为本机;
  • FORWARD:通过路由表,目的地部位本机;
  • OUTPUT:有本机产生,向外转发;
  • POSTROUTING:发送到网卡接口之前;

表与链其他详解
http://www.cnblogs.com/metoy/p/4320813.html

iptables基本语法

-A/-D:增加或删除一条规则;
-I:插入一条规则;
-F:清空规则;
-Z:清空计数,重新开始计数;
-t:清空指定表,后面必须带参数表名,-t nat;
-n:不针对ip反解析主机名;
-v:更加详细的信息;
-L:列出,与-v一起使用;
-p:表示指定协议,可以是tcp、udp、icmp;
--dport:跟-p一起使用,表示指定目标端口;
--sport:跟-p一起使用,表示指定源端口;
-s:表示指定源ip(可以是一个网段)
-d:表示指定目的ip(可以是一个网段)
-j:后面跟动作,其中ACCEPT表示允许包、DROP表示丢掉包、REJECT表示拒绝包;
-i:表示指定网卡(不常用);

清空规则

iptables -F
命令清除
service iptables save
保存到文件,重启生效;

[[email protected] ~]# iptables -F
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 10 packets, 740 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
[[email protected] ~]#

清空指定表

iptables -t nat
指定清空nat表,-t 参数就是指定表;
iptables -t nat -nvL 清空nat表,并显示规则;

[[email protected] ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

清空包以及流量计数器归零

iptables -Z

[[email protected] ~]# iptables -F
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 10 packets, 724 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 6 packets, 664 bytes)
pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

增加规则

-A:增加规则
增加指定源ip以及端口拒绝访问目标ip的某端口
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.2 --dport 80 -j DROP
将来源ip 192.168.188.1 的1234端口 访问192.168.188.2 的80端口 拒绝掉

[[email protected] ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.2 --dport 80 -j DROP
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 13 packets, 926 bytes)
pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.2        tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 6 packets, 808 bytes)
pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

插入规则

-I:插入规则
iptables -I INPUT -p tcp --dport 80 -j DROP
将拒绝所有的ip访问本机的80端口

[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 5 packets, 388 bytes)
pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.2        tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 4 packets, 560 bytes)
pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

删除规则

-D:删除
iptables -D INPUT -p tcp --dport 80 -j DROP
删除掉已知道命令的规则

[[email protected] ~]# iptables -D INPUT -p tcp --dport 80 -j DROP
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 5 packets, 388 bytes)
pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.2        tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 4 packets, 560 bytes)
pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

删除未知命令的规则

iptables -nvL --line-number
显示规则的序列号num

[[email protected] ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 85 packets, 6000 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.2        tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 41 packets, 4400 bytes)
num   pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

iptables -D INPUT 1
删除序列号为1的规则

[[email protected] ~]# iptables -D INPUT 1
[[email protected] ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 6 packets, 428 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 4 packets, 480 bytes)
num   pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]#

原文地址:http://blog.51cto.com/shuzonglu/2064720

时间: 2024-08-07 11:41:23

Linux防火墙(SElinux、netfilter)防火墙工具iptables的相关文章

十六、SELINUX、netfilter防火墙以及其iptables工具

SELINUX 关闭selinux有两种方法:暂时关闭selinux防火墙,下次重启后selinux还会开启.#setenforce 0 #getenforce #查看临时关闭selinux的状态命令永久关闭selinux #vi /etc/selinux/config #修改selinux的配置文件 更改"SELINUX=enforcing"为 SELINUX=disabled  保存退出. 此处需要重启.方能改成disabled. [[email protected] ~]# /u

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

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

Linux 防火墙:Netfilter iptables

一.Netfilter 简介 (1) Netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制,而 iptables 只是操作 netfilter 的一个命令行工具(2) Netfilter 是 Linux CentOS 6 内置的防火墙机制,Firewall 是 Linux CentOS 7 内置的防火墙机制,如果想在 CentOS 7 中使用 netfilter 而不是 firewall,操作如下 [[email protected] ~]# systemct

针对Red Hat Enterprise Linux 6.5 的防火墙详细讲解,iptables(netfilter)规则的

防火墙基础 Linux的防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙(或网络层防火墙).基于Linux内核编码实现,具有非常稳定的性能和高效率,因此获得广泛使用. 在Linux系统中,netfilter和iptables都用来指Linux防火墙. netfilter:指的是Linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于"内核态"(Kernel Space,又称为内核空间)的防火墙功能体系. iptables:指的是

Linux防火墙工具iptables基础介绍

iptables基础知识说明: 一.规则链:规则链是防火墙规则/策略的集合 INPUT:处理入站数据包 OUTPUT:处理出站数据包 FORWARD:处理转发数据包 POSTROUTING链:在进行路由选择后处理数据包 PREROUTING链:在进行路由选择前处理数据包 二.规则表:规则表是规则链的集合(优先顺序:raw.mangle.nat.filter) raw表:确定是否对该数据包进行状态跟踪(OUTPUT.PREROUTING) mangle表:为数据包设置标记(PREROUNTING.

Linux 下的(防火墙)iptables

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

Linux笔记基础篇-防火墙,selinux的关闭

时间:2016年10月30号 一般在做Linux的实验的时候,都会把Linux系统的防火墙关闭,避免在做实验时被影响. 系统安装完成后,把iptables防火墙,跟selinux关闭,首先查看防火墙的状态: service iptables status 查看selinux的状态命令: /usr/sbin/sestatus -v 临时关闭防火墙的命令:service iptables stop 永久关闭防火墙的命令:chkconfig  iptables off{需要重启才生效} 永久关闭sel

iptables/netfilter防火墙

既然名曰"防火墙",其对安全的主要性不言而喻,而防火墙分为硬件防火墙和软件防护墙,iptables/netfileter是软件防火墙,它又分为主机防火墙和网络防火墙.至于工作机制说白了就是预先定下规则然后对于进出本网络或主机的数据报文进行规则检测,然后对于满足规则的报文做出规则动作处理.而iptables组件就是在用户空间来让用户操作规则的工具,然后将这些规则放到内核空间的netfilter组件中来让内核对这些报文做指定的处理的(因为网络管理必然是要追根溯源到内核空间中的,那么就需要i

linux的SELinux的设置及防火墙服的设置

security-Enhanced  linux 美国NSA国家局主导开发,一套增强Linux系统安全的强制访问控制体系, 集成到Linux内核(2.6及以上)中运行. RHEL7基于SELinux体系针对用户.进程.目录和文件提供了预设的保护策略, 以及管理工具. SELinux的运行模式 enforcing(强制) permissive(宽松) disabled(彻底禁用) getenforce 查看当前SElinux状态 setenforce  0 或 1  设置当前SELinux状态 永