Linux常用脚本(七) ---简单防火墙管理

#!/bin/bash

# 检查是否以 root 权限运行
if [ "$(id -u)" -ne 0 ]; then
    echo "请使用 root 权限运行此脚本"
    exit 1
fi

# 用法
usage() {
    echo "用法: $0 {start|stop|status|flush}"
    echo "  start  : 启动防火墙并设置基本规则"
    echo "  stop   : 停止防火墙并清空规则"
    echo "  status : 查看防火墙规则"
    echo "  flush  : 清空所有规则"
    exit 1
}

# 启动防火墙并设置基本规则
start_firewall() {
    echo "启动防火墙并设置规则..."
    # 清空所有规则
    iptables -F
    # 设置默认策略:拒绝所有入站流量,允许所有出站流量
    iptables -P INPUT DROP
    iptables -P OUTPUT ACCEPT
    iptables -P FORWARD DROP
    # 允许本地回环接口流量
    iptables -A INPUT -i lo -j ACCEPT
    iptables -A OUTPUT -o lo -j ACCEPT
    # 允许 SSH 流量(22端口)
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    # 允许 HTTP 和 HTTPS 流量
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    echo "防火墙已启动,基本规则已设置"
}

# 停止防火墙并清空规则
stop_firewall() {
    echo "停止防火墙并清空规则..."
    iptables -F
    iptables -P INPUT ACCEPT
    iptables -P OUTPUT ACCEPT
    iptables -P FORWARD ACCEPT
    echo "防火墙已停止,规则已清空"
}

# 查看防火墙规则
status_firewall() {
    echo "当前防火墙规则:"
    iptables -L -v --line-numbers
}

# 清空防火墙规则
flush_rules() {
    echo "清空所有防火墙规则..."
    iptables -F
    echo "规则已清空"
}

# 根据用户输入的参数执行相应操作
case "$1" in
    start)
        start_firewall
        ;;
    stop)
        stop_firewall
        ;;
    status)
        status_firewall
        ;;
    flush)
        flush_rules
        ;;
    *)
        usage
        ;;
esac

测试:

[root@iZ2vch0mnibclcpxzrbu5rZ ~]# chmod -x firewall.sh
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# ./firewall.sh start
-bash: ./firewall.sh: 权限不够
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# ./firewall.sh start
启动防火墙并设置规则...
防火墙已启动,基本规则已设置
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# ./firewall.sh status
当前防火墙规则:
Chain INPUT (policy DROP 19 packets, 1220 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  lo     any     anywhere             anywhere            
2      312 28416 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh
3        0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:http
4        0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:https

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 499 packets, 96964 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  any    lo      anywhere             anywhere            
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# ./firewall.sh stop
停止防火墙并清空规则...
防火墙已停止,规则已清空
  • -F 用于清空规则。
  • -P 用于设置链的默认策略。
  • -A 用于向链添加规则。
  • -L 用于列出规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值