day16 防火墙和定时的服务
day16 防火墙和定时服务
基础服务的管理
防火墙服务
- 简单的学习firewalld 服务,怎么用,以及创建的规则
我们用的最多的是iptables但是也是要学习简单的开始
简单来说,防火墙是用来过滤外来的流量,以保证服务器的安全
Linux设置IP tables 有两个的方法
1.直接使用IP tables命令,
2.使用系统自带的firewall-cmd 提供的服务(firewalld)
这个也是创建iptables 的方法
ip tables -> 可以简称为 ip 的规则

1.查找防火墙服务的技巧 -->找到防火墙的服务名称
[root@linux-yzk ~]# systemctl list-units | grep fire*
firewalld.service loaded active running firewalld - dynamic firewall daemon
[root@linux-yzk ~]#
2.找到防火墙的脚本的文件
[root@linux-yzk ~]# ll /usr/lib/systemd/system | grep fire*
-rw-r--r--. 1 root root 657 4月 28 2021 firewalld.service
-rw-r--r--. 1 root root 469 6月 17 2016 firstboot-graphical.service
-rw-r--r--. 1 root root 826 12月 7 2023 systemd-firstboot.service
[root@linux-yzk ~]#
3.查看防火墙文件的详细的信息 可执行的文件在11,12,13行前面有exe
[root@linux-yzk ~]# 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
[root@linux-yzk ~]#
解释服务管理脚本的作用
其实就是帮你执行了软件提供的2进制的命令
- 例如 firewalld 命令
1. 可以使用which 命令查看2进制的文件
[root@linux-yzk ~]# which firewalld
/usr/sbin/firewalld
- 例如 niginx 命令
[root@linux-yzk ~]# which nginx
/usr/sbin/nginx
[root@linux-yzk ~]#
使用防火墙命令,查看系统提供了哪些模板
1.列出所有的区域模板
列出区域模板,以及具体的信息
[root@yuchao-linux01 ~]# firewall-cmd --list-all-zones
列出所有的区域的名字
[root@yuchao-linux01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
2.列出当前使用的区域是
[root@yuchao-linux01 ~]# firewall-cmd --get-default-zone
public
3.查看当前的public区域,以及其详细信息
[root@yuchao-linux01 ~]# # 列出当前使用的区域,以及详细信息
[root@yuchao-linux01 ~]#
[root@yuchao-linux01 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client ntp
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
如果忘记了防火墙的命令有关的知识
[root@linux-yzk ~]# firewall-cmd --help | grep add --> 这个命令
--list-all-zones List everything added for or enabled in all zones [P]
--ipset=<ipset> --add-entry=<entry>
--ipset=<ipset> --add-entries-from-file=<entry>
--icmptype=<icmptype> --add-destination=<ipv>
其实firewalld,作用其实是ipables 的规则
查看系统上所有的IP tables的命令
[root@linux-yzk ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
给ntpd的服务打开防火墙
systemctl stop firewalld
systemctl disable firewalld
直接给他关啦
这个我们一般给关了 一般用的是 IP ables
定时任务的学习 ,crontab
语法:
-l 列出当前用户有哪些计划
-e 编辑当前用户的计划
-r delete
重要的是定时任务的学习的学习

定时任务的编写 要使用命令的绝对的路径可以用which命令进行查看
1. crontab -e 编辑定时任务
[root@yuchao-linux01 ~]# crontab -e
crontab: installing new crontab
2. 写入正确的语法 注意用命令绝对路径
* * * * * /usr/bin/echo 'hello,i am your superman' >> /tmp/man.txt
3.查看定时任务:cromtab -l
学习定时任务最好的办法就是,练
学习定时任务,最简单的,就是直接通过案例,掌握其语法
* * * * *
分 时 日 月 周 命令的绝对路径
从左 向右,依次去写,不要跳级
问题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
* 9-18 1-5 * 1-5
* * * * *
分 时 日 月 周 命令的绝对路径
问题10:每1分钟向文件里写入一句话"超哥666",且实时监测文件内容变化。
* * * * * /usr/bin/echo "好快乐啊" >> /opt/t1.txt
禁止那些用户的创建定时的任务
/etc/cron.deny 黑名单文件 (将系统中,所有uid大于1000的用户,全部写入黑名单)
/etc/cron.allow 白名单 ,优先级高于黑名单
浙公网安备 33010602011771号