firewall防火墙(一)

2024-01-10 01:52
文章标签 防火墙 firewall

本文主要是介绍firewall防火墙(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、Linux防火墙基础

        防火墙是指设置在不同网络与网络安全域之间的一系列部件的组合,也是不同安全域之间信息的唯一出口。通过检测、限制并更改跨越防火墙的数据流,尽可能地对外屏蔽网络内部的信息、架构和运行状态,且有选择地接收外部网络访问。从传统意义上来说防火墙技术分为三类:包过滤、应用代理、状态检测。无论一个防火墙的实现过程有多复杂,归根结底都是在这三种技术的基础上进行扩展的

2、firewall-cmd命令。

 1)启动、停止、查看firewalld服务

        在安装CentOS 7系统时,会自动安装firewalld和图形化工具firewall-config。

[root@localhost ~]# systemctl start firewalld  //启动firewalld
[root@localhost ~]# systemctl enable firewalld  //设置开机自启动
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.

        如果firewalld正在运行,通过下面的命令可以查看运行状态

[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)Active: active (running) since 二 2024-01-09 16:05:04 CST; 5min agoDocs: man:firewalld(1)Main PID: 11193 (firewalld)CGroup: /system.slice/firewalld.service└─11193 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid1月 09 16:05:04 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
1月 09 16:05:04 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.[root@localhost ~]# firewall-cmd --state 
running

         如果想要禁用firewalld,执行以下命令即可实现

[root@localhost ~]# systemctl stop firewalld  //停止firewalld
[root@localhost ~]# systemctl disable firewalld  //设置开机不自启动

2)获得预定义信息

[root@localhost ~]# firewall-cmd --get-zones 
block dmz drop external home internal public trusted work[root@localhost ~]# firewall-cmd --get-services 
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server[root@localhost ~]# firewall-cmd --get-icmptypes 
address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option

3)区域管理

//显示当前系统中的默认区域
[root@localhost ~]# firewall-cmd --get-default-zone 
public//显示默认区域的所有规则
[root@localhost ~]# firewall-cmd --list-all
public (active)target: defaulticmp-block-inversion: nointerfaces: ens33 ens37 ens38sources: services: ssh dhcpv6-clientports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: //显示网络接口ens33对应区域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
public

4)将网络接口ens33对应区域更改为internal区域

[root@localhost ~]# firewall-cmd --zone=internal --change-interface=ens33
The interface is under control of NetworkManager, setting zone to 'public'.
success
[root@localhost ~]# firewall-cmd --zone=internal --list-interfaces 
ens33
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33 
internal

5)显示所有激活区域

[root@localhost ~]# firewall-cmd --get-active-zones 
publicinterfaces: ens33 ens37 ens38

2.2、服务管理

 1)为默认区域设置允许访问的服务

[root@localhost ~]# firewall-cmd --list-services 
ssh dhcpv6-client  //默认区域内允许访问的所有服务//设置默认区域允许访问http服务和https服务
[root@localhost ~]# firewall-cmd --add-service=http
success
[root@localhost ~]# firewall-cmd --add-service=https
success[root@localhost ~]# firewall-cmd --list-services 
ssh dhcpv6-client http https

2)为internal区域设置允许访问的服务

//设置internal区域允许访问mysql服务
[root@localhost ~]# firewall-cmd --zone=internal --add-service=mysql 
success//设置internal区域不允许访问samba-client服务
[root@localhost ~]# firewall-cmd --zone=internal --remove-service=samba-client 
success//显示internal区域允许访问的所有服务
[root@localhost ~]# firewall-cmd --zone=internal --list-services 
ssh mdns dhcpv6-client mysql

2.3、端口管理

1)在进行服务配置时,预定义的网络服务可以使用服务名配置,服务所涉及的端口就会自动打开。但是,对应非预定义的服务只能手动为指定的区域添加端口。

//在internal区域打开443/tcp端口
[root@localhost ~]# firewall-cmd --zone=internal --add-port=443/tcp
success//禁止443/tcp端口访问
[root@localhost ~]# firewall-cmd --zone=internal --remove-port=443/tcp
success

3、两种配置模式

        firewall-cmd命令工具有两种配置模式:运行时模式表示当前内存中运行的防火墙配置,在系统或firewall服务重启、停止时配置将失效;永久模式表示重启防火墙或重新加载防火墙时的规则配置,是永久存储在配置文件中的。

        firewall-cmd命令工具与配置模式相关的选项有三个:

4、firewalld防火墙应用案例

实验环境:

1)准备一台Linux网关服务器;一台LinuxWeb服务器

2)一台内网测试机;一台internet测试机

3)网关服务器设置三张网卡;分别为NAT、仅主机1和仅主机2

实验需求

1)ens33分配到external(外部)区域;ens37分配到trusted(信任)区域;ens38分配到dmz(非军事)。

2)网站服务器和网关服务均通过SSH来远程管理,为了安全,将SSH默认端口改为12345。

3)网站服务器开启https,过滤未加密的http流量

4)网站服务器拒绝ping

4.1、基本环境配置

1)在网关服务器上配置主机名即网卡地址

[root@localhost ~]# hostname HEY
[root@localhost ~]# vim /etc/hostname 
HEY[root@localhost ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group inet 192.168.136.24/24 brd 192.168.136.255 scope global noprefixroute ens33
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group inet 192.168.154.254/24 brd 192.168.154.255 scope global noprefixroute ens37
4: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP groupinet 192.168.177.254/24 brd 192.168.177.255 scope global noprefixroute ens38

2)开启网关服务器的路由转发功能


[root@localhost ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

4.2、网站服务器环境搭建

 1)配置Web服务器的主机及网卡地址

[root@localhost ~]# vim /etc/hostname 
web
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.177.100  netmask 255.255.255.0  broadcast 192.168.177.255

2)安装http和mod_ssl软件包

[root@web ~]# yum -y install httpd mod_ssl

3)启用并启动httpd服务

[root@web ~]# systemctl start httpd
[root@web ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

4)创建网站首页测试页index.html

[root@web ~]# vim /var/www/html/index.html

5)更改SSH的侦听地址,并重启sshd服务,注意需要将SELinux关闭

[root@web ~]# vim /etc/ssh/sshd_config 
Port 12345
[root@web ~]# systemctl restart sshd

4.3、在网站服务器上启动并配置firewalld防火墙

1)在网站服务器上启动firewalld防火墙并将默认区域设置为dmz区域,并将接口加入到dmz区域中

[root@web ~]# systemctl start firewalld
[root@web ~]# systemctl enable firewalld
[root@web ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)Active: active (running) since 二 2024-01-09 17:07:05 CST; 11s ago
[root@web ~]# firewall-cmd --change-interface=ens33 --zone=dmz

2)为dmz区域打开https服务及添加tcp的12345端口

[root@web ~]# firewall-cmd --zone=dmz --add-service=https --permanent 
success
[root@web ~]# firewall-cmd --zone=dmz --add-port=12345/tcp --permanent 
success

3)禁止ping

[root@web ~]# firewall-cmd --add-icmp-block=echo-request --zone=dmz --permanent
success

4)因为预定义的SSH服务已经更改默认端口,所以将预定义SSH服务移除

[root@web ~]# firewall-cmd --zone=dmz --remove-service=ssh --permanent 

5)重新加载firewalld激活配置,并查看刚才的配置

[root@web ~]# firewall-cmd --reload 
firsuccess
[root@web ~]# firewall-cmd --list-all --zone=dmz
dmztarget: defaulticmp-block-inversion: nointerfaces: sources: services: httpsports: 12345/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: echo-requestrich rules: 

4.4、在网关服务器上配置firewalld防火墙

1)验证firewalld在网关服务器上启动并正在运行

[root@HEY ~]# firewall-cmd --state
not running
[root@HEY ~]# systemctl start firewalld
[root@HEY ~]# systemctl enable firewalld
[root@HEY ~]# firewall-cmd --state
running

2)配置默认区域为external区域,并查看配置结果


[root@HEY ~]# firewall-cmd --set-default-zone=external 
success
[root@HEY ~]# firewall-cmd --list-all
external (active)target: defaulticmp-block-inversion: nointerfaces: ens37 ens38 ens33sources: services: sshports: protocols: masquerade: yesforward-ports: source-ports: icmp-blocks: rich rules: 

3)将ens37网卡配置到trusted区域,将ens38配置到dmz区域

[root@HEY ~]# firewall-cmd --change-interface=ens37 --zone=trusted 
The interface is under control of NetworkManager, setting zone to 'trusted'.
success
[root@HEY ~]# firewall-cmd --change-interface=ens38 --zone=dmz 
The interface is under control of NetworkManager, setting zone to 'dmz'.
success

4)查看配置情况

[root@HEY ~]# firewall-cmd --get-active-zones 
dmzinterfaces: ens38
externalinterfaces: ens33
trustedinterfaces: ens37

5)使用内网测试机访问Web网站

 

6)更改SSH的侦听端口,并重启服务(需关闭SELinux)

[root@HEY ~]# vim /etc/ssh/sshd_config 
Port 12345
[root@HEY ~]# systemctl restart sshd

7)配置external区域添加tcp的12345端口

[root@HEY ~]# firewall-cmd --zone=external --add-port=12345/tcp --permanent 
success

8)配置external区域移除SSH服务

[root@HEY ~]# firewall-cmd --zone=external --remove-service=ssh --permanent 
success

9)配置external区域禁止ping

[root@HEY ~]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent 
success

10)重新加载防火墙激活配置

[root@HEY ~]# firewall-cmd --reload 
success

11)使用internet测试机访问Web网站

这篇关于firewall防火墙(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/589154

相关文章

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断

【linux 防火墙设置】3条命令关闭linux防火墙

检查防火墙是否开启 firewall-cmd --state runing是防火墙运行中 如果 not runing的话不用次步骤 防火墙配置 说明:防火墙中有一个配置文件,表示当Linux系统启动时防火墙应该如何操作!!! 需求: 告诉linux系统以后开机不需要启动防火墙 命令: systemctl disable firewalld.service 手动关闭防火墙 说明:

防火墙 会话表

先查找会话表项,然后再目的 NAT 转换,路由查找,包过滤规则,防火墙策略、应用层匹配规则,审计策略,最后查询源 NAT 从设备转发出去。 DPtech FW1000系列防火墙技术白皮书 - 技术白皮书 - 杭州迪普科技股份有限公司 防火墙通过会话表(Session Table)来跟踪和管理经过防火墙的所有连接会话。会话表项是防火墙用来记录每个连接状态的信息,帮助防火墙在后续的

防火墙三种模式(路由/透明/混合模式)不支持主备部署 P3

防火墙的三种部署模式(路由、透明、混合),哪种不支持主备部署 在防火墙的三种部署模式中,透明模式通常不可以支持主备部署,但在某些情况下,配置和实现可能比较复杂。具体是否支持主备部署,取决于防火墙的品牌和型号。 部署模式解释 路由模式: 防火墙作为三层设备,具有IP地址。支持主备部署,适用于大多数网络环境。 透明模式: 防火墙工作在二层,没有IP地址,像交换机一样转发流量。支持主备部署,

CentOS 6.5和CentOS7 防火墙关闭步骤

在关闭防火墙之前,首先要确定Linux环境下安装的系统版本。使用以下命令查看: CentOS6.5和CentOS7关闭防火墙步骤不同 centOS 6.5关闭防火墙步骤 关闭命令:  service iptables stop         永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后查看防火墙关闭状态         service

Centos 开放端口 查看 防火墙 ping telnet

centos中安装telnet yum install telnet   1、开放端口 firewall-cmd --zone=public --add-port=5672/tcp --permanent   # 开放5672端口 firewall-cmd --zone=public --remove-port=5672/tcp --permanent  #关闭5672端口 fir

Linux运维--Firewall防火墙命令以及规则等详解(全)

Linux运维–Firewall防火墙命令以及规则等详解(全) 在Linux系统中,你可以使用firewalld和iptables来管理和设置防火墙规则。Firewalld是一个动态管理防火墙的工具,而iptables是一个更底层的工具,可以直接配置Linux内核的防火墙规则。 在RHEL 6.9及更早版本中,使用的是iptables作为防火墙管理工具,而在RHEL 7及更新版本中则使

Linux防火墙问题排查记录

问题描述 在业务当中,开通了防火墙,导致外部数据无法通过SFTP服务访问本机的服务,根据防火墙策略判断,应该是有一些IP没有被加进accept策略导致的,所以需要查看防火墙日志来追溯哪些IP被过滤掉了,只要放通这些IP,理论上就可以解决这个问题。 问题解决 防火墙添加日志、增加日志配置、重启防火墙 通过命令 systemctl status firewalld 查看防火墙状态,这是可以看到

Linux运维--iptables防火墙命令以及端口号等详解(全)

Linux之iptable防火墙命令以及端口号等详解(全) 在Linux系统中,你可以使用firewalld和iptables来管理和设置防火墙规则。Firewalld是一个动态管理防火墙的工具,而iptables是一个更底层的工具,可以直接配置Linux内核的防火墙规则。 在RHEL 6.9及更早版本中,使用的是iptables作为防火墙管理工具,而在RHEL 7及更新版本中则使用Fire

防火墙基本原理入门篇,小白一看就懂!

吉祥知识星球http://mp.weixin.qq.com/s?__biz=MzkwNjY1Mzc0Nw==&mid=2247485367&idx=1&sn=837891059c360ad60db7e9ac980a3321&chksm=c0e47eebf793f7fdb8fcd7eed8ce29160cf79ba303b59858ba3a6660c6dac536774afb2a6330&scene