iptables 是一个安装在Ubuntu Server上的默认防火墙。在正常的ubuntu安装过程中,iptables是被安装上了的,但是它默认允许所有的流量(不管防火墙是否是无效的)

关于iptables有价值的信息很多,但是大多都描述的很复杂。如果你想做些基本的配置,下面的 How To 很适合你。
◆ 基本命令
键入:
# 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 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
◆ 在指定端口上允许入站流量
阻断所有流量您也可以启动系统,但是您可能正在通过SSH工作,所有在您阻断其他流量前有必要允许SSH流量。
为了在22端口号(默认的SSH端口)上的允许流量入站,您可以告诉iptables允许您的网卡接受所有的目的端口为22的TCP流量。
# iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT
特别的,这将向表中追加(-A)INPUT规则,允许目的端口号为SSH的所有流量进入接口(-i) eth0,以便iptables完成跳转(-j)或动作:ACCEPT
让我们核对下这些规则:(这里仅显示了少数行,您应该看到更多)
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
现在,让我们允许所有的web流量
# iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
检查我们现有的规则
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:www
我们已经指定SSH和web端口为允许通过的TCP流量,但是因为我们还没阻断任何流量,所以到目前为止所有的流量仍然可以进入。
◆ 阻断流量
一旦一条规则对一个包进行了匹配,其他规则不再对这个包有效。因为我们的规则首先允许SSH和WEB流量,所以只要我们阻断所有流量的规则紧跟其後,我们依然能接受我们感兴趣的流量。我们要做的仅仅是把阻断所有流量的规则放在最後,所以我们需要再次用到它。
# iptables -A INPUT -j DROP
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:www
DROP all -- anywhere anywhere
因为我们刚才没有指定一个接口或一个协议,所以除了web和ssh流量外其他任何流量都会被阻断。
◆ 编辑 iptables

到目前为止我们设置过程中唯一的问题是回环端口(loopbakc)也被阻断了。我们本可以通过指定 -i eth0 来仅仅丢弃eth0上的数据包,但我们也可以为回环端口(loopback)添加一条规则。如果我们追加这条规则,这将太晚了----因为所有的流量已经 被丢弃。我们必须插入这条跪着到第4行。
# iptables -I INPUT 4 -i lo -j ACCEPT
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:www
ACCEPT all -- anywhere anywhere
DROP all -- anywhere anywhere
最後2行看起来几乎一样,因此我们可以让iptables列的更详细些。
# iptables -L -v
◆ 日志记录
在上面的例子中,所有的流量都不会被记录。如果您愿意在syslog中记录被丢弃的包, 下面将是最快捷的方式:
# iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
看 提示 段获得更多关于logging的ideas.
◆ 保存 iptables

如果您现在要重新启动机器的话,您的iptables配置将会消失。为了不用每次重新启动时敲入这些命令,您可以保存你的配置,让它在系统启动时自动启动。你可以通过iptables-save 和iptables-restore命令来保存配置。
◆ 配置启动时自动加载规则
保存您的防火墙股则到一个文件
# iptables-save > /etc/iptables.up.rules
接着修改 /etc/network/interfaces 脚本自动应用这些规则(末行是添加的)
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.up.rules
你也可以准备一组规则冰并自动应用它
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-restore < /etc/iptables.down.rules
◆ 提示
◆ 如果你要在一个规则基础上手动编辑iptables
下面的步骤复习了怎样建立你的防火墙规则,并假定它们相对固定(而且对于大多数人来说它们也应该是)。但是如果你要做许多研究工作,你也许想要你的 iptables在你每次重启时保存一次。你可以在 /etc/network/interfaces 里添加像下面的一行:
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-save > /etc/iptables.up.rules
"post-down iptables-save > /etc/iptables.up.rules" 此行将保存规则用于下次启动时使用。
◆ 用iptables-save/restore来测试规则
如果你超出了这个指南来编辑iptables,你可能想利用iptables-save和iptables-restore来编辑和测试你的规则。你可以通过使用你喜爱的文本编辑器(此处为gedit)来打开这些规则文件来完成编辑。
# iptables-save > /etc/iptables.test.rules
# gedit /etc/iptables.test.rules
你会得到一个如下类似的文件(下面是紧接上的例子文件):
# Generated by iptables-save v1.3.1 on Sun Apr 23 06:19:53 2006
*filter
:INPUT ACCEPT [368:102354]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [92952:20764374]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A INPUT -j DROP
COMMIT
# Completed on Sun Apr 23 06:19:53 2006
注意到这些都是减去iptables命令的iptables语句。随意编辑这些命令、完成後保存它们。然後简单的测试下:
# iptables-restore < /etc/iptables.test.rules
测试完毕後,如果你还没添加iptables-save命令 到 /etc/network/interfaces 里面,记得不要丢失了你的更改:
# iptables-save > /etc/iptables.up.rules
◆ 更详细的日志
为了在你的syslog中获得更多细节,你可能想创建一个额外的链。下面是个很简短的例子---我的 /etc/iptables.up.rules ,它将展示我是如何设置iptables记录到syslog中的:
# Generated by iptables-save v1.3.1 on Sun Apr 23 05:32:09 2006
*filter
:INPUT ACCEPT [273:55355]
:FORWARD ACCEPT [0:0]
:LOGNDROP - [0:0]
:OUTPUT ACCEPT [92376:20668252]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -j LOGNDROP
-A LOGNDROP -p tcp -m limit --limit 5/min -j LOG --log-prefix "Denied TCP: " --log-level 7
-A LOGNDROP -p udp -m limit --limit 5/min -j LOG --log-prefix "Denied UDP: " --log-level 7
-A LOGNDROP -p icmp -m limit --limit 5/min -j LOG --log-prefix "Denied ICMP: " --log-level 7
-A LOGNDROP -j DROP
COMMIT
# Completed on Sun Apr 23 05:32:09 2006
请注意 一个名为 LOGNDROP的链在文件顶部。而且,INPUT链底部标准的DROP被替换成了LOGNDROP,同时添加了协议描述so it makes sense looking at the log。最後我们在LOGNDROP链尾部丢弃了这些流量。下面的行告诉我们发生了什么:
* --limit 设置记录相同规则到syslog中的次数
* --log-prefix "Denied..." 添加一个前缀使得在syslog中查找更easy
* --log-level 7 设置syslog的消息级别 (see man syslog for more detail, but you can probably leave this)
◆ 禁用防火墙
如果您要临时禁用防火墙,您可以通过下面的命令清空所偶的规则:
# iptables -F
◆ 轻松配置通过 GUI
新手可以利用 Firetarter(一个gui工具)---仓库中的可用软件(新立德或apt-get 获得)来配置她或他的iptables规则,而需要命令行知识。请查看指南,尽管...... 配置很简单,但是对于高级用户来说可能远远不能满足。然而它对于大多数的家庭用户来说是足够的...... 。(我)建议您使用firestarter在策略表中将出站配置为 “限制”,而将您需要的连接类型(如用于http的80、https的443,msn chat的1683等等)加入白名单。您也可以通过它查看进出您计算机的活动连接...... 。防火墙会一直保持下去一旦通过向导配置完毕。拨号用户必须在向导中指定它在拨号时自动启动

在Ubuntu Server是配置iptables防火墙的更多相关文章

  1. Ubuntu 14.04 配置iptables防火墙

    Ubuntu默认安装是没有开启任何防火墙的,为了服务器的安全,建议大家安装启用防火墙设置,这里推荐使用iptables防火墙.如果mysql启本地使用,可以不用打开3306端口. # whereis ...

  2. Ubuntu Server如何配置SFTP

    SH File Transfer Protocol是一个比普通FTP更为安全的文件传输协议.(参考资料:http://en.wikipedia.org/wiki/SSH_File_Transfer_P ...

  3. [转]ubuntu server上网配置

    [转]ubuntu server上网配置 http://blog.sina.com.cn/s/blog_6c9d65a101011pyt.html 今天我的ubuntu server上不去网了,所以重 ...

  4. Ubuntu server 网络配置中遇到的问题

    Ubuntu server 网络配置中遇到的问题 图片中ip地址有可能和文字不符,请不要在意太多,知道原理即可 - 1.首先就是要配置ip地址 vim /etc/network/interfaces ...

  5. Ubuntu Server如何配置SFTP(建立用户监狱)

    Ubuntu Server如何配置SFTP(建立用户监狱)   SSH File Transfer Protocol是一个比普通FTP更为安全的文件传输协议.(参考资料:http://en.wikip ...

  6. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  7. 2-10~2-11 配置iptables防火墙增强服务 selinux简单讲解

    学习一个服务的过程: 1.此服务器的概述:名字,功能,特点,端口号 2.安装 3.配置文件的位置 4.服务启动关闭脚本,查看端口 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上, ...

  8. 在Ubuntu Server下配置LAMP环境

    1. 下载Ubuntu Server,地址https://www.ubuntu.com/download/server 2. 在虚拟机上安装Ubuntu Server.根据安装引导过程一步步安装,跟在 ...

  9. 实战Ubuntu Server上配置LXDE+VNC环境

    1.安装x-window 使用apt-get 安装 xorg sudo apt-get install xorg 如果提示以下内容,就说明需要update下源列表,使用sudo apt-get upd ...

随机推荐

  1. HTTP协议中 POST和GET的区别

    http://blog.csdn.net/whuslei/article/details/6667095 权威点的说明请参考:http://www.cs.tut.fi/~jkorpela/forms/ ...

  2. HEVC 实时编码720P不是梦!

    最近很少光顾我的博客, 因为一直很忙! 目前我的HEVC在i5机子上720P编码速度单核达到2~3帧每秒! 多核的话离线实时编码已经不是问题! 现在问题是在线实时编码还差那么一点! 在容忍一定低延迟下 ...

  3. andriod arcgis createPolygons创建带空的面

    private void createPolygons() { // create input polygon 1 PointCollection pointsPoly = new PointColl ...

  4. POPSpring动画参数详解

    POPSpring动画参数详解 效果 源码 https://github.com/YouXianMing/Animations // // POPSpringParameterController.m ...

  5. JS动态改变select选择变更option的index值

    document.getElementById("louyuming").options[0].selected=true; function jsSelectIsExitItem ...

  6. VC调用QT的UIDLL

    //VC程序#include "../QTDLL/ExportDll.h" int _tmain(int argc, _TCHAR* argv[]) { printf(" ...

  7. [开发工具]_[VS2010]_[vs2010的一个bug-使用stringstream时出现]

    1. 注冊Microsfot之后想提交bug, 发现有这个提示, 所以提交不了bug, 有能提交的提交下吧. You are not authorized to submit the feedback ...

  8. <mvc:annotation-driven />做了什么

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  9. 数据库实例: STOREBOOK > 用户 > 编辑 用户: MGMT_VIEW

    ylbtech-Oracle:数据库实例: STOREBOOK  >  用户  >  编辑 用户: MGMT_VIEW 编辑 用户: MGMT_VIEW 1. 一般信息返回顶部 1.1, ...

  10. QT QCharts QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失

    在最近接到的需求是这样的,画一个折线图,关键点使用空心的圆点标识出来,鼠标移动到关键点上,显示出当前数值:鼠标移走数值消失. 我们遇到这个需求的时候,第一时间就会想到使用 QLineSeries 画折 ...