linux下firewall简易设置[原创]

本文介绍了一种简单的Linux防火墙配置方法,通过iptables命令实现基本的网络流量控制,包括允许和拒绝特定IP地址及开放常用服务端口。

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

What Is a Linux Firewall?

 

A Linux firewall is a solution or service that regulates, protects, and blocks network traffic as it passes to and from a Linux-based environment. 

iptables.rule为firewall总规则;
iptables.allow为充许进入的主机;
iptables.deny为不许放的主机;
iptables.allow代码如下:
#!/bin/bash
# This is an esay firewall.
# the inside interface. if you don't have this one
# and you must let this be black ex> INIF=""
INIF="eth0"
INNET="192.168.160.0/20"
# 2.0 load the right module
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
  export PATH EXTIF INIF INNET
  modprobe ip_tables  > /dev/null 2>&1
  modprobe iptable_nat  > /dev/null 2>&1
  modprobe ip_nat_ftp   > /dev/null 2>&1
  modprobe ip_nat_irc  >  /dev/null 2>&1
  modprobe ip_conntrack > /dev/null 2>&1
  modprobe ip_conntrack_ftp > /dev/null 2>&1
  modprobe ip_conntrack_irc > /dev/null 2>&1
# 3.0 clear iptables rule
  /sbin/iptables -F
  /sbin/iptables -X
  /sbin/iptables -Z
  /sbin/iptables -F -t nat
  /sbin/iptables -X -t nat
  /sbin/iptables -Z -t nat
  /sbin/iptables -P INPUT  DROP
  /sbin/iptables -P OUTPUT  ACCEPT
  /sbin/iptables -P FORWARD ACCEPT
  /sbin/iptables  -t nat -P PREROUTING ACCEPT
  /sbin/iptables  -t nat -P POSTROUTING ACCEPT
  /sbin/iptables  -t nat -P OUTPUT  ACCEPT
# 4.0 start loading trusted and denied file.
if [ -f /usr/local/virus/iptables/iptables.allow ]; then
   sh /usr/local/virus/iptables/iptables.allow
fi
if [ -f /usr/local/virus/iptables/iptables.deny ]; then
   sh /usr/local/virus/iptables/iptables.deny
fi
# 5.0 if the following file exist ,please executed
if [ -f /usr/local/virus/httpd-err/iptables.http ];then
   sh /usr/local/virus/httpd-err/iptables.http
fi
# 6.0 allow icmp data packet and the establishd data
  /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
  /sbin/iptables -A INPUT -i $EXTIF="eth0" -p icmp --icmp-type $tyicmp -j ACCEPT
done
# 7.0 open the other service ports
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 25 -j ACCEPT # SMTP
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 53 -j ACCEPT # DNS
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 80 -j ACCEPT # WWW
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 110 -j ACCEPT # POP3
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 113  -j ACCEPT #AUTH
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 22222 -j ACCEPT #SSH    
   /sbin/iptables -A INPUT -p UDP -i $EXTIF="eth0" --dport 138 -j ACCEPT #138
   /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 139 -j ACCEPT #139
   /sbin/iptables -A INPUT -p UDP -i $EXTIF="eth0" --dport  137 -j ACCEPT #137
  /sbin/iptables -A INPUT -p TCP -i $EXTIF="eth0" --dport 445 -j ACCEPT #445
iptables.allow代码如下
#!/bin/bash
# this program is used to allow some IP or hosts to access your server
  /sbin/iptables -A INPUT -i $EXTIF="eth0" -s 192.168.161.242 -j ACCEPT
  /sbin/iptables -A INPUT -i $EXTIF="eth0" -s 192.168.160.178 -j ACCEPT
  /sbin/iptables -A INPUT -i $EXTIF="eth0" -s 192.168.160.218 -j ACCEPT
iptables.deny代码如下
#! /bin/bash
# This script will deny some IPs that I don't want in IN
/sbin/iptables -A INPUT -i $EXTIF="eth0"  -s 192.168.160.242 -j DROP
以上的三个文件都放在/usr/local/virus/iptables目录下,最在修改此文件/etc/rc.d/rc.local成如下代码
­
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
#1. Starting firewall settings
    /usr/local/virus/iptables/iptables.rule
以上就是linux下firewall简易设置啦

### 配置和管理Linux防火墙 #### 使用`firewalld` `Firewalld` 提供了一种动态管理防火墙的方式,允许更新规则而不中断现有连接。对于大多数现代Linux发行版,默认情况下会安装并启用 `firewalld`。 要查看当前状态可以运行如下命令: ```bash sudo systemctl status firewalld ``` 为了启动或停止服务可分别执行下面两条指令之一: ```bash sudo systemctl start firewalld # 启动服务 sudo systemctl stop firewalld # 停止服务 ``` 如果希望开机自动加载,则需设置为开启状态: ```bash sudo systemctl enable firewalld ``` 通过图形界面配置也很简单,只需打开应用程序菜单找到名为 "Firewall Configuration" 或者直接在终端里键入 `firewall-config` 即可进入GUI模式进行操作[^2]。 #### 添加端口和服务到特定区域 默认情况下,公共区(`public`)被选作活跃区;但是可以根据需求更改此设定。向指定区域内添加开放端口的例子如下所示: ```bash sudo firewall-cmd --zone=public --add-port=80/tcp --permanent ``` 上述命令永久性地将TCP协议下的HTTP(80)端口加入到了公网访问列表之中。记得每次修改后都要重新载入配置文件使改动生效: ```bash sudo firewall-cmm --zone=public --remove-port=80/tcp --permanent ``` 当涉及到具体的应用程序而非单一端口号时,比如SSH、HTTPS等常用的服务名同样适用这种方式处理: ```bash sudo firewall-cmd --zone=public --add-service=https --permanent ``` 这行代码的作用就是让HTTPS流量能够顺利穿过防火墙屏障到达目标主机上监听该类型的进程那里去[^1]。 #### 利用`iptables`实现更精细控制 尽管`firewalld`已经足够满足日常维护工作所需的功能特性了,但对于某些特殊场景下可能还是需要用到更为底层也更加灵活多变的工具——即`iptables`本身。它能让我们直接针对IPv4层面的数据流制定详尽而又复杂的过滤策略。 创建一条简单的INPUT链规则阻止来自某个IP地址的所有传入请求样例如下: ```bash sudo iptables -A INPUT -s 192.168.1.100 -j DROP ``` 这里的意思是从源地址为`192.168.1.100`过来的一切尝试都将遭到拒绝响应。当然实际应用当中往往不会这么绝对化,而是配合其他条件一起使用形成复杂逻辑判断结构[^3]。 保存这些自定义好的表项以便重启之后仍然有效非常重要: ```bash sudo sh -c "iptables-save > /etc/iptables/rules.v4" ``` 以上就是在Linux环境中围绕着两个主流防火墙解决方案展开的一些基本介绍与实践指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值