0418 日常运维3

19.iptables规则备份和恢复

在这里插入图片描述

service iptables save  保存iptables规则
[root@localhost~]#service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

规则会保存到 /etc/sysconfig/iptables中

iptables-save > 文件名       备份iptables规则到指定文件



iptables-restore < 文件名   恢复iptables规则

20.firewalld

在CentOS7之前的CentOS版本的防火墙为netfilter,CentOS7的防火墙为firewalld

切换firewalld步骤:

systemctl stop iptables      关闭iptables服务
systemctl disable iptables   禁止iptables服务开机启动
systemctl enable firewalld   允许firewalld开机启动
systemctl start firewalld    启动firewalld服务

firewalld的9个zone
firewalld默认有9个zone:drop、block、public、external、dmz、work、home、internal、trusted

drop(丢弃)      接收的任何网络数据包都会被丢弃,没有任何回复,仅能有发送出去的网络连接
block(限制)     接收的任何网络数据包都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝
public(公共)    在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收经过选取的连接
external(外部)  特别是为路由器启用了伪装功能的外部网,你不能信任来自网络的其他计算机,不能相信他们不会对你的计算机造成危害,只能接收经过选取的连接
dmz(非军事区)    用于你的非军事区内的电脑,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接收经过选取的连接
work(工作)      用于工作区,你可以基本相信网络内的其他计算机不会危害你的电脑,仅接收经过选取的连接
home(家庭)      用于家庭网络,你可以基本相信网络内的其他计算机不会危害你的电脑,仅接收经过选取的连接
internal(内部)  用于内部网络,你可以基本相信网络内的其他计算机不会危害你的电脑,仅接收经过选取的连接
tristed(信任)   可以接收所有的网络连接

21.firewalld关于zone的操作

在这里插入图片描述firewalld关于zone的操作示例:

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

[root@localhost~]#firewall-cmd --get-zones
block dmz drop external home internal public trusted work

firewall-cmd --get-default-zone可以查看firewalld的默认zone

[root@localhost~]#firewall-cmd --get-default-zone
public

firewall-cmd --set-default-zone=zone名 设置默认的zone

[root@localhost~]#firewall-cmd --set-default-zone=work
Warning: ZONE_ALREADY_SET: work
success
[root@localhost~]#firewall-cmd --get-default-zone
work

firewall-cmd --get-zone-of-interface=网卡名 查看指定网卡的zone

[root@localhost~]#firewall-cmd --get-zone-of-interface=ens33
public

firewall-cmd --zone=zone名 --add-interface=网卡名 给指定网卡设置zone

[root@localhost ~]# firewall-cmd --zone=public --add-interface=ens33:1
success

firewall-cmd --zone=zone名 --change-interface=网卡名 针对网卡更改zone

[root@localhost ~]# firewall-cmd --zone=dmz --change-interface=ens33:1
success

firewall-cmd --zone=zone名 --remove-interface=网卡名 针对网卡删除zone

[root@localhost ~]# firewall-cmd --zone=dmz --remove-interface=ens33:1
success

firewall-cmd --get-active-zones 查看系统所有网卡所在的zone

[root@localhost ~]# firewall-cmd --get-active-zones
public
  interfaces: ens33

22.firewalled关于service的操作

在这里插入图片描述
firewalld关于service的操作示例:

firewall-cmd --get-service 列出当前系统所有的service

[root@localhost ~]# firewall-cmd --get-service
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nfs3 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server

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

[root@localhost ~]# firewall-cmd --list-services
ssh dhcpv6-client

firewall-cmd --zone=zone名 --list-services 查看指定zone下有哪些service

[root@localhost ~]# firewall-cmd --zone=public --list-services
ssh dhcpv6-client

firewall-cmd --zone=zone名 --add-service=服务名 给指定zone添加服务

[root@localhost ~]# firewall-cmd --zone=public --add-service=http
success
[root@localhost ~]# firewall-cmd --zone=public --list-services
ssh dhcpv6-client http

目录 /usr/lib/firewalld/zones/ 下是每个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

/etc/firewalld/zones/ 目录下则是配置文件

firewall-cmd --zone=public --add-service=http –permanent
直接修改了public的配置文件并且永久生效

[root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@localhost ~]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="http"/>
</zone>

23. Linux任务计划Cron

crontab

常用选项:

-u:表示指定某个用户,不加-u选项则为当前用户
-e:表示制定计划任务
-l:表示列出计划任务
-r:表示删除计划任务

扩展:

  • crontab -u、-e、-l、-r

  • 格式:分 时 日 月 周 user command

  • 文件/var/spool/cron/username(使用crontab -e 去编辑,直接vi或vim编辑会出错)

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

  • 格式1-5表示一个范围1到5

  • 格式1,2,3表示1或者2或者3

  • 格式*/2表示被2整除的数字,比如小时,那就是每隔2小时

  • 要保证服务是启动状态 systemctl start crond.service

/etc/crontab 任务计划的配置文件路径

[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   //星期,0或7都表示周日,也可以写成英文的简写
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed    

用星期确定你的唯一性,比如说今年的某月某日和明年的某月眸如的星期数肯定是不同的,这样就可以确定某一天的唯一性

crontab -r 删除所有全部任务计划

示例:

[root@localhost ~]# crontab -e
30 20 14 6 4 echo "lzx" > /root/cron.log
01 20 1  7 0 echo "ok" > /root/cron.1.log

no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -l
30 20 14 6 4 echo "lzx" > /root/cron.log
01 20 1  7 0 echo "ok" > /root/cron.1.log
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root

25.systemd管理服务

chkconfig

CentOS6上的服务管理工具为chkconfig,linux系统所有的预设服务都可以通过查看 /etc/init.d/ 目录得到。

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

CentOS7可以使用chkconfig命令,系统的预设服务都可以这样实现:service 服务名 start/stop/restart 。例如,启动crond除了可以使用命令 service crond start 之外,还可以使用命令 /etc/init.d/crond start 来启动。

chkconfig --list 列出所有的服务即每个级别的开启状态

[root@localhost ~]# 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:关
  • CentOS7之前的版本采用的服务管理为SysV,而CentOS7换成了systemd。
  • 数字0~6为系统启动级别,CentOS7仅保留运行级别0、1、6
  • CentOS中,运行级别0表示关机,1表示重启至单用户模式,2表示无NFS支持的多用户模式,3表示完全多用户模式,4保留给用户自定义,5表示图形登录方式,6为重启

chkconfig --level 级别 服务名 on/off 修改某级别下某服务的开启状态

    [root@localhost ~]# chkconfig --level 3 network off
    [root@localhost ~]# chkconfig --list |grep network
    
    注:该输出结果只显示 SysV 服务,并不包含
    原生 systemd 服务。SysV 配置数据
    可能被原生 systemd 配置覆盖。 
    
          要列出 systemd 服务,请执行 'systemctl list-unit-files'。
          查看在具体 target 启用的服务请执行
          'systemctl list-dependencies [target]'。
    
    network        	0:关	1:关	2:开	3:关	4:开	5:开	6:关



[root@localhost ~]# chkconfig --level 345 network off
[root@localhost ~]# chkconfig --list |grep network

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

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

–level 后面可以指定多个级别

    [root@localhost ~]# chkconfig --list |grep network
    
    注:该输出结果只显示 SysV 服务,并不包含
    原生 systemd 服务。SysV 配置数据
    可能被原生 systemd 配置覆盖。 
    
          要列出 systemd 服务,请执行 'systemctl list-unit-files'。
          查看在具体 target 启用的服务请执行
          'systemctl list-dependencies [target]'。
    
    network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

[root@localhost ~]# chkconfig network off
[root@localhost ~]# chkconfig --list |grep network

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

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

省略级别,默认是针对级别2,3,4,5操作的

[root@localhost ~]# chkconfig --del network
[root@localhost ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
[root@localhost ~]# chkconfig --add network
[root@localhost ~]# 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 --add/del 服务名 用来 增加/删除 系统服务,通常用来把自定义的启动脚本加入到系统服务中

systemd

CentOS7将SysV改为systemd了,因为SysV只能一个一个地启动服务,而systemd支持多个服务并发启动

systemctl list-units --all --type=service 列出所有的服务
服务文件存储路径/usr/lib/systemd/system/

[root@localhost ~]# systemctl list-units --all --type=service |head
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                                        loaded    active   running Security Auditing Service
  brandbot.service                                      loaded    inactive dead    Flexible Branding Service
  chronyd.service                                       loaded    active   running NTP client/server
  cpupower.service                                      loaded    inactive dead    Configure CPU power related settings
  crond.service                                         loaded    active   running Command Scheduler
  dbus.service                                          loaded    active   running D-Bus System Message Bus
  display-manager.service                               not-found inactive dead    display-manager.service
  dracut-shutdown.service                               loaded    inactive dead    Restore /run/initramfs
  ebtables.service                                      loaded    inactive dead    Ethernet Bridge Filtering tables

常用命令(以cron服务为例):

systemctl enable crond.service    让服务开机启动(.service可以省略)
systemctl disable crond           不让开机启动
systemctl status crond            查看状态
systemctl stop crond              停止服务
systemctl start crond             启动服务
systemctl restart crond           重启服务
systemctl is-enabled crond        检查服务是否开机启动

26.unit

[root@localhost ~]# ls /usr/lib/systemd/system/ |head
arp-ethers.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service

service:系统服务
target:多个unit组成的组
device:硬件设备
mount:文件系统挂载点
automount:自动挂载点
path:文件或路径
scope:不是由systemd启动的外部进程
slice:进程组
snapshot:systemd快照
socket:进程间通信套接字
swap:swap文件
timer:定时器
上面每种类型的文件都是一个unit,这些unit组成了系统的各个资源(各个服务、各个设备等)

相关命令:

systemctl list-units   列出正在运行(active)的unit
并会提示,若要列出所有的units,则需要加 --all
systemctl list-units --all   列出所有的(包括失败的或者inactive的)unit
systemctl list-units --all --state=inactive     列出inactive的unit
systemctl list-units --type=service    列出状态为active的service
其中failed是一个特例,也会列出来
systemctl is-active crond.service    查看某个服务是否为active

27.target

target类似于CentOS6的启动级别,但target支持多个target同时启动。target是多个unit的组合,系统使用target来管理unit。

systemctl list-unit-files --type=target 查看当前系统所有的target

[root@localhost ~]# systemctl list-unit-files --type=target |head
UNIT FILE                 STATE   
basic.target              static  
bluetooth.target          static  
cryptsetup-pre.target     static  
cryptsetup.target         static  
ctrl-alt-del.target       disabled
default.target            enabled 
emergency.target          static  
final.target              static  
getty-pre.target          static 

systemctl list-dependencies target名 查看某一target包含的所有unit,以树形方式列出来

[root@localhost ~]# systemctl list-dependencies multi-user.target |head
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service

systemctl get-default 查看系统默认的target,multi-user.target等同于CentOS6的运行级别3

[root@localhost ~]# systemctl get-default
multi-user.target

systemctl set-default target名 设置默认的target

[root@localhost ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

运行级别和target的对比:

级别0:关闭系统,对应 poweroff.target
级别1:单用户模式,对应 rescure.target
级别2:用户自定义级别,通常识别为级别3,对应 muiti-user.target
级别3:多用户模式,无图形,对应 muiti-user.target
级别4:用户自定义级别,通常识别为级别3,对应 muiti-user.target
级别5:多用户模式,有图形,对应 graphical.target
级别6:重启,对应 reboot.target
扩展:

  • 一个service属于一种unit
  • 多个unit组成了target
  • 一个target里面包含了多个service,我们可以查看
    /usr/lib/systemd/system/sshd.service文件里面[install]部分的内容,它定义了该service属于哪一个target

直播笔记

1.firewalld 防火墙
iptabales -A INPUT -p tcp --sport 80 -j ACCEPT
firewall-cmd --add-port=80/tcp
firewall-cmd --permanent --add-port=80/tcp; firewall-cmd --reload
firewall-cmd --permanent --add-service=http; firewall-cmd --reload

2.firewall-cmd 如何添加自定义规则
https://blog.51cto.com/jevic/1785162
3.
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值