Ubuntu中iptables的使用

本文介绍了如何在Ubuntu中配置并管理iptables,包括设置开机启动、基本命令使用、规则保存与恢复、禁用防火墙等关键步骤。

Ubuntu中使用iptables

 

(一) 设置开机启动iptables

 

# sysv-rc-conf --level 2345 iptables on

 

 

 

(二) iptables的基本命令

 

1. 列出当前iptables的策略和规则

# iptables -L -n

 

-n: 用数字形式显示

 

# iptables -L -v

 

-v: 打印详细的信息

 

2. 允许已经建立的连接接收数据

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

 

 

3. 开放端口22(SSH的默认端口),您要告诉iptables允许接受到的所有目标端口为22的TCP报文通过

 

iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT

 

注:ssh代表22,可以在/etc/services中查到的服务都可以这样使用。

 

 

4. 添加策略。策略也是一种规则,当所有规则都不匹配时,使用链的“策略”

链:INPUT, PREROUTING, FORWARD, POSTROUTING, OUTPUT

 

链策略的默认值是:ACCEPT。

 

表:filter (默认),nat,mangle。

 

#iptables -P INPUT DROP

#iptables -P OUTPUT ACCEPT

#iptables -P FORWARD DROP

 

----------------------------------------------------

 

root@patrick:~# iptables -L -n

Chain INPUT (policy DROP)

target     prot opt source               destination         

ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           

ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 


Chain FORWARD (policy DROP)

target     prot opt source               destination         


Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:22 

----------------------------------------------------

 

5. 启动包转发功能

 

将内网的FTP请求转发到外网的一个主机上。

 

iptables -t nat -A PREROUTING -p tcp -dport 21 -j DNAT --to-dest 10.25.1.7:21

 

查看:

 

# iptables -L -t nat

 

要实现包转发,还需要编辑内核参数。

 

# cat /proc/sys/net/ipv4/ip_forward

0

 

默认包转发是禁止的。于是需要打开。编辑/etc/sysctl.conf,然后执行sysctl -p。

(三)保存iptables的规则

step 1) 保存当前iptables的规则到文件中。

 

# iptables-save > /etc/iptables.up.rules

 

step 2) 开机恢复iptables的规则。方法是添加下面这行到文件‘/etc/network/interfaces/’ 的末尾。

 

pre-up iptables-restore < /etc/iptables.up.rules

 

(四)禁用防火墙

 

iptables -F

 

似乎Ubuntu中没有类似service iptables stop这样的命令来暂停iptables。只能使用这种方法来禁用iptables(防火墙)。

 

使用前,请保证规则已经备份在文件中。

 

### 在 Ubuntu 中停止或禁用 iptables 防火墙的方法 在 Ubuntu 系统中,iptables 是一个强大的工具,用于配置网络包过滤规则。然而,与 UFW 不同的是,iptables 本身并不是一个服务,而是一个命令行工具,因此不能直接通过 `systemctl` 或其他服务管理工具来停止或禁用它[^1]。 #### 方法一:清空所有 iptables 规则 如果希望临时禁用 iptables 的功能,可以通过清空所有规则来实现: ```bash sudo iptables -F sudo iptables -X sudo iptables -t nat -F sudo iptables -t nat -X sudo iptables -t mangle -F sudo iptables -t mangle -X sudo iptables -P INPUT ACCEPT sudo iptables -P FORWARD ACCEPT sudo iptables -P OUTPUT ACCEPT ``` 上述命令的作用是清除所有规则,并将默认策略设置为接受所有流量[^3]。 #### 方法二:保存并加载空规则集 为了确保系统重启后仍然保持无规则状态,可以保存当前的空规则集到文件中,并在系统启动时加载该文件: ```bash sudo sh -c "iptables-save > /etc/iptables/rules.v4" ``` 此命令将当前的 iptables 规则保存到 `/etc/iptables/rules.v4` 文件中。如果需要重新加载规则,可以使用以下命令: ```bash sudo iptables-restore < /etc/iptables/rules.v4 ``` #### 方法三:卸载 iptables(不推荐) 虽然可以通过卸载 iptables 来彻底禁用它,但这通常不是推荐的做法,因为许多系统工具和服务可能依赖于 iptables。如果确实需要卸载,可以运行以下命令: ```bash sudo apt-get remove --purge iptables ``` 需要注意的是,卸载 iptables 可能会导致某些系统功能异常,因此建议仅在明确不需要 iptables 的情况下进行操作[^2]。 #### 方法四:禁用与 iptables 相关的服务 如果系统中有与 iptables 相关的服务(例如 firewalld 或 ufw),可以通过禁用这些服务间接影响 iptables 的行为。例如,禁用 UFW: ```bash sudo ufw disable ``` #### 注意事项 - 清空 iptables 规则可能会导致系统失去防火墙保护,请确保在执行此操作前了解潜在的安全风险。 - 如果系统依赖于 iptables 提供的网络隔离或安全功能,建议谨慎操作,避免意外暴露系统。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值