12 第十五课预习

10.19 iptables规则备份恢复

Linux防火墙—netfilter

1. 保存 iptables规则 

(默认规则保存到:/etc/sysconfig/iptables文件中。)

[root@hostname ~]# service iptables save

2. 备份 iptables规则重定向(保存)到指定路径 

[root@hostname ~]# iptables-save > /tmp/ipt.txt

[root@hostname ~]# cat /tmp/ipt.txt  #查看保存的iptables规则 

3. 清空 iptables规则 

[root@hostname ~]# iptables -t nat -F

4. 恢复 iptables规则(刚刚备份的) 

[root@hostname ~]# iptables-restore < /tmp/ipt.txt

扫描二维码关注公众号,回复: 3194802 查看本文章

10.20 firewalld的9个zone

[root@hostname ~]# systemctl disable iptables # 关闭 iptables(centos6 防火墙) 
[root@hostname ~]# systemctl stop iptables
[root@hostname ~]# systemctl enable firewalld #打开 firewalled(centos7防火墙 之前关掉了)
[root@hostname ~]# systemctl start firewalld

firewalld 默认9zone(规则);默认zonepublic,每个zone是一个规则集

[root@hostname ~]# firewall-cmd --get-zones #查看 firewalld 所有zone(共9个,默认zonepublic

[root@hostname ~]# firewall-cmd --get-default-zone #查询 firewalled默认zone(规则)是什么(默认zonepublic)  

10.21 firewalld关于zone的操作

1. 更改 firewalled默认zone(规则)

[root@hostname ~]# firewall-cmd --set-default-zone=work #drop/block/public/externel/dmz/work/home/internel/trusted

2. 查看 指定网卡zone(规则)firewall-cmd --get-zone-of-interface=网卡名

[root@hostname ~]# firewall-cmd --get-zone-of-interface=ens33
[root@hostname ~]# firewall-cmd --get-zone-of-interface=ens37
[root@hostname ~]# firewall-cmd --get-zone-of-interface=lo

3. 增加 指定网卡zone(规则)firewall-cmd --zone=定义zone --add-interface=网卡名

[root@hostname ~]# firewall-cmd --zone=public --add-interface=ens37
[root@hostname ~]# firewall-cmd --zone=public --add-interface=lo

4. 更改 指定网卡zonefirewall-cmd --zone=新zone --change-interface=网卡名

[root@hostname ~]# firewall-cmd --zone=block --change-interface=ens33
[root@hostname ~]# firewall-cmd --zone=block --change-interface=ens37
[root@hostname ~]# firewall-cmd --zone=block --change-interface=lo

5. 删除 指定网卡zone(变成默认zone)firewall-cmd --zone=网卡对应zone  --remove-interface=网卡名

[root@hostname ~]# firewall-cmd --zone=block  --remove-interface=ens33

6. 查看 系统所有网卡 对应zone(查看系统所有网卡所在的zone)

[root@hostname ~]# firewall-cmd --get-active-zones

10.22 firewalld关于service的操作

services是zone下的一个子单元,(指定的一个端口:如http,到service下,就不会被限制过滤)  
http操作80端口   https操作443端口   ssh操作22端口  

1. 查看 系统所有的servie (zone下servie相当于白名单,添加http等到servie下,就不会过滤直接放行)

[root@hostname ~]# firewall-cmd --get-services
[root@hostname ~]# firewall-cmd --get-default-zone #查看 默认zone 

2. 查看 默认zone,包含哪些service 

[root@hostname ~]# firewall-cmd --list-services

3. 查看 指定zone,包含哪些service firewall-cmd --zone=指定zone --list-services

[root@hostname ~]# firewall-cmd --zone=work --list-services

4. 增加 http指定zone下(临时储存在内存中) 

[root@hostname ~]# firewall-cmd --zone=work --add-service=http

5. 增加 http指定zone下(永久保存在zone配置文件) 

[root@hostname ~]# firewall-cmd --zone=work --add-service=http --permanent

6. 查看 zone配置文件所在目录 (更新永久保存到配置文件,会重新生成.xml文件,之前的配置文件变成.xml.old)

[root@hostname ~]# ls /etc/firewalld/zones

7. 查看 zone配置文件内容 

[root@hostname ~]# cat /etc/firewalld/zones/work.xml

8. 查看 firewalld配置文件所在目录 (默认无,还没有更改过firewalld配置文件)

[root@hostname ~]# ls /etc/firewalld/services

9. 查看 zone配置文件模板 

[root@hostname ~]# ls /usr/lib/firewalld/zones

10. 查看 services配置文件模板 

[root@hostname ~]# ls /usr/lib/firewalld/services

•需求:ftp服务自定义端口1121  •需要在work zone下面放行ftp

1. 拷贝 services/ftp.xml配置文件/etc/firewalld/services/

[root@hostname ~]# cp /usr/lib/firewalld/services/ftp.xml  /etc/firewalld/services

2. 编辑 ftp配置文件,22端口更改为自定义的 1121

[root@hostname ~]# vi /etc/firewalld/services/ftp.xml  #更改: port="1121"

3. 拷贝 zones/work.xml配置文件/etc/firewalld/services/

(注意:上面做实验把默认zone设定为work了,http设定到work的配置文件了,永久保存就会产生了一个work.xml文件,这里复制的模板覆盖掉之前的文件就可以了!)

[root@hostname ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones

4. 编辑 work配置文件,添加一条 service名字ftp 

[root@hostname ~]# vi /etc/firewalld/zones/work.xml #添加一行: <service name="ftp"/>

5. 重新加载 :

[root@hostname ~]# firewall-cmd --reload

6. 查看zone(work)下,包含service(ftp):

[root@hostname ~]# firewall-cmd --zone=work --list-services

10.23 linux任务计划cron

任务计划:我们可能凌晨备份数据,重启服务等等,这样在某个时间自动的执行脚本或命令,

1. 查看 任务计划配置文件 (写入格式) 

[root@hostname ~]# cat /etc/crontab

2. 编写 任务计划 

[root@hostname ~]# crontab -e # i 进入编辑模式

*表示全部,
每天的凌晨三点都执行,不限定日月周  执行12.sh脚本, 正确输出日志12.log  错误输出日志21.log
0分 3时 * * * /bin/bash /usr/local/sbin/12.sh >>/tmp/12.log 2>>/tmp/21.log

例1:12  14时 1-20号  每隔2个  星期1-星期6   执行命令  执行的脚本  >>正确输出日志路径  2>>错误输出日志路径

12  14  1-20  */2   1-6  /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/321.log

例2:格式: 分 时 日 月 周 执行命令  执行的脚本  >>正确输出日志路径  2>>错误输出日志路径

格式: 分 时 日 月 周 /bin/bash  /usr/local/sbin/123.sh >>/tmp/1234.log  2>>/tmp/4321.log
详解:*  表示(////)全部范围   注意: 执行命令要填写绝对路径!!!
范围:0-59范围:0-23范围:1-31范围:1-12范围:0-6(0表示星期天,7也表示星期天)
可用格式:  
1-5(表示一个范围1到5);可用格式:  1,2,3(表示1或2或3)  比如:星期一,星期二,星期三
可用格式:  
*/2(表示被2整除的数字)比如小时,每隔2小时; 比如月,双月,被2整除的月,每隔两个月

3. 启动 crond进程 :

[root@hostname ~]# systemctl start crond

4. 搜索 crond进程是否启动 ?

[root@hostname ~]# ps aux |grep crond

5. 查看 crond进程状态(判断是否启动?图中绿色表示启动了) :

[root@hostname ~]# systemctl status crond

6. 关闭 crond进程 :

[root@hostname ~]# systemctl stop crond

7. 列出 任务计划 

[root@hostname ~]# crontab -l

8. 列出 指定用户任务计划 

[root@hostname ~]# crontab -u root -l

9. 查看 任务计划存放目录(文件是用户名命名文件) :(备份用户的任务计划,直接备份这个cron目录即可!!!)

[root@hostname ~]# ls /var/spool/cron/   #文件是用户名命名文件!!!

10. 删除 任务计划 

[root@hostname ~]# crontab -r

10.24 chkconfig工具(服务)

1. 列出 当前系统使用chkconfig工具的服务有哪些?运行级 是什么?

[root@hostname ~]# chkconfig --list #不包含原生systemd服务,可用systemd查看

注意:2级别  3级别  4级别  5级别 根据自己需求,可开/可关 2\3多用户 5带图像
注意:0级别 1级别 6级别  必须0关机 1单用户 6重启

0级别   关机状态;1级别   单用户模式;2级别   多用户模式(带nfs服务);3级别   多用户模式(不带图形,没有nff服务)
4级别   保留状态(暂时没用);5级别   多用户模式(带图形);6级别   重启

2. 查看 启动脚本服务(文件脚本) :

[root@hostname ~]# ls /etc/init.d

3. 关闭 network服务 3级别 :

[root@hostname ~]# chkconfig --level 3 network off

4. 关闭 network服务 2级别3级别4级别5级别 :

[root@hostname ~]# chkconfig --level 2345 network off

5. 开启 network服务 2级别3级别4级别5级别 :

[root@hostname ~]# chkconfig --level 2345 network on

6. 查看 指定的network服务 运行级别 :

[root@hostname ~]# chkconfig --list network

7.0 进入 cd /etc/init.d目录下:

[root@hostname ~]# cd /etc/init.d

7.1 创建 123脚本(自定义),/etc/init.d目录下:

[root@hostname ~]# cp network 123 #启动脚本是123
[root@hostname ~]# chkconfig --list

7.2 新增 自定义服务 到 chkconfig服务列表下 :

[root@hostname ~]# chkconfig --add 123
[root@hostname ~]# chkconfig --list

8. 删除 自定义服务 

[root@hostname ~]# chkconfig --del network

注意:新增加的自定义服务脚本,按格式,添加到/etc/init.d/目录下

 

10.25 systemd管理服务

1. systemd列出所有units服务,类型为servie 

[root@hostname ~]# systemctl list-units --all --type=service

2. 未激活状态active不再列出 

[root@hostname ~]# systemctl list-units  --type=service

3. crond服务 开机不启动 

[root@hostname ~]# systemctl disable crond

4. crond服务 开机启动 

[root@hostname ~]# systemctl enable crond.service

5. 检查 crond服务 是否开机启动 

[root@hostname ~]# systemctl is-enabled crond

6. 查看 crond服务状态 :

[root@hostname ~]# systemctl status crond.service

7. 停止 crond服务 :

[root@hostname ~]# systemctl stop crond

8. 启动 crond服务 :

[root@hostname ~]# systemctl start crond

9. 重启 crond服务 :

[root@hostname ~]# systemctl restart crond

10.26 unit介绍

1. 列出 系统所有unit 

[root@hostname ~]# ls /usr/lib/systemd/system
unit
分为以下类型:service  系统服务;target    多个unit组成的组;device   硬件设备;mount   文件系统挂载点;automount   自动挂载点;path   文件或路径;scope   不是由systemd启动的外部进程;slice   进程组;snapshot   systemd快照;socket   进程间通信套接字;swap   swap文件;timer    定时器

unit相关的命令

1. 列出 正在运行unit 

[root@hostname ~]# systemctl list-units  

2. 列出 所有的(包括失败的或者inactive的) :

[root@hostname ~]# systemctl list-units --all  

3. 指定列出 状态为inactiveunit 

[root@hostname ~]# systemctl list-units --all --state=inactive  

4. 指定列出 状态为activeservice 

[root@hostname ~]# systemctl list-units --type=service    

5. 指定查看 crond.service服务,是否为active ?

[root@hostname ~]# systemctl is-active crond.service  

6. 指定查看 crond.service服务,是否为enabled 

[root@hostname ~]# systemctl is-enabled crond.service  

10.27 target介绍

 •系统为了方便管理,用target管理unit

1. 列出 系统所有target 

[root@hostname ~]# systemctl list-unit-files --type=target

2查看 指定target下面有哪些unit 

[root@hostname ~]# systemctl list-dependencies multi-user.target

3. 查看 系统默认target 

[root@hostname ~]# systemctl get-default

4. 设置(更改) 默认target

[root@hostname ~]# systemctl set-default multi-user.target

•一个service 是一种类型unit
• target多个unit组成的
• 一个
target里面,包含了多个(若干)service

5. 查看 sshd.serviceservice,属于哪个target 

[root@hostname ~]#  cat /usr/lib/systemd/system/sshd.service

 

猜你喜欢

转载自blog.csdn.net/xiaoyuerp/article/details/82085307