春风十里不如你 —— Taozi - 防火墙 https://www.xiongan.host/index.php/tag/%E9%98%B2%E7%81%AB%E5%A2%99/ zh-CN Tue, 29 Nov 2022 11:25:00 +0800 Tue, 29 Nov 2022 11:25:00 +0800 Linux下的防火墙 https://www.xiongan.host/index.php/archives/179/ https://www.xiongan.host/index.php/archives/179/ Tue, 29 Nov 2022 11:25:00 +0800 admin firewalld

特性

  • 支持ipv4和ipv6
  • 支持nat
  • 分防火墙区域
区域作用
Drop任何传入的网络数据包都被丢弃,没有回复。只能进行传出网络连接。
block任何传入的网络连接都会被拒绝,IPv4的icmp主机禁止消息和IPv6的icmp6 adm禁止消息通知被拒绝的主机。只有从系统内部主动发起的连接会被允许。
dmz英文"demilitarized zone"的缩写,此区域内可公开访问,它是非安全系统与安全系统之间的缓冲区。
external允许指定的外部网络进入连接,特别是为路由器启用了伪装功能的外部网。
home用于家庭网络。基本信任的网络,仅仅接收经过选择的连接。
internal内部访问。只限于本地访问,其他不能访问
public[默认]公网访问,不受任何限制。
trusted接收的外部网络连接是可信任、可接受的。
work用于工作区。基本信任的网络,仅仅接收经过选择的连接。

zone下的网卡

  • 查看网卡的 zone

    $ sudo firewall-cmd --get-active-zones
    public
      interfaces: lo
    # 查看 lo 的 zone
    ~$ sudo firewall-cmd --get-zone-of-interface=lo   
    public
  • 给指定网卡增加 zone

    ~$ sudo firewall-cmd --zone=public --add-interface=lo
    success
    ~$ sudo firewall-cmd --get-active-zones
    public
      interfaces: lo
  • 修改指定网卡的 zone

    ~$ sudo firewall-cmd --zone=internal --change-interface=lo
    success
    ~$ sudo firewall-cmd --get-active-zones
    internal
      interfaces: lo
  • 移除指定网卡的 zone

    ~$ sudo firewall-cmd --zone=internal --remove-interface=lo
    success

Zone下的 service

  • 查看所有的 servie

    ~$ sudo firewall-cmd --get-service
    client bgp bitcoin bitcoin-rpc ctdb dhcp dhcpv6 dns docker-registry ftp git http https imap imaps ipp ipsec irc ircs isns jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps mongodb mysql nfs nfs3 ntp nut openvpn privoxy proxy-dhcp ptp redis rpc-bind rsh rsyncd rtsp salt-master samba samba-client samba-dc smtp ssh svn syslog syslog-tls telnet tftp tftp-client xmpp-server zabbix-agent zabbix-server
  • 查看当前 zone下的 service

    ~$ sudo firewall-cmd --list-service
    dhcpv6-client ssh
  • 把 http 服务增加到 public 的 zone 下

    # 永久增加
    ~$ sudo firewall-cmd --zone=public --add-service=http --permanent
    success
    # 指定查看 zone=public 下的service
    ~$ sudo firewall-cmd --zone=public --list-service
    dhcpv6-client ssh http
  • zone/service的配置文件模板存放路径

    /usr/lib/firewalld/zones          -zones配置路径
    /usr/lib/firewalld/services       --services配置路径

自定义SSH端口号

假设自定义的 ssh 端口号为 11011,使用下面的命令来添加新端口的防火墙规则:

firewall-cmd --add-port=11011/tcp --permanent

在 /etc/firewalld/services/ 目录中添加 自定义配置文件 test-ssh.xml ,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>test-ssh</short>
  <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="11011"/>
  <module name="nf_conntrack_ftp"/>
</service>

将自定义的服务添加到zone下

firewall-cmd --add-service=test-ssh

重启生效

systemctl reload firewalld

一旦新的规则生效,旧的 ssh 端口规则旧可以被禁用掉:

firewall-cmd --remove-service=ssh

iptables

选项作用
--list-all
--list-all-zones

表:tables

  • filter:用于对数据包进行过滤,iptables默认使用filter表
  • nat:用于数据包的网络地址转换
  • mangle:用于改写数据包里的参数
  • 自定义

链:chains

链属于表,或者说链是被表调用

  • INPUT:进入linux系统本机的数据包
  • OUTPUT:Linux本机要送出的数据包
  • Forward:经过Linux系统的包,与linux本机无关
  • Prerouting:在路由判断之前进行过滤
  • postrouting:在路由判断之后进行过滤

表和链的关系

表是基本单位,链是属于具体的表,链下面设定具体的rule(规则)

一个表里可以包含多个链,一个链也可以属于多个表

iptables:示例

  • 清除规则、表、统计信息

    • iptables -F
    • iptables -X
    • iptables -Z
  • 设置预定义规则

    [root@www ~]# iptables -P INPUT   DROP
    [root@www ~]# iptables -P OUTPUT  ACCEPT
    [root@www ~]# iptables -P FORWARD ACCEPT
    [root@www ~]# iptables-save
  • 演示具体规则设定

    • 允许ssh连接

      iptables -t filter -A INPUT -i ens33 -s 192.168.180.1 -p tcp --dport 22 -j ACCEPT

      • -t:指定设置那个表
      • -A:在哪个链里添加规则
      • -i:入接口
      • -s:源ip地址
      • -p:协议
      • --dport:目标端口
      • -j:动作

        • ACCEPT:接收
        • REJECT:拒绝并通知
        • DROP:丢弃,但不通知
        • SNAT:源地址做nat转换(后面跟上--to-source newip
        • DNAT: 目标地址做nat转换(后面跟上--to-destination newip

安全策略

tcp_warpper配置

tcpwarpper只对TCP有效,但不代表是所有的tcp协议的应用有效,关键要看该应用是否依赖libwrap.so这个库文件。

111

TCPwrapper有两个配置文件。

  • /etc/hosts.allow
  • /etc/hosts.deny

TCPwrappers先查找/etc/hosts.allow,再查找/etc/hosts.deny,如果两个配置中有冲突,先匹配中的优先,也就是hosts.allow中的配置优先,如果两个配置都没命中,默认放行。

daemon_list : client_list[ : option : option ...]

#    daemon_list:程序文件名称列表
        (1) 单个应用程序文件名;
        (2) 程序文件名列表,以逗号分隔;
        (3) ALL:所有受tcp_wrapper控制的应用程序文件;
  
        daemon_list:必须是程序名  
  
  
#   client_list:
        (1) 单个IP地址或主机名;
        (2) 网络地址:n.n.n.n/m.m.m.m,n.n.n.;
        (3) 内建的ACL:
        ALL:所有客户端主机;
        LOCAL:Matches any host whose name does not contain a dot character.
        UNKNOWN #不能反解主机名
        KNOWN   #可以反解主机名 
        PARANOID    #反解的IP和主机名不对应
  
#    OPERATORS:
        EXCEPT
            list1 EXCEPT list2 EXCEPT list3
  
#   option
    deny:拒绝,主要用于hosts.allow文件中定义“拒绝”规则;
                allow:允许,主要用于hosts.deny文件中定义”允许“规则;
  
                spawn:生成,发起,触发执行用户指定的任意命令,此处通常用于记录日志;

/etc/hosts.allow

文件内写入ALL : ALL

代表的意思就是允许所有用户访问所有应用,即使deny文件写入了拒绝也会被忽略的

案例1

禁止192.168.123.1/24访问SSH服务

image.png

保存后再次用ssh工具连接 就被拒绝了

image.png

案例2

基于案例1,再加一条命令,只能192.168.123.1可以访问

test

test

selinux

selinux的上下文含义

linux

  • system_u代表系统用户文件
  • object_r代表文件或设备的角色
  • system_r代表系统进程的角色
  • unconfined_u代表未受限制用户

命令

  • chcon

    chcon

    -t :修改selinux上下文的类型

  • semanage

    semanage

    fcontext : 管理文件的安全上下文

    -a : 添加文件夹默认的安全上下文

    -t : 后面跟安全上下文的类型

    修改后不会立即生效需要配合下面的restorecon,执行后生效

  • restorecon

    restorecon

    -R :表示递归

    -v :显示修改进程

]]>
0 https://www.xiongan.host/index.php/archives/179/#comments https://www.xiongan.host/index.php/feed/tag/%E9%98%B2%E7%81%AB%E5%A2%99/
【Packmaker】集群部署实践+测试 https://www.xiongan.host/index.php/archives/86/ https://www.xiongan.host/index.php/archives/86/ Wed, 19 Oct 2022 08:13:00 +0800 admin 主机名ip角色Node01192.168.123.23节点1Node02192.168.123.123节点2

首先安装pssh远程服务

yum install -y pssh

安装pssh

两台虚拟机都需要编辑hosts文件

分别写入node01、node02的ip 和主机名

生成免密登录,就可以无需密码直接登陆node01和node02

ssh-keygen -t rsa

ssh-copy-id -i /root/.ssh/id_rsa.pub root@node02-tz

生成上传ssl密钥

Node02端也需要进行相同操作

之后安装pacemaker服务并设置开机自启

pssh -h host-list.txt -i 'yum install pacemaker pcs -y'

pssh -h host-list.txt -i 'systemctl enable --now pcsd'

安装pacemaker

c9f038aa01c6440942e0a9679fe1735.png

配置 Pacemaker Cluster:

设置cluster的账号密码

pssh -h host-list.txt -i 'echo 123456 | passwd --stdin hacluster'

设置cluster

设置防火墙:

pssh -h host-list.txt -i 'firewall-cmd --add-service=high-availability --permanent'

添加服务

pssh -h host-list.txt -i 'firewall-cmd --reload'

加载防火墙

防火墙操作

建立 Cluster 授权:

pcs cluster auth node01-tz node02-tz

授权node节点用户

配置 Cluster(集群名称为:tz-cluster):

pcs cluster setup --name tz-cluster node01-tz node02-tz

添加集群

添加集群

启动 Cluster:

pcs cluster start –all

启动

使 Cluster 随机启动:

pcs cluster enable --all

自启cluster

验证集群状态:

pcs status cluster

验证状态

添加集群资源:

两台节点需要安装httpd服务,不需要启动

pssh -h host-list.txt -i 'yum install httpd -y'

httpd例子

两台服务器都需要编辑一个新的配置文件

vim /etc/httpd/conf.d/server_status.conf

ExtendedStatus On
<Location /server-status>
 SetHandler server-status
 Require local
</Location>

并写入一个测试页面到/var/www/html/index.html

配置 Cluster 并设定 VIP:

[root@node01-tz ~]# pcs property set stonith-enabled=false <==关闭 stonith 功能

[root@node01-tz ~]# pcs property set no-quorum policy=ignore <==忽略仲裁

[root@node01-tz ~]# pcs property set default-resource-stickiness="INFINITY" <==设置资源超 时时间

[root@node01-tz ~]# pcs property list<==显示设定

显示集群信息

pcs resource create VIP ocf:heartbeat:IPaddr2 ip=192.168.123.111 cidr_netmask=24 op monitor interval=30s <==设定 VIP
pcs status resources <==显示资源信息
添加 httpd 资源:
pcs resource create Web123-Cluster ocf:heartbeat:apache configfile=/etc/httpd/conf/httpd.conf statusurl="http://127.0.0.1/server-status" op monitor interval=1min

添加httpd资源

[root@node01-tz ~]# pcs constraint colocation add Web123-Cluster with VIP INFINITY

[root@node01-tz ~]# pcs constraint order VIP then Web123-Cluster <==设定启动顺讯:先启动 VIP 在启动 WEB123-Cluster

设置vip资源

pcs constraint <==显示资源情况

资源情况

客户端测试:

测试

停止 ndoe1 节点的 httpd 资源,客户端再测试:

pcs cluster stop node01-tz

测试

就自动切换到了node02上

使用 WEB GUI 管理 PaceMaker

firewall-cmd --add-port=2224/tcp --permanent

firewall-cmd --reload

访问https://192.168.123.111:2224

测试

注:

①80端口占用查询:

netstat -tlnp|grep 80

杀死已经存在的80端口服务

Kill -9 进程号

firewall-cmd --zone=public
--add-port=80/tcp --permanent ###添加80端口

Firewall-cmd --reload ###重载防火墙

②ssh密钥生成信息只能存在一个,需要手动添加到本机与对方的auth开头文件和konw文件中>>>>>/root/.ssh/

]]>
0 https://www.xiongan.host/index.php/archives/86/#comments https://www.xiongan.host/index.php/feed/tag/%E9%98%B2%E7%81%AB%E5%A2%99/