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>