CentOS 7.0如何安装配置iptables和seLinux以及firewalld

 一、配置防火墙,开启80端口、3306端口

CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

 

2安装iptables防火墙

yum install iptables-services #安装
显示ok以后,就表示已经安装成功,那么就继续执行下面

vi /etc/sysconfig/iptables #编辑防火墙配置文件
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-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 -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
 二、关闭SELINUX

vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出

setenforce 0 #使配置立即生效,如果永远生效的话,直接reboot重启。省略布尔值介绍。。。。
具体介绍:http://cn.linux.vbird.org/linux_basic/0440processcontrol_5.php#selinux_daemon

 

三: firewalld的介绍与简单应用

CentOS7的默认防火墙是firewalld,在之前使用iptables时,关闭了firewalld服务,现在反过来关闭iptables服务,打开firewalld服务:
systemctl disable iptables
systemctl stop iptables
systemctl enable firewalld
systemctl start firewalld

打开firewalld服务之后,iptables相关的命令其实也可以继续使用的,但在CentOS7中不用那么操作,因为firewalld有自己的命令。
firewalld有两个基础概念,zone和service,每个zone里面有不同的iptables规则,默认一共有9个zone,CentOS7默认zone为public,查看系统所有zone,使用命令
firewall-cmd --get-zones                           //查看所有zone
firewall-cmd --get-default-zone                //查看默认zone

9个zone的简单介绍:

drop(丢弃):任何接受的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
block(限制):任何接受的网络连接被IPv4的 icmp-host-prohibited 信息和 IPv6 的icmp6-adm-prohibited 信息所拒绝。
public (公共) :在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收经过选取的连接。
external (外部):特别是为路由器启用了伪装功能的外部网。你不能信任来自网络的其他计算,不能相信它们不会对你的计算机造成危害,只能接收经过选择的连接。
dmz(非军事区):用于的非军事区内的电脑,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接收经过选择的连接。
work(工作):用于工作区。你可以基本相信网络内的其他电脑不会危害你的电脑。仅仅接收经过选择的连接。
home(家庭):用于家庭网络。你可以基本信任网络内的其他计算机不会危害你的计算机。仅仅接收经过选择的连接。
internal(内部):用于内部网络。你可以基本上信任网络内的其他计算机不会威胁你的计算机。仅仅接收经过选择的连接。
trusted(信任):可接受所有的网络连接

几个关于zone的命令 
# firewall-cmd --set-default-zone=work                     //设定默认zone
# firewall-cmd --get-zone-of-interface=ens33           //查看指定网卡
# firewall-cmd --zone=public --add-interface=lo       //给指定网卡设置zone
# firewall-cmd --zone=dmz --change-interface=lo    //针对网卡更改zone
# firewall-cmd --zone=dmz --remove-interface=lo   //针对网卡删除zone
# firewall-cmd --get-zctive-zones                  //查看系统所有网卡所在的zone

fired中的service

service 就是zone下面的一个子单元,每个zone里都使用了不同的service,而service就是针对服务(端口)做的iptables规则。
firewall-cmd --get-services          //查看当前系统所有service
这些service都是由一个个配置文件定义的,配置文件的模版子啊/usr/lib/firewalld/services/目录下,真正生效的配置文件则在/etc/firewalld/services/目录下(默认为空)

关于service的一些操作命令:
# firewall-cmd --get-services                          
 //查看所有的servies
# firewall-cmd --list-services                          
//查看当前zone下有哪些service
# firewall-cmd --zone=public --add-service=http           
//把http服务增加到public zone下面
# firewall-cmd --zone=public --remove-service=http     
//把http服务移除
# ls /usr/lib/firewalld/zones/                                          
 //zone的配置文件模板
# firewall-cmd --zone=public --add-service=http --permanent           
//更改配置文件,否则只是保存在内存中,之后会在/etc/firewalld/zones目录下面生成配置文件
实例:
需求:ftp服务自定义端口1121,需要在work zone下面放行ftp。
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
 vi /etc/firewalld/services/ftp.xml 
修改ftp端口号

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>FTP</short>
  <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="1121"/>       ##修改端口为1121
  <module name="nf_conntrack_ftp"/>
</service>
[root@zlinux ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@zlinux ~]# vi /etc/firewalld/zones/work.xml    //增加ftp相关配置

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Work</short>
  <description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="ftp"/>       ##增加ftp
</zone>
[root@zlinux ~]# firewall-cmd --reload               //重新加载
success
[root@zlinux ~]# firewall-cmd --zone=work --list-services         //查看是否有ftp
ssh dhcpv6-client ftp
firewalld一些常用的语句
查看默认的zone,结果为trusted
firewall-cmd --get-default-zone
public
设置默认zone为trusted
firewall-cmd --set-default-zone=trusted
success
显示当前正在使用的zone消息
firewall-cmd --get-active-zones
trusted
  interfaces: eno16777736
显示系统预定义的zone,默认是9个
firewall-cmd --get-zones
block dmz drop external home internal public trusted work
显示系统预定义的服务名称:
firewall-cmd --get-services
amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https
查询eno16接口和那个zone匹配,网卡与trusted匹配,则该网卡的流量执行eno16中定义的规则,默认允许访问所以的服务:
firewall-cmd --get-zone-of-interface=eno16777736
trusted
显示所以zone及其相对应的规则信息
firewall-cmd --list-all-zone
block
  interfaces: 
  sources: 
  services: 
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
    
dmz
  interfaces: 
  sources: 
  services: ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
.......
在public这个zone中添加允许访问ftp服务的规则
firewall-cmd --add-service=ftp --zone=public
success
从public这个zone中删除允许访问ftp服务的规则
firewall-cmd --remove-service=ftp --zone=public
success
在public这个zone中添加允许访问3306端口的规则
firewall-cmd --add-port=3306/tcp --zone=public
从public这个zone中删除允许访问3306端口的规则
firewall-cmd --remove-port=3306/tcp --zone=public
success
将eno16网卡和public绑定,以后从该接口进入的流量,匹配到public中的规则
firewall-cmd --add-interface=eno16 --zone=public
success
将eno16网卡与public解除绑定
firewall-cmd --remove-interface=eno16 --zone
trusted
将源ip地址1.1.1.1与public绑定,以后该主机访问本机时匹配public中的规则
firewall-cmd --add-source=1.1.1.1 --zone=public
success
在public这个zone中添加一条永久规则(允许访问3306端口),规则重启防火墙后依然生效
firewall-cmd --permanent --add-port=3306/tcp --zone=public
重新加载读取防火墙规则
firewall-cmd --reload

 

转载于:https://www.cnblogs.com/fengzhongzhuzu/p/9087518.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值