目录

Linux防火墙-netfilter

iptables规则备份和恢复

Linux防火墙-firewalled

firewalld的9个zone

firewalld关于zone的操作

firewalld关于service的操作

Linux任务计划cron

Linux系统服务管理-chkonfig工具

systemd管理服务

unit介绍

target介绍


Linux防火墙-netfilter

iptables规则备份和恢复

service iptables save ##会把规则保存到/etc/sysconfig/iptables配置文件中

把iptables规则备份到自定义my.ipt文件中:命令iptables-save > my.ipt

[root@zyshanlinux-001 ~]# iptables -t nat -A PREROUTING -d 192.168.43.32 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
[root@zyshanlinux-001 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.43.32
[root@zyshanlinux-001 ~]# iptables-save > /tmp/myipt.txt  ##把前面2条nat规则保存到自定义文件
[root@zyshanlinux-001 ~]# cat !$  ##查看保存规则的内容
cat /tmp/myipt.txt
# Generated by iptables-save v1.4.21 on Tue Jun 12 22:40:22 2018
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -d 192.168.43.32/32 -p tcp -m tcp --dport 1122 -j DNAT --to-destination 192.168.100.100:22
-A POSTROUTING -s 192.168.100.100/32 -j SNAT --to-source 192.168.43.32
COMMIT
# Completed on Tue Jun 12 22:40:22 2018

恢复刚才备份的规则:命令iptables-restore < my.ipt

[root@zyshanlinux-001 ~]# iptables -nvL  ##filter表是空的
Chain INPUT (policy ACCEPT 0 packets, 0 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 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination
[root@zyshanlinux-001 ~]# iptables -t nat -F  ##清空nat表
[root@zyshanlinux-001 ~]# iptables -t nat -nvL  ##确认nat表是空的
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination
​
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination
​
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination
​
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination
[root@zyshanlinux-001 ~]# iptables-restore < /tmp/myipt.txt  ##从自定义文件备份规则中恢复规则
[root@zyshanlinux-001 ~]# iptables -t nat -nvL  ##确实是把nat表的规则恢复
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.43.32        tcp dpt:1122 to:192.168.100.100:22
​
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination
​
Chain OUTPUT (policy ACCEPT 1 packets, 76 bytes)pkts bytes target     prot opt in     out     source               destination
​
Chain POSTROUTING (policy ACCEPT 1 packets, 76 bytes)pkts bytes target     prot opt in     out     source               destination         0     0 SNAT       all  --  *      *       192.168.100.100      0.0.0.0/0            to:192.168.43.32

Linux防火墙-firewalled

firewalld的9个zone

打开firewalld,需要把前面开启的netfilter先关闭,再启动firewalled.

[root@zyshanlinux-001 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@zyshanlinux-001 ~]# systemctl stop iptables
​
[root@zyshanlinux-001 ~]# 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@zyshanlinux-001 ~]# systemctl start firewalld
[root@zyshanlinux-001 ~]# 

firewalld默认有9个zone,zone是firewalld的单位,每个zone相当于一个规则集合。

[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

默认zone为public

[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-default-zone
public

每个zone的含义

firewalld关于zone的操作

1、更改默认zone 命令firewall-cmd --set-default-zone=work

[root@zyshanlinux-001 ~]#  firewall-cmd --set-default-zone=work
success
[root@zyshanlinux-001 ~]#  firewall-cmd --get-default-zone
work

2、查看指定网卡用的是什么zone 命令firewall-cmd --get-zone-of-interface=lo

[root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens33
work
[root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens37
work
[root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
[root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens33:0
no zone

3、给指定网卡设置zone 命令firewall-cmd --zone=public --add-interface=lo

##新增网卡没有zone##
[root@zyshanlinux-001 ~]# firewall-cmd --get-zone-of-interface=ens37
no zone
##到路径/etc/sysconfig/network-scripts/去##
[root@zyshanlinux-001 ~]# cd /etc/sysconfig/network-scripts/
[root@zyshanlinux-001 network-scripts]# ls
ifcfg-ens33    ifdown-ippp    ifdown-sit       ifup-bnep  ifup-plusb   ifup-TeamPort
ifcfg-ens33:0  ifdown-ipv6    ifdown-Team      ifup-eth   ifup-post    ifup-tunnel
ifcfg-lo       ifdown-isdn    ifdown-TeamPort  ifup-ippp  ifup-ppp     ifup-wireless
ifdown         ifdown-post    ifdown-tunnel    ifup-ipv6  ifup-routes  init.ipv6-global
ifdown-bnep    ifdown-ppp     ifup             ifup-isdn  ifup-sit     network-functions
ifdown-eth     ifdown-routes  ifup-aliases     ifup-plip  ifup-Team    network-functions-ipv6
##复制ifcfg-ens33文件,改为ifcfg-ens37,修改名字、IP等参数##
[root@zyshanlinux-001 network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@zyshanlinux-001 network-scripts]# ls
ifcfg-ens33    ifdown-ippp    ifdown-Team      ifup-ippp   ifup-routes       network-functions
ifcfg-ens33:0  ifdown-ipv6    ifdown-TeamPort  ifup-ipv6   ifup-sit          network-functions-ipv6
ifcfg-ens37    ifdown-isdn    ifdown-tunnel    ifup-isdn   ifup-Team
ifcfg-lo       ifdown-post    ifup             ifup-plip   ifup-TeamPort
ifdown         ifdown-ppp     ifup-aliases     ifup-plusb  ifup-tunnel
ifdown-bnep    ifdown-routes  ifup-bnep        ifup-post   ifup-wireless
ifdown-eth     ifdown-sit     ifup-eth         ifup-ppp    init.ipv6-global
[root@zyshanlinux-001 network-scripts]# vim ifcfg-ens37
##再重启firewalld服务##
[root@zyshanlinux-001 network-scripts]# systemctl restart firewalld
##仍然没有zone##
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
no zone
##用命令赋予zone##
[root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=dmz --add-interface=ens37
success
##zone设置完成##
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
dmz
##给网卡lo设置zone##
[root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=public --add-interface=lo
success
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=lo
public

4、针对某个网卡更改zone 命令firewall-cmd --zone=dmz --change-interface=lo

[root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=drop --change-interface=lo
success
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=lo
drop

5、针对某个网卡删除zone 命令firewall-cmd --zone=dmz --remove-interface=lo

[root@zyshanlinux-001 network-scripts]# firewall-cmd --zone=drop  --remove-interface=lo
success
##如果该网卡有默认的zone,就恢复默认zone;没就会删除zone
[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone

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

[root@zyshanlinux-001 network-scripts]# firewall-cmd --get-active-zones
dmzinterfaces: ens33:0
workinterfaces: ens33
publicinterfaces: ens37 lo

firewalld关于service的操作

service是zone下的一个子单元。

1、查看所有的services 命令firewall-cmd --get-services(s可加可不加)

[root@zyshanlinux-001 ~]# firewall-cmd --get-services
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 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
[root@zyshanlinux-001 ~]# 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 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

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

[root@zyshanlinux-001 ~]# firewall-cmd --get-default-zone
work
[root@zyshanlinux-001 ~]# firewall-cmd --list-services
ssh dhcpv6-client
[root@zyshanlinux-001 ~]# firewall-cmd --list-service
ssh dhcpv6-client

3、指定某个zone下拥有的services 命令firewall-cmd --zone=public --list-services

[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --list-services
ssh dhcpv6-client
[root@zyshanlinux-001 ~]# firewall-cmd --zone=block --list-services
​
[root@zyshanlinux-001 ~]# firewall-cmd --zone=trusted --list-services
​
[root@zyshanlinux-001 ~]# 

4、把http和ftp添加到public zone下面

命令firewall-cmd --zone=public --add-service=http

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

5、把http和ft从public zone下面移除

命令firewall-cmd --zone=public --remove-service=http

[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --remove-service=http
success
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --remove-service=ftp
success
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client

6、前面2步都是在内存上操作,想把新的service永久加载到zone下,需要更改配置文件

命令firewall-cmd --zone=public --add-service=http --permanent

之后会在/etc/firewalld/zones目录下面生成配置文件

zone的配置文件模板在这个路径下/usr/lib/firewalld/zones/

service的配置文件模板在这个路径下/usr/lib/firewalld/services/

[root@zyshanlinux-001 ~]# ls /usr/lib/firewalld/zones/  ##zone的配置文件模板##
block.xml  dmz.xml  drop.xml  external.xml  home.xml  internal.xml  public.xml  trusted.xml  work.xml
[root@zyshanlinux-001 ~]# ls /etc/firewalld/zones
public.xml  public.xml.old
##添加"http"到public zone配置文件下##
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@zyshanlinux-001 ~]# ls /etc/firewalld/zones
public.xml  public.xml.old
[root@zyshanlinux-001 ~]# 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"/>  ##添加的"http"##
</zone>
##添加"ftp"到public zone配置文件下##
[root@zyshanlinux-001 ~]# firewall-cmd --zone=public --add-service=ftp --permanent
success
[root@zyshanlinux-001 ~]# 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"/><service name="ftp"/>  ##添加的"ftp"##
</zone>

7、需求:ftp服务自定义端口1121,需要在work zone下面放行ftp

[root@zyshanlinux-001 ~]# ls /etc/firewalld/zones/
public.xml  public.xml.old
[root@zyshanlinux-001 ~]# ls /etc/firewalld/services/
##把services中的ftp.xml模板拷贝到配置文件的services中##
[root@zyshanlinux-001 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@zyshanlinux-001 ~]# ls /etc/firewalld/services/
ftp.xml
[root@zyshanlinux-001 ~]# vi /etc/firewalld/services/ftp.xml
​
<?xml version="1.0" encoding="utf-8"?>
<service><short>FTP</short><description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description><port protocol="tcp" port="1121"/>  ##把21端口改为1121端口##<module name="nf_conntrack_ftp"/>
</service>
​
##把zones中的work.xml模板拷贝到配置文件的zones中##
[root@zyshanlinux-001 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@zyshanlinux-001 ~]# ls /etc/firewalld/zones/
public.xml  public.xml.old  work.xml
[root@zyshanlinux-001 ~]# vim /etc/firewalld/zones/work.xml
​
<?xml version="1.0" encoding="utf-8"?>
<zone><short>Work</short><description>For use in work areas. You mostly 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="ftp"/>  ##添加一行,把"ftp"加进去##
</zone>

重新加载firewall服务

[root@zyshanlinux-001 ~]# firewall-cmd --reload
success
[root@zyshanlinux-001 ~]# firewall-cmd --zone=work --list-services  ##确认生效##
ssh dhcpv6-client ftp

Linux任务计划cron

需求:在凌晨备份数据,启动服务,操作过程可以时shell脚本或一个单独的命令

任务计划的配置文件:

[root@zyshanlinux-001 ~]# 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
​
[root@zyshanlinux-001 ~]# 

格式:分 时 日 月 周 user command

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

在配置文件中可以把计划的输出结果输出到指定文件,方便追溯。

[root@zyshanlinux-001 ~]# crontab -e
no crontab for root - using an empty one
​
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

为什么没有年?它用周来区别唯一性,每年的某一天日期所在的周都是不同的。

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

可用格式1,2,3表示1或者2或者3

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

如何定义后面的命令呢,与vi同样操作,*代表所有(每天),用命令Crontab  –u指定用户、 -e编辑、 -l列出、 -r删除

[root@zyshanlinux-001 ~]# crontab -l
no crontab for root
[root@zyshanlinux-001 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@zyshanlinux-001 ~]# crontab -l
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
[root@zyshanlinux-001 ~]# crontab -r
[root@zyshanlinux-001 ~]# crontab -l
no crontab for root
[root@zyshanlinux-001 ~]# crontab -u root -l
no crontab for root

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

有cron这个进程,就证明上面的命令成功启动了服务.

查看该服务的启动与停止状态

查看cron计划里的命令,命令保存在/var/spool/cron/root路径下,以用户的名字命名.

可能出现问题:计划没执行,原因可能是没用绝对命令而是用了命令,该命令并没有在PATH环境变量里面。养成写脚本时用绝对路径命令的习惯。计划命令写正确错误输出方便后期追溯。

Linux系统服务管理-chkonfig工具

该工具是在centos6中使用的,在centos7中已经不用了,但向之前版本兼容,所以还能用,过度作用。只剩2个服务了netconsole,network,这2服务在/etc/init.d/服务脚本下.

chkconfig --list

[root@zyshanlinux-001 ~]# 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:关
[root@zyshanlinux-001 ~]# ls /etc/init.d/
functions  netconsole  network  README

0关机,1单用户,2多用户但少了nfs服务(网络文件系统),3多用户少了图型,4保留的级别暂时没用,5带图形多用户,6级别重启.

仅关闭级别3 命令chkconfig –level 3 network off

[root@zyshanlinux-001 ~]# chkconfig --level 3 network off
[root@zyshanlinux-001 ~]# 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:关

把级别345都关闭 命令chkconfig –level 345 network off

[root@zyshanlinux-001 ~]# chkconfig --level 345 network off
[root@zyshanlinux-001 ~]# 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:关

把级别345都开启 命令chkconfig –level 345 network on

[root@zyshanlinux-001 ~]# chkconfig --level 345 network on
[root@zyshanlinux-001 ~]# 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 networkd

[root@zyshanlinux-001 ~]# ls /etc/init.d/
functions  netconsole  network  README
##到脚本目录下##
[root@zyshanlinux-001 ~]# cd !$
cd /etc/init.d/
[root@zyshanlinux-001 init.d]# ls
functions  netconsole  network  README
##拷贝服务文件##
[root@zyshanlinux-001 init.d]# cp network 123
[root@zyshanlinux-001 init.d]# ls -l
总用量 48
-rwxr-xr-x  1 root root  7293 6月  14 23:12 123
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--  1 root root  1160 4月  11 15:36 README
##拷贝的服务文件没在chkconfig里面##
[root@zyshanlinux-001 init.d]# 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:关
##必须用下面这个命令才能把123服务添加到chkconfig##
[root@zyshanlinux-001 init.d]# chkconfig --add 123
[root@zyshanlinux-001 init.d]# chkconfig --list
​
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
​要列出 systemd 服务,请执行 'systemctl list-unit-files'。查看在具体 target 启用的服务请执行'systemctl list-dependencies [target]'。
​
123             0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole      0:关 1:关 2:关 3:关 4:关 5:关 6:关
network         0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@zyshanlinux-001 init.d]# ls
123  functions  netconsole  network  README

vim 123 该服务文件

其中最重要的规则和描述

# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.

把服务从chkconfig中删除 命令chkconfig –del networkd

[root@zyshanlinux-001 init.d]# chkconfig --del 123
[root@zyshanlinux-001 init.d]# 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:关

systemd管理服务

查看所有的unit和service,相对来说是第2条命令比较有序。

systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。

[root@zyshanlinux-001 init.d]# systemctl list-unit-files
[root@zyshanlinux-001 init.d]# systemctl list-units --all --type=service

几个常用的服务相关的命令

  • systemctl enable crond.service //让服务开机启动
  • systemctl disable crond //不让开机启动
  • systemctl status crond //查看状态
  • systemctl stop crond //停止服务
  • systemctl start crond //启动服务
  • systemctl restart crond //重启服务
  • systemctl is-enabled crond //检查服务是否开机启动
  • systemctl list-unit-files|grep enabled //查看已启动的服务列表

  • systemctl --failed //查看启动失败的服务列表

systemd有系统和用户区分;系统(/user/lib/systemd/system/)、用户(/etc/lib/systemd/user/).

一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target

配置文件内容,实际上是一个软连接

[root@zyshanlinux-001 ~]# systemctl disable crond
[root@zyshanlinux-001 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@zyshanlinux-001 ~]# cat /etc/systemd/system/multi-user.target.wants/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[root@zyshanlinux-001 ~]# ls -l !$
ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 6月  14 23:43 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

启动就创建一个软连接,关闭就是把软连接删除.

[root@zyshanlinux-001 ~]# ls -l /usr/lib/systemd/system/crond.service
-rw-r--r--. 1 root root 284 8月   3 2017 /usr/lib/systemd/system/crond.service
[root@zyshanlinux-001 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@zyshanlinux-001 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
ls: 无法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录

unit介绍

ls /usr/lib/systemd/system //系统所有unit,分为以下类型

  • service 系统服务
  • target 多个unit组成的组
  • device 硬件设备
  • mount 文件系统挂载点
  • automount 自动挂载点
  • path 文件或路径
  • scope 不是由systemd启动的外部进程
  • slice 进程组
  • snapshot systemd快照
  • socket 进程间通信套接字
  • swap swap文件
  • timer 定时器
[root@zyshanlinux-001 ~]# cd /usr/lib/systemd/system
[root@zyshanlinux-001 system]# ls -l runlevel*
lrwxrwxrwx  1 root root 15 6月  13 18:53 runlevel0.target -> poweroff.target
lrwxrwxrwx  1 root root 13 6月  13 18:53 runlevel1.target -> rescue.target
lrwxrwxrwx  1 root root 17 6月  13 18:53 runlevel2.target -> multi-user.target
lrwxrwxrwx  1 root root 17 6月  13 18:53 runlevel3.target -> multi-user.target
lrwxrwxrwx  1 root root 17 6月  13 18:53 runlevel4.target -> multi-user.target
lrwxrwxrwx  1 root root 16 6月  13 18:53 runlevel5.target -> graphical.target
lrwxrwxrwx  1 root root 13 6月  13 18:53 runlevel6.target -> reboot.target

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
  • systemctl is-active crond.service //查看某个服务是否为active
[root@zyshanlinux-001 ~]# systemctl is-active crond.service
active
[root@zyshanlinux-001 ~]# systemctl is-enabled crond.service
disabled
[root@zyshanlinux-001 ~]# systemctl is-active crond.service
active

target介绍

系统为了方便管理用target来管理unit

systemctl list-unit-files --type=target  // 列出所有的target

systemctl list-dependencies multi-user.target  // 查看指定target下面有哪些unit

[root@zyshanlinux-001 ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.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

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

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

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

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

一个service属于一种类型的unit

多个unit组成了一个target

一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service //看[install]部分

[root@zyshanlinux-001 ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s[Install]
WantedBy=multi-user.target  ##这里看sshd.service是属于multi-user.target##

扩展

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

Linux日常运维管理技巧(三)iptables规则备份和恢复、firewalld的9个zone、任务计划cron、chkconfig系统服务管理、添加服务命令、systemctl管理服务相关推荐

  1. Linux日常运维管理技巧

    Linux日常运维管理技巧 监控系统状态 1.w/uptime查看系统状态, 执行这个命令可得知目前登入系统的用户有那些人,以及他们正在执行的程序,以及当前负载的情况. 2.cat /proc/cpu ...

  2. Linux日常运维管理技巧(二)Linux防火墙:你可以ping别人,别人ping不了你、转发、代理

    目录 Linux防火墙 netfilter iptables filter表小案例 nat表应用 Linux防火墙 selinux临时关闭 setenforce 0 [root@zyshanlinux ...

  3. Linux日常运维管理技巧(二)

    Linux防火墙 selinux临时关闭 setenforce 0 [root@zyshanlinux-01 ~]# getenforce ##防火墙状态开启 Enforcing [root@zysh ...

  4. Linux日常运维管理技巧(一)监控系统状态、监控网卡流量、监控IO性能、查看系统进程、查看网络状态、Linux下抓包tcpdump、Linux网络相关、DNS配置

    目录 监控系统状态 监控网卡流量 监控IO性能 查看系统进程 查看网络状态 Linux下抓包 Linux网络相关 监控系统状态 w/uptime查看系统负载 [root@zyshanlinux-01 ...

  5. Linux日常运维管理技巧(四)文件同步工具-rsync、Linux系统日志、dmesg命令、lastb命令查看登录失败的用户、screen工具虚拟屏幕

    目录 Linux文件同步工具-rsync Linux系统日志 dmesg命令 lastb命令 screen工具 Linux文件同步工具-rsync rsync命令是一个远程数据同步工具,可通过LAN/ ...

  6. iptables规则备份和恢复 firewalld服务

    2019独角兽企业重金招聘Python工程师标准>>> 10月29日任务 10.19 iptables规则备份和恢复 10.20 firewalld的9个zone 10.21 fir ...

  7. 日常运维管理技巧十五(htop使用说明top增强版)(转载)

    htop 官网:http://htop.sourceforge.net/ Linux top命令的用法详细详解:https://www.cnblogs.com/zhoug2020/p/6336453. ...

  8. 日常运维管理技巧十七(Nethogs进程流量监控工具)(转载)

    有很多适用于Linux系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况.netstat用来查看接口统计报告,还有top监控系统当前运行进程.但是如果你想要找一个能够按进程实时统 ...

  9. 日常运维管理技巧十六(iftop网卡流量监控工具)(转载)

    iftop是类似于top的实时流量监控工具. 官方网站:http://www.ex-parrot.com/~pdw/iftop/ 1.iftop的作用 iftop可以用来监控网卡的实时流量(可以指定网 ...

最新文章

  1. java 反射调用静态方法
  2. 宏基因组扩增子最新分析流程QIIME2:官方中文帮助文档
  3. 逻辑电路 - 与非门Nand Gate
  4. 我们网管不能自己贬低自己
  5. Dools的DMN运行时示例
  6. JS手动实现一个new操作符
  7. (转)Quartz任务调度(1)概念例析快速入门
  8. 关于datetimepicker和vue v-model指令双向数据绑定失败的问题
  9. 简单编译器的实现 (四)
  10. 计算机攻击方式有哪些,常用的dos攻击方式是什么
  11. vue公式编辑器 加强版(简化版)
  12. 正面管教读书笔记 10 你的性格对孩子性格的影响
  13. Modelsim搭建具有各组件的UVM验证平台
  14. 仿bilibili微信小程序3
  15. arm64 ext指令图解
  16. VS code:代码出现蓝色波浪线,提示显示 xxxx: Unknown word.cSpell
  17. 中考表格计算机函数求和方法,2016信息技术中考电子表格处理模块
  18. java 81 08_java io系列08之 File总结
  19. Linux的hdparm工具参数详解:硬盘检查、测速、设定、优化
  20. 阿朱重新定义SaaS

热门文章

  1. 无监督学习 | PCA 主成分分析之客户分类
  2. 卷积神经网络CNN(8)—— Pix2Pix Application -- Aerialmap Lane Line Detection (Pix2Pix应用:航拍图车道线检测)
  3. nvidia.dali:深度学习加速神器!
  4. Science:把这个人类特有基因转入猴子后,它们的大脑更大更强了,但科学家没敢让它出生...
  5. PS滤镜Nik Collection 2 for mac中文版合集
  6. PS亮度蒙版扩展插件:Lumenzia for Mac 支持ps2021
  7. 适合新手使用的编辑制作管理软件:Substance Alchemist Mac版
  8. Invisor for Mac(媒体文件检查工具)v3.14免激活版
  9. indexof java_java indexOf()简单字符查找实例
  10. linux定时监控端口并重新启动shell脚本命令