2018-06-13(日常运维三)

10.19 iptables规则备份和恢复

service iptables save 将规则保存到默认配置文件/etc/sysconfig/iptables
iptables如果不使用service iptables save命令,则重启之后规则会全部消失。
如果想启动服务就让有规则,就把iptables保存在/etc/sysconfig/iptables
利用iptables-save保存规则到指定路径:iptables-save > /tmp/my.ipt
利用iptables-restore恢复指定路径保存的规则:iptables-restore < /tmp/my.ipt

[root@luo ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
[root@luo ~]# iptables-save > /tmp/my.ipt
[root@luo ~]# ls /tmp/
my.ipt
[root@localhost ~]# ls /tmp
systemd-private-16dd9b4d15f944eea512c061edfabb11-chronyd.service-E303Bu
systemd-private-16dd9b4d15f944eea512c061edfabb11-vgauthd.service-9oSMYF
systemd-private-16dd9b4d15f944eea512c061edfabb11-vmtoolsd.service-7EvtoR
systemd-private-fdeffbbc916c4c189ba146d0f76f7067-chronyd.service-gDPSVB
systemd-private-fdeffbbc916c4c189ba146d0f76f7067-vgauthd.service-mEHtob
systemd-private-fdeffbbc916c4c189ba146d0f76f7067-vmtoolsd.service-M3C6xo
[root@luo ~]# iptables-restore < /tmp/my.ipt

10.20 firewalld的9个zone

打开firewalld,先关闭iptables

systemctl disable iptables
systemctl stop iptables
systemctl enable firewalld
systemctl start firewalld

firewalld默认的9个zone :默认zone为public

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

firewalld有zone和service两个基础概念

10.21 firewalld关于zone的操作

查看所有zone:

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

查看默认zone:

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

firewall-cmd命令zone的操作:

[root@luo ~]# firewall-cmd --set-default-zone=work   设定默认zone为work
success
[root@luo ~]# firewall-cmd --get-zone-of-interface=ens33 查看指定网卡所在的zone
work
[root@luo ~]# firewall-cmd --zone=public --add-interface=lo 给指定网卡设置zone
success
[root@luo ~]# firewall-cmd --zone=dmz --change-interface=lo 针对指定网卡更改zone
success
[root@luo ~]# firewall-cmd --zone=dmz --remove-interface=lo 针对网卡删除zone
success
[root@luo ~]# firewall-cmd --get-active-zones 查看系统所有网卡所在的zone
work
  interfaces: ens33

10.22 firewalld关于service的操作

配置文件模板路径:/usr/lib/firewalld/services/
实际生效配置目录:/etc/firewalld/services/
把系统文件内所有的service列出来

[root@luo ~]# 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 nrpe ntp open*** 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

列出默认zone下的service

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

列出指定的zone下的service :firewall-cmd --zone=(zone名) --list-service

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

把service加入到指定zone下;只是暂时写入内存中;firewall-cmd --zone=(zone名) --add-service=(service名)

[root@luo ~]# firewall-cmd --zone=public --add-service=http  添加http服务到public zone
success
[root@luo ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client http

修改配置文件,添加http服务到public zone,永久的

[root@luo ~]# firewall-cmd --zone=public --add-service=http --permanent
success

10.23 linux任务计划cron

crontab 命令
-u 表示指定某个用户,不加-u则表示当前用户
-e 表示指定任务计划
-l 表示列出任务计划
-r 表示删除任务计划

[root@luo ~]# 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

格式:分 时 日 月 周 命令
分的范围:0-59 (* 表示所有)
时的范围:0-23 (* 表示所有)
日的范围:1-31 (* 表示所有)
月的范围:1-12 (* 表示所有)
周的范围:0-6(Sunday=0 or 7) (* 表示所有)
任务计划不执行,一般有两种情况:1.crond服务没启动,默认是开机启动的。2.PATH没有命令的路径,脚本中没使用绝对路径导致命令找不到
启动crond服务

[root@luo ~]# systemctl start crond                    #启动crond服务
[root@luo ~]# ps aux |grep crond                       #查看有没有crond进程
root        557  0.0  0.1 126280  1608 ?        Ss   19:37   0:00 /usr/sbin/crond -n
root       2429  0.0  0.0 112720   972 pts/2    R+   21:58   0:00 grep --color=auto crond
[root@luo ~]# systemctl status crond                  #查看crond状态
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2018-06-13 10:57:50 CST; 1 day 11h ago
 Main PID: 557 (crond)
   CGroup: /system.slice/crond.service
           └─557 /usr/sbin/crond -n
6月 13 10:57:50 luo systemd[1]: Started Command Scheduler.
6月 13 10:57:50 luo systemd[1]: Starting Command Scheduler...
6月 13 10:57:51 luo crond[557]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 93% if used.)
6月 13 10:57:51 luo crond[557]: (CRON) INFO (running with inotify support)

停止crond服务:

[root@luo ~]# systemctl stop crond.service

10.24 chkconfig工具

查看预设服务:

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

列出服务和级别开启状态:chkconfig --list

[root@luo ~]# 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;7则换成了system
--add  增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
--del  删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。
--level<等级代号>  指定读系统服务要在哪一个执行等级中开启或关毕。
0:关机 1:单用户 2:无网络连接的多用户命令行模式 3:有网络连接的多用户命令行模式(常用级别) 4:系统保留 5:图形化界面 6:重启
更改某级别下的开机状态:

[root@luo ~]# chkconfig --level 3 mysqld off                       #指定mysqld在运行级别3不开机启动
[root@luo ~]# chkconfig --list

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

多个级别:chkconfig --level 345 mysqld off
不加级别表示省略;默认针对级别2、3、4、5、
删除:chkconfig --del 服务名
添加:chkconfig --add 服务名

10.25 systemd管理服务

服务对应的启动脚本路径:/usr/lib/systemd/system/
查看所有unit:ls /usr/lib/systemd/system/

[root@luo ~]# ls /usr/lib/systemd/system/
Display all 257 possibilities? (y or n)
arp-ethers.service                             rhel-configure.service
auditd.service                                 rhel-dmesg.service
[email protected]                                rhel-domainname.service
basic.target                                   rhel-import-state.service
basic.target.wants/                            rhel-loadmodules.service
blk-availability.service                       rhel-readonly.service
bluetooth.target                               rpcbind.target
brandbot.path                                  rsyncd.service
brandbot.service                               [email protected]
chrony-dn***[email protected]                         rsyncd.socket
chrony-dn***[email protected]                           rsyslog.service
chronyd.service                                runlevel0.target
chrony-wait.service                            runlevel1.target
console-getty.service                          runlevel1.target.wants/
console-shell.service                          runlevel2.target
[email protected]                       runlevel2.target.wants/
cpupower.service                               runlevel3.target
crond.service                                  runlevel3.target.wants/
cryptsetup-pre.target                          runlevel4.target
cryptsetup.target                              runlevel4.target.wants/
ctrl-alt-del.target                            runlevel5.target
dbus-org.freedesktop.hostname1.service         runlevel5.target.wants/
dbus-org.freedesktop.import1.service           runlevel6.target
dbus-org.freedesktop.locale1.service           saslauthd.service
dbus-org.freedesktop.login1.service            [email protected]
dbus-org.freedesktop.machine1.service          sendmail.service
dbus-org.freedesktop.timedate1.service         [email protected]
dbus.service                                   shutdown.target
dbus.socket                                    shutdown.target.wants/
dbus.target.wants/                             sigpwr.target
debug-shell.service                            sleep.target
default.target                                 -.slice
default.target.wants/                          slices.target
dev-hugepages.mount                            smartcard.target
dev-mqueue.mount                               sm-client.service
dracut-cmdline.service                         sockets.target
dracut-initqueue.service                       sockets.target.wants/
dracut-mount.service                           sound.target
dracut-pre-mount.service                       sshd-keygen.service
dracut-pre-pivot.service                       sshd.service

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

[root@luo ~]# systemctl list-units --all --type=service
  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 setting
  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
  emergency.service                      loaded    inactive dead    Emergency Shell
● exim.service                           not-found inactive dead    exim.service
  firewalld.service                      loaded    active   running firewalld - dynamic firewall daemon
  [email protected]                     loaded    active   running Getty on tty1
  ip6tables.service                      loaded    inactive dead    IPv6 firewall with ip6tables
● ipset.service                          not-found inactive dead    ipset.service
  iptables.service                       loaded    inactive dead    IPv4 firewall with iptables
  irqbalance.service                     loaded    inactive dead    irqbalance daemon
● kdump.service                          loaded    failed   failed  Crash recovery kernel arming
  kmod-static-nodes.service              loaded    active   exited  Create list of required static devi
● ldap.service                           not-found inactive dead    ldap.service
  microcode.service                      loaded    inactive dead    Load CPU microcode update
  mysqld.service                         loaded    inactive dead    LSB: start and stop MySQL
  network.service                        loaded    active   exited  LSB: Bring up/down networking
  NetworkManager-wait-online.service     loaded    active   exited  Network Manager Wait Online
  NetworkManager.service                 loaded    active   running Network Manager
● nscd.service                           not-found inactive dead    nscd.service
● ntpd.service                           not-found inactive dead    ntpd.service
● ntpdate.service                        not-found inactive dead    ntpdate.service
  plymouth-quit-wait.service             loaded    inactive dead    Wait for Plymouth Boot Screen to Qu
  plymouth-quit.service                  loaded    inactive dead    Terminate Plymouth Boot Screen
  plymouth-read-write.service            loaded    inactive dead    Tell Plymouth To Write Out Runtime 
  plymouth-start.service                 loaded    inactive dead    Show Plymouth Boot Screen
  polkit.service                         loaded    active   running Authorization Manager
  postfix.service                        loaded    inactive dead    Postfix Mail Transport Agent
  rc-local.service                       loaded    inactive dead    /etc/rc.d/rc.local Compatibility
  rescue.service                         loaded    inactive dead    Rescue Shell

常用服务相关命令
systemctl enable crond.service //让某个服务开机启动
systemctl disable crond //不让开机启动
systemctl status crond //查看服务状态
systemctl stop crond //停止服务
systemctl start crond //启动服务
systemctl restart crond //重启服务
systemctl is-enabled crond //检查服务是否开机启动

10.26 unit介绍

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 //列出所有的unit
systemctl list-units --all --state=inactive //列出所有inactive的unit
systemctl list-units --all --type=service //列出状态的service
systemctl is-active crond.service //查看某个unit是否active

[root@luo ~]# systemctl list-units                                              列出正在运行的unit
[root@luo ~]# systemctl list-units --all                                      列出所有的unit
[root@luo ~]# systemctl list-units --all --state=inactive           列出所有inactive的unit
[root@luo ~]# systemctl list-units --all --type=service            列出类型为service的unit
[root@luo ~]# systemctl is-active crond                                 查看某个unit是否active   
active

10.27 target介绍

查看当前系统所有target;(unit组合):systemctl list-unit-files --type=target

[root@luo ~]# systemctl list-unit-files --type=target
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.target              static  
graphical.target          static  
halt.target               disabled
hibernate.target          static  
hybrid-sleep.target       static  
initrd-fs.target          static  
initrd-root-fs.target     static  
initrd-switch-root.target static  
initrd.target             static  
iprutils.target           disabled
kexec.target              disabled
local-fs-pre.target       static  
local-fs.target           static  
machines.target           disabled
multi-user.target         enabled 
network-online.target     static  
network-pre.target        static  
network.target            static  
nss-lookup.target         static  
nss-user-lookup.target    static  
paths.target              static  
poweroff.target           disabled
printer.target            static  
reboot.target             disabled
remote-fs-pre.target      static  
remote-fs.target          enabled 
rescue.target             disabled
rpcbind.target            static  
runlevel0.target          disabled
runlevel1.target          disabled
runlevel2.target          enabled 

查看target包含所有的unit:systemctl list-dependencies multi-user.target

[root@luo ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─mysqld.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─rsyslog.service
● ├─sendmail.service
● ├─sm-client.service
● ├─sshd.service
● ├─sysstat.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
● ├─systemd-readahead-collect.service
● ├─systemd-readahead-replay.service
● ├─systemd-update-utmp-runlevel.service
● ├─systemd-user-sessions.service
● ├─tuned.service
● ├─vmtoolsd.service
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
● │ ├─rhel-autorelabel.service
● │ ├─rhel-configure.service
● │ ├─rhel-dmesg.service
● │ ├─rhel-loadmodules.service
● │ ├─[email protected]
● │ ├─paths.target
● │ ├─slices.target
● │ │ ├─-.slice
● │ │ └─system.slice
● │ ├─sockets.target

查看系统默认的target:systemctl get-default

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

设置默认的target:systemctl set-default multi-user.target

[root@luo ~]# 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.

service、unit、target之间的联系:
一个service属于一种unit;
多个unit组成了一个target;
一个target包含多个service;查看/usr/lib/systemd/system/sshd.service里面[install]部份的内容,定义了该service属于哪一个target。

[root@luo ~]# cat /usr/lib/systemd/system/crond.service 
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

猜你喜欢

转载自blog.51cto.com/13736286/2129531