• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > 一键配置CentOS iptables防火墙的Shell脚本分享

一键配置CentOS iptables防火墙的Shell脚本分享

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了centos iptables,centos iptables配置,centos 7 iptables,centos iptables命令,centos iptables详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

手里几台VPS配置iptables太繁琐,看到了朱哥的LNMP脚本里有一个自动配置iptables防火墙的脚本,借来改了一下,给需要的人用;
只提供常用端口的设置,如果你有特殊需求只需自行添加或减少相应的端口即可;

使用方法:
./iptables.sh</div>
设置iptables开机自动启动:

完整Shell:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function support_distro(){
if [ -z "`egrep -i "centos" /etc/issue`" ];then
echo "Sorry,iptables script only support centos system now."
exit 1
fi
}
support_distro
echo "============================iptables configure============================================"
# Only support CentOS system
# 获取SSH端口
if grep "^Port" /etc/ssh/sshd_config>/dev/null;then
sshdport=`grep "^Port" /etc/ssh/sshd_config | sed "s/Port\s//g" `
else
sshdport=22
fi
# 获取DNS服务器IP
if [ -s /etc/resolv.conf ];then
nameserver1=`cat /etc/resolv.conf |grep nameserver |awk 'NR==1{print $2 }'`
nameserver2=`cat /etc/resolv.conf |grep nameserver |awk 'NR==2{print $2 }'`
fi
IPT="/sbin/iptables"
# 删除已有规则
$IPT --delete-chain
$IPT --flush
# 禁止进,允许出,允许回环网卡
$IPT -P INPUT DROP  
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT
$IPT -A INPUT -i lo -j ACCEPT
# 允许已建立的或相关连接的通行
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# 限制80端口单个IP的最大连接数为10
$IPT -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP
# 允许80(HTTP)/873(RSYNC)/443(HTTPS)/20,21(FTP)/25(SMTP)端口的连接
$IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
# 允许SSH端口的连接,脚本自动侦测目前的SSH端口,否则默认为22端口
$IPT -A INPUT -p tcp -m tcp --dport $sshdport -j ACCEPT
# 允许ping
$IPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
$IPT -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
# 允许DNS
[ ! -z "$nameserver1" ] && $IPT -A OUTPUT -p udp -m udp -d $nameserver1 --dport 53 -j ACCEPT
[ ! -z "$nameserver2" ] && $IPT -A OUTPUT -p udp -m udp -d $nameserver2 --dport 53 -j ACCEPT
# 保存规则并重启IPTABLES
service iptables save
service iptables restart
echo "============================iptables configure completed============================================"
</div>

</div>

您可能想查找下面的文章:

  • 一键配置CentOS iptables防火墙的Shell脚本分享

相关文章

  • Shell脚本字符串单引号和双引号的区别浅析
  • Linux SHELL if命令参数说明
  • Shell字符串截取的详细方法
  • shell统计pv和uv、独立ip的方法
  • Shell脚本实现检测Cygwin最快的镜像站点
  • shell中冒号的特殊用法分享
  • 检查linux网络状态的两个脚本
  • linux shell命令行选项与参数用法详解
  • centos中mysql备份数据库脚本分享
  • shell脚本编写的俄罗斯方块游戏代码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Linux下JDK中文字体乱码的解决方法
    • Linux 中C语言getcwd()函数的用法
    • linux中kvm的安装及快照管理
    • linux 环境 mysql写入中文报错
    • 对用户输入的判断的shell实现代码
    • linux 驱动之Kconfig文件和Makefile文件实例
    • Shell脚本while、until循环语句简明教程
    • linux链接编译详解
    • Shell脚本实现删除邮件队列
    • 判断文件是否存在的shell脚本代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有