• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • bios
  • 系统安装
  • 系统进程
  • Windows
  • LINUX
  • RedHat/Centos
  • Ubuntu/Debian
  • Fedora
  • Solaris
  • 麒麟系统
  • 红旗Linux
  • 苹果MAC
  • 注册表
  • 其它系统
您的位置:首页 > 操作系统 >Ubuntu/Debian > 在Ubuntu Server是配置iptables防火墙

在Ubuntu Server是配置iptables防火墙

作者:佚名 字体:[增加 减小] 来源:互联网 时间:2017-05-12

佚名 通过本文向大家介绍了ubuntu iptables,ubuntu关闭iptables,ubuntu下iptables,ubuntu安装iptables,ubuntu iptables 保存等相关知识,希望对您有所帮助,也希望大家多多支持linkedu.com
关于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 s
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 在Ubuntu Server是配置iptables防火墙
  • ubuntu iptables 配置脚本代码

相关文章

  • 2017-05-12在Ubuntu上如何手动安装LibreOffice
  • 2017-05-12chkconfig ubuntu 系统服务设置命令
  • 2017-05-12Ubuntu 9.10下搭建LAMP服务器完全图解教程
  • 2017-05-12Ubuntu下VNC远程桌面的客户端和服务器端使用方法
  • 2017-05-12apt-get下载文件的存放目录处理方法
  • 2017-05-12Ubuntu 搭建LNMP环境图文教程 安装MySQL数据库
  • 2017-05-12ubuntu14.04 LTS 下vsftpd安装与配置教程详解
  • 2017-05-12解决Ubuntu系统下Wireshark无响应的方法
  • 2017-05-12Ubuntu上安装字体的教程
  • 2017-05-12ubuntu系统的笔记本触摸板不能单击怎么办?

文章分类

  • bios
  • 系统安装
  • 系统进程
  • Windows
  • LINUX
  • RedHat/Centos
  • Ubuntu/Debian
  • Fedora
  • Solaris
  • 麒麟系统
  • 红旗Linux
  • 苹果MAC
  • 注册表
  • 其它系统

最近更新的内容

    • Ubuntu 15.10系统中怎么使用微信?
    • ubuntu vsftpd匿名用户上传下载的配置方法
    • Ubuntu 面向对象的框架Qt移植到开发板后如何显示中文
    • Ubuntu 13.10安装最新Linux内核的可行方法
    • Ubuntu开启.htaccess的支持配置方法分享
    • VirtualBox虚拟机XP与宿主机Ubuntu互访共享文件夹的实现方法
    • 在Ubuntu系统上安装Webalizer来分析HTTP流量
    • Ubuntu中如何设置vim的行号?Ubuntu中设置vim的行号的方法
    • 解决Ubuntu启动时的Routine check检查时间过长
    • 在Ubuntu系统中定制 Compiz Fusion 特效

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有