Linux安全之——Ubuntu的iptable命令使用

设置开机自动启动iptables

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

 

列出当前iptables的策略和规则

# iptables -L -n

 

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

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

 

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

# iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT  // 注:ssh代表22,可以在/etc/services中查到的服务都可以这样使用

 

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

链: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 

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

 

启动包转发功能

将内网的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的规则

# iptables-save > /etc/iptables.up.rules  // step 1) 保存当前iptables的规则到文件中。

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

 

禁用防火墙

# iptables -F

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

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

转载于:https://www.cnblogs.com/alanfang/p/4994516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值