一.初识firewalld
1.出了点小故障防火墙文件找不到,解决方式如下:
[root@localhost ~]# yum install firewalld -y
[root@localhost ~]# systemctl unmask firewalld
[root@localhost ~]# systemctl enable firewalld
[root@localhost ~]# systemctl start firewalld
2.查找防火墙服务名的技巧
[root@localhost ~]# systemctl list-units |grep fire
firewalld.service loaded active running firewalld - dynamic firewall daemon
3.这个命令,其实是找到一个服务脚本文件
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-11-07 04:41:25 CST; 2h 39min ago
Docs: man:firewalld(1)
Main PID: 112106 (firewalld)
CGroup: /system.slice/firewalld.service
└─112106 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Nov 07 04:41:25 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 07 04:41:25 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 07 04:41:25 localhost.localdomain firewalld[112106]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration o...it now.
Hint: Some lines were ellipsized, use -l to show in full.
4.这个firewalld.service文件在哪?
[root@localhost ~]# find / -type f -name 'firewalld.service'
/usr/lib/systemd/system/firewalld.service
5.这个脚本,其实就是执行了运行防火墙命令的一个脚本文件 ,直接看这个脚本的,第11 12 13行
[root@localhost ~]# cat -n /usr/lib/systemd/system/firewalld.service
1 [Unit]
2 Description=firewalld - dynamic firewall daemon
3 Before=network-pre.target
4 Wants=network-pre.target
5 After=dbus.service
6 After=polkit.service
7 Conflicts=iptables.service ip6tables.service ebtables.service ipset.service
8 Documentation=man:firewalld(1)
9
10 [Service]
11 EnvironmentFile=-/etc/sysconfig/firewalld
12 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS
13 ExecReload=/bin/kill -HUP $MAINPID
14 # supress to log debug and error output also to /var/log/messages
15 StandardOutput=null
16 StandardError=null
17 Type=dbus
18 BusName=org.fedoraproject.FirewallD1
19 KillMode=mixed
20
21 [Install]
22 WantedBy=multi-user.target
23 Alias=dbus-org.fedoraproject.FirewallD1.service
二.使用防火墙命令,查看系统提供了哪些模板
1.列出区域模板,以及具体信息
[root@localhost ~]# firewall-cmd --help |grep list
[root@localhost ~]# firewall-cmd --list-all-zones
2.列出所有区域的名字
[root@localhost ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
3.列出当前使用的区域
[root@localhost ~]# firewall-cmd --get-default-zone
public
4.列出当前使用的区域,以及详细信息
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
5.先运行一个80端口的服务
[root@localhost ~]# python -m SimpleHTTPServer 80
6.给当前的防火墙区域,添加一个策略,允许80端口通过
[root@localhost ~]# firewall-cmd --add-port=80/tcp
success
就可以直接在浏览器上访问了,可以看到里面的文件
7.删除,添加的端口规则
[root@localhost ~]# firewall-cmd --remove-port=80/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
8.针对服务名添加,比如ntp服务
[root@localhost ~]# firewall-cmd --add-service=ntp
success
9.查看当前public区域,使用了哪些规则
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ntp ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
10. firewalld,作用其实是添加iptables的规则
查看系统上所有iptables的命令
[root@localhost ~]# iptables -L
tcp 是一个安全可靠的连接,需要双向确认,客户端,和服务端,都要确认对方以及连接上了
udp 是一个不可靠的额连接协议,客户端可以随便给服务端发,不需要对方确认
比如一个很差的网络环境下,网页无法访问,无法做dns解析(网络服务,网站服务,用的都是tcp协议)
但是qq可以收发消息(qq用的是udp协议,以及ntp用的也是udp协议)
11.查看到firewalld命令,添加的防火墙规则如下
[root@localhost ~]# iptables -L | grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW,UNTRACKED
三.永久修改防火墙规则
1. 永久性添加 8000/tcp的策略
[root@localhost ~]# firewall-cmd --permanent --add-port=8000/tcp
2.需要重新加载firewalld服务
[root@localhost ~]# firewall-cmd --reload
success
3.重新加载后,规则自动生效了
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 8000/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
四.防火墙放行http服务
1.查询支持的所有服务名字有哪些(nginx,httpd,统一被他制作为了 http服务,放行80端口)
[root@localhost ~]# firewall-cmd --get-services
2. 查询当前区域,所用的服务名有哪些
[root@localhost ~]# firewall-cmd --list-services
dhcpv6-client ssh
3. 添加http服务,放行80端口即可
[root@localhost ~]# firewall-cmd --add-service=http
success
4.移除该服务,禁用80端口的请求
[root@localhost ~]# firewall-cmd --remove-service=http
success
5.建议,最好还是直接针对端口号,协议号,添加规则,
firewalld真不好用,不可用
iptables 支持很多复杂的参数,针对协议,来源端口,目标端口,等等
公有云的安全组(阿里云提供的硬件防火墙),也是基于iptables这样的规则添加的
五.防火墙放行ntp
1.firewalld允许ntp请求(注意ntpd有同步等待时间)
[root@localhost ~]# firewall-cmd --get-services |grep ntp
###移除ntp的防火墙则
[root@localhost ~]# firewall-cmd --remove-service=ntp
success
###允许客户端,来这台机器,进行ntp时间同步
[root@localhost ~]# firewall-cmd --add-service=ntp
success
###同步上游机器
[root@localhost ~]# ntpd -u 192.168.106.129
六.定时任务crontab
语法:
crontab
-l 列出当前用户有哪些计划任务
-e 编辑当前用户的计划任务
-r 删除当前用户的计划任务
1.先看系统默认的定时任务配置文件,语法长什么样
[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59) ###分钟
# | .------------- hour (0 - 23) ###小时
# | | .---------- day of month (1 - 31) ###日
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... ###月
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat ###周
# | | | | |
# * * * * * user-name command to be executed
###用户名 ###需要执行的命令
注意:日期和星期几是不能同时写的
取值范围(常识):
分:0~59
时:0~23
日:1~31
月:1~12
周:0~6,0 和 7 表示星期天
四个符号:
*:表示取值范围中的每一个数字
-:做连续区间表达式的,要想表示1~7,则可以写成:1-7
/:表示每多少个,例如:想每 10 分钟一次,则可以在分的位置写:*/10
,:表示多个取值,比如想在 1 点,2 点 6 点执行,则可以在时的位置写:1,2,6
* * * * *
分 时 日 月 周 命令的绝对路径
2.定时任务编写流程
crontab -e 编辑定时任务
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
写入正确的语法 注意用命令绝对路径
[root@localhost ~]# which echo
/usr/bin/echo
* * * * * /usr/bin/echo 'hello,我是一原' >> /tmp/man.txt
查看定时任务
[root@localhost ~]# crontab -l
* * * * * /usr/bin/echo 'hello,我是一原' >> /tmp/man.txt
验证文件是否存在
[root@localhost ~]# tail -f /tmp/man.txt
hello,我是一原
hello,我是一原
hello,我是一原
hello,我是一原
定时任务写入后,会自动记录到一个文件中,文件路径在如下路径,以用户名区分,不同的定时任务
[yiyuan@localhost ~]$ crontab -e
* * * * * /usr/bin/echo 'hello, i am yiyua' >> /tmp/yiyuan.txt
[root@localhost ~]# ls /var/spool/cron
root yiyuan
[root@localhost ~]# cat /var/spool/cron/yiyuan
* * * * * /usr/bin/echo 'hello, i am yiyua' >> /tmp/yiyuan.txt
3.例子
* * * * *
分 时 日 月 周 命令的绝对路径
从左 向右,依次去写,不要跳级
问题1:每月1、10、22 日的4:45 重启network 服务
45 4 1,10,22 * * /usr/bin/systemctl restart network
问题2:每周六、周日的下午1:10 重启network 服务
10 13 * * 6,7 /usr/bin/systemctl restart network
问题3:每天18:00 至23:00 之间每隔30 分钟重启network 服务
*/30 18-23 * * * /usr/bin/systemctl restart network
问题4:每隔两天的上午8点到11点的第3和第15分钟执行一次重启
3,15 8-11 */2 * *
问题5 :每天凌晨整点重启nginx服务。
0 0 * * * /usr/bin/systemctl restart nginx
问题6:每周4的凌晨2点15分执行命令
15 2 * * 4
问题7:工作日的工作时间内的每小时整点执行脚本。
工作日 1-5
工时 9-18
0 9-18 * * 1-5
问题8:每天凌晨2点30,执行ntpdate命令同步ntp.aliyun.com,且不输出任何信息,把命令结果,重定向到黑洞文件
/dev/null
备注:定时任务的命令执行,会产生日志
30 2 * * * /usr/sbin/ntpdate -u ntp.aliyun.com &> /dev/null
如果定时任务的时间,没法整除,定时任务就没有意义了,得通过其他手段,自主控制定时任务频率
crontab提供最小分钟级别的任务,想完成秒级别的任务,得通过编程语言自己写
七.定时任务黑白名单
禁止哪些用户创建定时任务
该文件在
/etc/cron.deny 黑名单文件 (将系统中,所有uid大于1000的用户,全部写入黑名单)
/etc/cron.allow 白名单 ,优先级高于黑名单
定时任务,默认存放的路径
[yiyuan@localhost ~]$ ls /var/spool/cron
定时任务,服务端的运行日志,可以用于给运维,进行故障排查
/var/log/cron
最后,定时任务,crontab会在系统中,生成大量的邮件日志,会占用磁盘,因此我们都会关闭邮件服务即可
[root@localhost ~]# find / -type f -name 'post*.service'
/usr/lib/systemd/system/postfix.service
systemctl服务管理命令
[root@localhost ~]# systemctl list-units |grep post
postfix.service loaded active running Postfix Mail Transport Agent
systemctl status postfix 查看状态
systemctl stop postfix 停止
systemctl disable postfix 禁止开机自启