iptables 使用

------------实践-----------------

1 iptables -L -v shows (note

the counts for INPUT and OUTPUT


2  iptables-save >/root/my.active.firewall.rules

   iptables-restore </root/my.active.firewall.rules

3 删除一个

-L 显示当前的行号

-D 删除具体行数

iptabels -D INPUT -s 192.168.1.1/24 -j DROP


4  记录日志功能 iptables



5 阻断icmp包

iptables -A INPUT -p icmp --icmp-type echo-request -j DROP


6

The following only accepts limited type of ICMP requests:
### ** assumed that default INPUT policy set to DROP ** #############
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
## ** all our server to respond to pings ** ##
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT


7 开放一个范围的ip地址对apache访问

iptables -A INPUT -p tcp --destination-port 80 -m iprange --src-range 192.168.1.100-192.168.1.200 -j ACCEPT


7 优化

Established Connections and Restaring The Firewall

When you restart the iptables service it will drop established connections as it unload modules from the system under RHEL / Fedora / CentOS Linux. Edit, /etc/sysconfig/iptables-config and set IPTABLES_MODULES_UNLOAD as follows:

IPTABLES_MODULES_UNLOAD = no
 Help Iptables Flooding My Server Screen

Use the crit log level to send messages to a log file instead of console:
iptables -A INPUT -s 1.2.3.4 -p tcp --destination-port 80 -j LOG --log-level crit



注意:如果var/log/message 中没有记录  可以重启一下rsyslog 服务器


Block a Specific ip-address

Before we proceed further will other examples, if you want to block a specific ip-address, you should do that first as shown below. Change the “x.x.x.x” in the following example to the specific ip-address that you like to block.

BLOCK_THIS_IP="x.x.x.x"iptables -A INPUT -s "$BLOCK_THIS_IP" -j DROP

This is helpful when you find some strange activities from a specific ip-address in your log files, and you want to temporarily block that ip-address while you do further research.


10 

Allow Incoming HTTP and HTTPS

The following rules allow all incoming web traffic. i.e HTTP traffic to port 80.

iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPTiptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

The following rules allow all incoming secure web traffic. i.e HTTPS traffic to port 443.

iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPTiptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT



The following example allows all incoming SSH, HTTP and HTTPS traffic.

iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISHED -j ACCEPTiptables -A OUTPUT -o eth0 -p tcp -m multiport --sports 22,80,443 -m state --state ESTABLISHED -j ACCEPT
11  Allow Loopback Access
You should allow full loopback access on your servers. i.e access using 127.0.0.1iptables -A INPUT -i lo -j ACCEPTiptables -A OUTPUT -o lo -j ACCEPT
12   Allow MySQL connection only from a specific network
If you are running MySQL, typically you don’t want to allow direct connection from outside. In most cases, you might have web server running on the same server where the MySQL database runs.However DBA and developers might need to login directly to the MySQL from their laptop and desktop using MySQL client. In those case, you might want to allow your internal network to talk to the MySQL directly as shown below.iptables -A INPUT -i eth0 -p tcp -s 192.168.100.0/24 --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPTiptables -A OUTPUT -o eth0 -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
13 
Prevent DoS AttackThe following iptables rule will help you prevent the Denial of Service (DoS) attack on your webserver.iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
14

15
 /sbin/iptables -P INPUT DROP       /sbin/iptables -P OUTPUT ACCEPT       /sbin/iptables -A INPUT -i lo -j ACCEPT       /sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP       /sbin/iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 139 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 631 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT       /sbin/iptables -A INPUT -p tcp -m tcp --dport 445 -j ACCEPT       /sbin/iptables -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT       /sbin/iptables -A INPUT -p all -m state --state INVALID,NEW -j DROP
16 iptables 插入指定条目
# First get the iptables list with the line numbers enabled$ iptables -nL --line-numbers# Look up the line number you want to use (the exisitng rule will shift down) and insert your rule$ iptables -I INPUT {LINE_NUMBER} -i eth1 -p tcp --dport 21 -s 123.123.123.123 -j ACCEPT -m comment --comment "This rule is here for this reason"# Aftarwards i always save my rules to a file in etc so i can reload them at the next reboot$ iptables-save > /etc/iptables.local# (To do this, add the following rule to your /etc/rc.local file)/sbin/iptables-restore < /etc/iptables.local
16  iptables -I INPUT -p icmp --icmp-type ping -m limit --limit 3/s -j DROP  
17 替换
iptables -R INPUT 1  -p icmp --icmp-type ping -m limit --limit 10/s -j DROP
时间: 2024-08-28 15:40:43

iptables 使用的相关文章

CentOS7安装iptables防火墙

CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #先检查是否安装了iptables service iptables status #安装iptables yum install -y iptables #升级iptables yum update iptables #安装iptables-services yum install iptables-services 禁用/停止自带的firewalld服务 #停止fir

iptables端口转发

1. 确定forward开启 # cat /proc/sys/net/ipv4/ip_forward1 2. 转发进来的包 iptables -t nat -A PREROUTING -d 111.111.111.111 -p tcp -m tcp --dport 16922 -j DNAT --to-destination 192.168.0.169:22 这表示将目的地为111.111.111.111:16922的包发往 192.168.0.169:22 3. 设置回路 iptables -

Iptables防火墙(一)

一.Linux防火墙基础 Linux防火墙主要工作在网络层,属于典型的包过滤防火墙. netfilter和iptables都用来指Linux防火墙,主要区别是: netfilter:指的是linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于"内核态"的防火墙功能体系. iptables:指的是用来管理linux防火墙的命令程序,通常位于/sbin/iptables目录下,属于"用户态"的防火墙管理体系. 1.iptables的表.链结构 Ipt

防火墙iptables

防火墙 主配置文件:vim /etc/sysconfig/iptables 想要自定义防火墙,需要把这里的规则清空并且权限设置成DROP 防火墙名字:netfilter,工具:iptables 防火墙有三个表filter,nat,mangle 每个表下面还有链: filter表主要用于过滤包,系统预设的表.内建三个链INPUT.OUTPUT.FORWARD,INPUT作用于进入本机的包,OUTPUT作用于本机送出的包,FORWARD作用于跟本机无关的包. nat表主要用处是网络地址转换,PRER

【整理笔记-防火墙】实现iptables防火墙搭建

搭建防火墙,配置防火墙. - - 系统centos7 . centos7自带firewalld,由于看firewalld命令行没有接触过,所以安装iptables防火墙. 1:禁用firewalld firewall-cmd --state 查看系统自带防火墙状态. 用systemctl stop firewalld.service   禁止立即生效, systemctl disable firewalld.service  永久关闭firewalld.执行完再看一下防火墙状态, 显示为not

iptables man中文手册

名称        iptables - IP包过滤器管理 总览        iptables -ADC  指定链的规则  [-A  添加 -D 删除 -C 修改]        iptables - RI        iptables -D chain rule num[选项]        iptables -LFZ 链名 [选项]        iptables -[NX] 指定链        iptables -P chain target[选项集]        iptables

service iptables start 无反应的解决方法

[[email protected] ~]# service iptables start[[email protected] ~]# service iptables status防火墙已停解决方法:一.初始化iptables.iptables -Fservice iptables saveservice iptables restartvi /etc/sysconfig/iptables 二.把预置的iptables规则添加进去就可以了:# Firewall configuration wr

关闭系统不必要的服务;关闭selinux,关闭iptables

关闭系统不必要的服务:关闭selinux,关闭iptables:关闭ctrl+alt+del重启:设置ssh端口,关闭DNS解析:设置系统最大文件描述符:设置系统关键文件权限:配置安装ntp:安装vim:配置安装阿里云yum源和epel源: #!/bin/bash #written by [email protected] #system optimization script #The fllow apply to CentOS 6.x . /etc/init.d/functions func

针对Red Hat Enterprise Linux 6.5 的防火墙详细讲解,iptables(netfilter)规则的

防火墙基础 Linux的防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙(或网络层防火墙).基于Linux内核编码实现,具有非常稳定的性能和高效率,因此获得广泛使用. 在Linux系统中,netfilter和iptables都用来指Linux防火墙. netfilter:指的是Linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于"内核态"(Kernel Space,又称为内核空间)的防火墙功能体系. iptables:指的是

linux防火墙--iptables(二)

五.filter过滤和转发 a.打开内核的IP转发 # sysctl -w net.ipv4.ip_forward=1 或 # echo 1 > /proc/sys/net/ipv4/ip_forward b.基本匹配条件 ·通用匹配 → 可直接使用,不依赖于其他条件或扩展 → 包括网络协议.IP地址.网络接口等条件 ·隐含匹配 → 要求以特定的协议匹配作为前提 → 包括端口.TCP标记.ICMP类型等条件 类别 选项 用法 通用匹配 协议匹配 -p 协议名 地址匹配 -s 源地址