春风十里不如你 —— Taozi - 防火墙 https://www.xiongan.host/index.php/tag/%E9%98%B2%E7%81%AB%E5%A2%99/ Linux下的防火墙 https://www.xiongan.host/index.php/archives/179/ 2022-11-29T11:25:00+08:00 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 successZone下的 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 httpzone/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=sshiptables选项作用--list-all --list-all-zones 表:tablesfilter:用于对数据包进行过滤,iptables默认使用filter表nat:用于数据包的网络地址转换mangle:用于改写数据包里的参数自定义链:chains链属于表,或者说链是被表调用INPUT:进入linux系统本机的数据包OUTPUT:Linux本机要送出的数据包Forward:经过Linux系统的包,与linux本机无关Prerouting:在路由判断之前进行过滤postrouting:在路由判断之后进行过滤表和链的关系表是基本单位,链是属于具体的表,链下面设定具体的rule(规则)一个表里可以包含多个链,一个链也可以属于多个表iptables:示例清除规则、表、统计信息iptables -Fiptables -Xiptables -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这个库文件。TCPwrapper有两个配置文件。/etc/hosts.allow/etc/hosts.denyTCPwrappers先查找/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服务保存后再次用ssh工具连接 就被拒绝了案例2基于案例1,再加一条命令,只能192.168.123.1可以访问selinuxselinux的上下文含义system_u代表系统用户文件object_r代表文件或设备的角色system_r代表系统进程的角色unconfined_u代表未受限制用户命令chcon-t :修改selinux上下文的类型semanagefcontext : 管理文件的安全上下文-a : 添加文件夹默认的安全上下文-t : 后面跟安全上下文的类型修改后不会立即生效需要配合下面的restorecon,执行后生效restorecon-R :表示递归-v :显示修改进程 【Packmaker】集群部署实践+测试 https://www.xiongan.host/index.php/archives/86/ 2022-10-19T08:13:00+08:00 主机名ip角色Node01192.168.123.23节点1Node02192.168.123.123节点2首先安装pssh远程服务yum install -y pssh两台虚拟机都需要编辑hosts文件分别写入node01、node02的ip 和主机名生成免密登录,就可以无需密码直接登陆node01和node02ssh-keygen -t rsassh-copy-id -i /root/.ssh/id_rsa.pub root@node02-tzNode02端也需要进行相同操作之后安装pacemaker服务并设置开机自启pssh -h host-list.txt -i 'yum install pacemaker pcs -y'pssh -h host-list.txt -i 'systemctl enable --now pcsd'配置 Pacemaker Cluster:设置cluster的账号密码pssh -h host-list.txt -i 'echo 123456 | passwd --stdin hacluster'设置防火墙: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配置 Cluster(集群名称为:tz-cluster):pcs cluster setup --name tz-cluster node01-tz node02-tz启动 Cluster:pcs cluster start –all使 Cluster 随机启动:pcs cluster enable --all验证集群状态:pcs status cluster添加集群资源:两台节点需要安装httpd服务,不需要启动pssh -h host-list.txt -i 'yum install httpd -y'两台服务器都需要编辑一个新的配置文件vim /etc/httpd/conf.d/server_status.confExtendedStatus 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[root@node01-tz ~]# pcs constraint colocation add Web123-Cluster with VIP INFINITY[root@node01-tz ~]# pcs constraint order VIP then Web123-Cluster <==设定启动顺讯:先启动 VIP 在启动 WEB123-Clusterpcs constraint <==显示资源情况客户端测试:停止 ndoe1 节点的 httpd 资源,客户端再测试:pcs cluster stop node01-tz就自动切换到了node02上使用 WEB GUI 管理 PaceMakerfirewall-cmd --add-port=2224/tcp --permanentfirewall-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/