你也想在Linux上设置防火墙。相反,因为它被用作公司中的服务器,所以我想要比个人计算机更加小心。
这个页面讲述了在Linux上设置防火墙的iptables。
什么是防火墙?
一般来说,它意味着“防火墙”。它类似于灭火和防止火势蔓延。
在计算机网络环境中,它安装在主机本身与网络和网络之间,以阻止未经授权的数据包并提高安全性。
最常见的示例是Windows防火墙和内置于路由器中的数据包过滤器。
这次我们将介绍Linux中常用的iptables包过滤器。
什么是iptables?
iptables是安装在Linux中的通用数据包过滤器。
数据包是通过网络流动的数据,因为它通常来自智能手机数据包通信成本。数据包过滤器意味着对该数据包进行分类以分离传递的内容和不传递的内容。
iptables是一种非常高性能和高性能的数据包过滤器,可与商业产品相媲美。感谢您免费使用它。
由于它不仅支持服务器本身,还支持数据包传输等,因此可以通过在具有两个网络适配器的机器上操作并控制数据包传输,将其用作网络类型防火墙。
当与Snort等结合使用时,它可以用作UTM(集成威胁管理系统)。它还具有状态功能,以掌握数据包的状态和日志功能。
尽管有些困难,但这个页面解释了iptables的基础。
iptables设置确认和解释
通常,执行iptables命令时,会自动加载并使其可用。
要显示应用该设置的过滤规则,请键入以下命令。使用“-L”选项执行时,将显示当前设置的策略和这些规则的列表。
后续工作应该以root权限完成。
iptables -L
您应该看到类似下面的内容。
<! – Crayon Syntax Highlighter v_2.7.2_beta – >
|
Chain
INPUT(
policy
DROP
)
target
prot
opt
source
destination
ACCEPT
all
—
anywhere
anywhere
ACCEPT
all
—
anywhere
anywhere
state
RELATED
,
ESTABLISHED
LOG
all
—
anywhere
anywhere
LOG
level
warning
prefix“drop_packet: “
Chain
FORWARD(
policy
DROP
)
target
prot
opt
source
destination
Chain
OUTPUT(
policy
ACCEPT
)
target
prot
opt
source
destination
|
<! – [格式时间:0.0002秒] – >
这个时间只是一个例子,我们已经在已经进行了一些设置时发出了显示。
应用相同设置的脚本将在本页末尾介绍。如果您有兴趣,请参考它。
此页面的目标是使其可读并设置该设置。
让我们一一解释。
链
从网络接收的数据包按以下过程处理。
对于INPUT,OUTPUT和FORWARD三个路由中的每一个,设置要传递的数据包规则。这三条路径称为“链”。
还应确认上述三个例子。
- 链输入(策略DROP)
- Chain FORWARD(政策DROP)
- Chain OUTPUT(政策接受)
这些都是基本政策。如果它不适用于任何规则,则规则是这些是基本设置。
policy DROP意味着如果未指定规则,它将拒绝所有规则。如果是ACCEPT,则全部允许。
Chain下的下一部分是规则。
|
target
prot
opt
source
destination
|
|
target
prot
opt
source
destination
ACCEPT
all
—
anywhere
anywhere
ACCEPT
all
—
anywhere
anywhere
state
RELATED
,
ESTABLISHED
LOG
all
—
anywhere
anywhere
LOG
level
warning
prefix“drop_packet: “
|
iptables -L -v -t filter
|
Chain
INPUT(
policy
DROP
532
packets
,
38457
bytes
)
pkts
bytes
target
prot
opt
in
out
source
destination
4
240
ACCEPT
all
—
lo
any
anywhere
anywhere
180K
320M
ACCEPT
all
—
any
any
anywhere
anywhere
state
RELATED
,
ESTABLISHED
532
38457
LOG
all
—
any
any
anywhere
anywhere
LOG
level
warning
prefix“drop_packet: “
Chain
FORWARD(
policy
DROP
0
packets
,
0
bytes
)
pkts
bytes
target
prot
opt
in
out
source
destination
Chain
OUTPUT(
policy
ACCEPT
139K
packets
,
14M
bytes
)
pkts
bytes
target
prot
opt
in
out
source
destination
|
|
pkts
bytes
target
prot
opt
in
out
source
destination
4
240
ACCEPT
all
—
lo
any
anywhere
anywhere
|
|
LOG
all
—
anywhere
anywhere
LOG
level
warning
prefix“drop_packet: “
|
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
|
iptables 〈テーブル〉〈コマンド〉 〈マッチ〉〈ターゲット〉
iptables -t filter -A INPUT -p tcp -j ACCEPT
iptables -A INPUT -p tcp -j ACCEPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/bin/bash
# 設定をクリア
# ポリシー設定
iptables
–
P
FORWARD
DROP
iptables
–
P
OUTPUT
ACCEPT
# ほかACCEPTやDROPなどユーザによる設定
#ローカルループバックの接続を許可する。
iptables
–
A
INPUT
–
i
lo
–
j
ACCEPT
#こちらから求めたパケットは許可する。
iptables
–
A
INPUT
–
m
state
—
state
ESTABLISHED
,
RELATED
–
j
ACCEPT
#それ以外はログを残す。
iptables
–
A
INPUT
–
j
LOG
—
log
–
prefix
“drop_packet:”
|
iptables -A INPUT -m state NEW -m tcp -p tcp -dport 22 -j ACCEPT
iptables-save > /etc/myiptables-set
iptables-restore < /etc/myiptables-set