启动iptables [論壇 - 新手村]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



訪客
启动iptables
Anon:訪客
查到资料,
ubuntu中启动及关闭iptable,在ubuntu中由于不存在 /etc/init.d/iptales文件,所以无法使用service等命令来启动iptables,需要用modprobe命令。
  启动iptables
  modprobe ip_tables

请看,我的终端
sudo modprobe ip_tables
ps -e
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
6 ? 00:00:00 migration/0
7 ? 00:00:00 cpuset
8 ? 00:00:00 khelper
9 ? 00:00:00 netns
10 ? 00:00:00 sync_supers
11 ? 00:00:00 bdi-default
12 ? 00:00:00 kintegrityd
13 ? 00:00:00 kblockd
14 ? 00:00:00 kacpid
15 ? 00:00:00 kacpi_notify
16 ? 00:00:00 kacpi_hotplug
17 ? 00:00:00 ata_sff
18 ? 00:00:00 khubd
19 ? 00:00:00 md
21 ? 00:00:00 kworker/u:1
22 ? 00:00:00 khungtaskd
23 ? 00:00:01 kswapd0
24 ? 00:00:00 ksmd
25 ? 00:00:00 fsnotify_mark
26 ? 00:00:00 aio
27 ? 00:00:00 ecryptfs-kthrea
28 ? 00:00:00 crypto
32 ? 00:00:00 kthrotld
37 ? 00:00:00 scsi_eh_0
39 ? 00:00:00 scsi_eh_1
41 ? 00:00:00 kworker/u:5
42 ? 00:00:00 scsi_eh_2
43 ? 00:00:00 scsi_eh_3
45 ? 00:00:00 kmpathd
46 ? 00:00:00 kmpath_handlerd
48 ? 00:00:00 kondemand
49 ? 00:00:00 kconservative
292 ? 00:00:00 kjournald
338 ? 00:00:00 upstart-udev-br
356 ? 00:00:00 udevd
494 ? 00:00:00 kpsmoused
536 ? 00:00:00 led_workqueue
581 ? 00:00:00 pccardd
634 ? 00:00:00 upstart-socket-
647 ? 00:00:00 hd-audio0
731 ? 00:00:00 kdmflush
758 ? 00:00:00 kcryptd_io
759 ? 00:00:00 kcryptd
804 ? 00:00:00 kjournald
811 ? 00:00:00 kjournald
816 ? 00:00:00 kjournald
821 ? 00:00:00 kjournald
826 ? 00:00:00 kjournald
831 ? 00:00:00 kjournald
840 ? 00:00:00 flush-8:0
843 ? 00:00:00 kjournald
848 ? 00:00:00 kjournald
869 ? 00:00:00 smbd
882 ? 00:00:00 dbus-daemon
899 ? 00:00:00 gdm-binary
910 ? 00:00:00 udevd
911 ? 00:00:00 udevd
920 ? 00:00:00 avahi-daemon
921 ? 00:00:00 avahi-daemon
924 ? 00:00:00 cupsd
946 ? 00:00:00 NetworkManager
952 ? 00:00:00 modem-manager
955 ? 00:00:00 polkitd
957 ? 00:00:00 console-kit-dae
958 ? 00:00:00 smbd
969 ? 00:00:00 wpa_supplicant
1032 ? 00:00:00 dhclient
1037 ? 00:00:00 gdm-simple-slav
1045 tty7 00:05:17 Xorg
1083 tty4 00:00:00 getty
1087 tty5 00:00:00 getty
1094 tty2 00:00:00 getty
1095 tty3 00:00:00 getty
1097 tty6 00:00:00 getty
1099 ? 00:00:00 acpid
1107 ? 00:00:00 cron
1108 ? 00:00:00 atd
1599 ? 00:00:00 nmbd
1692 ? 00:00:00 gdm-session-wor
1707 ? 00:00:00 upowerd
1720 ? 00:00:00 rtkit-daemon
1754 ? 00:00:05 clamd
1912 ? 00:00:03 freshclam
1967 ? 00:00:00 dirmngr
2020 ? 00:00:00 miredo
2027 ? 00:00:00 miredo
2029 ? 00:00:00 miredo-privproc
2179 ? 00:00:00 master
2190 ? 00:00:00 qmgr
2521 ? 00:00:05 snort
2860 ? 00:00:00 gnome-keyring-d
2879 ? 00:00:00 gnome-session
3088 ? 00:00:02 ibus-daemon
3113 ? 00:00:00 ssh-agent
3116 ? 00:00:00 dbus-launch
3117 ? 00:00:01 dbus-daemon
3118 ? 00:00:00 miniserv.pl
3121 tty1 00:00:00 getty
3123 ? 00:00:00 ibus-gconf
3127 ? 00:00:15 python
3129 ? 00:00:00 ibus-x11
3133 ? 00:00:02 gconfd-2
3138 ? 00:00:02 ibus-engine-pin
3140 ? 00:00:00 flush-ecryptfs-
3152 ? 00:00:01 gnome-settings-
3157 ? 00:00:00 gvfsd
3164 ? 00:00:00 gvfs-fuse-daemo
3169 ? 00:00:03 metacity
3171 ? 00:00:00 pulseaudio
3173 ? 00:00:14 nautilus
3182 ? 00:00:00 notify-osd
3183 ? 00:00:00 gconf-helper
3185 ? 00:00:00 nm-applet
3186 ? 00:00:04 gnome-panel
3188 ? 00:00:00 bluetooth-apple
3190 ? 00:00:01 gnome-power-man
3192 ? 00:00:00 evolution-alarm
3194 ? 00:00:00 polkit-gnome-au
3206 ? 00:00:00 syndaemon
3208 ? 00:00:00 gvfs-gdu-volume
3210 ? 00:00:00 udisks-daemon
3212 ? 00:00:00 udisks-daemon
3220 ? 00:00:00 gvfs-gphoto2-vo
3222 ? 00:00:00 gnome-screensav
3224 ? 00:00:00 gvfsd-trash
3227 ? 00:00:00 e-calendar-fact
3228 ? 00:00:00 gvfs-afc-volume
3244 ? 00:00:00 e-addressbook-f
3248 ? 00:00:00 bonobo-activati
3272 ? 00:00:03 wnck-applet
3274 ? 00:00:00 trashapplet
3276 ? 00:00:07 indicator-apple
3278 ? 00:00:00 notification-ar
3309 ? 00:00:00 gvfsd-burn
3312 ? 00:00:00 gdu-notificatio
3319 ? 00:00:00 gvfsd-metadata
3341 ? 00:00:00 dconf-service
3350 ? 00:00:01 indicator-appli
3351 ? 00:00:00 indicator-datet
3354 ? 00:00:00 indicator-sessi
3356 ? 00:00:00 indicator-sound
3358 ? 00:00:00 indicator-me-se
3360 ? 00:00:00 indicator-messa
3388 ? 00:00:00 geoclue-master
3397 ? 00:19:11 firefox-bin
3450 ? 00:00:00 applet.py
3489 ? 00:07:36 plugin-containe
3492 ? 00:00:10 ubuntuone-syncd
3514 ? 00:00:00 zeitgeist-daemo
3525 ? 00:00:00 cat
3529 ? 00:00:00 zeitgeist-datah
3540 ? 00:00:00 update-notifier
3905 ? 00:00:00 bamfdaemon
5749 ? 00:00:00 kworker/0:4
5937 ? 00:00:00 kworker/0:2
6324 ? 00:00:00 kworker/0:1
6350 ? 00:00:00 rsyslogd
6422 ? 00:00:00 pickup
7503 ? 00:00:00 kworker/0:0
7570 ? 00:00:00 gnome-terminal
7574 ? 00:00:00 gnome-pty-helpe
7575 pts/0 00:00:00 bash
7635 pts/0 00:00:00 ps
没有看见iptables这个进程运行?

2011/6/2 12:09 | 8e295 d5e39 c84c0 21df3
應用擴展 工具箱
回覆: 启动iptables

註冊日期:
2009/12/6 10:32
所屬群組:
已註冊使用者
等級: 71
HP : 1053 / 1756
MP : 4456 / 70809
EXP: 24
離線
How To Start iptables
service iptables start
service iptables stop
service iptables restart
service iptables status

2011/6/2 15:29
應用擴展 工具箱
訪客
回覆: 启动iptables
Anon:訪客
service iptables status
iptables: unrecognized service
sudo service iptables status
iptables: unrecognized service
不行呀?

2011/6/2 22:25 | 4c4fd 20658 3fb49 3fd13
應用擴展 工具箱
回覆: 启动iptables

註冊日期:
2009/12/6 10:32
所屬群組:
已註冊使用者
等級: 71
HP : 1053 / 1756
MP : 4456 / 70809
EXP: 24
離線
1. 抱歉 我在 # 2 的 貼文內容 是從 linux 而來 要上去下載 他們的 套件包安裝才行
在 ubuntu 可能需修正如下
sudo apt-get install iptables # 安裝 iptables (安裝 ubuntu 時 應該是 預裝了)
你 把 iptables 輸入 本論壇 右上方 搜尋框
可以找到許多 與 iptables 有關的文章
系統/管理/synaptic
把 iptables 輸入 右上方 快速過濾框
鼠標指向 iptables 按右鍵 選屬性 / 已安裝檔案
可以看到 所有的 安裝檔案 與 安裝路徑
凡是在 /bin 或 /sbin 資料夾後面的檔案名稱 就是 指令
參考指令
iptables — administration tool for IPv4 packet filtering and NAT
凡是在 /doc 資料夾後面的檔案 就是 說明檔案

2. 如果 你只是要簡單使用 防火牆 或 初次 接觸 防火牆 不必專業水平
建議你先使用 ufw :
The Uncomplicated FireWall is a front-end for iptables, to make managing a
Netfilter firewall easier. It provides a command line interface with syntax
similar to OpenBSD's Packet Filter. It is particularly well-suited as a
host-based firewall.
裝 ubuntu 時 就預裝好 ufw
一樣把 ufw 輸入 本論壇 右上方 搜尋框
可以找到許多 與 ufw 有關的文章
sudo ufw enable # 啓用 ufw
sudo ufw status
sudo ufw allow 21 # 開啓 port 21
簡單好用

2011/6/2 23:01
應用擴展 工具箱
訪客
回覆: 启动iptables
Anon:訪客
查看了外文资料,
http://serverfault.com/questions/129086/how-to-start-stop-iptables-on-ubuntu
On Ubuntu, iptables is not a service. In order to stop it, you have to do the following :

sudo iptables-save > /root/firewall.rules
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

In order to restore your previous rules :

iptables-restore < /root/firewall.rules
现在,我的问题是,我可否让它变成一个service,
使得
service iptables start ,stop,restart可以运行?

2011/6/3 9:50 | 594bd 052ed 6574d bbbcc
應用擴展 工具箱
回覆: 启动iptables

註冊日期:
2009/12/6 10:32
所屬群組:
已註冊使用者
等級: 71
HP : 1053 / 1756
MP : 4456 / 70809
EXP: 24
離線
我的问题是,我可否让它变成一个service,
使得
service iptables start ,stop,restart可以运行?


1. Netfilter (封包過濾機制)
在 Linux 上面我們使用核心內建的 Netfilter 這個機制,而 Netfilter 提供了 iptables 這個軟體來作為防火牆封包過濾的指令。由於 Netfilter 是核心內建的功能,因此他的效率非常的高! 非常適合於一般小型環境的設定呢!Netfilter 利用一些封包過濾的規則設定,來定義出什麼資料可以接收, 什麼資料需要剔除,以達到保護主機的目的喔!
所以 Netfilter 與 iptables 都是 compiled 進 linux-kernel 一開機 就支援 Netfilter 與 iptables
如果 你想 改變 Netfilter 與 iptables 的預設行爲 你必須 重新編譯 linux-kernel
Kernel setup
User-land setup

2. ubuntu 的 linux-kernel
ubuntu 因 內設 Network Manager 爲 系統與使用者 的 上網 管理機制 希望 儘量 方便網路 進出
與 Netfilter 與 iptables 的 網路安全 相衝突
所以 自己 編譯 特別的 linux-kernel 雖然 是 一開機 就 啓用 Netfilter 與 iptables
但是 預設 是 全部允許 網路進出 而且 這個 設定是與 Network Manager 的設定掛鉤
如果 你想去 變更它 就會影響 Network Manager 的穩定 (除非 你重新 編譯 ubuntu 的 linux-kernel)
Configuration on startup
Configuration on Startup for NetworkManager
因爲 ubuntu 的 linux-kernel 是 自己編譯過得 與其它 linux 的 distros 的 linux-kernel 稍有差異
所以 你看到的 Netfilter 與 iptables 文章 只能當參考
實際引用時要以 ubuntu 的 文章 爲準 例如:
IptablesHowTo 及其 Further Information
而且 ubuntu 不同版本的 linux-kernel 可能也稍有差異
所以要以 manpage 爲準
man 指令
man iptables

3. ubuntu 的 linux-kernel 是 使用 linux-kernel 並且 經過 ubuntu 重新 編譯的
一開機 就 啓用 Netfilter 與 iptables
所以 你不必去 重新下指令 start
在 ubuntu 如果 你想 stop iptables
請參考
Disabling the firewall
或 參考
#4 系統/管理/synaptic 有提到 有哪些可以使用的指令
但是 這些指令 使用方法 比較複雜
ufw 可以提供你簡單的文字指令 stop 或 restart
sudo ufw disable
sudo ufw status
sudo ufw enable
sudo ufw status

按 alt + F2
gksudo gufw 則可以提供圖形界面

如果 你覺得 這樣 太陽春
無法 滿足 你的需要
那你要 親自 重新 編譯
ubuntu 的 linux-kernel
有需要 有興趣 重新 編譯 請再貼文

2011/6/3 14:05
應用擴展 工具箱
訪客
回覆: 启动iptables
Anon:訪客
iptables 是 Linux 核心 kernel 的一個模組
這個模組名稱為: Netfilter,是 kernel 內建的
操作指令為:iptables

它是核心的一部分,不是軟體(services)
所以一般不會去開關它

---------------------------------------------

iptables 防火牆使用上
如果系統是 run-level3 純文字鍾端機的(沒有安裝 X 視窗界面),一般網路上對外服務的主機大概都是這種,就是啥鍵盤、滑鼠、螢幕通通沒有,只有主機一台,靠遠端 ssh 連線操作。

那麼會使用:
腳本(shell script)來執行 iptables 防火牆規則,或是控制全部通行。

防火牆全部通行 = 也可以看作關閉 iptables 防火牆,作用是相同的

比如:
你可以寫一支腳本

名稱隨意,例:iptables-stop.sh


# 清空 filter 表格規則(無指定表格,則為 filter 表格)
iptables -X

# 清空 nat 表格規則
iptables -t nat -F
iptables -t nat -X

# 清空 mangle 表格規則
iptables -t mangle -F
iptables -t mangle -X

# 指定 filter 表格預設規則
# INPUT、FORWARD、OUTPUT 皆為放行
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT


將這支 iptables-stop.sh 腳本,給予執行權限
擁有者:root:root
權限值:770 或 750(僅 root 可執行,須 sudo)

然後執行這支腳本時,就會清除所有 iptables 防火牆規則,並且預設全部放行(作用相當於關閉防火牆)。


另外將這支腳本,放在 /usr/local/bin 中
這樣執行時,就不必指定路徑
直接打 iptables-stop.sh 就會執行了

/usr/local/bin 是系統預設環境 path 的其中一個
當執行一個檔案,目前目錄沒有時,系統就會去 path 找

還有若覺得 iptables-stop.sh 又臭又長,難打
那取名叫 istop 也行,副檔名也不是必須的
Linux 可執行檔,是看權限的,不看副檔名
總之,腳本的檔名都是隨意,自行高興命名


以上就是:腳本 shell script 的概念

有點類似 windwos 下那個 .bat 批次檔
但是 shell script 比較強大,還可以寫程式


例如:樓主說的

在ubuntu中由于不存在 /etc/init.d/iptales文件,所以无法使用service等命令来启动iptables,需要用modprobe命令。


其實 /etc/init.d 底下的文件,若仔細去看,會發現它們全是 shell script 腳本。

若真的很想要 /etc/init.d/iptales
那麼自己寫也是可行的

還可以這樣子
/etc/init.d/iptables start
/etc/init.d/iptables stop

後面那個 start、stop 參數,就是 shell script 程式設計中的 $1 參數傳入。

因為 shell script 程式設計比較深,非三言兩語可以說的清,這邊就不談了,網上很多教程的。

-----------------------------------------------

再說說 iptables 防火牆腳本

一般在寫這種腳本,不會寫死,而是用變數傳入指令,這樣的好處是以後要修改腳本,只要腳本的檔頭處變數區改一改就好,不必去改程式碼。


比方說我舉個例子:

下面這行是典型的 NAT 語法,IP 偽裝
讓區網內電腦可把 Linux 閘道當 ip 分享器


iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -j SNAT --to-source 11.22.33.44


但是這上面這語法若寫成腳本-不通用

因為不是每個人的網路環境
對外網卡都是:eth0
內部區網都是:192.168.0.0/24
外網 IP 都是:11.22.33.44

那樣要是今天的環境是:
eth1、192.168.1.0/24、55.66.77.88 怎麼辦?

當然就是去改那一行語法,但忙中有錯,眼睛酸看錯

還有這範例只有一行,但實際上 iptables 腳本可不會只有一行
假如有數百、上千行,難道也這樣改?
那成天光改這個,浪費的時間,就什麼事也別幹囉


所以,較佳的寫法是下面這樣



# iptables 防火牆 NAT 腳本

## 變數區

# 對外網卡界面
$EXT_IF="eth0"

# 內部區網網段
$LAN_NAT_SRC="192.168.0.0/24"

# 對外網路 IP
$EXT_IP="11.22.33.44"

#####################################

# #程式區
# 以下為程式區,若不懂 shell script,請勿隨意更動

iptables -t nat -A POSTROUTING -o $EXT_IF -s $LAN_NAT_SRC -j SNAT --to-source $EXT_IP



如上,是不是變的清楚易懂呢

叫算不會寫 shell 程式的人,改改前面的變數區,應該也不是什麼難事吧,而且註解說明都那麼清楚了。


-------------------------------------

上面 shell script 腳本的想法很好,很給力

但是,難道真的得自己寫?
學這個要花多久啊?(數天)
寫這個要多久啊?(看個人資質)

有沒有現成的!?

呵呵呵,這個你就問對了人

現成在此:

單機防火牆 firewall.sh
用途:用於只有一片網卡的 Linux 主機
http://www.box.net/shared/976ftatjbh

閘道防火牆 gw-firewall.sh
用途:用於 Linux 主機有兩片以上網卡,且兼當 NAT 主機(IP 分享器、閘道器))
http://www.box.net/shared/femc55u3q2

( ̄ c ̄)y▂ξ

2011/6/4 1:28 | b16be d83ac a0ef6 afa60
應用擴展 工具箱
訪客
回覆: 启动iptables
Anon:訪客
修正:上面忘了打

shell script 腳本,第一行必須為:

#!/bin/bash


#!/bin/sh 也行

這是宣告要用來執行腳本的 shell
這樣子才能執行腳本。

2011/6/4 1:32 | b16be d83ac a0ef6 afa60
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.