(一) 设置开机启动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的更多相关文章

  1. Ubuntu中保存iptables防火墙规则

    Ubuntu中保存iptables防火墙规则的例子 打开防火墙 ufw disableufw statusufw enable ufw allow 22/tcp ufw reload iptables ...

  2. ubuntu中防火墙iptables配置

    特别说明:此文章完全转载于https://www.cnblogs.com/EasonJim/p/6851007.html 1.查看系统是否安装防火墙 root@localhost:/usr# whic ...

  3. IPTables系列:如何配置Ubuntu 14.04中的IPTables防火墙

    IPTables基本命令 在向大家介绍复杂防火墙规则之前,还是先上一些简单的料,让大家对IPTables最为基本的命令有一些简单了解. 首先要说明的是IPTables命令必需以root权限运行,这意味 ...

  4. ubuntu 中iptables

    ubuntu中启动及关闭iptables 在ubuntu中由于不存在 /etc/init.d/iptales文件,所以无法使用service等命令来启动iptables,需要用modprobe命令. ...

  5. Ubuntu中iptables的使用

    (一) 设置开机启动iptables# sysv-rc-conf --level 2345 iptables on (二) iptables的基本命令 1. 列出当前iptables的策略和规则# i ...

  6. ubuntu 19.10 中防火墙iptables配置

    $sudo which iptables   /usr/sbin/iptables说明有安装 如果没有安装,那么使用sudo apt-get install iptables 安装. 刚装机,是这个样 ...

  7. Ubuntu中开启和关闭防火墙-摘自网络

    1.关闭ubuntu的防火墙 ufw disable开启防火墙ufw enable 2.卸载了iptablesapt-get remove iptables3.关闭ubuntu中的防火墙的其余命令ip ...

  8. ubuntu中rc.local无效

    在ubuntu中写了一点iptables规则,但是,竟然iptables竟然无效,经过多方查找问题...眼泪... 终于发现是rc.local竟然没有运行,我晕.仔细检查iptables脚本n遍,没有 ...

  9. ubuntu中开启、关闭防火墙

    1.关闭ubuntu的防火墙        ufw disable 开启防火墙 ufw enable 2.卸载了iptables        apt-get remove iptables 3.关闭 ...

随机推荐

  1. PHP编译

    编译php-5.3.6的时候需要先打一个内存泄露的补丁,具体的操作如下 tar vzxf suhosin-0.9.32.1.tar.gz tar vjxf php-5.3.6.tar.bz2 cd p ...

  2. go 基础语法

    时间有限,简单记一些常用的,麻烦的不写了 定义变量:可以连续定义,也可以单个定义 var a int    int类型 var a="ds"   默认string类型 a:=&qu ...

  3. java利用反射获取类的属性及类型

    java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...

  4. c#中foreach的一种用法

    以下定义的MyClass类中的addnum方法使用了一个数组型参数b: public class MyClass { ; public void addnum(ref int sum, params ...

  5. php5.6在yum下安装gd库

    yum install php-gd --enablerepo=remi,remi-php56 php.ini配置文件中增加 extension=gd.so 重启web服务器即可

  6. phpexcl导出数据

    <?php public function export_do_one(){ //excel 导出数据 import('ORG.Util.Page_new');// 导入分页类 ,修改了原来的P ...

  7. 给linux虚拟机添加Samba用户

    Window系统连上我们的开发机Linux,自然需要在Samba里添加一个新用户. linux-06bq:/usr/local/services/samba/bin # ./smbpasswd -a  ...

  8. 查看表结构命令(mysql和oracle)

    MySQL查看表结构SQL语句 = mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名; ...

  9. 插入多行数据的时候,一个insert插入多行

    如:insert into t_users(a,b,c)value('1','2','3'),('3','4','5'),('6','7','8') ('1','2','3'),('3','4','5 ...

  10. 使用asp.net mvc部分视图渲染html

    为了提升用户体验,一般我们采用ajax加载数据然后根据数据渲染html,渲染html可以使用前端渲染和服务器端渲染. 前端渲染 使用前端模版引擎或MVC框架,例如underscore.js的templ ...