第十章、日常运维(下)

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.28拓展

10.29课堂笔记

 

10.19 iptables规则备份和恢复

service iptables save    #将规则保存到 /etc/sysconfig/iptables
[root@xinlinux-02 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
 
iptables-save > my.ipt         #把iptables规则备份到my.ipt文件
iptables-restore  <  my.ipt    #恢复杠备份的文件
[root@xinlinux-02 ~]# iptables-save > my.ipt
[root@xinlinux-02 ~]# iptables -F
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# iptables-restore  <  my.ipt
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@xinlinux-02 ~]# systemctl stop iptables
[root@xinlinux-02 ~]# 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.
[root@xinlinux-02 ~]# systemctl start firewalld
[root@xinlinux-02 ~]#
 
firewalld默认有9个zone
#默认zone为public
firewall-cmd --get-zones           #查看所有zone
[root@xinlinux-02 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
 
firewall-cmd --get-default-zone        #查看默认zone
[root@xinlinux-02 ~]# 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-d时,后面的可以Tab键补全到 --set-default-zone=
1、设定默认zone
f irewall-cmd --set-default-zone=work    #设定默认zone为work
[root@xinlinux-02 ~]# firewall-cmd --set-default-zone=work
success
 
2、查指定网卡的zone
firewall-cmd --get-zone-of-interface=ens33 (网卡名)      #查看ens33的zone    
[root@xinlinux-02 ~]# firewall-cmd --get-zone-of-interface=ens33
work
 
3、指定网卡设置zone
firewall-cmd --zone=public  --add-interface=ens33     #  给ens33设置zone为public
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# firewall-cmd --zone=dmz --change-interface=lo
success
 
5、针对网卡删除zone
firewall-cmd --zone=dmz  --remove-interface=lo      #删除lo网卡为dmz的zone
##ps:将网卡的zone删除后,该网卡的zone会变为默认的zone
[root@xinlinux-02 ~]# firewall-cmd --zone=dmz --change-interface=lo
success
[root@xinlinux-02 ~]# firewall-cmd --zone=dmz  --remove-interface=lo
success
[root@xinlinux-02 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
 
6、查看系统所有网卡所在的zone
firewall-cmd --get-active-zones  
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# firewall-cmd --list-service
ssh dhcpv6-client
[root@xinlinux-02 ~]# firewall-cmd --list-services
ssh dhcpv6-client
 
3、在zone内增加服务
firewall-cmd --zone=public --add-service=http   #把http服务增加到public zone下面
[root@xinlinux-02 ~]# firewall-cmd --zone=public --add-service=http
success
 
firewall-cmd --zone=public --remove-service=http   #删除在public zone下的http服务
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@xinlinux-02 ~]# 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下的服务
[root@xinlinux-02 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@xinlinux-02 ~]# vi /etc/firewalld/services/ftp.xml
[root@xinlinux-02 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@xinlinux-02 ~]# vi /etc/firewalld/zones/work.xml
[root@xinlinux-02 ~]# firewall-cmd --reload
success
[root@xinlinux-02 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp
 
 
10.23 linux任务计划cron
格式:分 时 日 月 周 user command(用户命令)(需要用绝对路径
#分范围0-59,时范围0-23,日范围1-31,月范围1-12(或者英文简写),周1-7
 
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进程,有进程说明启动了
[root@xinlinux-02 ~]# systemctl start crond.service
[root@xinlinux-02 ~]# 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表示服务停止
[root@xinlinux-02 ~]# 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 -e          #进入crontab配置文件
 
crontab -l           #查看配置文件内容
 
crontab -r           #删除配置文件内容
 
crontab -u  用户  #指定配置文件中的用户
 
备份crontab配置文件/var/spool/cron/username
#crontab配置文件名是以用户名命名,放在 /var/spool/cron/目录下
 
 

10.24 chkconfig工具

管理服务工具chkconfig,CentOS6及以前使用的,现在CentOS7用的是systemd
##chkconfig工具管理的运行脚本在/etc/init.d/目录下
chkconfig --list          #列出所有使用chkconfig的服务
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# chkconfig  network off
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# chkconfig  network on
[root@xinlinux-02 ~]# 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管理服务

systemctl list-unit-files    # 列出所有使用systemd的服务 
[root@xinlinux-02 ~]# systemctl list-unit-files
UNIT FILE                                     STATE
proc-sys-fs-binfmt_misc.automount             static
dev-hugepages.mount                           static
dev-mqueue.mount                              static
proc-sys-fs-binfmt_misc.mount                 static
sys-fs-fuse-connections.mount                 static
sys-kernel-config.mount                       static
sys-kernel-debug.mount                        static
tmp.mount                                     disabled
brandbot.path                                 disabled
systemd-ask-password-console.path             static
systemd-ask-password-plymouth.path            static
systemd-ask-password-wall.path                static
session-5.scope                               static
session-6.scope                               static
session-9.scope                               static
arp-ethers.service                            disabled
auditd.service                                enabled
 
systemctl list-units --all --type=service      #只列出systemd下的service服务
[root@xinlinux-02 ~]# systemctl list-units --all --type=service
  UNIT                      LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service            loaded    active   running Security Auditing Se
  chronyd.service           loaded    active   running NTP client/server
  cpupower.service          loaded    inactive dead    Configure CPU power
  crond.service             loaded    active   running Command Scheduler
  dbus.service              loaded    active   running D-Bus System Message
● display-manager.service   not-found inactive dead    display-manager.serv
  dm-event.service          loaded    inactive dead    Device-mapper event
 
systemctl enable crond.service      #让服务开机启动
systemctl disable crond                 #禁止开机启动
systemctl status crond                  # 查看状态
systemctl stop crond                    #停止服务
systemctl start crond                   #启动服务
systemctl restart crond               #重启服务
systemctl is-enabled crond       #检查服务是否开机启动
 
ps:其实enable的时候系统会在/usr/lib/systemd/system下创建crond的软链接文件,一旦disable时,文件就会消失
 
 

10.26 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
 
 
10.28扩展
提供一个iptables系列文章的博客  https:#www.zsythink.net/archives/tag/iptables/page/2/
systemd自定义启动脚本  http:#www.jb51.net/article/100457.htm
 
 

10.29课堂笔记

一、iptables规则备份和恢复
service iptables  save          #保存设置规则
 
iptables-save   >1.iptables        #备份iptables规则到1.iptables 文件
 
iptables-restore     #恢复备份iptables规则的文件1.iptables
 
systemctl    stop  iptables.service     #停止iptables服务
systemctl    status  iptables.service     #查看iptables服务状态
 
 
二、firewalld的9个zone
区域
说明
drop(丢弃)
任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
block(限制)
任何接收的网络连接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝。
public(公共)
在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。
external(外部)
特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的连接。
dmz(非军事区)
用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接。
work(工作)
用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接。
home(家庭)
用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。
internal(内部)
用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。
trusted(信任)
可接受所有的网络连接。
##安装时,firewalld 里的默认zone被设定为public
firewall-cmd --get-zones     #查看所有zone命令
 
 
三、firewalld关于zone的操作
firewalld -cmd --get-default-zones     #获取firewalld的域
 
 firewall-cmd --get-zone-of-interface=ens32 #查看指定网卡的zone
 
firewall-cmd --zone=public --add-interface=ens32  #给指定网卡设置zone
 
firewall-cmd --zone=public --change-interface=ens34   #给指定网卡变更zone
 
firewall-cmd --zone=public --remove-interface=ens34  #删除指定网卡的zone
 
firewall-cmd --get-active-zones    #查看活动zone的列表
 
firewall-cmd --zone=public --list-interfaces  #查看某区域下绑定的接口
 
firewall-cmd --zone=public --list-all  #查看指定区域的所有设置
 
 
四、firewalld关于service的操作(重点
firewall-cmd --get-services   #列出系统里所有的service
 
firewall-cmd --list-services   #查看默认zone下的service
 
firewall-cmd --zone=work --list-services   #查看指定zone下的service
 
给指定zone来添加服务
#可增加 --permanent选项并重新加载防火墙,使之成为永久性设置( 保存到zone默认配置文件中)。
firewall-cmd --reload    #重载防火墙不中断已建立连接 
 
firewall-cmd --complete-reload  #不仅仅中断您已经移除的服务,还会中断所有已经建立的连接。
 
需求:ftp服务自定义端口改为1121,在work,zone下面放行ftp
1、首先先将 /usr/lib/firewalld/services/ftp.xml复制到/etc/firewalld/services/下
2、2.编辑/etc/firewalld/services/ftp.xml文件,修改端口为1211
3、将/usr/lib/firewalld/zones/work.xml拷贝到 /etc/firewalld/zones/
4、编辑/etc/firewalld/zones/work.xml文件,增加一行<service name="ftp"/>
5、重新加载防火墙firewall-cmd --reload
6、验证 firewall-cmd --zone=work --list-services
 
 
五、linux任务计划crond(重点
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小时
 
2、针对系统的例行性任务
直接编辑/etc/crontab。基本上这个服务的最低侦测限制是分钟。cron会每分钟读取一次/etc/crontab与/var/spool/cron里的资料内容。
/etc/crontab文件中的命令支持两种执行命令的方式
1.直接执行命令
[root@lanquark ~]# crontab -e 
no crontab for root - using an empty one  
#用法和vim差不多
 
2.目录规则
#以建立一个每隔5分钟执行一次的命令为例 
#建立/root/five目录 
[root@lanquark ~]# mkdir /root/five
 [root@lanquark ~]# 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变量里面去
建议:
命令都用绝对路径的形式
写脚本的时候,添加日志记录功能。
 
 
六、chkconfig工具
##CentOS7已经没有运行级别这个 概念,只是为了向上兼容,只有CentOS6及之前版本才有运行级别
运行级别 面试可能问到
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:多用户模式, 不带图形,少nfs服务
等级3表示:多用户模式,不带图形
等级4表示:是一种保留的级别
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
 
chkconfig netconsole off  -level  2  #关闭2运行级别
 
c hkconfig netconsole off  -level  2345  #关闭2345运行级别
 
 
七、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     #仅仅显示内核消息
 
 
八、unit介绍
系统初始化需要做的事情非常多。需要启动后台服务,比如启动 SSHD 服务;需要做配置工作,比如挂载文件系统。这个过程中的每一步都被 systemd 抽象为一个配置单元,即 unit。可以认为一个服务是一个配置单元;一个挂载点是一个配置单元;一个交换分区的配置是一个配置单元
 
systemctl list-units     #列出正在运行的unit
 
systemctl list-units --all    #列出所有,包括失败的或者inactive的
 
systemctl list-units --type=service  #列出状态为active的service
 
systemctl is-active firewalld.service  #查看某个服务是否为active
 
 
九、target介绍
在Centos7之前的版本,使用运行级别代表特定的操作模式。运行级别被定义为七个级别,用数字0到6表示,每个级别可以启动特定的一些服务。Centos7使用target替换运行级别。
 
systemctl list-unit-files --type=target    #列出系统中所有的target
 
systemctl list-dependencies multi-user.target  #查看指定target下面有哪些unit
 
systemctl list-units --type=target --all    #查看所有的target
 
systemctl get-default           #查看系统默认的target
 
systemctl set-default multi-user.target  #设置默认的target
第章、日常运维(下)
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.28拓展
 
 
10.19 iptables规则备份和恢复
service iptables save    #将规则保存到 /etc/sysconfig/iptables
[root@xinlinux-02 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
 
iptables-save > my.ipt         #把iptables规则备份到my.ipt文件
iptables-restore  <   my.ipt    #恢复杠备份的文件
[root@xinlinux-02 ~]# iptables-save > my.ipt
[root@xinlinux-02 ~]# iptables -F
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# iptables-restore  <  my.ipt
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@xinlinux-02 ~]# systemctl stop iptables
[root@xinlinux-02 ~]# 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.
[root@xinlinux-02 ~]# systemctl start firewalld
[root@xinlinux-02 ~]#
 
firewalld默认有9个zone
#默认zone为public
firewall-cmd --get-zones           #查看所有zone
[root@xinlinux-02 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
 
firewall-cmd --get-default-zone        #查看 默认zone
[root@xinlinux-02 ~]# 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-d 时,后面的可以Tab键补全到 --set-default-zone=
1、 设定默认zone
firewall-cmd --set-default-zone=work    #设定默认zone为work
[root@xinlinux-02 ~]# firewall-cmd --set-default-zone=work
success
 
2、查指定网卡的zone
firewall-cmd --get-zone-of-interface=ens33 (网卡名)      #查看ens33的zone    
[root@xinlinux-02 ~]# firewall-cmd --get-zone-of-interface=ens33
work
 
3、指定网卡设置zone
firewall-cmd --zone=public  --add-interface=ens33     #  给ens33设置zone为public
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# firewall-cmd --zone=dmz --change-interface=lo
success
 
5、针对网卡删除zone
firewall-cmd --zone=dmz  --remove-interface=lo      #删除lo网卡为dmz的zone
##ps:将网卡的zone删除后,该网卡的zone会变为默认的zone
[root@xinlinux-02 ~]# firewall-cmd --zone=dmz --change-interface=lo
success
[root@xinlinux-02 ~]# firewall-cmd --zone=dmz  --remove-interface=lo
success
[root@xinlinux-02 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
 
6、查看系统所有网卡所在的zone
firewall-cmd --get-active-zones  
[root@xinlinux-02 ~]# 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 --l ist-service
[root@xinlinux-02 ~]# firewall-cmd --list-service
ssh dhcpv6-client
[root@xinlinux-02 ~]# firewall-cmd --list-services
ssh dhcpv6-client
 
3、在zone内增加服务
firewall-cmd --zone=public --add-service=http   #把http服务增加到public zone下面
[root@xinlinux-02 ~]# firewall-cmd --zone=public --add-service=http
success
 
firewall-cmd --zone=public --remove-service=http   #删除在public zone下的http服务
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@xinlinux-02 ~]# 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下的服务
[root@xinlinux-02 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@xinlinux-02 ~]# vi /etc/firewalld/services/ftp.xml
[root@xinlinux-02 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@xinlinux-02 ~]# vi /etc/firewalld/zones/work.xml
[root@xinlinux-02 ~]# firewall-cmd --reload
success
[root@xinlinux-02 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp
 
 
10.23 linux任务计划cron
格式: 分 时 日 月 周 user command(用户命令)( 需要用绝对路径
#分范围0-59,时范围0-23,日范围1-31,月范围1-12(或者英文简写),周1-7
 
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进程,有进程说明启动了
[root@xinlinux-02 ~]# systemctl start crond.service
[root@xinlinux-02 ~]# 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表示服务停止
[root@xinlinux-02 ~]# 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 -e          #进入crontab配置文件
 
crontab -l           #查看配置文件内容
 
crontab -r           #删除配置文件内容
 
crontab -u  用户  #指定配置文件中的用户
 
备份crontab配置文件/var/spool/cron/username
#crontab配置文件名是以用户名命名,放在 /var/spool/cron/目录下
 
 
10.24 chkconfig工具
管理服务工具chkconfig,CentOS6及以前使用的,现在CentOS7用的是systemd
##chkconfig工具管理的运行脚本在/etc/init.d/目录下
chkconfig --list          #列出所有使用chkconfig的服务
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# chkconfig  network off
[root@xinlinux-02 ~]# 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
[root@xinlinux-02 ~]# chkconfig  network on
[root@xinlinux-02 ~]# 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管理服务
systemctl list-unit-files    # 列出所有使用systemd的服务 
[root@xinlinux-02 ~]# systemctl list-unit-files
UNIT FILE                                     STATE
proc-sys-fs-binfmt_misc.automount             static
dev-hugepages.mount                           static
dev-mqueue.mount                              static
proc-sys-fs-binfmt_misc.mount                 static
sys-fs-fuse-connections.mount                 static
sys-kernel-config.mount                       static
sys-kernel-debug.mount                        static
tmp.mount                                     disabled
brandbot.path                                 disabled
systemd-ask-password-console.path             static
systemd-ask-password-plymouth.path            static
systemd-ask-password-wall.path                static
session-5.scope                               static
session-6.scope                               static
session-9.scope                               static
arp-ethers.service                            disabled
auditd.service                                enabled
 
systemctl list-units --all --type=service      #只列出systemd下的service服务
[root@xinlinux-02 ~]# systemctl list-units --all --type=service
  UNIT                      LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service            loaded    active   running Security Auditing Se
  chronyd.service           loaded    active   running NTP client/server
  cpupower.service          loaded    inactive dead    Configure CPU power
  crond.service             loaded    active   running Command Scheduler
  dbus.service              loaded    active   running D-Bus System Message
● display-manager.service   not-found inactive dead    display-manager.serv
  dm-event.service          loaded    inactive dead    Device-mapper event
 
systemctl enable crond.service      #让服务开机启动
systemctl disable crond                 #禁止开机启动
systemctl status crond                  # 查看状态
systemctl stop crond                    #停止服务
systemctl start crond                   #启动服务
systemctl restart crond               #重启服务
systemctl is-enabled crond       #检查服务是否开机启动
 
ps:其实enable的时候系统会在 /usr/lib/systemd/system 下创建crond的软链接文件,一旦disable时,文件就会消失
 
 
10.26 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
 
 
10.28扩展
提供一个iptables系列文章的博客  https:#www.zsythink.net/archives/tag/iptables/page/2/
systemd自定义启动脚本  http:#www.jb51.net/article/100457.htm

猜你喜欢

转载自www.cnblogs.com/Lucky-LGX/p/9559193.html
今日推荐