《Linux操作系统实用教程》笔记之防火墙使用

《Linux操作系统实用教程》笔记之防火墙使用

摘要

本文是在WMware上的 CentOS-7-x86_64 计算机上测试的。使用 iptables 的步骤是:

  1. 开启IP转发。

  2. 先用yum安装iptables

    yum install iptables-services

  3. 启动iptables

    systemctl enable iptables #开启启动
    systemctl start iptables.service #重启

    centos7操作iptables命令

    systemctl [start|stop|restart] iptables.service

4.再清空旧规则(依据情况而定)

#清空防火墙规则
iptables -F
#显示防火墙规则
iptables -L

5.添加新规则

Linux防火墙构成

netfilteriptables 构成。

netfilter 组件也叫内核空间( kernelspace ),接收指令和读取防火墙配置文件使配置生效。

iptables 组件也叫用户空间( userspace ),执行防火墙命令或修改配置文件。

个人理解:真正的防火墙是 netfilteriptables 只是一个规则表。

iptables

iptables 构成

iptables 有三张表:

  1. filter 表:不修改数据,只过滤。
  2. nat 表:对数据报的源地址进行转发。
  3. mangle 表:修改数据报。
IP 转发

执行 iptables 前需激活 IP 数据包的转发功能,激活 IP 转发功能的命令如下。

/usr/lib/sysctl.d/50-default.conf 添加配置:

net.ipv4.ip_forward = 1
#sysctl -p  #运行时修改系统内核参数

IP转发配置是否生效,没有检测,但 iptables 生效了。

iptables配置规则

参数很多,先只记主要的。

  1. INPUT:接收。
  2. FORWAR:转发。
  3. OUTPUT:发出。
iptables配置实例
配置方法

亲测可用。

  1. 先用yum安装iptables

    yum install iptables-services

  2. 启动iptables

    systemctl enable iptables #开启启动
    systemctl start iptables.service #重启

    centos7操作iptables命令

    systemctl [start|stop|restart] iptables.service

3.再清空旧规则(依据情况而定)

#清空防火墙规则
iptables -F
#显示防火墙规则
iptables -L

4.添加新规则

#屏蔽 IP 192.168.88.1 的访问 
iptables -I INPUT -s 192.168.88.1 -j DROP
#删掉防火墙规则中 INPUT 为1的那条
iptables -D INPUT 1

在centos7检测结果,不需要重启iptables,防火墙配置就生效了。遇到不生效的情况,试着重启防火墙。

实例
#禁止某个IP访问本机
iptables -I INPUT -s 192.168.88.1 -j DROP
iptables -A INPUT -s 192.168.88.1 -j DROP
#禁止本机访问某个IP
iptables -A OUTPUT -d 192.168.88.1 -j REJECT

还有一些复杂的实例,以后再学习。

错误及解决方法
root@localhost conf]# systemctl restart iptables.service
Failed to issue method call: Unit iptables.service failed to load: No such file or directory.

解决:安装iptables

yum install iptables-services

root@localhost conf]# systemctl restart iptables.service
ln -s '/usr/lib/systemd/system/iptables.service' '/etc/systemd/system/basic.target.wants/iptables.service'

解决:用 systemctl 启动

systemctl restart iptables.service #也可执行,执行后没有任何输出信息

配置之前,先删除旧规则,命令:

iptables -F

参考资料

With RHEL 7 / CentOS 7, firewalld was introduced to manage iptables. IMHO, firewalld is more suited for workstations than for server environments.

It is possible to go back to a more classic iptables setup. First, stop and mask the firewalld service:

systemctl stop firewalld
systemctl mask firewalld

Then, install the iptables-services package:

yum install iptables-services

Enable the service at boot-time:

systemctl enable iptables

Managing the service

systemctl [stop|start|restart] iptables

Saving your firewall rules can be done as follows:

service iptables save

or

/usr/libexec/iptables/iptables.init save

How can i use iptables on centos 7?

疑问

iptables-apply(8), iptables-save(8), iptables-restore(8), iptables-extensions(8)

这些命令的作用是什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值