Getting started with iptables in Linux开始使用Linux的iptables

by Sukrit Dhandhania on December 8, 2008 Sukrit Dhandhania 08年12月8日

Linux操作系统安全

Linux machines are known to be pretty secure. Linux的机器被称为是很安全。 Studies have shown that Linux has been designed in a secure manner.研究表明, Linux已经设计了一个安全的方式进行。 Yet, despite all the security features that come bundled with a Linux installation, you need to configure these features correctly to make them work for you.然而,尽管所有的安全功能,捆绑了Linux的安装,你需要配置这些功能的正确,使他们为你工作。 I’ll guide you through the process of setting up of one of the tools that help secure your machine - the firewall.我将引导您完成的过程中设立的一个工具,帮助确保您的机器-防火墙。 We will use the iptables firewall for this exercise.我们将利用iptables防火墙这项工作。 I am assuming that you are using a server running Red Hat Enterprise Linux 4 or similar.我假设您使用的是服务器上运行红帽企业Linux 4或相似的。 However, most of the steps should work fine on other Linux distributions as well.然而,大多数的工作步骤,罚款应在其他Linux发行版,以及。 In this article we will setup a firewall on a Linux server running the Apache Web Server, FTP, and SSH.在本文中我们将设置一个防火墙,一个Linux服务器上运行Apache Web服务器, FTP和的SSH 。

Let us first see what ports these applications use and which of them need to have a port open on the firewall.让我们先看看这些应用程序接口,并使用他们需要有一个开放的端口在防火墙上。

The Apache web server runs on port 80 by default. Apache Web服务器上运行的端口80的默认。 Apache is going to server all our web content on this port, therefore we need to keep this port open on the firewall. Apache是将服务器所有的网站内容在这个港口,因此,我们必须保持开放此端口在防火墙上。 The SSH service runs on port 22.所使用的SSH服务上运行的端口22 。 We need to be able to remotely connect to our server to work, so we keep it open.我们需要能够远程连接到我们的服务器的工作,所以我们保持开放。 FTP runs on port 21 and it too needs the port to be open to communication.上运行的FTP端口21 ,它也需要港口的开放沟通。

Next, make sure you have iptables installed.下一步,请确认你已经安装了iptables 。 Run this command as the root user:运行此命令的根使用者:

# rpm -qa | grep iptables转基地| grep iptables

If you have iptables installed the system should give you the version of iptables you have installed.如果您已经安装了iptables系统应该给你的版本iptables您已经安装。 In case you don’t you can try something like the following to get it and start it:如果您没有您可以尝试像下列事项,以获得它和启动它:

# yum install iptables百胜安装iptables

# /etc/init.d/iptables start的/ etc / init.d / iptables启动

To check what kind of configuration iptables is currently running with:要检查什么样的配置iptables目前正在执行的:

# iptables –listiptables名单
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目标蛋白质来源的目的地选择

This command will list out all the firewall rules that have been set currently.此命令将列出所有防火墙规则已经确立目前。 I will proceed with the assumption that you do not have any firewall rules in your iptables configuration.我将着手进行,前提是你没有任何防火墙规则中的iptables的配置。 Let’s now configure the firewall to allow open communication on the ports 80 for your web server, 22 for SSH, and port 21 for FTP.我们现在配置防火墙允许开放式通信的端口80为您的Web服务器, 22日的SSH ,端口21用于FTP 。 We’ll also make sure that we block communication to any port other than specified.我们也将确保我们的沟通,以阻止任何港口以外的其他规定。

Here’sa firewall script configuration script.下面的防火墙脚本配置脚本。 Create a new file and call it iptable-firewall.sh .创建一个新的文件,并要求它iptable - firewall.sh Copy the following text into it:复制下列文字到它:

#!/bin/sh # !程序/ bin / sh

ANY=”0/0″任何= “ 0 / 0 ”
OPEN_PORTS=”21 22 80″ OPEN_PORTS = “ 21 22 80 ”

iptables -P INPUT ACCEPT iptables磷输入接受
iptables -P FORWARD ACCEPT iptables磷正接受
iptables -P OUTPUT ACCEPT iptables磷输出接受

# Flush (-F) all specific rules #冲洗( - F )的所有具体规则
iptables -F INPUT iptables氟输入
iptables -F FORWARD iptables氟演
iptables -F OUTPUT iptables氟输出

for port in $OPEN_PORTS港口OPEN_PORTS美元
do
iptables -A INPUT -i eth0 -p tcp -s $ANY -d $ANY –destination-port $port –syn -j ACCEPT iptables一个输入一eth0这个一p的TCP美元的任何一d $任何目的地端口$港口综合征约翰逊接受
iptables -A INPUT -i eth1 -p tcp -s $ANY -d $ANY –destination-port $port –syn -j ACCEPT iptables一输入我需要连接它一p的TCP美元的任何一d $任何目的地端口$港口综合征约翰逊接受
done做完

iptables -A INPUT -i eth1 -p icmp -s $ANY -d $ANY -j ACCEPT iptables一输入我需要连接它一p ICMP数据,美元的任何一d $任何约翰逊接受

#Allow any related/established connections #允许任何相关/建立连接
iptables -A INPUT -i eth0 -m state –state ESTABLISHED,RELATED -j ACCEPT iptables一个输入一eth0这个米国国家设立,有关约翰逊接受
iptables -A INPUT -i eth1 -m state –state ESTABLISHED,RELATED -j ACCEPT iptables一输入我需要连接它米国有国家设立,有关约翰逊接受

#Kill everything else #杀一切
iptables -A INPUT -i eth0 -j DROP iptables一个输入一eth0这个约翰逊降
iptables -A INPUT -i eth1 -j DROP iptables一输入我需要连接它约翰逊降

#write for boot #写的开机
iptables-save > /etc/sysconfig/iptables iptables保存“的/ etc / sysconfig / iptables

Now save the above file, grant it executable permissions and then run it:现在保存的上述档案,给予可执行权限,然后运行它:

# chmod +x iptable-firewall.sh属性+ x的iptable - firewall.sh

# ./iptable-firewall.sh / iptable - firewall.sh

Now check your firewall rules:现在检查你的防火墙规则:

# iptables –listiptables名单

All your firewall rules should now be set.您所有的防火墙规则现在应该设定。 Your server is now secure.您的服务器现在是安全的。 To make any modification or additions to this set of rules, edit the line where the OPEN_PORTS parameter is defined and add or remove ports form the list.为了使任何修改或补充这套规则,编辑线的OPEN_PORTS参数的定义和添加或删除港口组建名单。 Remember to run the script again after making any changes to it.请记住,运行该脚本后再次作出任何更改。

If all of this command line stuff has you a bit leery, see the tutorial如果所有的此命令行的东西你已经有点持怀疑态度,看到教程 How to setup Firestarter - an easy to use Linux Firewall如何设置纵火-易于使用的L inux防火墙 - which has an easy to use graphical interface. -其中有一个易于使用的图形界面。

Related Posts: 相关文章:
  • How to set up Evolution for email如何建立电子邮件的演变
  • How to send email from the Linux command line如何发送电子邮件从Linux命令行
  • How to install, setup and use Google Desktop Search in Ubuntu如何安装,安装和使用谷歌桌面搜索在Ubuntu的
  • How to install Windows programs in Linux using Wine如何安装Windows程序在Linux中使用酒
  • How to play .rmvb files in Ubuntu如何发挥。 rmvb Ubuntu的档案
  • Get Simple Help tutorials just like this one in your email inbox every day - for free!获取简单的帮助教程就这样一个在您的电子邮件收件匣每一天-是免费的! Just enter your email address below:只要输入您的电子邮件地址如下:

    You can always opt out of this email subscription at any time.您可以随时退出这个邮件订阅在任何时候。


    Bookmark and Share 书签和共享

    { 0 comments… 0评论... add one now现在添加一个 }

    Leave a Comment发表您的评论

    You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> 您可以使用这些HTML标记和属性: href="" title="">的<a <abbr title=""> <acronym title="">的<b> <blockquote cite=""> <cite>的<code> <del datetime="">的<em> <i> <q cite=""> <strike>的<strong>