linux日常运维3

10.17 iptables规则备份与恢复

  • service iptables save 
    将规则保存至/etc/sysconfig/iptables中

  • 将规则保存至另外的一份文件中

iptables-save > iptables.bak 
  • 恢复规则
 iptables-restore < iptables.bak

10.18 firewalld

  • 开启firewalld

[root@localhost ~]# systemctl disable iptiables      禁止iptables自启
[root@localhost ~]# systemctl stop iptables           停用iptables服务
[root@localhost ~]# systemctl enable firewalld      允许firewalld自启
[root@localhost ~]# systemctl start firewalld          开启firewalld服务

[root@localhost ~]# systemctl status firewalld       查看firewalld服务状态

  • firewalld默认有9个zone,默认zone为public
  • 查看所有zone
[root@localhost ~]# firewall-cmd --get-zones 
work drop internal external trusted home dmz public block
  • 查看默认zone
[root@localhost ~]# firewall-cmd --get-default-zone
public
  • firewalld9个zone
drop(丢弃):任何接收的网络数据包都被丢弃,没有任何回复,仅能有发送出去的网络连接。

block(限制):任何接收的网络连接都被IPv4的icmp-host-prohibited信息和IPv6的icmp6-adm-prohibited信息所拒绝。

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

external(外部):特别是为路由器启用了伪装功能的外部网,您不能信任来自网络的其他计算机,不能想他他们不会对你的计算机造成危害,只能接收经过选择的连接。

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

work(工作):用于工作区,你可以基本相信网络内的其他电脑不会危害你的电脑,仅仅接收经过选择的连接。

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

internal(内部):用于内部网络,你可以基本相信网络内的其他计算机不会危害你的计算机,仅仅接收经过选择的连接。

trusted(信任):可接受所有的网络连接。

10.19 firewalld关于zone的操作

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

10.20 firewalld关于service的操作

  • 查看所有的services
firewall-cmd --get-services
  • 查看当前zone下有哪些service 
firewall-cmd –list-services
  • 查看指定zone下有哪些service 
firewall-cmd –zone=public –list-service
  • 把http增加到public zone下面
firewall-cmd --zone=public --add-service=http
  • 把http从public zone删除
firewall-cmd --zone=public --remove-service=http
  • zone的配置文件模板
[root@localhost ~]# ls /usr/lib/firewalld/zones/ 
block.xml  drop.xml      home.xml      public.xml   work.xml
dmz.xml    external.xml  internal.xml  trusted.xml
  • 将更改的services写入相应的配置文件,永久生效。之后会在/etc/firewalld/zones目录下面生成配置文件
[root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanent 
success
  • ftp服务自定义端口1121,需要在work zone下面放行ftp
 cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
 vi /etc/firewalld/services/ftp.xml //把21改为1121
 cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
 vi /etc/firewalld/zones/work.xml //增加一行
 <service name="ftp"/>
 firewall-cmd --reload //重新加载
 firewall-cmd --zone=work --list-services

10.21 Linux任务计划cron

  • 查看corn用法
[root@localhost ~]# cat /etc/crontab 
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
  • crontab命令
-u:指定某个用户,不加-u 选项则为当前用户 
-e:制定计划任务 
-l:列出计划任务 或者查看/var/spool/cron/root
-r:删除计划任务 

[root@localhost ~]# crontab -e

格式:分,时,日,月,周,命令行
01 10 05 06 3 /bin/bash /usr/local/sbin/123.sh >> /root/1.log 2>> /root/2.log
  • 启动crond服务
[root@localhost ~]# systemctl start crond
[root@localhost ~]# systemctl status crond
  • 备份crontab 

复制/var/spool/cron/root到另外路径即可

  • TIPS 

在脚本中使用命令绝对路径或者将命令声明变量 
每一条任务计划使用追加重定向或者追加错误重定向


10.22 系统服务管理chkconfig

  • chkconfig
[root@localhost ~]# ls /etc/init.d/
functions  netconsole  network  README

--list              查看sysv服务
--level             指定级别
--add               添加启动服务
--del               删除启动服务
  • 指定级别开启或关闭
[root@localhost ~]# chkconfig --level 3 network  off
  • 自定义启动脚本
[root@localhost ~]# mv 123 /etc/init.d/
[root@localhost init.d]# chkconfig --add 123

自定义脚本格式必须要有以下部分:
#! /bin/bash
# chkconfig: 2345 10 90
# description: 描述内容

10.23 systemd管理服务

centos7 管理服务

  • 查看系统相关服务
[root@localhost ~]# systemctl list-units --all --type=service

去掉--all,只会列出active状态的service
  • 几个常见的服务相关命令
 systemctl enable crond.service     让服务开机启动
 systemctl disable crond            不让开机启动
 systemctl status crond             查看状态
 systemctl stop crond               停止服务
 systemctl start crond              启动服务
 systemctl restart crond            重启服务
 systemctl is-enabled crond         检查服务是否开机启动

10.24 unit介绍

  • 查看系统所有unit
[root@localhost ~]# 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
systemct is-active crond.service                       查看某个服务是否为active

10.25 target介绍

系统为了方便管理用target来管理unit,target是unit组合。

  • 列出所有的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

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

  • 查看某个服务属于哪个target
# cat /usr/lib/systemd/system/sshd.service 

看[install]部分

课堂笔记:

# service iptables save 

iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] 

//etc/sysconfig/iptables为iptables的配置文件

  • linux任务计划cron

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

# systemctl status crond.service 
//如果没有启动,则通过systemctl start crond.service 命令启动

当使用者执行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的模式是:分 时 日 月 周 命令

分范围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小时

添加计划任务: crontab -e
# crontab -e
//使用方式类似vim
//格式: 分 时 日 月 周 命令
//命令最好使用绝对路径
//每天凌晨三点,执行123.sh脚本文件,正确的和错误的日志都输出到123.log文件中
0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log
//因为是每天三点执行脚本,所以可以写成追加,每天都去记录日志
0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
//若想1-10号,双月去执行该脚本,后面就不在执行了——>只要 被2 整除,就符合条件
0 3 1-10 */2 *  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
//只要周2和周5执行该文件
0 3 1-10 */2 2,5  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

建议:

命令都用绝对路径的形式

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

  • chkconfig工具

显示系统服务列表

# chkconfig --list

关闭指定服务的自动启动
# chkconfig mysql off
# chkconfig --list mysql
指定服务在某一运行级的关闭与开启
//要2级别关闭
# chkconfig mysql off --level 2
# chkconfig --list mysql

0和1和6级别不能设置成开
0级别在关机状态是不可能开启的
1级别是单用户模式,服务是不可能开启的
6级别在重启的时候,是不可能开启的——>重启相当于先关闭在启动(重启的那一刻是先关闭才对)。
  • systemd管理服务

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

systemd 的目标是:

尽可能启动更少的进程

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

显示单元依赖关系
# systemctl list-dependencies 
default.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
显示sockets信息和哪些是活动的
# systemctl list-sockets

查看活动的system任务

# systemctl list-jobs
No jobs running.

查看单元文件及状态

# systemctl list-unit-files

拓展:

一个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

xinetd守护进程 http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html

猜你喜欢

转载自blog.csdn.net/xzl18779631005/article/details/82181609