第十章、日常运维(下)

10.19 iptables规则备份和恢复

10.20 firewalld的9个zone

10.21 firewalld关于zone的操作

10.22 firewalld关于service的操作

10.23 linux任务计划cron

10.24 chkconfig工具

10.25 systemd管理服务

10.26 unit介绍

10.27 target介绍

 

 

10.19 iptables规则备份和恢复

service iptables save    #将规则保存到/etc/sysconfig/iptables

[[email protected] ~]# service iptables save

iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

 

iptables-save > my.ipt         #把iptables规则备份到my.ipt文件

iptables-restore  <  my.ipt    #恢复杠备份的文件

[[email protected] ~]# iptables-save > my.ipt

[[email protected] ~]# iptables -F

[[email protected] ~]# iptables -nvL

Chain INPUT (policy ACCEPT 35 packets, 2485 bytes)

pkts bytes target     prot opt in     out     source               destination

 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

 

Chain OUTPUT (policy ACCEPT 18 packets, 1880 bytes)

pkts bytes target     prot opt in     out     source               destination

[[email protected] ~]# iptables-restore  <  my.ipt

[[email protected] ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

    6   428 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0

    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0

    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22

    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

 

Chain OUTPUT (policy ACCEPT 4 packets, 480 bytes)

pkts bytes target     prot opt in     out     source               destination

 

 

10.20 firewalld的9个zone

 ##禁掉iptables,打开firewalld

systemctl disable iptables       #禁止iptables开机自启    

systemctl stop iptables          #关闭iptables服务

systemctl enable firewalld     #firewalld开机自启

systemctl start firewalld        #开启firewalld

[[email protected] ~]# systemctl disable iptables

Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.

[[email protected] ~]# systemctl stop iptables

[[email protected] ~]# systemctl enable firewalld

Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.

Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.

[[email protected] ~]# systemctl start firewalld

[[email protected] ~]#

 

firewalld默认有9个zone

#默认zone为public

firewall-cmd --get-zones           #查看所有zone

[[email protected] ~]# firewall-cmd --get-zones

block dmz drop external home internal public trusted work

 

firewall-cmd --get-default-zone        #查看默认zone

[[email protected] ~]# firewall-cmd --get-default-zone

public

 

##firewalld的9个zone

drop(丢弃)

任何接受的网络数据包被丢弃,每一任何恢复。仅能有发送出去的网络连接

block

(限制)

任何接受的网络连接都被IPv4的cimp-host-prohibited信息和IPv6的cimp6-host-prohibited信息所拒绝

public

(公共)

在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收经过选取的连接。

external

(外部)

特别是为路由器启用了伪装功能的外部网。你不能信任来自网络的其他计算机,不能相信他们不会对你的计算机早餐危害,只能就收经过选择的连接。

dmz

(非军事区)

用于你的非军事区的电脑,此区域内科公开访问,可以有限地进入你的内部网络,仅仅接收经过选择的连接

work(工作)

用于工作区。你可以基本信任网络内的其他计算机不会危害你的电脑。仅仅接收经过选择的连接。

home

(家庭)

用于家庭网络,你可以基本信任网络内的其他计算机不会危害你的计算机,仅仅接受经过选择的连接。

internal

(内部)

用于内部网络。你可以基本信任网络内的其他计算机不会威胁你的计算机。仅仅接收经过选择的连接。

trusted

(信任)

可接收所有网络连接

 

 

10.21 firewalld关于zone的操作

##输入时输入到--set-def时,后面的可以Tab键补全到--set-default-zone=

1、设定默认zone

firewall-cmd --set-default-zone=work    #设定默认zone为work

[[email protected] ~]# firewall-cmd --set-default-zone=work

success

 

2、查指定网卡的zone

firewall-cmd --get-zone-of-interface=ens33 (网卡名)      #查看ens33的zone    

[[email protected] ~]# firewall-cmd --get-zone-of-interface=ens33

work

 

3、指定网卡设置zone

firewall-cmd --zone=public  --add-interface=ens33     #  给ens33设置zone为public

[[email protected] ~]# firewall-cmd --zone=public  --add-interface=ens33

The interface is under control of NetworkManager, setting zone to 'public'.

success

 

4、针对网卡更改zone

firewall-cmd --zone=dmz --change-interface=lo     #更改lo网卡的zone为dmz

[[email protected] ~]# firewall-cmd --zone=dmz --change-interface=lo

success

 

5、针对网卡删除

firewall-cmd --zone=dmz  --remove-interface=lo      #删除lo网卡为work的zone

##ps:将网卡的zone删除后,该网卡的zone会变为默认的zone

[[email protected] ~]# firewall-cmd --get-zone-of-interface=lo

work

[[email protected] ~]# firewall-cmd --zone=work --remove-interface=lo

success

[[email protected] ~]# firewall-cmd --get-zone-of-interface=lo

no zone

 

6、查看系统所有网卡所在的zone

firewall-cmd --get-active-zones  

[[email protected] ~]# firewall-cmd --get-active-zones

public

  interfaces: ens33

 

 

10.22 firewalld关于service的操作(重点

##ps:services加不加s都行service

1、查看所有的servies

firewall-cmd --get-services  

firewall-cmd --get-service

第十章、日常运维(下)

 

2、查看当前zone下有哪些service

firewall-cmd --list-services 

firewall-cmd --list-service

[[email protected] ~]# firewall-cmd --list-service

ssh dhcpv6-client

[[email protected] ~]# firewall-cmd --list-services

ssh dhcpv6-client

 

3、在zone内增加服务

firewall-cmd --zone=public --add-service=http   #把http服务增加到public zone下面

[[email protected] ~]# firewall-cmd --zone=public --add-service=http

success

 

firewall-cmd --zone=public --remove-service=http   #删除在public zone下的http服务

[[email protected] ~]# firewall-cmd --zone=public --remove-service=http

success

 

4、将zone内增加的服务变为永久

#zone配置文件模板存放位置/usr/lib/firewalld/zones/

#使用-- permanent,会更改配置文件,然后在/etc/firewalld/zones目录下面生成配置文件

ps:每当保存一次配置文件,系统会将保存前的配置文件.old结尾的作为备份,例public zone要增加服务保存到配置文件public.xml,系统将保存前文件的内容备份到public.xml.old

 

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

#把http服务增加到public zone并在/etc/firewalld/zones目录下生成配置文件public.xml

[[email protected] ~]# firewall-cmd --zone=public --add-service=http --permanent

success

[[email protected] ~]# ls /etc/firewalld/zones/

public.xml  public.xml.old

 

5、案例一

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

##在某个zone下放行服务步骤

(服务配置文件位置/usr/lib/firewalld/services/)、(zone配置文件位置/usr/lib/firewalld/zones/

1、先将服务配置文件放到/etc/firewalld/services/下,某zone配置文件放到/etc/firewalld/zones/下;

2、修改该zone的配置文件,将放行服务名字加进去<service name="服务名字"/>

 

步骤一:将ftp配置文件拷贝到/etc/firewalld/services

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

 

步骤二:编辑ftp配置文件,将"port=21"改为"port=1121";然后将work的配置文件放到/etc/firewalld/zones/;编辑work配置文件;最后重新加载firewalld

 

vi /etc/firewalld/services/ftp.xml 

 

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

 

vi /etc/firewalld/zones/work.xml   

#增加一行 <service name="ftp"/>

 

firewall-cmd --reload       #重新加载firewalld

 

firewall-cmd --zone=work --list-services      #检查work zone下的服务

[[email protected] ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

[[email protected] ~]# vi /etc/firewalld/services/ftp.xml

[[email protected] ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

[[email protected] ~]# vi /etc/firewalld/zones/work.xml

[[email protected] ~]# firewall-cmd --reload

success

[[email protected] ~]# firewall-cmd --zone=work --list-services

ssh dhcpv6-client ftp

 

 

10.23 linux任务计划cron(重点

ps:对于需要周期性执行的任务可以使用crontab命令,该命令所使用的服务是crond。因此在使用之前一定要先启动crond服务。

当使用者执行crontab命令时,系统会按如下步骤操作

1.先查找/etc/corn.allow文件,在该文件中存在的用户可以使用crontab,不在该文件中的用户不能使用crontab(即使用没有写在/etc/cron.deny中)

2.如果没有/etc/cron.allow就寻找/etc/cron.deny文件,在该文件中存在的用户不能使用crontab,在该文件中不存的用户就可以用crontab

3.如果两个文件都不存在,则只有root可以使用crontab。

多数linux版本默认的文件是/etc/cron.deny,而且该文件为空

 

crontab建立例行性任务的方式

1.针对用户的例行性任务,用crontab -e命令来管理任务

2.针对系统的例行性任务,可以通过/etc/crontab文件来管理任务

 

1、针对用户的例行性任务

语法: crontab [-u username] [-l|-e|-r]      需要用绝对路径

选项说明

-u   只用root才有权限使用这个参数,用于帮助其他用户建立或删除crontab

-e   编辑crontab的工作内容,即进入crontab编辑模式

-l    查看crontab的工作内容

-r     移除crontab所有的工作内容,如果要删除某一项的内容,只能使用crontab -e手动删除

crontab的模式是:分 时 日 月 周 命令

分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7

"-"表示一个时间段范围,可用格式1-5表示一个范围1到5

","表示分割时段的意思,可用格式1,2,3表示1或者2或者3

"*"表示任何时间都能够接受,任何时间都可以执行该命令可用格式。"/n"代表每隔n个时间单位。*/2表示被2整除的数字,比如小时,那就是每隔2小时

 

0 3 *  * *         /bin/bash    /usr/local/sbin/123.sh >> /tmp/123.log  2>>/123.log

#0 3 * * *表示每天3点;整体表示每天3点执行123.sh脚本并将执行命令的正确和错误信息追加到123.log文件

 

#可用格式1-5表示一个范围1到5

 * 1-5 * * *   表示每天1点到5点

*  * * 1-5 *   表示每1月到5月

 

#可用格式1,2,3表示1或者2或者3

*  * *  *  1,2   表示每周一或者周二

*  1,2 * *  *    表示每天1点或者2点

 

#可用格式*/2表示被2整除的数字(偶数),比如小时,那就是每隔2小时

*  * */2  *    表示每两个月(2,4,6,8,10,12月)

 

#要保证服务是启动状态

systemctl start crond.service           #启动crond服务

 

查看crond服务是否启动两种方法

1、ps aux |grep crond      #查看crond进程,有进程说明启动了

[[email protected] ~]# systemctl start crond.service

[[email protected] ~]# ps aux |grep crond

root        573  0.0  0.1 126284  1704 ?        Ss   09:22   0:02 /usr/sbin/crond -n

root       2848  0.0  0.0 112720   976 pts/0    R+   11:14   0:00 grep --color=auto crond

 

2、systemctl status crond  #查看crond状态,active表示服务已启动,inactive表示服务停止

[[email protected] ~]# systemctl status crond

● crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since 四 2018-08-30 09:22:21 CST; 1h 52min ago

Main PID: 573 (crond)

   CGroup: /system.slice/crond.service

           └─573 /usr/sbin/crond -n

 

8月 30 09:22:21 xinlinux-02 systemd[1]: Started Command Scheduler.

8月 30 09:22:21 xinlinux-02 systemd[1]: Starting Command Scheduler...

8月 30 09:22:22 xinlinux-02 crond[573]: (CRON) INFO (RANDOM_DELAY will...)

8月 30 09:22:27 xinlinux-02 crond[573]: (CRON) INFO (running with inot...)

Hint: Some lines were ellipsized, use -l to show in full.

 

备份crontab配置文件/var/spool/cron/username

#crontab配置文件名是以用户名命名,放在/var/spool/cron/目录下

 

2、针对系统的例行性任务

直接编辑/etc/crontab。基本上这个服务的最低侦测限制是分钟。cron会每分钟读取一次/etc/crontab与/var/spool/cron里的资料内容。

/etc/crontab文件中的命令支持两种执行命令的方式

1.直接执行命令

[[email protected] ~]# crontab -e 

no crontab for root - using an empty one  

#用法和vim差不多

 

2.目录规则

#以建立一个每隔5分钟执行一次的命令为例 

#建立/root/five目录 

[[email protected] ~]# mkdir /root/five

 [[email protected] ~]# vim /etc/crontab 

#增加*/5 * * * *root run-parts /root/file 

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 

*/5 * * * *root run-parts /root/file

 

任务计划不执行的原因分析

不执行的原因很有可能是你写的脚本里面,没有使用绝对路径导致不执行。如果你使用的命令不在PATH里面,就无法找到该命令。所以要么将命令写一个绝对路径,要么将命令的路径加入到PATH变量里面去

建议:

命令都用绝对路径的形式

写脚本的时候,添加日志记录功能。

 

 

10.24 chkconfig工具

管理服务工具chkconfig,CentOS6及以前使用的,现在CentOS7用的是systemd

##chkconfig工具管理的运行脚本在/etc/init.d/目录下

 

chkconfig --list          #列出所有使用chkconfig的服务

[[email protected] ~]# chkconfig --list

 

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。

 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。

      查看在具体 target 启用的服务请执行

      'systemctl list-dependencies [target]'。

 

netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关

network            0:关    1:关    2:开    3:开    4:开    5:开    6:关

 

#七个运行级别(面试可能问到

init 0    关机状态 

init 1    单用户模式

init 2    多用户模式(不带图形,没有nfs服务)

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

init 4    保留的状态

init 5    多用户模式(带图形)

init 6    重启

 

chkconfig  network off    #关闭network

[[email protected] ~]# chkconfig  network off

[[email protected] ~]# chkconfig --list

 

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。

 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。

      查看在具体 target 启用的服务请执行

      'systemctl list-dependencies [target]'。

 

netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关

network            0:关    1:关    2:关    3:关    4:关    5:关    6:关

 

chkconfig  network on   #开启network

[[email protected] ~]# chkconfig  network on

[[email protected] ~]# chkconfig --list

 

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。

 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。

      查看在具体 target 启用的服务请执行

      'systemctl list-dependencies [target]'。

 

netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关

network            0:关    1:关    2:开    3:开    4:开    5:开    6:关

 

chkconfig --level 3 network off   #关闭network的3级别     

 

chkconfig --level 345 network off    #关闭network的345级别        

 

chkconfig --del network    #删除network 启动脚本 

 

chkconfig --add network   #增加network 启动脚本 

 

 

10.25 systemd管理服务(重点

Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。

systemd 的目标是:

尽可能启动更少的进程

尽可能将更多进程并行启动

 

1、查看systemd信息

systemctl list-dependencies     #显示单元依赖关系

 

systemctl list-sockets         #显示sockets信息和哪些是活动的

 

systemctl list-jobs              #查看活动的system任务

 

systemctl list-units      #显示单元是否载入及状态

 

systemctl get-default    #显示默认的目标

 

2、管理服务

systemctl list-units --all --type=service   #列出所有的服务

 

systemctl status firewalld.service             #查看服务状态

 

systemctl status firewalld.service           #停止服务

 

systemctl restart firewalld.service         #重启服务

 

systemctl reload firewalld.service        #重载服务的配置文件

 

systemctl disable firewalld.service        #将服务取消开机自动启动

 

systemctl enable firewalld.service       #将服务设置为开机自动启动

 

systemctl is-enabled firewalld.service  #检查是否开机启动

 

systemctl show firewalld.service      #显示服务或单元详细信息

 

3、改变系统状态

重启

systemctl reboot

关机

systemctl poweroff

进入紧急模式

systemctl emergency

恢复默认目标

systemctl default

 

4、查看日志消息

journalctl      #显示收集的所有日志消息

 

journalctl -u network.service   #查看网络服务的消息

 

journalctl -f     #动态跟踪消息(类似于tail -f /var/log/message)

 

journalctl -k     #仅仅显示内核消息

 

ps:其实enable的时候系统会在/usr/lib/systemd/system下创建crond的软链接文件,一旦disable时,文件就会消失

 

 

10.26 unit介绍

系统初始化需要做的事情非常多。需要启动后台服务,比如启动 SSHD 服务;需要做配置工作,比如挂载文件系统。这个过程中的每一步都被 systemd 抽象为一个配置单元,即 unit。可以认为一个服务是一个配置单元;一个挂载点是一个配置单元;一个交换分区的配置是一个配置单元

 

ls /usr/lib/systemd/system       #系统所有unit,分为以下类型:

  • service 系统服务

  • target 多个unit组成的组

  • device 硬件设备

  • mount 文件系统挂载点

  • automount 自动挂载点

  • path 文件或路径

  • scope 不是由systemd启动的外部进程

  • slice 进程组

  • snapshot systemd快照

  • socket 进程间通信套接字

  • swap  swap文件

  • timer 定时器

 

unit相关的命令

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                 #查看crond服务是否为active

 

 

10.27 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      #看[install]部分可查看sshd服务属于哪个target