Debian安装配置Iptables防火墙

服务器通常会安装防火墙,Debian上有很防火墙,Iptables为比较常用的免费防火墙,Iptables能够提供数据包过滤,网络地址转换(NAT)等功能.在Debian上手工配置Iptables的资料比较少,本文做一个详细的介绍.

第一步,首先确定你的系统已经安装Iptables.打开SSH终端,输入
whereis iptables
如果能看到如下类似信息,说明你已经安装了iptables
iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz
如果不是这个提示,或者没有任何提示,那你的Debian上可能没有安装iptables
请使用如下命令安装:
sudo apt-get install iptables
注意:本文所有命令在普通帐号下完成,本普通帐号使用sudo具有root权限,本人不建议直接使用root用户

第二步:查看Iptables目前的配置信息
可以使用如下命令查看
sudo iptables -L
如果你是第一次安装配置iptables,你可能会看到如下结果:
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,我们先把一个基本的Iptables的规则文章保存起来,这个规则文章做为测试用
sudo vim /etc/iptables.test.rules
然后在这个文章中输入如下规则内容,这个内容是debian官方给出的基本配置

01*filter
02  
03# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
04-A INPUT -i lo -j ACCEPT
05-A INPUT -i ! lo -d 127.0.0.0/8 -j REJECT
06  
07# Accepts all established inbound connections
08-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
09  
10# Allows all outbound traffic
11# You could modify this to only allow certain traffic
12-A OUTPUT -j ACCEPT
13  
14# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
15-A INPUT -p tcp --dport 80 -j ACCEPT
16-A INPUT -p tcp --dport 443 -j ACCEPT
17  
18# Allows SSH connections for script kiddies
19# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
20-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
21  
22# Now you should read up on iptables rules and consider whether ssh access
23# for everyone is really desired. Most likely you will only allow access from certain IPs.
24  
25# Allow ping
26-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
27  
28# log iptables denied calls (access via 'dmesg' command)
29-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
30  
31# Reject all other inbound - default deny unless explicitly allowed policy:
32-A INPUT -j REJECT
33-A FORWARD -j REJECT
34  
35COMMIT


保存本文件,然后把本规则加载,使之生效,注意,iptables不需要重启,加载一次规则就成了
sudo iptables-restore < /etc/iptables.test.rules
然后再查看最新的配置,应该所有的设置都生效了.
sudo iptables -L

第四步:保存生效的配置,让系统重启的时候自动加载有效配置
iptables提供了保存当前运行的规则功能
iptables-save > /etc/iptables.up.rules

注意,如果当前用户不是root,即使使用了sudo,也会提示你没有权限,无法保存,所以执行本命令,你必须使用root用户.
可以使用sudo -i快速转到root,使用完成,请及时使用su username切换到普通帐户.

为了重启服务器后,规则自动加载,我们创建如下文件:
sudo vim /etc/network/if-pre-up.d/iptables
本文章的内容如下:
#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules
最后,设置本文章具体可执行仅限
chmod +x /etc/network/if-pre-up.d/iptables

第五:其它
如果你想设置某ip段可以访问所有服务,你需要在iptables.test.rules文件中加入-A INPUT -m iprange --src-range 192.168.1.1-192.168.1.199 -j ACCEPT,然后从第三步再设置一次.注意iptables.test.rules不是必须的,它只是让你的修改时,能更好的测试.

在Linux虚拟机上安装配置iptables防火墙通常包括以下几个步骤: 1. **安装iptables**: 如果你的系统还没有预装iptables,可以使用包管理器来安装。例如,在基于Debian或Ubuntu的系统上,你可以运行以下命令: ``` sudo apt-get update sudo apt-get install iptables-persistent ``` 对于基于Red Hat家族的系统(如CentOS、Fedora),可以用 yum 或 dnf 安装: ``` sudo yum install iptables-services sudo dnf install iptables-services ``` 2. **启动并启用iptables**: 安装完成后,你需要重启系统让iptables配置生效,或者手动加载配置: ``` sudo service iptables start sudo systemctl enable iptables ``` 3. **编辑iptables配置文件**: 主要的配置文件是 `/etc/sysconfig/iptables` 或 `/etc/iproute2/iptables-nat`,视乎你的系统版本。使用文本编辑器打开它,这将允许你添加规则。 4. **创建规则集**: - 添加基本规则:使用 `-A` 命令追加到链(chain)。比如阻止所有未授权的入站连接: ``` sudo iptables -A INPUT -j DROP ``` - 确保允许某些特定端口或IP范围的流量通过: ``` sudo iptables -A INPUT -s your.ip.address -p tcp --dport 80 -j ACCEPT ``` 5. **保存配置**: 要让更改持久化到系统重启,使用 `sudo iptables-save > /etc/sysconfig/iptables` 将当前配置保存。 6. **查看和测试规则**: 使用 `sudo iptables -L` 可以查看当前的规则。你可以尝试一些测试流量,确认规则是否按预期工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值