Debian配置iptables
阿新 • • 發佈:2018-08-08
save 密碼登錄 chkconfig sco udp端口 pad cal dpt ruby 前言
iptables在Linux發行版本如Centos、Debian、Ubuntu、Redhat等的配置內容基本一致,但是配置方式有所不同。由於工作日常用的是Centos 6.x,它的配置較簡單。下面對Debian上配置iptables做一個說明。
配置過程
環境要求
一臺Debian 6.x/7.x及以上版本的機器
登錄用戶名/密碼
配置步驟
使用用戶名/密碼登錄系統
root@localhost:~#
安裝iptables
root@localhost:~# apt-get install iptables
安裝成功後查看iptables端口開放情況
root@localhost:~# iptables -LChain 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 root@localhost:~#
INPUT、FORWARD和OUTPUT鏈中均沒有內容,說明此時iptables是空的,沒有進行任何配置。
配置iptables
(1)編輯iptables.test.rules,保存其配置
root@localhost:~# vi /etc/iptables.test.rules// 內容如下# Generated by iptables-save*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -I INPUT -p tcp --dport 8888 -j ACCEPT -I INPUT -p udp --dport 8888 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT# Completed
上述是一個模板,可以根據需求進行修改。如上開放了tcp22端口,以及8888的tcp和udp端口。
(2)加載iptables.test.rules規則,並查看
root@localhost:/home# iptables-resotre < /etc/iptables.test.rulesroot@localhost:/home# iptables -LChain INPUT (policy ACCEPT) target prot opt source destination ACCEPT udp -- anywhere anywhere udp dpt:8888ACCEPT tcp -- anywhere anywhere tcp dpt:8888ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination root@localhost:/home#
(3)設置開機啟動
root@localhost:/home# vi /etc/network/if-pre-up.d/iptables// 內容如下#!/bin/bash/sbin/iptables-restore < /etc/iptables.test.rules root@localhost:/home# chmod +x /etc/network/if-pre-up.d/iptables
(4)重啟查看是否開機加載
root@localhost:/home# rebootroot@localhost:~# iptables -L
結束語
按照上述步驟進行配置,可以實現開機加載iptables規則。如果發生問題,多配置幾遍就可以。Centos的配置主要在/etc/sysconfig/iptables進行配置,然後使用chkconfig開啟開機啟動即可。在實際的運維需求中,可以根據應用系統情況,合理的開放端口。
Debian配置iptables