命令行增加防火墙入站规则

 批量增加入站规则可参考:批量增加防火墙入站规则_wsdhla的博客-CSDN博客

如何通过命令行增加防火墙入/出站规则?

Windows系统是可以直接从防火墙高级里面增加入站出站规则的,一步一步的挺方便,但是有的时候需要添加多个端口或IP,这样就显得十分笨拙,还是用命令行更省事。

一、Windows系统

以Windows Server 2008为例:

# 创建的规则类型为程序
# program="C:\xxx.exe"

# 创建的规则类型为端口
# protocol=TCP
# localport=1521

# 规则方向:入站
# dir=in

# 指定在连接与规则中指定的条件相匹配时要执行的操作
# action=allow

# 指定此规则应用的配置文件:域,专用,公用
# profile=ALL

# 指定作用域中的远程IP地址
# remoteip

# 示例1
netsh advfirewall firewall add rule name="应用程序入站规则测试" dir=in action=allow program="C:\xxx.exe" enable=yes remoteip=xxx.xxx.xxx.xxx,xxx.xxx.xxx.xx/xx,LocalSubnet profile=private,domain


# 示例2
netsh advfirewall firewall add rule name="端口入站规则测试" dir=in action=allow protocol=TCP localport=1234  enable=yes remoteip=xxx.xxx.xxx profile=domain



# 示例3
@echo off

REM 端口列表
set port_list=80,443,1521

REM IP列表
set ip_list=xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx

REM 先删
netsh advfirewall firewall delete rule name="入站规则-端口"
REM 后增
netsh advfirewall firewall add rule name="入站规则-端口" dir=in action=allow protocol=TCP localport=%port_list% enable=yes profile=domain,private remoteip=%ip_list%

​​​​​​官方直飞

更新已存在的规则

netsh advfirewall firewall set rule name="远程桌面(TCP-In)" new remoteip=XXX.XXX.XXX.XXX,LocalSubnet

启用程序示例:

# 旧版
netsh firewall add allowedprogram C:\MyApp\MyApp.exe "My Application" ENABLE


netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=Domain


netsh firewall add allowedprogram program=C:\MyApp\MyApp.exe name="My Application" mode=ENABLE scope=CUSTOM addresses=157.60.0.1,172.16.0.0/16,LocalSubnet profile=ALL



# 新版
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes


netsh advfirewall firewall add rule name="My Application" dir=in action=allow program= "C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain


netsh advfirewall firewall add rule name="My Application" dir=in action=allow program= "C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=domain
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\MyApp.exe" enable=yes remoteip=157.60.0.1,172.16.0.0/16,LocalSubnet profile=private

二、Linux系统

以CentOS7为例:

目前有个缺点,命令行没有找到增加多个IP的方法,有了解的大佬可以交流下。

可以临时编辑/etc/firewalld/zones/public.xml文件用来批量添加。

批量已解决,详见《批量增加防火墙入站规则《》

比如:

<rule family="ipv4">
    <source address="10.72.143.252"/>
    <port protocol="tcp" port="10250-10258"/>
    <accept/>
</rule>

补充firewall-cmd命令:

firewall-cmd --list-all

firewall-cmd --permanent --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" port port=6379 protocol=tcp accept'


# 限制IP地址段
# 针对IP:192.168.172.18,从左侧开始,每一个点位代表8位,规律为
# 192.0.0.0/8,代表192开头的所有ip;
# 192.168.0.0/16,代表192.168开头的所有ip;
# 192.168.172.0/24,代表192.168.172开头的所有ip。
firewall-cmd --permanent --zone=public --add-source=192.168.1.0/24
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.172.0/24" port protocol="tcp" port="443" reject"

# 开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent

# 关闭端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent


# 批量添加区间端口
firewall-cmd --zone=public --add-port=4400-4600/udp --permanent
firewall-cmd --zone=public --add-port=4400-4600/tcp --permanent

firewall-cmd --zone=public --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="4400-4600" accept"

# 查看端口列表
firewall-cmd --permanent --list-ports
firewall-cmd --permanent --list-all
firewall-cmd --permanent --list-rich-rules


# 删除rule
firewall-cmd --permanent --remove-rich-rule "rule family="ipv4" port port="22" protocol="tcp" accept"


# 端口转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 # 将80端口的流量转发至8080
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.1.0.1 # 将80端口的流量转发至192.168.0.1
firewall-cmd --add-forward-port=proto=80:proto=tcp:toaddr=192.168.0.1:toport=8080 # 将80端口的流量转发至192.168.0.1的8080端口

firewall-cmd --reload

补充脚本方式:

# iptables: Saving firewall rules to /etc/sysconfig/iptables

# 查看规则
iptables -L -n --line-number

# -I 插入  -A 追加


# 禁用
iptables -I INPUT -p tcp --dport 1521 -j DROP

# 单IP
iptables -I INPUT -s 192.168.10.0 -p tcp --dport 1521 -j ACCEPT

# 多IP
iptables -I INPUT -s 192.168.10.49,192.168.10.33 -p tcp --dport 54321 -j ACCEPT

# 掩码
iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 1521 -j ACCEPT

# IP段
iptables -I INPUT -m iprange --src-range 192.168.10.0-192.168.10.255 -p tcp --dport 1521 -j ACCEPT

# 指定网卡
iptables -I INPUT -s 192.168.10.0 -p tcp -i eno1 --dport 1521 -j ACCEPT

# 指定目的IP
iptables -I INPUT -s 192.168.10.0 -p tcp -d 192.168.10.1 --dport 1521 -j ACCEPT

# 端口区间
iptables -I INPUT -s 192.168.10.0 -p tcp --dport 0:65535 -j ACCEPT

# 保存规则,永久生效(否则重启服务器则失效)
iptables-save > /etc/sysconfig/iptables
service iptables save
# 老版:service iptables restart
# /etc/init.d/iptables restart
systemctl restart iptables.service

# 提示:The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl

# 需安装iptables服务,重新执行 service iptables save
yum install iptables-services

# 配置iptables开机自启
# 老版:service iptables on
# 新版
systemctl enable iptables.service
systemctl is-enabled iptables.service

# 先看一下防火墙的状态
firewall-cmd --state
# 或者
systemctl status firewalld


# 若没有安装
yum install firewalld


# 如果没有启用,先启用
systemctl start firewalld


# 查询端口
firewall-cmd --query-port=5005/tcp

# 开启某个端口
# 临时:
firewall-cmd --zone=public --add-port=80/tcp
# 永久:
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=10250/tcp 

# 为特定IP开放特定端口
firewall-cmd --zone=public --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="3443" accept"

firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" port port=22 protocol=tcp accept' --permanent

# 示例2(使用rich-rule)
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="3306" accept'

# 示例3(禁用规则)
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="3306" drop'

# 关闭开放的某个端口
firewall-cmd --zone=public --remove-port=3306/tcp

# 重新加载规则
firewall-cmd --reload
# 重启防火墙 
service firewalld restart

# 查看端口开放情况、查看设置的所有规则
firewall-cmd --list-all

注意有些操作系统重启服务不再通过 service  操作,而是通过 systemctl 操作

# 查看 firewalld 服务是否启动
systemctl status firewalld
systemctl status firewalld.service


# 启动 firewalld 服务
systemctl start firewalld

# 重启 firewalld 服务
systemctl restart firewalld

# 设置 firewalld 服务开启自启动
systemctl enable firewalld


特殊场景:22端口(ssh服务)

ssh的22端口实测无法直接适用以上规则,搜索了一番,网上提供的思路是找另外的端口替代22,然后再给新端口添加防火墙规则

务必!!!谨慎操作!!!

# 禁用ssh服务
# firewall-cmd --permanent --zone=public --remove-service=ssh
# firewall-cmd --reload


# 直接禁用22端口,会导致所有新的会话都无法连接
# firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" port protocol="tcp" port="22" drop'
# firewall-cmd --reload

1、修改SSH服务器的配置文件/etc/ssh/sshd_config

修改前

#Port 22

修改后

# 22先放开以防万一,等2002端口正常之后再注释掉
Port 22
Port 2002

2、重启sshd
systemctl reload sshd.service
3、启用新端口
# firewall-cmd --zone=public --remove-port=22/tcp --permanent
# firewall-cmd --zone=public --add-port=2002/tcp --permanent

# firewall-cmd --permanent --remove-rich-rule "rule family="ipv4" port port="22" protocol="tcp" accept"
firewall-cmd --zone=public --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="2002" accept"

firewall-cmd --reload
4、修改/etc/ssh/sshd_config,关闭22端口,再重启sshd
# Port 22
Port 2002

注意:

如果发现新端口依然无法实现IP白名单控制的话,请参考以下内容:

运行firewall-cmd --list-all,仔细观察发现services里有一个ssh

所以需要删除ssh服务的规则

# 禁用ssh服务
firewall-cmd --permanent --zone=public --remove-service=ssh
firewall-cmd --reload


还有的服务器遇到以下问题

[root@localhost ~]# firewall-cmd --permanent --remove-rich-rule "某rule"
You're performing an operation over default zone ('public'),
but your connections/interfaces are in zone 'docker' (see --get-active-zones)
You most likely need to use --zone=docker option.
[root@localhost ~]# firewall-cmd --get-active-zones
docker
  interfaces: docker0

执行:firewall-cmd --zone=public --change-interface=docker0 

之后

临时删除docker0

sudo ip link delete docker0

firewall-cmd --permanent --zone=public --change-interface=ens192
firewall-cmd --get-zone-of-interface=ens192

但是一直提示 no zone

重启了一下就可以了。 

最好发现这台服务器是CentOS8,并且启用了SELinux,SELinux会阻止ssh绑定其他端口

sudo sestatus

  • 确认防火墙或 SELinux 没有阻止 SSH 服务绑定到端口 2002。
  • SELinux 可能会对端口绑定有限制。
# 暂时将SELinux设为 permissive 模式以排除干扰
sudo setenforce 0

# 检查 SELinux 端口策略
sudo semanage port -l | grep ssh

# 如果没有显示端口 2002,你可以使用以下命令添加端口 2002 到 SSH 服务的 SELinux 策略
sudo semanage port -a -t ssh_port_t -p tcp 2002


# 检查防火墙规则
sudo firewall-cmd --permanent --zone=public --add-port=2002/tcp
sudo firewall-cmd --reload


# 检查网络配置: 确认网络配置没有阻止 SSH 服务绑定到端口 2002
sudo ip addr show

# 重新启动 SSH 服务
sudo systemctl restart sshd

# 检查服务状态
sudo systemctl status sshd

# 检查端口监听情况:
sudo netstat -tuln | grep 2002
sudo ss -tuln | grep 2002


其他乱七八糟的

firewall-cmd --permanent --zone=public --remove-interface=docker0
firewall-cmd --permanent --zone=public --add-interface=ens192
firewall-cmd --permanent --zone=public --change-interface=ens192

firewall-cmd --zone=public --list-services

firewall-cmd --get-services
firewall-cmd --get-active-zones
firewall-cmd --zone=public --query-service=ssh

firewall-cmd --set-default-zone=public
firewall-cmd --get-default-zone


firewall-cmd --permanent --zone=public --change-interface=ens192
firewall-cmd --get-zone-of-interface=ens192
ls /etc/firewalld/zones/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsdhla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值