防火墙 SELinux,netfilter, iptables,firewalld

10.12- 10.22 SELinux,netfiler5表5链,iptables语法, firewalld的zone和service

SELinux

selinux是Linux特有的安全机制,但是因为配置太麻烦,所以几乎没有人真正的应用它。安装完系统后我们一般会选择关闭selinux。

setenforce 0 //临时关闭,但是重启后会失效

//永久关闭需要更改配置文件 /etc/selinux/config

vim /etc/selinux/config
更改SELINUX=disabled这样就能永久禁用selinux

[root@centos-011 ~]# getenforce
Disabled

netfilter

在centos5和6上用的防火墙是netfiler,其配置工具为iptables。centos7则用的是firewalld防火墙,其配置工具也是iptables。但是现在依然有很多企业使用centos6。

firewalld向下兼容netfilter,所以在firewalld里面也可以用netfilter的设置方法。

[root@centos-011 ~]# systemctl stop firewalld //关闭firewalld服务
[root@centos-011 ~]# systemctl disable firewalld //禁止firewalld开机启动
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

[root@centos-011 ~]# yum install -y iptables-services //安装iptables-services启用之前版本的iptables

[root@centos-011 ~]# systemctl enable iptables //设置开机启动
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@centos-011 ~]# systemctl start iptables //启动iptables服务

iptables已经有默认规则,我们可以用以下命令查看。但是我们在设置自己 的规则之前,建议先将其清除。

iptables命令选项:
-A 在指定链的末尾添加(append)一条新的规则
-D 删除(delete)指定链中的某一条规则,可以按规则序号和内容删除
-I 在指定链中插入(insert)一条新的规则,默认在第一行添加
-R 修改、替换(replace)指定链中的某一条规则,可以按规则序号和内容替换
-L 列出(list)指定链中所有的规则进行查看
-E 重命名用户定义的链,不改变链本身
-F 清空(flush)
-N 新建(new-chain)一条用户自己定义的规则链
-X 删除指定表中用户自定义的规则链(delete-chain)
-P 设置指定链的默认策略(policy)
-Z 将所有表的所有链的字节和数据包计数器清零
-n 使用数字形式(numeric)显示输出结果
-v 查看规则表详细信息(verbose)的信息
-V 查看版本(version)
-h 获取帮助(help)

[root@centos-011 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  125  8368 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
    6   468 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 73 packets, 9072 bytes)
 pkts bytes target     prot opt in     out     source               destination     
[root@centos-011 ~]# iptables -F;service iptables save  //-F清除。service iptables save保存设置。
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

netfilter的5个表和5个链

这里写图片描述

这里写图片描述

5个表:filter,nat,mangle,raw,security。常用的只有前两个。
5个链:prerouting,input,forward,output,postrouting。

filter表主要用于过滤包,用到的链有input,forward,output
nat表主要用于网络地址转换,用到的链有prerouting,postrouting,output

iptables基本语法

[root@centos-011 ~]# iptables -t nat -nvL  // -t后面跟表名,如果不跟则默认指filter表
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        
iptables -F //-F把所有规则全部清除,如果不加-t指定表,默认只清除filter表的规则
iptables -Z //-Z把包和流量计数器清零

增加删除规则的用法

-A 增加一条规则
-D删除一条规则
-I插入一条规则 insert
-p指定协议 protocol,tcp,udp,icmp
–dport 和-p一起使用,指定目标端口destination port
–sport 和-p一起使用,指定源端口source port
-s指定源ip,可以是一个ip段
-d指定目的ip,可以是一个ip段
-j 判断规则judge,后面跟动作ACCEPT REJECT DROP
-i指定网卡 interface

[root@centos-011 ~]# iptables -I INPUT -s 1.1.1.1 -j DROP //丢掉所有来自此ip的数据包
[root@centos-011 ~]# iptables -D INPUT -s 1.1.1.1 -j DROP //-D删除一条规则
[root@centos-011 ~]# iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP //丢掉来自此ip协议为tcp的且到本机80端口的数据包
[root@centos-011 ~]# iptables -I OUTPUT -p tcp --dport 22 -d 10.0.1.14 -j DROP //把发送到此ip 协议为tcp 端口为22的数据包丢掉

[root@centos-011 ~]# iptables -A INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT //来此此ip段且作用在网卡eth0的数据包放行
[root@centos-011 ~]# iptables -nvL --line-numbers //查看现有的iptables规则
Chain INPUT (policy ACCEPT 62 packets, 4092 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       2.2.2.2              0.0.0.0/0            tcp dpt:80
2        0     0 DROP       tcp  --  *      *       2.2.2.2              0.0.0.0/0           
3        0     0 ACCEPT     all  --  eth0   *       192.168.1.0/24       0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 32 packets, 3008 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       0.0.0.0/0            10.0.1.14            tcp dpt:22
[root@centos-011 ~]# iptables -D INPUT 1 //-D删除上面列出的规则,后面跟表名,然后跟行号即可

关于icmp有一个特殊的应用:
iptables -I INPUT -p icmp --icmp-type 8 -j DROP //8指的是本机能ping通其他机器,而其他机器不能ping通本机

nat表的应用
iptables规则如此的强大,可以实现很多功能。路由器共享上网的功能就是通过linux的iptables的nat表实现。

echo "1" > /proc/sys/net/ipv4/ip_forward //echo 1到配置文件打开转发功能
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE //-o表表示出口的网卡,MASQUERADE表示伪装

保存iptables规则
service iptables save 可以把设置保存在文件中永久生效。文件地址为/etc/sysconfig/iptables

service iptables stop 停止防火墙服务,这样防火墙就失效了。但是如果重新设定任意一条规则,防火墙就会重新启用。

firewalld

我们先关闭netfilter然后才能启用firewalld的设置。

[root@centos-011 ~]# systemctl disable iptables //关闭开机启动
[root@centos-011 ~]# systemctl stop iptables //关闭服务
[root@centos-011 ~]# systemctl enable firewalld //打开开机启动
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@centos-011 ~]# systemctl start firewalld //开启服务

[root@centos-011 ~]# iptables -nvL //可以查看现在的iptables规则


[root@centos-011 ~]# firewall-cmd --get-zones //获取有哪些zone
block dmz drop external home internal public trusted work
[root@centos-011 ~]# firewall-cmd --get-default-zone //获取默认zone
public

[root@centos-011 ~]# firewall-cmd --set-default-zone=work //设置默认zone为work
success 
[root@centos-011 ~]# firewall-cmd --get-zone-of-interface=ens33 //查看指定网卡的zone
work
[root@centos-011 ~]# firewall-cmd --zone=public --add-interface=lo //为指定网卡设置zone
success
[root@centos-011 ~]# firewall-cmd --zone=dmz --change-interface=lo //为指定网卡更改zone
success
[root@centos-011 ~]# firewall-cmd --zone=dmz --remove-interface=lo //移除指定网卡的zone
success
[root@centos-011 ~]# firewall-cmd --get-active-zones //查看系统所有网卡各自所在的zone
work
  interfaces: ens33

zone和services
zone相当于模式设置,一个zone里面可以自由的配置多个不同的service,这些services就构成了iptables规则。

以下是zone和service的模板文件位置:
/usr/lib/firewalld/zones
/usr/lib/firewalld/services

以下是zone和service文件生效的位置,在配置之前,我们需要把模板文件拷贝到这里
/etc/firewalld/zones
/etc/firewalld/services

[root@centos-011 ~]# firewall-cmd --get-service //列出当前系统所有的services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp gang

[root@centos-011 ~]# firewall-cmd --list-services  //查看当前zone的服务
ssh dhcpv6-client
[root@centos-011 ~]# firewall-cmd --zone=public --list-services  //查看指定zone的服务
dhcpv6-client ssh


[root@centos-011 ]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services  //复制模板
[root@centos-011 ]# vi /etc/firewalld/services/ftp.xml //编辑ftp配置文件

[root@centos-011 ]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones  //复制模板
[root@centos-011 ]# vim /etc/firewalld/zones/work.xml  /编辑work zone的配置文件
[root@centos-011 ]# firewall-cmd --reload //重新加载
success
[root@centos-011 services]# firewall-cmd --zone=work --list-services  //查看指定zone的服务
ssh ftp dhcpv6-client
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值