开机扫描网络执行iptables控制,仅允许特定ip端口被同网段访问

本文提供了一个用于配置iptables防火墙规则的Shell脚本实例。该脚本首先确保iptables服务已启动并运行,然后定义了一系列规则来限制特定端口的访问,并允许指定网段的流量通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此处以samba服务为例

#!/bin/bash
# chkconfig: 2345 10 90
# description: iptables ctrl
export_path=/usr/local/iptables_ctrl.sh

function start_iptables_ctrl() {
    # start iptables
    iptables_status=$(systemctl status iptables | grep 'Active' | awk '{print $2}')
    if [[ ( -n $iptables_status ) && $iptables_status -eq 'Active' ]]; then
      echo "iptables started"
    else
      systemctl enable iptables
      systemctl start iptables
      echo "iptables restart"
    fi

    sleep 30
    echo `iptables -nL | grep 'Chain DBAAS'`
    iptables -t filter -N DBAAS # 创建DBaas链
    iptables -t filter -I INPUT -j DBAAS # 关联DBaas链

    iptables -I DBAAS -p tcp --dport 139 -j DROP  # 拒绝所有主机访问本机 tcp/udp xx 端口
    iptables -I DBAAS -p tcp --dport 445 -j DROP  # 拒绝所有主机访问本机 tcp/udp xx 端口
    iptables -I DBAAS -p udp --dport 137 -j DROP  # 拒绝所有主机访问本机 tcp/udp xx 端口
    iptables -I DBAAS -p udp --dport 138 -j DROP  # 拒绝所有主机访问本机 tcp/udp xx 端口

    ips=($(ip addr | grep -v inet6 | grep inet | awk '{print $2}'))

    for i in ${ips[@]} ; do
        echo $i
        ip1=$i
        ip1g=`echo $ip1 | awk -F '/' '{print $2}'`
        ip1cal=`ipcalc -n $ip1 | awk -F '=' '{print $2}'`
        ipresp=$ip1cal"/"$ip1g
        echo $ipresp

        iptables -I DBAAS -s $ipresp -p tcp --dport 139 -j ACCEPT # 配置允许某一网段访问本机所有端口
        iptables -I DBAAS -s $ipresp -p tcp --dport 445 -j ACCEPT # 配置允许某一网段访问本机所有端口
        iptables -I DBAAS -s $ipresp -p udp --dport 138 -j ACCEPT # 配置允许某一网段访问本机所有端口
        iptables -I DBAAS -s $ipresp -p udp --dport 137 -j ACCEPT # 配置允许某一网段访问本机所有端口
    done

    echo "config iptables success!"

    # start check task
#    sed -n  '/start_iptables_ctrl.sh/p' /var/spool/cron/root |grep start_iptables_ctrl.sh
#    let res=$?
#    if [[ $res = 1 ]]; then
#      echo "* * * * * ${export_path}/start_iptables_ctrl.sh" >> /var/spool/cron/root
#    fi
}

function main() {
    start_iptables_ctrl
}

main "$@"
exit $?

cd /etc/init.d

vi servicename  # 此步骤千万不要带文件后缀,这里只有服务名称,这里服务名称使用iptablesctrl,将脚本复制进来

chmod 755 servicename

chkconfig servicename on
reboot # 重启检查是否执行

iptables -nL # 查看规则添加情况

iptables -F DBAAS # 删除规则(此处仅记录经常使用命令,非必要执行)
要实现对特定IP地址的端口控制访问控制,可以使用iptables配置相应的规则。首先,确保你已经安装了iptables并且拥有管理员权限。以下是一个具体的操作步骤和示例代码,帮助你设置针对特定IP地址的端口控制访问控制规则: 参考资源链接:[Linux防火墙Iptables详解:规则、功能与实现](https://wenku.youkuaiyun.com/doc/65kgnu5z39) 1. 首先,清理当前的所有规则,以便开始一个新的配置。这一步非常关键,可以防止新规则与旧规则发生冲突: ``` iptables -F iptables -X iptables -Z ``` 2. 设置默认策略。为了确保安全性,你可以将默认策略设置为拒绝所有进入的连接: ``` iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT ``` 3. 添加规则以允许来自特定IP的连接。例如,如果你想允许IP地址为***.***.*.***的机器访问本机的HTTP(80端口)和HTTPS(443端口),可以使用以下命令: ``` iptables -A INPUT -s ***.***.*.*** -p tcp --dport 80 -j ACCEPT iptables -A INPUT -s ***.***.*.*** -p tcp --dport 443 -j ACCEPT ``` 4. 如果你还想限制来自IP的其他端口访问,例如只允许访问特定服务的端口,可以继续添加相应的规则: ``` iptables -A INPUT -s ***.***.*.*** -p tcp --dport 21 -j ACCEPT # 允许访问FTP服务端口 iptables -A INPUT -s ***.***.*.*** -p tcp --dport 22 -j ACCEPT # 允许访问SSH服务端口 ``` 5. 保存规则,以便在重启后规则依然有效。可以使用iptables的保存脚本或第三方工具如iptables-persistent: ``` service iptables save ``` 或者 ``` iptables-persistent save ``` 通过以上步骤,你可以对特定IP地址的端口进行控制访问控制。请记住,正确的配置防火墙规则对于网络安全至关重要。在应用规则之前,务必仔细检查每一条规则,避免误操作导致服务不可用或不必要的安全漏洞。 如果你希望进一步学习关于iptables的高级应用、安全策略的制定以及如何防御各种网络攻击,请查阅《Linux防火墙Iptables详解:规则、功能与实现》。这本书将为你提供更全面的iptables知识,帮助你成为网络安全领域内的专家。 参考资源链接:[Linux防火墙Iptables详解:规则、功能与实现](https://wenku.youkuaiyun.com/doc/65kgnu5z39)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值