基础运维:iptables防火墙入门到掌握

一、简介

IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。
防火墙在做数据包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的数据包过滤表中,而这些表集成在 Linux 内核中。在数据包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfilter/iptables IP 数据包过滤系统是一款功能强大的工具,可用于添加、编辑和移除规则。

虽然 netfilter/iptables IP 信息包过滤系统被称为单个实体,但它实际上由两个组件netfilter 和 iptables 组成。
netfilter 组件也称为内核空间(kernelspace),是内核的一部分,由一些信息包过滤表组成,这些表包含内核用来控制信息包过滤处理的规则集。

iptables 组件是一种工具,也称为用户空间(userspace),它使插入、修改和除去信息包过滤表中的规则变得容易。除非您正在使用 Red Hat Linux 7.1 或更高版本,否则需要下载该工具并安装使用它。

二、IPtables结构

Linux下的iptables表结构

三、基础iptables

Linux centos7.X中iptables地位变低了,改成firewall。但日常使用的都是iptables。

1.安装iptables防火墙服务
[[email protected] ~]# yum install -y iptables

2.查找系统关于iptables防火墙的文件或者目录

[[email protected] ~]# find / -name iptables*    ( * 代表任意长度字符串)
/etc/sysconfig/iptables-config      #iptables防火墙的配置文件
/etc/selinux/targeted/active/modules/100/iptables       #selinux下的防护规则目录
/usr/bin/iptables-xml       #iptables的二进制xml文件
/usr/sbin/iptables-restore      #iptables的管理命令,重置或恢复iptables规则命令
/usr/sbin/iptables-save     #iptables的管理命令,保存iptables规则命令
/usr/sbin/iptables          #iptables管理命令,设置iptables规则命令
/usr/share/doc/iptables-1.4.21      #iptables的目录(带版本)
/usr/share/man/man1/iptables-xml.1.gz   #对应上面文件或命令的压缩软件包
/usr/share/man/man8/iptables-extensions.8.gz    #对应上面文件或命令的压缩软件包
/usr/share/man/man8/iptables-restore.8.gz       #对应上面文件或命令的压缩软件包
/usr/share/man/man8/iptables-save.8.gz      #对应上面文件或命令的压缩软件包
/usr/share/man/man8/iptables.8.gz       #对应上面文件或命令的压缩软件包
[[email protected] ~]# 

3.减少实验障碍操作(非生产环境)

[[email protected] ~]# iptables -F       #清除iptables规则链中已有的条目
[[email protected] ~]# iptables -X       #清除iptables自定义的规则链
[[email protected] ~]# iptables -Z       #清除iptables规则链中的数据包计算器和字节计算器(归零)

温馨提醒:该操作能在实验中少犯错,降低入门的门槛,在运维进阶中会详细说明各个规则及生产用处。

4.设置iptables规则链

4.1 官方设置说明:

[[email protected] ~]# iptables --help
iptables v1.4.21

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain        Append to chain
  --check   -C chain        Check for the existence of a rule
  --delete  -D chain        Delete matching rule from chain
  --delete  -D chain rulenum
                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                Print the rules in a chain or all chains
  --flush   -F [chain]      Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                Zero counters in chain or all chains
  --new     -N chain        Create a new user-defined chain
  --delete-chain
            -X [chain]      Delete a user-defined chain
  --policy  -P chain target
                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                Change chain name, (moving any references)
Options:
    --ipv4  -4      Nothing (line is ignored by ip6tables-restore)
    --ipv6  -6      Error (line is ignored by iptables-restore)
[!] --protocol  -p proto    protocol: by number or name, eg. `tcp‘
[!] --source    -s address[/mask][...]
                source specification
[!] --destination -d address[/mask][...]
                destination specification
[!] --in-interface -i input name[+]
                network interface name ([+] for wildcard)
 --jump -j target
                target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match   -m match
                extended match (may load extension)
  --numeric -n      numeric output of addresses and ports
[!] --out-interface -o output name[+]
                network interface name ([+] for wildcard)
  --table   -t table    table to manipulate (default: `filter‘)
  --verbose -v      verbose mode
  --wait    -w [seconds]    maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]    wait time to try to acquire xtables lock
                default is 1 second
  --line-numbers        print line numbers when listing
  --exact   -x      expand numbers (display exact values)
[!] --fragment  -f      match second or further fragments only
  --modprobe=<command>      try to insert modules using this command
  --set-counters PKTS BYTES set the counter during insert/append
[!] --version   -V      print package version.

4.2 通俗(常用)设置说明:

iptables(选项)(参数)
-t<表>:指定要操纵的表;
-A:向规则链中添加条目;
-D:从规则链中删除条目;
-i:向规则链中插入条目;
-R:替换规则链中的条目;
-L:显示规则链中已有的条目;
-F:清楚规则链中已有的条目;
-Z:清空规则链中的数据包计算器和字节计数器;
-N:创建新的用户自定义规则链;
-P:定义规则链中的默认目标;
-h:显示帮助信息;
-p:指定要匹配的数据包协议类型;
-s:指定要匹配的数据包源ip地址;
-j<目标>:指定要跳转的目标;
-i<网络接口>:指定数据包进入本机的网络接口;
-o<网络接口>:指定数据包要离开本机所使用的网络接口。

5.规则链设置

5.1 设置说明

iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作

举个例子:放行22端口,暴露在互联网中,任意地址都能通过22端口连接到服务器
解释:只需要在入站规则设置就好(入站就是进入服务器,出站就是从服务器出去)

[[email protected] ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[[email protected] ~]# iptables -I INPUT -p tcp --dport 22  -j ACCEPT
[[email protected] ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

5.2 显示规则详情

[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 3 packets, 234 bytes)
 pkts bytes target     prot opt in     out     source               destination
  128  9372 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

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

Chain OUTPUT (policy ACCEPT 90 packets, 14552 bytes)
 pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]# 

6.进阶iptables防火墙规则
这一部分有点复杂,放到运维进阶中,有兴趣可以关注动态,按照全栈运维路线更新。

原文地址:http://blog.51cto.com/leoheng/2162158

时间: 2024-08-25 11:51:56

基础运维:iptables防火墙入门到掌握的相关文章

基础运维:SELINUX入门到掌握

一.介绍 1.官方意思SELinux是一种基于 域-类型 模型(domain-type)的强制访问控制(MAC)安全系统,它由NSA编写并设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SELinux的补丁,最后还有一个相应的安全策略.任何程序对其资源享有完全的控制权.假设某个程序打算把含有潜在重要信息的文件扔到/tmp目录下,那么在DAC情况下没人能阻止他.SELinux提供了比传统的UNIX权限更好的访问控制. 2.简单意思selinux就像是一种系统的保护机制,如果关闭它的话,

基础运维:shell入门

一.系统变量 在命令行提示符直接执行env.set查看系统或环境变量.env显示用户环境变量,set显示Shell预先定义好的变量以及用户变量.可以通过export导出成用户变量. [[email protected] ~]# env XDG_SESSION_ID=1 HOSTNAME=leo SELINUX_ROLE_REQUESTED= TERM=xterm SHELL=/bin/bash HISTSIZE=1000 SSH_CLIENT=192.168.116.1 61498 22 SEL

如果你还是基础运维,那么请仔细看看。

如果你还是基础运维,只会配配网卡,                                  只会调调时间,                                  只会安装mysql,                                  只会搭建web,                                  只会写写shell. 工作上,            只会\cp,然后重启进程的话. 那么我坚信你的工资 也就一万左右了, 现在运维开发这么火,

运维工程师从入门到放弃必备书籍

By:Linxu  http://www.walker0909.com/ <鸟哥的私房菜>(基础版/服务器版) <构建之法--现代软件工程> <RabbitMQ实战高效部署分布式消息队列> <程序员必读软件架构> <OpenStack开源云 王者归来> <ELKstack权威指南> <Nginx高性能Web服务器详解> <Docker容器与容器云> <Zabbix企业级分布式监控系统> <奔跑

linux基础运维

一.系统常用的配置文件位置.和语法格式 1.常用的网络配置文件 /etc/sysconfig/network     网络设置(主机名也在这个文件配置 HOSTNAME=localhost.localdomain) NETWORK=yes/no 表示网络是否被配置: HOSTNAME=hostname hostname 表示服务器的主机名: GATEWAY=gw-ip gw-ip 表示网络网关的IP 地址: FORWARD_IPV4=yes/no 是否开启IP 转发功能: /etc/hosts

linux 基础运维命令

Linux运维日常命令操作 1.linux启动过程 开启电源 --> BIOS开机自检 --> 引导程序lilo或grub --> 内核的引导(kernel boot)--> 执行init(rc.sysinit.rc)--> mingetty(建立终端) --> shell 2.网卡绑定多IP ifconfig eth0:1 192.168.1.99 netmask 255.255.255.0 3.设置DNS.网关 echo "nameserver 202.1

基础运维项目经验-SDH/MSTP专线接入部署类型一部署篇

今天半夜突然想起来,已经连续俩周没有更新了,我去.我居然脱更了,瞬间感觉到作为一个网工,特对不起自己的未来,故此时毅然决定起床更新一篇基础的专线接入的经验文章.文章很简单,其实说到底就是静态路由的运用,其他的没了. 所以我这里就不多讲技术,多聊聊经验,多聊聊自己的想法. 直接上拓扑,有图才有的说,大家懂得 环境非常的简单,我就不装逼了. 说下情景: 某公司上海地区,需要新增一条MSTP到深圳自家IDC的核心交换机上,保证核心交易数据正常同步,俩边内网传输走专线(未来会跟大家聊下,专线传输加密的需

linux 基础运维命令2

1.查找当前目录下所有以.tar结尾的文件然后移动到指定目录: find . -name "*.tar" -exec mv {} ./backup/ ; 注解:find –name 主要用于查找某个文件名字,-exec .xargs可以用来承接前面的结果,然后将要执行的动作,一般跟find在一起用的很多,find使用我们可以延伸-mtime查找修改时间.-type是指定对象类型(常见包括f代表文件.d代表目录),-size 指定大小,例如经常用到的:查找当前目录30天以前大于100M的

Linux -日常运维- iptables语法

iptables -nvL #列出规则cat /etc/sysconfig/iptables #若提示无此文件, yum install iptables-servicesiptables -F #清空规则 service iptables save #将刚才设置的规则保存到配置文件里 #所有操作,默认filter 表iptables -t nat -nvL //-t指定表pkts #多少个数据包bytes #数据包的字节iptables -Z 可以把计数器清零 #就是清空上面的两个值 设置防火