This Linux based firewall is controlled by the program called iptables to handles filtering for IPv4, and ip6tables handles filtering for IPv6. I strongly recommend that you first read our quick tutorial that explains how to configure a host-based firewall called Netfilter (iptables) under CentOS / RHEL / Fedora / Redhat Enterprise Linux. This post list most common iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders.
IPTABLES Rules Example
- Most of the actions listed in this post are written with the assumption that they will be executed by the root user running the bash or any other modern shell. Do not type commands on remote system as it will disconnect your access.
- For demonstration purpose I’ve used RHEL 6.x, but the following command should work with any modern Linux distro.
- This is NOT a tutorial on how to set iptables. See tutorial here. It is a quick cheat sheet to common iptables commands.
#1: Displaying the Status of Your Firewall
Type the following command as root:# iptables -L -n
-v
Sample outputs:
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
Above output indicates that the firewall is not active. The following sample shows an active firewall:# iptables -L -n -v
Sample outputs:
Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID 394 43586 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 93 17292 ACCEPT all -- br0 * 0.0.0.0/0 0.0.0.0/0 1 142 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- br0 br0 0.0.0.0/0 0.0.0.0/0 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID 0 0 TCPMSS tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x06/0x02 TCPMSS clamp to PMTU 0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 wanin all -- vlan2 * 0.0.0.0/0 0.0.0.0/0 0 0 wanout all -- * vlan2 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- br0 * 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 425 packets, 113K bytes) pkts bytes target prot opt in out source destination Chain wanin (1 references) pkts bytes target prot opt in out source destination Chain wanout (1 references) pkts bytes target prot opt in out source destination
Where,
- -L : List rules.
- -v : Display detailed information. This option makes the list command show the interface name, the rule options, and the TOS masks. The packet and byte counters are also listed, with the suffix ‘K’, ‘M’ or ‘G’ for 1000, 1,000,000 and 1,000,000,000 multipliers respectively.
- -n : Display IP address and port in numeric format. Do not use DNS to resolve names. This will speed up listing.
#1.1: To inspect firewall with line numbers, enter:
# iptables -n -L -v --line-numbers
Sample outputs:
Chain INPUT (policy DROP) num target prot opt source destination 1 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy DROP) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 2 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID 3 TCPMSS tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:0x06/0x02 TCPMSS clamp to PMTU 4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 5 wanin all -- 0.0.0.0/0 0.0.0.0/0 6 wanout all -- 0.0.0.0/0 0.0.0.0/0 7 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT) num target prot opt source destination Chain wanin (1 references) num target prot opt source destination Chain wanout (1 references) num target prot opt source destination
You can use line numbers to delete or insert new rules into the firewall.
#1.2: To display INPUT or OUTPUT chain rules, enter:
# iptables -L INPUT -n -v
# iptables -L OUTPUT -n -v
--line-numbers
#2: Stop / Start / Restart the Firewall
If you are using CentOS / RHEL / Fedora Linux, enter:# service
iptables stop
# service iptables start
# service iptables
restart
You can use the iptables command itself to stop the firewall and delete all rules:# iptables -F
# iptables -X
# iptables -t
nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t
mangle -X
# iptables -P INPUT ACCEPT
# iptables -P OUTPUT ACCEPT
#
iptables -P FORWARD ACCEPT
Where,
- -F : Deleting (flushing) all the rules.
- -X : Delete chain.
- -t table_name : Select table (called nat or mangle) and delete/flush rules.
- -P : Set the default policy (such as DROP, REJECT, or ACCEPT).
#3: Delete Firewall Rules
To display line number along with other information for existing rules, enter:# iptables -L INPUT -n --line-numbers
# iptables -L OUTPUT -n
--line-numbers
# iptables -L OUTPUT -n --line-numbers | less
# iptables -L
OUTPUT -n --line-numbers | grep 202.54.1.1
You will get the list of IP. Look at the number on the left, then use number to delete it. For example delete line number 4, enter:# iptables -D INPUT 4
OR find source IP 202.54.1.1 and delete from rule:# iptables -D INPUT -s
202.54.1.1 -j DROP
Where,
- -D : Delete one or more rules from the selected chain
#4: Insert Firewall Rules
To insert one or more rules in the selected chain as the given rule number use the following syntax. First find out line numbers, enter:
# iptables -L INPUT -n –line-numbers
Sample outputs:
Chain INPUT (policy DROP) num target prot opt source destination 1 DROP all -- 202.54.1.1 0.0.0.0/0 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW,ESTABLISHED
To insert rule between 1 and 2, enter:# iptables -I INPUT 2 -s
202.54.1.2 -j DROP
To view updated rules, enter:# iptables
-L INPUT -n --line-numbers
Sample outputs:
Chain INPUT (policy DROP) num target prot opt source destination 1 DROP all -- 202.54.1.1 0.0.0.0/0 2 DROP all -- 202.54.1.2 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW,ESTABLISHED
#5: Save Firewall Rules
To save firewall rules under CentOS / RHEL / Fedora Linux, enter:#
service iptables save
In this example, drop an IP and save firewall rules:# iptables -A INPUT -s 202.5.4.1 -j DROP
# service iptables
save
For all other distros use the iptables-save command:#
iptables-save > /root/my.active.firewall.rules
# cat
/root/my.active.firewall.rules
#6: Restore Firewall Rules
To restore firewall rules form a file called /root/my.active.firewall.rules, enter:# iptables-restore <
/root/my.active.firewall.rules
To restore firewall rules under CentOS / RHEL / Fedora Linux, enter:# service iptables restart
#7: Set the Default Firewall Policies
To drop all traffic:# iptables -P INPUT DROP
# iptables -P
OUTPUT DROP
# iptables -P FORWARD DROP
# iptables -L -v -n
#### you
will not able to connect anywhere as all traffic is dropped ###
# ping
cyberciti.biz
# wget
http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.2-rc5.tar.bz2
#7.1: Only Block Incoming Traffic
To drop all incoming / forwarded packets, but allow outgoing traffic, enter:# iptables -P INPUT DROP
# iptables -P FORWARD DROP
#
iptables -P OUTPUT ACCEPT
# iptables -A INPUT -m state --state
NEW,ESTABLISHED -j ACCEPT
# iptables -L -v -n
### *** now ping and wget
should work *** ###
# ping cyberciti.biz
# wget
http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.2-rc5.tar.bz2
#8:Drop Private Network Address On Public Interface
IP spoofing is nothing but to stop the following IPv4 address ranges for private networks on your public interfaces. Packets with non-routable source addresses should be rejected using the following syntax:# iptables -A
INPUT -i eth1 -s 192.168.0.0/24 -j DROP
# iptables -A INPUT -i eth1 -s
10.0.0.0/8 -j DROP
#8.1: IPv4 Address Ranges For Private Networks (make sure you block them on public interface)
- 10.0.0.0/8 -j (A)
- 172.16.0.0/12 (B)
- 192.168.0.0/16 (C)
- 224.0.0.0/4 (MULTICAST D)
- 240.0.0.0/5 (E)
- 127.0.0.0/8 (LOOPBACK)
#9: Blocking an IP Address (BLOCK IP)
To block an attackers ip address called 1.2.3.4, enter:# iptables
-A INPUT -s 1.2.3.4 -j DROP
# iptables -A INPUT -s 192.168.0.0/24 -j
DROP
#10: Block Incoming Port Requests (BLOCK PORT)
To block all service requests on port 80, enter:# iptables -A INPUT
-p tcp --dport 80 -j DROP
# iptables -A INPUT -i eth1 -p tcp --dport 80 -j
DROP
To block port 80 only for an ip address 1.2.3.4, enter:# iptables
-A INPUT -p tcp -s 1.2.3.4 --dport 80 -j DROP
# iptables -A INPUT -i eth1 -p
tcp -s 192.168.1.0/24 --dport 80 -j DROP
#11: Block Outgoing IP Address
To block outgoing traffic to a particular host or domain such as cyberciti.biz, enter:# host -t a cyberciti.biz
Sample outputs:
cyberciti.biz has address 75.126.153.206
Note down its ip address and type the following to block all outgoing traffic to 75.126.153.206:# iptables -A OUTPUT -d 75.126.153.206 -j
DROP
You can use a subnet as follows:# iptables -A OUTPUT -d
192.168.1.0/24 -j DROP
# iptables -A OUTPUT -o eth1 -d 192.168.1.0/24 -j
DROP
#11.1: Example – Block Facebook.com Domain
First, find out all ip address of facebook.com, enter:# host -t a
www.facebook.com
Sample outputs:
www.facebook.com has address 69.171.228.40
Find CIDR for 69.171.228.40, enter:# whois 69.171.228.40 | grep
CIDR
Sample outputs:
CIDR: 69.171.224.0/19
To prevent outgoing access to www.facebook.com, enter:# iptables -A
OUTPUT -p tcp -d 69.171.224.0/19 -j DROP
You can also use domain name, enter:# iptables -A OUTPUT -p tcp -d www.facebook.com -j DROP
#
iptables -A OUTPUT -p tcp -d facebook.com -j DROP
From the iptables man page:
… specifying any name to be resolved with a remote query such as DNS (e.g., facebook.com is a really bad idea), a network IP address (with /mask), or a plain IP address …
#12: Log and Drop Packets
Type the following to log and block IP spoofing on public interface called eth1# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j LOG --log-prefix
"IP_SPOOF A: "
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
By default everything is logged to /var/log/messages file.# tail -f
/var/log/messages
# grep --color 'IP SPOOF' /var/log/messages
#13: Log and Drop Packets with Limited Number of Log Entries
The -m limit module can limit the number of log entries created per time. This is used to prevent flooding your log file. To log and drop spoofing per 5 minutes, in bursts of at most 7 entries .# iptables -A INPUT -i eth1
-s 10.0.0.0/8 -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix "IP_SPOOF
A: "
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
#14: Drop or Accept Traffic From Mac Address
Use the following syntax:# iptables -A INPUT -m mac --mac-source
00:0F:EA:91:04:08 -j DROP
## *only accept traffic for TCP port # 8080 from
mac 00:0F:EA:91:04:07 * ##
# iptables -A INPUT -p tcp --destination-port 22
-m mac --mac-source 00:0F:EA:91:04:07 -j ACCEPT
#15: Block or Allow ICMP Ping Request
Type the following command to block ICMP ping requests:# iptables
-A INPUT -p icmp --icmp-type echo-request -j DROP
# iptables -A INPUT -i eth1
-p icmp --icmp-type echo-request -j DROP
Ping responses can also be limited to certain networks or hosts:# iptables -A INPUT -s
192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT
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
#16: Open Range of Ports
Use the following syntax to open a range of ports:iptables -A INPUT
-m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT
#17: Open Range of IP Addresses
Use the following syntax to open a range of IP address:## only
accept connection to tcp port 80 (Apache) if ip is between 192.168.1.100 and
192.168.1.200 ##
iptables -A INPUT -p tcp --destination-port 80 -m iprange
--src-range 192.168.1.100-192.168.1.200 -j ACCEPT
## nat example ##
iptables -t nat -A POSTROUTING -j SNAT --to-source
192.168.1.20-192.168.1.25
#18: 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
#19: 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
#20: Block or Open Common Ports
The following shows syntax for opening and closing common TCP and UDP ports:
Replace ACCEPT with DROP to block port: ## open port ssh tcp port 22 ## iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT ## open cups (printing service) udp/tcp port 631 for LAN users ## iptables -A INPUT -s 192.168.1.0/24 -p udp -m udp --dport 631 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 631 -j ACCEPT ## allow time sync via NTP for lan users (open udp port 123) ## iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT ## open tcp port 25 (smtp) for all ## iptables -A INPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT # open dns server ports for all ## iptables -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT ## open http/https (Apache) server port to all ## iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT ## open tcp port 110 (pop3) for all ## iptables -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT ## open tcp port 143 (imap) for all ## iptables -A INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT ## open access to Samba file server for lan users only ## iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT ## open access to proxy server for lan users only ## iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 3128 -j ACCEPT ## open access to mysql server for lan users only ## iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
#21: Restrict the Number of Parallel Connections To a Server Per Client IP
You can use connlimit module to put such restrictions. To allow 3 ssh connections per client host, enter:# iptables -A INPUT -p tcp --syn
--dport 22 -m connlimit --connlimit-above 3 -j REJECT
Set HTTP requests to 20:# iptables -p tcp --syn --dport 80 -m
connlimit --connlimit-above 20 --connlimit-mask 24 -j DROP
Where,
- –connlimit-above 3 : Match if the number of existing connections is above 3.
- –connlimit-mask 24 : Group hosts using the prefix length. For IPv4, this must be a number between (including) 0 and 32.
#22: HowTO: Use iptables Like a Pro
For more information about iptables, please see the manual page by typing man iptables from the command line:$ man iptables
You can see the help using the following syntax too:# iptables -h
To see help with specific commands and targets, enter:# iptables -j DROP
-h
#22.1: Testing Your Firewall
Find out if ports are open or not, enter:# netstat
-tulpn
Find out if tcp port 80 open or not, enter:# netstat
-tulpn | grep :80
If port 80 is not open, start the Apache, enter:# service httpd start
Make sure iptables allowing access to the port 80:# iptables -L INPUT -v -n | grep
80
Otherwise open port 80 using the iptables for all users:#
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
# service
iptables save
Use the telnet command to see if firewall allows to connect to port 80:$ telnet www.cyberciti.biz 80
Sample outputs:
Trying 75.126.153.206... Connected to www.cyberciti.biz. Escape character is '^]'. ^] telnet> quit Connection closed.
You can use nmap to probe your own server using the following syntax:$ nmap -sS -p 80 www.cyberciti.biz
Sample outputs:
Starting Nmap 5.00 ( http://nmap.org ) at 2011-12-13 13:19 IST Interesting ports on www.cyberciti.biz (75.126.153.206): PORT STATE SERVICE 80/tcp open http Nmap done: 1 IP address (1 host up) scanned in 1.00 seconds
I also recommend you install and use sniffer such as tcpdupm and ngrep to test your firewall settings.
相关推荐
本文档主要介绍了 Linux 中的iptables 命令的实例,涵盖了 iptables 的基本用法、规则设定、端口控制、NAT 转发等方面的知识点。 一、iptables 的基本概念 iptables 是 Linux 系统中的一个防火墙工具,用于控制...
这条命令表示在 `filter` 表的 `INPUT` 链路中添加一条规则,该规则匹配所有目标端口为23的TCP数据包,并拒绝这些数据包。 2. **列出规则**:使用 `-L` 选项列出指定表中的所有规则。 ```bash iptables -L ``` ...
### Linux防火墙iptables常用规则详解 #### 一、iptables基础操作与配置 ##### 删除现有规则 在使用iptables之前,我们通常需要先清除已有的规则,以便于重新建立新的规则集。这可以通过`iptables -F`命令来实现。...
Linux 上的 iptables 防火墙是一种常用的防火墙软件,能够控制访问 Linux 系统的流量。iptables 防火墙的基本应用包括安装、清除规则、开放指定端口、屏蔽指定 IP、删除已添加的规则等。 安装 iptables 防火墙 若...
在IT领域,Linux操作系统是开发者和系统管理员的重要工具。它以其开源、稳定和高度可定制性而备受青睐。...阅读"Linux常用命令详解.doc"文档,将为你提供更详尽的解释和实例,帮助你更好地理解和应用这些命令。
### Linux常用系统维护命令知识点详解 #### 一、获取系统基本信息 **1. 查看Linux内核版本** - **命令**: `uname -a` - **用途**: 该命令可以显示当前系统的内核版本信息,包括操作系统名称、内核版本号等。 - *...
`iptables`位于`/sbin/iptables`路径下,它不仅是一种命令行工具,更是Linux防火墙“用户态”的体现,用于设定过滤规则和策略,决定数据包的过滤或处理方式。相对应的,“内核态”则是指位于Linux内核内部的`...
### CentOS 6 iptables 常用操作及规则配置 #### 概述 在Linux系统中,`iptables`是一款强大的工具,用于管理网络流量并控制数据包过滤规则。CentOS 6作为一款广泛使用的服务器操作系统,其内置的`iptables`功能...
- **SUSE Linux 常用命令**:此部分详细介绍了一系列在 SUSE Linux 环境下常用的命令,这些命令对于系统管理员来说至关重要。 - **Novell 相关服务停止和启动命令**:这部分将涉及如何管理 Novell 相关的服务,例如...
- `iptables -I INPUT 2 -p udp -j ACCEPT` 表示在INPUT链的第二条位置插入一条规则,允许所有UDP协议的数据包。 2. 列出规则: - -L 选项用于列出所有规则。 - -n 选项以数字形式显示地址和端口等信息。 - -v ...
例如,iptables -F 命令可以清除所有防火墙规则,而 iptables -L 命令可以显示所有防火墙规则。 防火墙安全 防火墙安全是非常重要的,需要小心地使用防火墙命令,以避免带来不必要的麻烦。例如,在应用每一个规则...
这个"Linux常用命令合集"涵盖了多个方面,包括内核态与用户态的通信、ISO镜像的制作、文件管理、系统维护以及网络配置。下面将详细介绍这些关键知识点。 一、内核态与用户态通信 在Linux系统中,程序执行有两种状态...
### Linux常用命令与服务器配置详解 #### 一、用户管理命令 **1.1 添加用户:useradd** - **命令语法**: - `useradd [选项] 用户名` - **选项说明**: - `-m`:自动创建用户的主目录,并把框架目录(`/etc/...
### Linux常用命令50个 学习操作系统必备 在Linux操作系统中,掌握一系列基础命令对于初学者来说至关重要。本文将详细介绍一系列基本且实用的Linux命令,帮助用户更好地理解和操作Linux系统。 #### 1. `cd` **功能...
这里记录了iptables 防火墙规则的一些常用的操作指令。 下面的操作以 CentOS 为基础介绍,应该对不同的 Linux 发行版都差不多。在 CentOS 5.x 和 6.x 中,iptables 是默认安装的(如果没有安装,先安装 iptables ...
Linux iptables是基于netfilter框架的防火墙工具,它允许系统管理员通过一系列规则来控制进出Linux系统的数据包。iptables的工作原理主要依赖于定义在内核中的几个表,这些表根据数据包处理的类型被细分为不同的链。...
#### 三、Iptables命令详解 - **基本命令格式**: ``` iptables [-t table] {command} [chain] [match] [-j target] ``` - **命令参数解释**: - `-t table`: 指定使用的表。 - `{command}`: 操作命令,如 `...
虽然iptables在Unix系统中不是服务,但是为了方便平时的管理也对iptables写了chkconfig的启动脚本,定义在/etc/init.d/即rc.d/init.d目录下,但是iptables和正常的service服务又多了几种命令 Usage: /etc/init.d/...