linux防火墙使用以及配置

linux防火墙使用以及配置

Centos 7 firewall :

1、firewalld的基本使用

启动: systemctl start firewalld

关闭: systemctl stop firewalld

查看状态: systemctl status firewalld 

开机禁用  : systemctl disable firewalld

开机启用  : systemctl enable firewalld

 

 

内容2:

linux的防火墙管理

oricle-linux7系统后,发现iptables的管理方法有不小的改动,记录一下遇到的问题。

iptables

  • linux系统已经默认安装了iptablesfirewalld两款防火墙管理工具,但是在使用service iptables save命令的时候可能提示找不到命令;另外iptables的配置文件在/etc/sysconfig/iptables,但是新装的服务器可能没有这个文件,一般需要安装iptables-service;
  • 本人习惯使用iptables防火墙工具;

yum -y install iptables-services   # 先更新iptables-services,可以发现在/etc/sysconfig/目录下已经有Iptables文件,同时可以使用service来管理iptables

 

systemctl stop firewalld     # 先关闭防火墙

systemctl start iptables     # 启动防火墙

systemctl enable iptables    # 将防火墙设置成开机自启动

systemctl iptables save      # 将当前配置的防火墙设置保存到/etc/sysconfig/iptables目录下

注意:

  1. firewalldiptables是相互独立的防火墙管理工具;
  2. ‘systemctl iptables save’ 执行时,iptables会去读取/etc/sysconfig/iptables-config文件,然后读取/etc/sysconfig/iptables文件;

扩展

service iptables status  # 查看防火墙的状态

service iptables start   # 开启防火墙

service iptables stop    # 关闭防火墙

service iptables restart # 重启防火墙

  • 配置iptables参数

防火墙需要开放端口给外部访问,设置过滤等。因此需要配置文件,所有的配置保存在/etc/sysconfig/iptables中,有两种方法可以配置:

  • 方法一:直接修改iptables文件

*filter

:INPUT ACCEPT [0:0]   # 允许流量输入

:FORWARD ACCEPT [0:0] # 允许转发

:OUTPUT ACCEPT [0:0]  # 允许流出

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j ACCEPT  # 允许ping

-A INPUT -i lo -j ACCEPT  # 默认允许网卡流入数据

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT  # 添加一条规则,允许22端口

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT  # 提交设置

# 在后面添加相关的命令行

  • 参数详解

-A  :添加一条规则,针对什么数据包,INPUT流入的数据包,OUTPUT流出的数据包,FORWARD转发的数据包

-p  :指定协议,如TCPUDP

–dport:目标端口,当数据从外部进入服务器为目标端口

–sport:源端口,数据从服务器出去,则为数据源端口使用

–j :指定策略规则,ACCEPT(允许接收)DROP(拒绝接收,无响应)REJECT(拒绝接收,有响应)

-s : 指定某一个IP地址的访问,加叹号“!”表示除这个IP

 

-L  查看规则链

-F  清空规则链

-I num  在规则链的头部加入新规则

-D num  删除某一条规则

-d  匹配目标地址

-i 网卡名称 匹配从这块网卡流入的数据

-o 网卡名称 匹配从这块网卡流出的数据

  • 方法二:使用命令行动态修改保存

iptables -P INPUT -j DROP

service iptables save 

注意

  1. 防火墙的规则是按照从上往下依次匹配的,因此一定要把允许动作放到拒绝动作前面,否则所有的流量就将被拒绝掉,从而导致任何主机都无法访问。

firewalld

  • linux的另一款防火墙的管理工具;

service firewalld status  # 查看防火墙的状态

service firewalld start   # 开启防火墙

service firewalld stop    # 关闭防火墙

service firewalld restart # 重启防火墙

  • 管理端口

# 开启一个端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

 

# 重新载入

firewall-cmd --reload

 

# 查看某个端口的状态是否开启

firewall-cmd --zone=public --query-port=80/tcp

# 关闭某个端口

firewall-cmd --zone=public --remove-port=80/tcp --permanent

  • 参数

--permanent : 设置命令永久有效

--add-port : 开启一个端口

--remove-port : 关闭一个端口

使用systemctl

  • systemctl是融合了chkconfigservice的系统管理工具,更加的强大和方便;

systemctl start firewalld    # 开启防火墙

systemctl stop firewalld    # 关闭防火墙,开机启动

systemctl restart firewalld  # 重新启动防火墙

systemctl status firewalld   # 查看防火墙的状态

systemctl disable firewalld  # 永久关闭防火墙,不再开机自启动

systemctl enable firewalld   # 设置防火墙开机自启动

systemctl is-enabled firewalld

 

systemctl start iptables    # 开启防火墙

systemctl stop iptables    # 关闭防火墙,开机启动

systemctl restart iptables  # 重新启动防火墙

systemctl status iptables   # 查看防火墙的状态

systemctl disable iptables  # 永久关闭防火墙,不再开机自启动

systemctl enable iptables   # 设置防火墙开机自启动

策略表

#-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#-A INPUT -p icmp -j ACCEPT

#-A INPUT -i lo -j ACCEPT

#-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

#-A INPUT -j REJECT --reject-with icmp-host-prohibited

#-A FORWARD -j REJECT --reject-with icmp-host-prohibited

#-A OUTPUT -j ACCEPT

#-A INPUT -j REJECT

-A FORWARD -j ACCEPT

-I INPUT -s 2.1.1.7 -j ACCEPT

-A INPUT -i lo -j ACCEPT

-A INPUT -j REJECT

#-A OUTPUT -j ACCEPT

#-A FORWARD -j ACCEPT

 

重启iptables

service iptables restart

 

 

 

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值