1 iptables规则备份和恢复
2 firewalld的9个zone
3 firewalld关于zone的操作
4 firewalld关于service的操作
5 linux任务计划cron
6 chkconfig工具
7 systemd管理服务
8 unit介绍
9 target介绍

扩展
提供一个iptables系列文章的博客  https://www.zsythink.net/archives/tag/iptables/page/2/
anacron https://www.jianshu.com/p/3009a9b7d024?from=timeline
systemd自定义启动脚本  http://www.jb51.net/article/100457.htm

1、iptables规则备份和恢复

Linux防火墙及系统服务管理

iptables备份配置文件到一个文件中

iptables-save > /tmp/ipt.txt

Linux防火墙及系统服务管理

清空iptables

iptables -t nat -F

Linux防火墙及系统服务管理

恢复iptables

iptables-restore < /tmp/ipt.txt

iptables -t nat -nvL 只查看nat规则


2、firewalld的9个zone

firewalld有9个zone,zone是firewalld的单位,默认使用public zone

关闭iptables开机启动

systemctl disable iptables

临时关闭iptables

systemctl stop iptables

开启firewalld开机启动

systemctl enable firewalld

临时开启firewalld

systemctl start firewalld

查看所有zone

Linux防火墙及系统服务管理

firewall-cmd --get-zones

查看默认zone

firewall-cmd --get-default-zone

Linux防火墙及系统服务管理


3、firewalld关于zone的操作

Linux防火墙及系统服务管理

设定默认zone

firewall-cmd --set-default-zone=work

查指定网卡

firewall-cmd --get-zone-of-interface=ens32

给指定网卡设置zone

firewall-cmd --zone=dmz --add-interface=ens32:0

针对网卡更改zone

firewall-cmd --zone=block --change-interface=ens32:0

针对网卡删除zone

firewall-cmd --zone=block --remove-interface=ens32:0

查看系统所有网卡所在zone

firewall-cmd --get-active-zones


4、firewalld关于service的操作

查看所有的service有哪些

firewall-cmd --get-services

Linux防火墙及系统服务管理

查看当前的zone

firewall-cmd  --get-default-zone

Linux防火墙及系统服务管理

查看当前work的zone里面有哪些service

firewall-cmd --list-services或者firewall-cmd --list-service

查看某个zone里面有哪些service

firewall-cmd --zone=work --list-service

Linux防火墙及系统服务管理

把http增加到public zone下面

firewall-cmd --zone=public --add-service=http
Linux防火墙及系统服务管理

把更改写入配置文件(永久生效)

firewall-cmd --zone=public --add-service=ftp --permanent

Linux防火墙及系统服务管理

查看配置文件

cat /etc/firewalld/zones/public.xml
Linux防火墙及系统服务管理

ftp服务器自定义端口1121,需要在work zone下面放行ftp

把ftp的配置文件拷贝到etc下面去

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

编辑/etc/firewalld/services/ftp.xml配置文件,把21端口改成1121

vi /etc/firewalld/services/ftp.xml

Linux防火墙及系统服务管理

把work配置文件拷贝到zones下面

cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

编辑/etc/firewalld/zones/work.xml配置文件,增加标记的这行

vi /etc/firewalld/zones/work.xml

Linux防火墙及系统服务管理

重新加载加载服务

firewall-cmd --reload

Linux防火墙及系统服务管理

查看

firewall-cmd --zone=work  --list-services

Linux防火墙及系统服务管理

5、linux任务计划cron

Linux防火墙及系统服务管理

查看任务计划配置文件(会定义SHELL,PATH环境变量、命令的路径,MAILTO发送邮件给谁)

格式

 *  *  *  *  * user-name  command to be executed

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第五位表示星期,范围是0到6,0/7表示周日

user-name 第六位表示用户

command to be executed 第七列表示要执行的命令

cat /etc/crontab

Linux防火墙及系统服务管理

定义任务计划配置文件

crontab -e

每天3点执行/usr/local/sbin/123.sh脚本,并每天记录正确和错误日志到tmp123.log

Linux防火墙及系统服务管理

1到10号,双月,周二和周五执行,确定某一天的唯一性,用星期确认

Linux防火墙及系统服务管理

启动crond服务

systemctl start crond

检查服务是否启动成功

ps aux |grep cron

查看crond服务状态是否正常

systemctl status crond
Linux防火墙及系统服务管理

如果任务没有执行很有可能是命令没有用绝对路径,因为用的命令不在变量PATH里,所以要么使用绝对路径,要么将命令加入到PATH变量里
每一个执行的脚本最好都写上追加正确与错误的内容到指定的文件,这样就可以有迹可寻,当有错误时可以查看下错误的文件日志就知道了
备份脚本的内容,可以把下面的root直接拷贝就行了,如果是user用户就会是user名称的文件
Linux防火墙及系统服务管理
crontab -r是删除脚 本
Linux防火墙及系统服务管理

6、chkconfig工具

chkconfig --list,列出所有的系统服务

Linux防火墙及系统服务管理

服务的脚本存放位置:

Linux防火墙及系统服务管理

chkconfig network off,将network服务关闭

Linux防火墙及系统服务管理

7个运行级别分别表示:

0(关机);

1(单用户);

2(多用户模式,没nfs服务)

3(多用户模式,不带图形)

4(保留)

5(多用户模式带图形)

6(重启)

针对某一个或几个运行级别状态打开、关闭,多个运行级别之前不用符号隔开

Linux防火墙及系统服务管理

自定义添加和删除服务

进入/etc/init.d/目录下,只有在这个目录下,才能添加到服务列表里去

Linux防火墙及系统服务管理

Linux防火墙及系统服务管理

vim 123,对文件内容有要求,首先要是一个shell脚本,然后有chkconfig和description的格式,才能被识别,格式如下:

Linux防火墙及系统服务管理

删除服务

Linux防火墙及系统服务管理

7、systemd管理服务

systemctl list-unit-files,查看所有的服务

systemctl list-units --all --type=service,指定type,列出所有的service,按空格可以往下翻,不加-all,则不会列出inactive项

Linux防火墙及系统服务管理

几个常用的和服务相关的命令

systemctl enable crond.service,让服务开机启动(.service可以省略);systemctl disable crond,不让服务开机启动

Linux防火墙及系统服务管理

systemctl status crond,查看状态

Linux防火墙及系统服务管理

systemctl stop crond,停止服务;

systemctl start crond,启动服务;

systemctl restart crond,重启服务;

systemctl is-enabled crond ,检查服务是否开机启动

Linux防火墙及系统服务管理

8、unit介绍

Linux防火墙及系统服务管理

unit相关命令

Linux防火墙及系统服务管理

systemctl list-units ,列出正在运行的unit

systemctl list-units --all,列出所有包括失败的或者inactive的

systemctl list-units --all --state=inactive,列出inactive的unit

systemctl list-units --type=service,列出状态为active的service

systemctl is-active crond.service,查看某个服务是否为active

9、target介绍

系统为例方便管理用target来管理unit

systemctl list-unit-files --type=target,列出系统中所有的target

systemctl list-dependencies multi-user.target,查看指定target下面有哪些unit

systemctl get-default,查看系统默认的target

systemctl set-default multi-user.target,设置默认的target

一个service属于一种类型的unit,多个unit组成了一个target,一个target里面包含了多个service。

cat /usr/lib/systemd/system/sshd.service ,查看sshd.service属于哪个target,看Install部分可知

转载于:https://my.oschina.net/u/3992081/blog/2878425

相关文章: