Debian 系统如何配置 iptables

栏目: 服务器 · 发布时间: 7年前

内容简介:iptables在Linux发行版本如Centos、Debian、Ubuntu、Redhat等的配置内容基本一致,但是配置方式有所不同。由于工作日常用的是Centos 6.x,它的配置较简单。下面对Debian上配置iptables做一个说明。INPUT、FORWARD和OUTPUT链中均没有内容,说明此时iptables是空的,没有进行任何配置。上述是一个模板,可以根据需求进行修改。如上开放了tcp22端口,以及8888的tcp和udp端口。

iptables在 Linux 发行版本如Centos、Debian、Ubuntu、Redhat等的配置内容基本一致,但是配置方式有所不同。由于工作日常用的是Centos 6.x,它的配置较简单。下面对Debian上配置iptables做一个说明。

配置步骤

  1. 使用用户名/密码登录系统
root@localhost:~#
  1. 安装iptables
root@localhost:~# apt-get install iptables
  1. 安装成功后查看iptables端口开放情况
root@localhost:~# iptables -L
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         
root@localhost:~#

INPUT、FORWARD和OUTPUT链中均没有内容,说明此时iptables是空的,没有进行任何配置。

  1. 配置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 8080 -j ACCEPT
-I INPUT -p udp --dport 8080 -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.rules
root@localhost:/home# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere             udp dpt:8080
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:8080
ACCEPT     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# reboot
root@localhost:~# iptables -L

结束语

按照上述步骤进行配置,可以实现开机加载iptables规则。如果发生问题,多配置几遍就可以。Centos的配置主要在/etc/sysconfig/iptables进行配置,然后使用chkconfig开启开机启动即可。在实际的运维需求中,可以根据应用系统情况,合理的开放端口。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算的本质

计算的本质

[英] Tom Stuart / 张伟 / 人民邮电出版社 / 2014-11 / 69.00元

《计算的本质:深入剖析程序和计算机》借助Ruby全面介绍计算理论和编程语言的设计。作者注重实用性,不仅尽量抛开复杂难懂的数学符号,而且特别选用简单快捷的编程语言Ruby,在读者熟知的背景知识下,以明晰的可工作代码阐明形式语义、自动机理论,以及通过lambda演算进行函数式编程等计算机科学知识,并为让其自行探索做足准备。 本书适合计算机科学系学生,以及熟知现代编程语言,想要系统地学习计算机科学......一起来看看 《计算的本质》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具