(一) 设置开机启动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. Bash命令行编辑

    1.Readline库和命令行编辑 bash shell提供了两个内置编辑器:emacs和vi,利用它们可以以交互模式对命令行列表进行编辑,这项特性是通过Readline库的软件包实现的.当使用命令行 ...

  2. Perl正则表达式例子

    Perl正则表达式 一.介绍 正则表达式各语言都有自己的规范,但是基本都差不多,都是由元字符的组合来进行匹配:由于Nmap内嵌的服务与版本探测是使用的Perl正则规范,因此此篇博客记录一下Perl正则 ...

  3. RPM基础知识

    RPM包命名原则 httpd-2.2.15-15.el6.centos.1.i686.rpm httpd       软件包名 2.2.15       软件版本 15      软件发布的次数 el ...

  4. PHP判断字符串中是否含有中文

    <?php $str = "测试中文"; echo $str; echo "<hr>"; //if (preg_match("/^[ ...

  5. [leetcode-557-Reverse Words in a String III]

    Given a string, you need to reverse the order of characters in each word within a sentence whilestil ...

  6. 【Android Developers Training】 17. 停止和重启一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. jq-animate

    jq-animate: <!doctype html> <html> <head> <meta charset="utf-8"> & ...

  8. JDBC01 利用JDBC连接数据库【不使用数据库连接池】

    目录: 1 什么是JDBC 2 JDBC主要接口 3 JDBC编程步骤[学渣版本] 5 JDBC编程步骤[学神版本] 6 JDBC编程步骤[学霸版本] 1 什么是JDBC JDBC是JAVA提供的一套 ...

  9. Tomcat管理页面配置

    详情参考:http://www.365mini.com/page/tomcat-manager-user-configuration.htm 修改$CATALINA_BASE/conf/tomcat- ...

  10. ssh自动化出现的莫名报错

    代码如: ssh -q user@host <<EOF localhost EOF 会出现提示如: Pseudo-terminal will not be allocated becaus ...