Debian下配置防火墙iptables
debian下iptables输入命令后即时生效,但重启之后配置就会消失,可用iptables-save快速保存配置,因为Debian上iptables是不会保存规则的,然后在开机自动的时候让iptables自动加载刚刚导出的配置文件,方法如下:
若要停止iptables,iptables -F清空所有配置效果等同于停止。
whereis iptables 查找iptables 所在的路径。
1、将iptables配置保存到/etc/iptables,这个文件名可以自己定义,与下面的配置一致即可
iptables-save > /etc/iptables
2、创建自启动配置文件,并授于可执行权限
iptables-save > /etc/iptables
3、编辑该自启动配置文件,内容为启动网络时恢复iptables配置
vi /etc/network/if-pre-up.d/iptables
内容为:
#!/bin/sh
/sbin/iptables-restore < /etc/iptables
保存并退出。这样重启之后iptables就自动加载规则了。
##注意:在下次修改iptables规则之后要重新导出配置文件。
#清空配置
iptables -F
iptables -X
iptables -Z
#配置,禁止进,允许出,允许回环网卡
iptables -P INPUT DROP
iptables -A OUTPUT -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
#允许ping
iptables -A INPUT -p icmp -j ACCEPT
#允许ssh
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允许ftp
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#允许ftp被动接口范围,在ftp配置文件里可以设置
iptables -A INPUT -p tcp --dport 20000:30000 -j ACCEPT
#学习felix,把smtp设成本地
iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT -s 127.0.0.1
iptables -A INPUT -p tcp -m tcp --dport 25 -j REJECT
#允许DNS
iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
#允许http和https
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT #(注意:如果22端口未加入允许规则,SSH链接会直接断开。)
iptables -A FORWARD -j REJECT
#保存配置
iptables-save > /etc/iptables
由于Debian安装iptables后默认不是服务,service iptables会提示unrecognized service,需要添加脚本到/etc/init.d/,脚本如下
建议将其保存为/etc/init.d/iptables,然后chmod +x /etc/init.d/iptables 添加运行权限。
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: iptables
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: start and stop iptables firewall
# Description: Start, stop and save iptables firewall
### END INIT INFO
PATH=”/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin”
IPTABLES=/sbin/iptables
IPTABLES_SAVE=/sbin/iptables-save
IPTABLES_RESTORE=/sbin/iptables-restore
IPTABLES_CONFIG=/etc/iptables.conf
[ -x $IPTABLES ] || exit
. /lib/lsb/init-functions
case "$1" in
start)
log_action_begin_msg "Starting firewall"
type usplash_write >/dev/null >/dev/null && usplash_write "TIMEOUT 120" || true
if $IPTABLES_RESTORE < $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
type usplash_write >/dev/null >/dev/null && usplash_write "TIMEOUT 15" || true
;;
stop)
log_action_begin_msg "Saving current firewall configuration"
if $IPTABLES_SAVE > $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
log_action_begin_msg "Flushing ALL firewall rules from chains!"
if $IPTABLES -F ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
log_action_begin_msg "Deleting ALL firewall chains [Warning: ACCEPTING ALL PORT SERVICES!]"
if $IPTABLES -X ; then
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
log_action_end_msg $?
else
log_action_end_msg $?
fi
;;
save)
log_action_begin_msg "Saving current firewall configuration"
if $IPTABLES_SAVE > $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
;;
force-reload|restart)
log_action_begin_msg "Reloading firewall configuration [Warning: POTENTIAL NETWORK INSECURITY DURING RELOAD]"
$IPTABLES -F
$IPTABLES -X
if $IPTABLES_RESTORE < $IPTABLES_CONFIG ; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
;;
*)
echo "Usage: /etc/init.d/iptables {start|stop|save|restart|force-reload}"
exit
;;
esac
exit
原文出处:https://my.oschina.net/u/2404183/blog/509676?p=1
Debian下配置防火墙iptables的更多相关文章
- CentOS下配置防火墙 配置nat转发服务
CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/sysconfig/network 这里可以更改主机 ...
- debian下配置keepalived ha
抄袭自http://blog.51yip.com/server/1417.html,做了一些修改 可以参考http://blog.linuxphp.org/archives/1615/ 备注:NAT模 ...
- CentOS7下配置防火墙放过Keepalived
Keepalived是一个轻量级的HA集群解决方案,但开启防火墙后各节点无法感知其它节点的状态,各自都绑定了虚拟IP.网上很多文章讲要配置防火墙放过tcp/112,在CentOS7下是无效的,正确的做 ...
- linux下的防火墙iptables
防火墙(firewall),也称为防护墙,是由Check Point创立者Gil Shwed于1993年发明并引入国际互联网.它是一项信息安全的防护系统,依照特定的规则,允许或者是限制传输的数据通过. ...
- 阿里云中Centos下配置防火墙
一.检查iptables服务状态 [root@woxplife ~]# service iptables status iptables: Firewall is not running. 说明ipt ...
- debian下配置nginx缓存
root权限下 新建/usr/nginx/cache/webpages目录 在/etc/nginx/sites-available下找到自己所需配置的文件,打开文件后在开头加上proxy_cache_ ...
- debian下配置dynamic printk以及重新编译内核
在以前的一篇博文<编译debian内核>已经提过了重新编译内核的方法,但是整个过程花费时间较长,并且生成deb包. 这里我采用稍微简单一些的方法,因为我并没有对内核或者驱动代码做任何修改, ...
- centos下配置防火墙port失败
问题:将规则加入到防火墙中.总是port无法开启 (1)改动文件 首先vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp ...
- Debian下配置SSH服务器的方法
Debian 503版本中实现的,Debian默认好像是没有ssh支持的. SSH的安装apt-get install openssh-serverapt-get install sshSSH的配置O ...
随机推荐
- [TJOI2018]碱基序列
嘟嘟嘟 现在看到字符串就想到SAM,所以很担心kmp啥的会不会忘了-- 这题感觉挺暴力的:首先当然要把\(s\)建成SAM,然后令\(dp[i][j]\)表示到第\(i\)组时,SAM上节点\(j\) ...
- JavaScript模块化思想requireJS的使用
1. 使用require.js的意义 (1)实现JS文件的异步加载,避免网页因为加载JS文件缓慢造成网页未响应 (2)管理模块之间的依赖性,便于代码的编写和维护.页面中只需要引入require.j ...
- 【强化学习】用pandas 与 numpy 分别实现 q-learning, saras, saras(lambda)算法
本文作者:hhh5460 本文地址:https://www.cnblogs.com/hhh5460/p/10159331.html 特别感谢:本文的三幅图皆来自莫凡的教程 https://morvan ...
- vscode快捷键大全
一般Ctrl + Shift + P,F1显示命令调色板 Ctrl + P快速打开,转到文件...Ctrl + Shift + N新窗口/实例 Ctrl + Shift + W关闭窗口/实例 Ctrl ...
- 在GridControl控件上绑定图片的几种操作方式
我们知道,基于DevExpress的开发Winform的项目界面的时候,GridControl控件是经常用来绑定数据的,一般以常规的字符内容为主,有时候也会有图片的显示需要,那么如果显示图片,我们应该 ...
- SpringBoot热部署的实现方式
一:热部署的实现 1.使用Spring-boot-devtools 2.使用Spring Loaded 二:devtools(推荐) 一般情况下直接在pom.xml文件添加下面的依赖即可,但eclip ...
- 【全网最全的博客美化系列教程】02.添加QQ交谈链接
全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...
- socketserver + ftp
--------------------------------------------生活不止眼前的苟且,还有诗和远方的田野. day 29 socketserver + ftp # # ----- ...
- functools下的partial模块应用
问题 你有一个被其他python代码使用的callable对象,可能是一个回调函数或者是一个处理器, 但是它的参数太多了,导致调用时出错. 解决方案 如果需要减少某个函数的参数个数,你可以使用 fun ...
- Linux查看端口
1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 2.netstat -tunlp |grep 端口号 用于查看指定的端口号的进程情况 ...