开发者社区> 问答> 正文

云服务器 ECS Linux Centos 7 系统 下iptables 服务应该如何使用


说明:Centos 7 默认的防火墙是 firewall ,鉴于 iptables 使用的比较广,本文简要介绍在 CentOS7.0 系统下 iptables 服务的安装以及相关的使用方法。

禁用 firewall 开机启动



为了防止与 iptables 服务冲突,先禁用 firewall 开机启动:
  • 使用 systemctl status firewalld 查看服务状态,active/inactive表明服务是运行/关闭状态(如下图)
    1. systemctl status firewalld

  • 如果服务是运行状态,先关闭 firewall  服务,命令如下:
    1. systemctl stop firewalld
    停止以后然后执行下面命令禁用
    1. systemctl disable firewalld


安装 iptables


  1. yum install -y iptables-services

出现 complete! 表示安装成功。


启动 iptables


  1. systemctl start iptables



查看 iptables 默认规则


  1. iptables -L


在默认规则下,INTPUT 链中 ACCEPT 来自任何主机的访问。
需要对规则进行修改,步骤如下:
  • 如之前已经设置过规则策略的,将原有的 iptables 文件保存一份,,避免之前设置的策略丢失,命令如下;
    1. cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak

  • 清空服务器上所有的规则
    1. iptables -F
  • 设置 INPUT 方向所有的请求都拒绝,这条策略加上以后所有访问服务器的请求全都会被拒绝掉,如果是线上业务请勿直接操作,会导致业务直接中断:
    1. iptables -P INPUT DROP
  • 放行系统常用的端口,比如 80 和 22 端口,实际的业务使用端口可以根据自己的需求来加,更多的协议放行可以自行再研究:
    1. iptables -I INPUT -p tcp —dport 80 -m state —state NEW -j ACCEPT
    1. iptables -I INPUT -p tcp —dport 22 -m state —state NEW -j ACCEPT
  • 然后使用 iptables -L 查看一下添加的规则是否生效
    1. iptables -L
  • 保存添加的规则
    1. iptables-save > /etc/sysconfig/iptables



设置 iptables 开机启动


  1. systemctl enable iptables.service

操作完成后,重启服务器进行配置验证:
  1. systemctl reboot


如果问题还未能解决,您可以到阿里云社区进行 免费咨询,或联系 云市场商家寻求帮助。

展开
收起
boxti 2017-10-23 14:39:17 3496 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载