Centos 7 firewall :

1、firewalld的基本使用

启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld 
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld
 
 

2.systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。

启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

3.配置firewalld-cmd

查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息:  firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
 
那怎么开启一个端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent
 
调整默认策略(默认拒绝所有访问,改成允许所有访问):
firewall-cmd --permanent --zone=public --set-target=ACCEPT
firewall-cmd --reload
对某个IP开放多个端口:
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.159.60.29" port protocol="tcp" port="1:65535" accept"
firewall-cmd --reload
 

Centos 6 iptables:

1、iptables的基本使用

启动: service iptables start
关闭: service iptables stop
查看状态: service iptables status
开机禁用  : chkconfig iptables off
开机启用  : chkconfig iptables on

2、开放指定的端口

-A和-I参数分别为添加到规则末尾和规则最前面。

#允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允许所有本机向外的访问
iptables -P INPUT ACCEPT
iptables -A OUTPUT -j ACCEPT
# 允许访问22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -s 10.159.1.0/24 --dport 22 -j ACCEPT
注:-s后可以跟IP段或指定IP地址
#允许访问80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#允许FTP服务的21和20端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#如果有其他端口的话,规则也类似,稍微修改上述语句就行
#允许ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT #(注意:如果22端口未加入允许规则,SSH链接会直接断开。)
iptables -A FORWARD -j REJECT

3、屏蔽IP

#如果只是想屏蔽IP的话 “3、开放指定的端口” 可以直接跳过。
#屏蔽单个IP的命令是
iptables -I INPUT -s 123.45.6.7 -j DROP
#封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP
#封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP
#封IP段即从123.45.6.1到123.45.6.254的命令是
iptables -I INPUT -s 123.45.6.0/24 -j DROP

4、查看已添加的iptables的规则

iptables -L -n

N:只显示IP地址和端口号,不将IP解析为域名

删除已添加的iptables的规则

将所有iptables以序号标记显示,执行:

iptables -L -n --line-numbers

比如要删除INPUT里序号为8的规则,执行:

iptables -D INPUT 8

5、也可以直接编辑配置文件,添加iptables防火墙规则:

iptables的配置文件为/ etc / sysconfig / iptables

编辑配置文件:

vi /etc/sysconfig/iptables

文件中的配置规则与通过的iptables命令配置,语法相似:

如,通过iptables的命令配置,允许访问80端口:

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

那么,在文件中配置,只需要去掉句首的iptables,添加如下内容:

-A INPUT -p tcp --dport 80 -j ACCEPT

保存退出。

有两种方式添加规则

iptables -A 和iptables -I

iptables -A 添加的规则是添加在最后面。如针对INPUT链增加一条规则,接收从eth0口进入且源地址为192.168.0.0/16网段发往本机的数据。

[root@localhost ~]# iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j ACCEPT

iptables -I 添加的规则默认添加至第一条。

如果要指定插入规则的位置,则使用iptables -I 时指定位置序号即可。

删除规则

如果删除指定则,使用iptables -D命令,命令后可接序号。效果请对比上图。

或iptables -D 接详细定义;

如果想把所有规则都清除掉,可使用iptables -F。

备份iptabes rules

使用iptables-save命令,如:

[root@localhost ~]# iptables-save > /etc/sysconfig/iptables.save

恢复iptables rules

使用iptables命令,如:

[root@localhost ~]# iptables-restore < /etc/sysconfig/iptables.save

iptables 配置保存

以上做的配置修改,在设备重启后,配置将丢失。可使用service iptables save进行保存。

[root@localhost ~]# service iptables save

重启iptables的服务使其生效:

service iptables save   添加规则后保存重启生效。

service iptables restart

后记

关于更多的iptables的使用方法可以执行:

iptables --help

linux防火墙使用以及配置的更多相关文章

  1. Linux防火墙简介 – iptables配置策略

    Linux防火墙简介 – iptables配置策略 Netfilter/iptables简介 要想真正掌握Linux防火墙体系,首先要搞清楚Netfilter和iptables的关系,Netfilte ...

  2. Linux防火墙iptables安装配置--使用远程工具Xmanager和ftp服务安装配置

    一.linux关闭防火墙:    a.用户直接在终端输入:service iptables stop     查看防火墙状况:service iptables status  b.root用户在终端输 ...

  3. Linux防火墙--IPtables企业级配置策略思路

    一.防火墙简介 防火墙定义:是通过有机结合各类用于安全管理与筛选的软件和硬件设备,帮助计算机网络于其内.外网之间构建一道相对隔绝的保护屏障,以保护用户资料与信息安全性的一种技术. 防火墙发展应用:最早 ...

  4. Linux防火墙--iptables--白名单配置

    1.服务器22端口和1521端口开通给指定IP [root@node2 sysconfig]# iptables -t filter -nL INPUT Chain INPUT (policy ACC ...

  5. Linux防火墙firewalld安全设置

    背景描述 防火墙是具有很好的保护作用.入侵者必须首先穿越防火墙的安全防线,才能接触目标计算机.在公司里数据安全是最重要的,要求安全部门进行全公司进行服务器防火墙安全搭建,在原有的基础上进行安全的防火墙 ...

  6. 要想重启后也生效LINUX防火墙配置

    新配置的一台服务器,安装的是CentOS6.3系统,在安装完LNMP之后,发现nginx进程存在,且php解析正常,但是用分配的独立IP去访问的时候发现无法访问. 查了下网上的资料,发现可能是Linu ...

  7. Linux防火墙配置—SNAT2

    1.实验目标 以实验"Linux防火墙配置-SNAT1"为基础,为网关增加外网IP地址,为eth1创建虚拟接口,使外网测试主机在Wireshark中捕获到的地址为eth1虚拟接口的 ...

  8. Linux防火墙配置—允许转发

    一.实验目标 在上一次"Linux基础网络搭建实验"中,内.外网虚拟机之所以能Ping通,是因为暂时关闭了防火墙,然而现实中这样操作显然存在很大的安全隐患,所以本次实验在上次实验的 ...

  9. 安装linux下面用来配置网络,防火墙,系统服务等设置的图形小工具Setup

    Setup命令是linux下面用来配置网络,防火墙,系统服务等设置的图形小工具.使用起来非常方便简单,可是centos\redhat最小化安装之后发现setup命令工具用不了. 接下来介绍如何安装se ...

随机推荐

  1. linux的可中断sleep_on函数分析

    void interruptible_sleep_on (struct task_struct **p)// **p是个全局变量 { struct task_struct *tmp; if (!p)# ...

  2. maven项目里的mapper不被加载,解析

    出现这个错误是因为maven加载配置文件是从resource里加载的,所以要配置一下

  3. springMVC接受json并打开新页面

    背景:框架中,两个web工程A,B,我的B工程开发了一个对外action接口,A来连,要实现的功能是,A的页面发起一个action请求,到达B的springmvc,通过验证后,打开一个B工程新的tab ...

  4. Memcache操作类

    using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.Linq; us ...

  5. Spring Cloud (6)A config 服务端配置 与github通信

    1. 在git上创建一个库,并复制库地址 2.用git clone项目到本地,并创建application.yml application.yml 内容 --下一步 3.将application.ym ...

  6. C++Builder debug 程序的时候 structure required

    C++Builder debug 程序的时候, deub一个变量 dm->avar; E2288 Pointer to structure required on left side of -& ...

  7. Event 对象的属性和方法

    事件触发时,会将一个 Event 对象传递给事件处理程序,比如: document.getElementById("testText").addEventListener(&quo ...

  8. WeakReference与SoftReference

    WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. WeakReference是弱引用,其中保存的对象实例可以被GC回收掉.这个类通常用于在某 ...

  9. 区分slice,splice和split方法

    1.slice(数组) 用法:array.slice(start,end) 解释:该方法是对数组进行部分截取,并返回一个数组副本:参数start是截取的开始数组索引,end参数等于你要取的最后一个字符 ...

  10. LeetCode 题解 Permutation Sequence 需要优化!

    题目大意:给出n和k,找到1..n这些数组成的有序全排列中的第k个. 首先,n的全排列可以分成n组,每一组由n-1个数组成. 例如  3的全排列,分成三组: 1 2 3  和 1 3 2 2 1 3 ...