Linux日常运维(三)

iptables规则备份和恢复

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

将规则保存到 /etc/sysconfig/iptables
- 备份iptables规则:iptables-save > 文件名

[root@localhost~]#iptables-save > zx.ipt
[root@localhost~]#ls
Desktop    Downloads  Music     Public  scripts  Templates  zx.ipt
Documents  htop       Pictures  rsync   sed      Videos
[root@localhost~]#cat zx.ipt
# Generated by iptables-save v1.4.21 on Thu Jun 14 13:54:30 2018
*nat
:PREROUTING ACCEPT [10:1511]
:INPUT ACCEPT [7:807]
:OUTPUT ACCEPT [11:855]
:POSTROUTING ACCEPT [11:855]
COMMIT
# Completed on Thu Jun 14 13:54:30 2018
# Generated by iptables-save v1.4.21 on Thu Jun 14 13:54:30 2018
*filter
:INPUT ACCEPT [244:22087]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [175:19538]
COMMIT
# Completed on Thu Jun 14 13:54:30 2018

将当前规则备份到zx.ipt中
- 恢复iptables规则:iptables-restore < 文件名

[root@localhost~]#iptables-restore < zx.ipt

将zx.ipt中的规则恢复为当前规则


10.5 firewalld

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

firewalld的9个zone

要使用firewalld,需要我们关闭iptables服务,开启firewalld服务。

# systemctl stop iptables   //关闭iptables服务
# systemctl disable iptables   //禁止iptables服务开机启动
# systemctl enable firewalld   //让firewalld开机启动
# systemctl start firewalld   //启动firewalld服务

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(信任):可以接收所有的网络连接
[root@localhost~]#firewall-cmd --get-zones
block dmz drop external home internal public trusted work

命令firewall-cmd –get-zones 可以查看所有的zone

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

命令firewall-cmd –get-default-zone可以查看firewalld的默认zone,firewalld的默认zone是public

firewalld关于zone的操作

关于firewalld有两个基础的概念,分别是zone和service,每一个zone里面有不同的iptables规则。

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

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

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

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

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

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

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

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

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

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

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

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

firewalld关于service的操作

之所以有9个zone,是因为每一个zone里面都使用了不同的service,而service是针对一个服务(端口)做的iptables规则。
service都是由一个个配置文件定义的,配置文件的模板在 /usr/libfirewalld/services/ 目录下,真正生效的配置则是在 /etc/firewalld/services 目录下面。

[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 –get-service 列出当前系统所有的service

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

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

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

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

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

命令 firewall-cmd –zone=zone名 –add-service=服务名 给指定zone添加服务 (该方式只在内存中生效,没有修改配置文件,重启消失)

每个zone都有自己的配置文件,可以查看目录 /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/ 目录下面


[root@localhost ~]# firewall-cmd --zone=public --list-services
ssh dhcpv6-client http
[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"/>
</zone>

上面我们可以看到,之前添加的http服务并没有在配置文件中看到,所以重启后public这个zone中就会没有http服务

给指定zone添加服务还可以直接修改该zone的配置文件,永久生效。修改配置文件,需要加一个选项:–permanent

[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>

上面我们可以看到,命令 firewall-cmd –zone=public –add-service=http 后面加 –permanent 选项,直接修改了public的配置文件,在配置文件中我们可以看到 字样,说明重启后public中也会有http服务


Linux任务计划Cron

crontab

linux任务计划功能的操作是通过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

查看任务计划的配置文件

[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     //用户,不写用户就是root 最后一列,是你要执行的命令

为什么没有年份呢?
用星期确定你的唯一性,比如说今年的6月14号和明年的6月14号的星期肯定是不同的,这样就可以确定某一天的唯一性

另外,删除计划任务尽量少用 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

删除计划任务,可以使用 crontab -e 进入crontab进行编辑


Linux服务管理

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 来启动。

[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 –list 列出所有的服务即每个级别的开启状态

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

命令 chkconfig –level 级别 服务名 on/off 修改某级别下某服务的开启状态,–level 指定级别 后面跟服务名,然后是on或off

[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支持多个服务并发启动。

[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

命令 systemctl list-units –all –type=service 列出所有的服务

常用命令(以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 //检查服务是否开机启动

使用命令 systemctl list-units –all –type=service 可以列出所有的服务,这些服务对应的的启动脚本文件位于 /usr/lib/systemd/system/ 目录下

[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

target

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

[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-unit-files –type=target 查看当前系统所有的target

[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 list-dependencies target名 查看某一target包含的所有unit,以树形方式列出来

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

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

[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.

命令 systemctl set-default target名 设置默认的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

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

更多资料参考:
iptables详解
anacron功能
systemd自定义启动脚本

猜你喜欢

转载自blog.csdn.net/miss1181248983/article/details/80699162