2018-06-13 (第十五课)

目录

  1. iptables 规则的备份与恢复
    1.1 iptables 规则的保存
    1.2 iptables 规则的备份:iptables-save
    1.3 iptables 规则的还原:iptables-restore
  2. firewalld
    2.1 关闭 iptables,启动 firewalld
    2.2 firewalld 的9个 zone
    2.3 对 zone 的操作
    2.4 对 service 的操作
    2.5 示例
  3. crontab
    3.1服务的操作
    3.2 相关文件
    3.3 选项
    3.4 示例
  4. chkconfig
    4.1 相关文件
    4.2 相关命令
  5. systemd
    5.1 什么是systemd
    5.2 systemd 下的服务管理命令
    5.3 unit
    5.4 target\
  6. 扩展知识

1. iptables 规则的备份与恢复

使用命令在 iptables 中改动了规则后,如果不进行规则的保存,重启服务后修改的规则将失效。如果要永久改变规则,可以手动修改 /etc/sysconfig/iptables 文件或者使用命令将当前的规则保存至该文件中。

1.1 iptables 规则的保存:service iptables save

  • 看一下iptables的当前规则
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 54 packets, 3980 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       1.1.1.1              0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 37 packets, 3756 bytes)
 pkts bytes target     prot opt in     out     source               destination    
  • 发现与 /etc/sysconfig/iptables 中的内容不同
[root@localhost ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
  • 将当前规则保存到 /etc/sysconfig/iptables 中
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

这回再看一下配置文件,已经保存进来了
[root@localhost ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Fri Jun 15 08:40:18 2018
*filter
:INPUT ACCEPT [128:9758]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [94:9932]
-A INPUT -s 1.1.1.1/32 -j DROP
COMMIT
# Completed on Fri Jun 15 08:40:18 2018

1.2 iptables 规则的备份:iptables-save

[root@localhost ~]# iptables-save > /tmp/ipt.txt
[root@localhost ~]# cat /tmp/ipt.txt
# Generated by iptables-save v1.4.21 on Fri Jun 15 08:47:31 2018
*filter
:INPUT ACCEPT [256:19623]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [185:18672]
-A INPUT -s 1.1.1.1/32 -j DROP
COMMIT
# Completed on Fri Jun 15 08:47:31 2018

1.3 iptables 规则的还原:iptables-restore

[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 22 packets, 1660 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 16 packets, 1468 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@localhost ~]# iptables-restore < /tmp/ipt.txt 
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 24 packets, 1688 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       1.1.1.1              0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 15 packets, 1428 bytes)
 pkts bytes target     prot opt in     out     source               destination         

2. firewalld (该章仅做好上课笔记,内容暂时还不太会用到)

2.1 关闭 iptables,启动 firewalld

[root@localhost ~]# systemctl stop iptables
[root@localhost ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@localhost ~]# 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@localhost ~]# systemctl start firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-06-15 08:53:57 CST; 9s ago
     Docs: man:firewalld(1)
 Main PID: 1256 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─1256 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid...

Jun 15 08:53:57 localhost.localdomain systemd[1]: Starting firewalld - dyn...
Jun 15 08:53:57 localhost.localdomain systemd[1]: Started firewalld - dyna...
Hint: Some lines were ellipsized, use -l to show in full.

2.2 firewalld 的9个 zone

firewalld 默认有 9 个 zone(zone是firewalld的单位),其实有点类似防火墙设备中的安全域

默认 zone 为 public

  • drop
    任何接受的网络数据包都被丢弃,没有任何恢复,仅能有发送出去的网络连接(数据包不能进来,但是可以出去)

  • block
    任何接受的网络连接都被IPv4的icmp-host-prohibited信息和IPv6的icmp6-adm-prohibited信息所拒绝。(和drop相比,比较宽松一些,主要是为了针对icmp)

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

  • external
    特别是为路由器启用了伪装功能的外部网,你不能信任来自网络的其他计算,不能相信他们不会对你造成伤害,只能接受经过选择的连接

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

  • work
    用于工作区,你可以基本信任网络内的其他电脑不会对你造成危害,仅仅接收经过选择的连接

  • home
    用于内部网络,你可以基本上信任网络内其他电脑不会对你造成危害,仅仅接收经过选择的连接

  • internal
    用于内部网络,你可以基本上信任网络内其他电脑不会对你造成危害,仅仅接收经过选择的连接

  • trusted
    可接受所有的网络连接

2.3 对 zone 的操作

  • 设定默认 zone
    firewall-cmd --set-default-zone=work
  • 查看指定网卡
    firewall-cmd --get-zone-of-interface=ens33
  • 给指定网卡设置 zone
    firewall-cmd --zone=public --add-interface=lo
  • 针对网卡更改 zone
    firewall-cmd --zone=dmz --change-interface=lo
  • 针对网卡删除 zone
    firewall-cmd --zone=dmz --remove-interface=lo
  • 查看系统所有网卡所在的 zone
    firewall-cmd --get-active-zones

2.4 对 service 的操作

  • 查看所有的 services
    firewall-cmd --get-services

  • 查看当前 zone 先有哪些 service
    firewall-cmd --list-services

  • 把 http 增加到 public zone 下
    firewall-cmd --zone=public --add-service=http

  • 把 http 从 public zone 中删除
    firewall-cmd --zone=public --remove-service=http

  • zone 的配置文件模板
    ls /usr/lib/firewalld/zones/

  • 更改配置文件,之后会在 /etc/firewalld/zones 目录下生成配置文件
    firewall-cmd --zone=public --add-service=http --permanent

2.5 示例

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

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
vi /etc/firewalld/services/ftp.xml
cp /usr/lib/firewalld/zones/work.xml
vi /etc/firewalld/zones/work.xml
<service name="ftp"/>
firewall-cmd --reload
firewall-cmd --zone-work --list-services

3. crontab

总有些任务需要定期执行,并且执行时用户可能并不在操作电脑,此时就要用到 Linux 的定时任务管理器—— crontab

3.1 服务的操作

  • 启用:systemctl enable crond.service
  • 禁用:systemctl disable crond.service
  • 开启:systemctl start crond.service
  • 停止:systemctl stop crond.service
  • 重载配置:systemctl reload crond.service
  • 查看状态:systemctl status crond.service

3.2 相关文件

3.2.1 /etc/crontab:可在该文件中配置 crontab 任务,但权限仅限 root

用户

[root@localhost ~]# vim /etc/crontab 

  1 SHELL=/bin/bash
  2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
  3 MAILTO=root
  4 
  5 # For details see man 4 crontabs
  6 
  7 # Example of job definition:
  8 # .---------------- minute (0 - 59)
  9 # |  .------------- hour (0 - 23)
 10 # |  |  .---------- day of month (1 - 31)
 11 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
 12 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,we    d,thu,fri,sat
 13 # |  |  |  |  |
 14 # *  *  *  *  * user-name  command to be executed
  • SHELL:指定执行任务时使用的 shell 类型
  • PATH:指定可执行文件的目录
  • MAILTO:crontab执行时若发生错误,指定向哪个用户发送信息
  • * * * * *:分(059)、时(023)、日(131)、月(112)、周(0~7,0和7都代表周日
  • user-name:指定以哪个用户的权限执行后续命令
  • command to be executed:真正要执行的任务

ps:crond 中的任务执行时,并不会调用用户自身的环境变量,它有自己的环境变量,当你用到一些需要特殊配置 PATH 变量才能使用的命令时,手工执行脚本往往是正常的,但用 crond 执行的时候就不行了,这时你要么写完整的绝对路径,要么将环境变量添加到 /etc/crontab 中

3.2.2 /var/spool/cron:所有用户的定时任务都放置在该目录下,且每个用户的定时任务配置文件以该用户的用户名命名
[root@localhost ~]# 
[root@localhost ~]# ll /var/spool/cron/
total 8
-rw------- 1 chocolee911 chocolee911 36 Jun 15 09:43 chocolee911
-rw------- 1 root        root        30 Jun 15 09:38 root

3.3 选项

  • -e:编辑当前用户的 crontab 任务
[root@localhost ~]# crontab -e

      1 */1 * * * * root ls /
  • -l:列出当前用户的 crontab 任务
[root@localhost ~]# crontab -l
*/1 * * * * chocolee911 ls /
  • -r:删除当前用户的 crontab 任务
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
  • -u <用户名称>:指定要设定 crontab 任务的用户名称,常配合前三个选项一起使用

上述的“增删查”仅能针对当前的用户,如果想设置其他用户的定时任务,可以使用 -u <username> 进行指定

[root@localhost ~]# crontab -l
no crontab for root
[root@localhost ~]# crontab -u chocolee911 -l
*/2 * * * * chocolee911 /usr/bin ls
*/10 * * * * chocolee911 ifconfig

ps:在制定 cron 任务时,有要求填写用户名(比如说A),但那个用户名(A)仅仅是说明执行任务时是以哪个用户(A)的权限进行执行,但 cron 任务仍然是制定任务的用户(B)的。此时在 /var/spool/cron/A 中是看不到该任务的,同样的,使用 crontab -u A -l 也是看不到这条任务的

3.4 示例

crontab 的重点在于时间的灵活配置上,下面将以一些示例进行说明

30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启apache


4. chkconfig

设定某个服务在系统的哪个 runlevel 中会启用

4.1 相关文件

  • 启动脚本的目录:/etc/init.d/**
    ps:该目录的脚本中必须带有
# chkconfig: <runlevel> <start_seq> <stop_seq>
# description:.......

其中,chekconfig行后面的数字分别代表:启用该服务的runlevel、第几个被启动、第几个被关闭。description行为服务的描述

[root@localhost ~]# vim /etc/init.d/network 

  1 #! /bin/bash
  2 #
  3 # network       Bring up/down networking
  4 #
  5 # chkconfig: 2345 10 90
  6 # description: Activates/Deactivates all network interfaces configured to     \
  7 #              start at boot time.

4.2 相关命令

  • 查看
    chkconfig --listchkconfig
[root@localhost system]# chkconfig

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

  • 设定某个服务在特定的 runlevel 中启用或禁用
    chkconfig --level 3 network on
    chkconfig --level 345 network off
  • 删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据
    chkconfig --del network

  • 增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据
    chkconfig --add network


5. systemd

5.1 什么是systemd

CentOS 7 之前的版本,都是采用 init 进程来启动服务的,但是这种方法有两个缺点

  1. 启动时间长。init 进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  2. 启动脚本复杂。init 进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

systemd 就是为解决这两个问题而诞生的,其设计目标是:“提供更优秀的框架以表示系统服务间的依赖关系,并依此实现系统初始化时服务的并行启动,同时达到降低Shell的系统开销的效果”

在CentOS7 中,systemd 已经取代 init,但为了进行平滑过渡,仍然可以使用 init 类的命令来启动服务

5.2 systemd 下的服务管理命令

  • 启用服务
    systemctl enable <service>
  • 禁用服务
    systemctl disable <service>
  • 开启服务
    systemctl start <service>
  • 关闭服务
    systemctl stop <service>
  • 重启服务
    systemctl restart <service>
  • 服务状态查看
    systemctl status <service>
    systemctl is-enabled <service>
  • 查看 systemd 管理的全部服务
    systemctl list-units --all --type=service # 不加 --all 将仅列出 active 的服务

5.3 unit

Systemd 可以管理所有系统资源,不同的资源统称为 Unit(单位)

看一下当前系统都有哪些 unit

[root@localhost system]# ls
basic.target.wants                           local-fs.target.wants
dbus-org.fedoraproject.FirewallD1.service    multi-user.target.wants
dbus-org.freedesktop.NetworkManager.service  network-online.target.wants
dbus-org.freedesktop.nm-dispatcher.service   sysinit.target.wants
default.target                               system-update.target.wants
default.target.wants                         vmtoolsd.service.requires
getty.target.wants

unit 的分类

  • service:系统服务
  • target:多个 unit 构成的组
  • device:硬件设备
  • mount:文件系统挂载点
  • automount:自动挂载点
  • path:文件或路径
  • scope:不是由 systemd 启动的外部进程
  • slice:进程组
  • snapshot:systemd快照
  • socket:进程间通信的套接字
  • swap:swap 文件
  • timer:定时器

5.4 target

5.4.1 target 的概念

启动计算机的时候,需要启动大量的 unit。如果每一次启动,都要写明本次启动需要哪些 unit,会很麻烦。而 systemd 的解决方案就是 target。

target 其实就是一组 unit,这个组中包含一些相关的 unit。启动某个 target的时候,systemd 就会启动该 target 中所有的 unit。

传统的 init 启动模式里面,有 runLevel 概念,其实跟 target 的作用相似。不同的是,runLevel 是互斥的,不可能多个 runLevel 同时启动,但是多个 target 可以同时启动。

5.4.2 target 与 run-level 的对应关系
target 与 run-level 的对应关系
5.4.3 target 与 run-level 的区别
  • 默认的RunLevel(在/etc/inittab文件设置)现在被默认的Target取代,位置是/etc/systemd/system/default.target,通常符号链接到graphical.target(图形界面)或者multi-user.target(多用户命令行)。

  • 启动脚本的位置,以前是/etc/init.d目录,符号链接到不同的RunLevel目录(比如/etc/rc3.d、/etc/rc5.d等),现在则存放在/lib/systemd/system和/etc/systemd/system目录。

  • 配置文件的位置,以前init进程的配置文件是/etc/inittab,各种服务的配置文件存放在/etc/sysconfig目录。现在的配置文件主要存放在/lib/systemd目录,在/etc/systemd目录里面的修改可以覆盖原始设置

5.4.4 target 的相关命令
  • 查看当前系统的所有 target
    systemctl list-unit-files --type=target

  • 查看一个 Target 包含的所有 Unit
    systemctl list-dependencies multi-user.target

  • 查看启动时的默认 Target
    systemctl get-default

  • 设置启动时的默认Target
    systemctl set-default multi-user.target

  • 关闭前一个Target里面所有不属于后一个Target的进程
    systemctl isolate multi-user.target

6. 扩展

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容