ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux服务管理

2022-08-01 18:35:47  阅读:201  来源: 互联网

标签:systemd off target service 管理 list systemctl linux 服务


linux服务管理

目录

简介

初始化系统

下面是 Linux 系统中最常用的三个初始化系统:

  • System V (Sys V)
  • Upstart
  • systemd

System V (Sys V)

System V(Sys V)是类Unix系统第一个也是传统的初始化系统。init是内核引导系统启动过程中第一支启动的程序,它是所有程序的父进程。

大部分Linux 发行版最开始使用的是叫作 System V(Sys V)的传统的初始化系统。在过去的几年中,已经发布了好几个初始化系统以解决标准版本中的设计限制,例如:launchdService Management FacilitysystemdUpstart

但是systemd 已经被几个主要的 Linux发行版所采用,以取代传统的 SysV初始化系统。

Upstart

Upstart 是一个基于事件的/sbin/init守护进程的替代品,它在系统启动过程中处理任务和服务的启动,在系统运行期间监视它们,在系统关机的时候关闭它们。

它最初是为 Ubuntu而设计,但是它也能够完美的部署在其他所有Linux系统中,用来代替古老的 System-V

Upstart被用于 Ubuntu 9.10Ubuntu 14.10 和基于 RHEL 6的系统,之后它被systemd取代

systemd

systemd是一个新的初始化系统和系统管理器,它被用于所有主要的 Linux发行版,以取代传统的SysV初始化系统。

systemd兼容SysVLSB初始化脚本。它可以直接替代SysV 初始化系统。systemd 是被内核启动的第一个程序,它的PID1

systemd是所有程序的父进程,Fedora 15 是第一个用systemd 取代upstart的发行版。systemctl 用于命令行,它是管理 systemd的守护进程/服务的主要工具,例如:(开启、重启、关闭、启用、禁用、重载和状态)

systemd使用 .service 文件而不是bash 脚本(SysVinit 使用的)。systemd 将所有守护进程添加到 cgroups中排序,你可以通过浏览/cgroup/systemd文件查看系统等级。

service

service命令,顾名思义,就是用于管理Linux操作系统中服务的命令。

这个命令不是在所有的linux发行版本中都有。主要是在redhatfedoramandrivacentos中。

查看所有服务当前的运行状态
service --status-all

查看指定服务(vsftpd)的运行状态
service vsftpd status

停止指定服务(vsftpd)
service vsftpd stop

重启网络服务
service network restart

chkconfig

chkconfig实用程序是一个命令行工具,允许你在指定运行级别下启动所选服务,以及列出所有可用服务及其当前设置。

此外,它还允许我们从启动中启用或禁用服务。前提是你有超级管理员权限(root 或者sudo)运行这个命令。

所有的服务脚本位于/etc/init.d文件中

[root@fyh gits]# ls /etc/init.d/
functions  netconsole  network  README
  • chkconfig --list //列出所有的服务
[root@fyh ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
  • chkconfig network off //将network服务关闭

    [root@fyh ~]# chkconfig network off
    [root@fyh ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    network        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    
  • chkconfig network on //将network服务开启

    chkconfig [服务] on //on 默认开启是就2345

    [root@fyh ~]# chkconfig network on
    [root@fyh ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    

在系统中有七个级别等级列表:

  • 等级0表示:表示关机
  • 等级1表示:单用户模式
  • 等级2表示:多用户模式,少nfs服务
  • 等级3表示:多用户模式,不带图形
  • 等级4表示:是一种保留的级别
  • 等级5表示:带图形界面的多用户模式
  • 等级6表示:重新启动

如果想要指定关闭或者开启某一个模式

  • chkconfig --level 3 network off //指定network中的3级别关闭

    [root@fyh ~]# chkconfig --level 3 network off 
    [root@fyh ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
    network        	0:off	1:off	2:on	3:off	4:on	5:on	6:off
    
    

将脚本加入到服务列表中

  1. 首先将启动脚本放入到/etc/init.d 这个目录下——>只有在这个目录下,才可以添加到服务列表中去
  2. 文件名称无所谓,但内容有格式要求
    • 首先是是一个shell脚本
    • 然后chkconfig指定运行级别启动顺序,第10位启动,第90位关闭
    • 下面代码为它的固定格式,必须要有的!!!
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.

举个例子

  • chkconfig --del network //删除服务列表中的脚本
  • chkconfig --add network //增加服务列表中的脚本
[root@fyh ~]# cd /etc/init.d/
[root@fyh init.d]# ls
functions  netconsole  network  README
[root@fyh init.d]# cp network test
[root@fyh init.d]# ls
functions  netconsole  network  README  test
[root@fyh init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:off	4:on	5:on	6:off
[root@fyh init.d]# chkconfig --add test
[root@fyh init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:off	4:on	5:on	6:off
test           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@fyh init.d]# chkconfig --del test
[root@fyh init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:off	4:on	5:on	6:off

systemctl

systemd核心概念unit(单元)类型:unit表示不同类型的systemd对象,通过配置文件进行标识和配置; 文件中主要包含了系统服务监听socket保存的系统快照以及其它与init相关的信息

下面为unit类型 :

  • service:文件扩展名为.service, 用于定义系统服务

  • target:文件扩展名为.target,用于模拟实现运行级别

  • device :用于定义内核识别的设备

  • mount:定义文件系统挂载点

  • socket:用于标识进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动

  • snapshot :管理系统快照

  • swap:用于标识swap设备

  • automount :文件系统的自动挂载点

  • path:用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务

可以使用-t加上类型去查看,以service为例

[root@fyh init.d]# systemctl -t service
  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service                     loaded active running Security Auditing Service
  crond.service                      loaded active running Command Scheduler
  dbus.service                       loaded active running D-Bus System Message Bus
  firewalld.service                  loaded active running firewalld - dynamic firewall daemon
  getty@tty1.service                 loaded active running Getty on tty1
  irqbalance.service                 loaded active running irqbalance daemon
● kdump.service                      loaded failed failed  Crash recovery kernel arming
  kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for th
  mysqld.service                     loaded active running MySQL Server

systemd的配置文件目录

  • /usr/lib/systemd/system/:每个服务最主要的启动脚本的配置放在这,有点类似以前的/etc/init.d
[root@fyh init.d]# ls /usr/lib/systemd/system/
arp-ethers.service                      rescue.target.wants
auditd.service                          rhel-autorelabel-mark.service
autovt@.service                         rhel-autorelabel.service
basic.target                            rhel-configure.service
basic.target.wants                      rhel-dmesg.service
blk-availability.service                rhel-domainname.service
bluetooth.target                        rhel-import-state.service
brandbot.path                           rhel-loadmodules.service
brandbot.service                        rhel-readonly.service
console-getty.service                   rpcbind.target
  • /run/systemd/system/:系统执行过程中所产生的服务脚本所在目录,这些脚本的优先级要比/usr/lib/systemd/system/高;
[root@fyh init.d]# ls /run/systemd/system/
session-1.scope  session-1.scope.d  user-0.slice  user-0.slice.d
  • /etc/systemd/system/:管理员根据主机系统的需求所创建的执行脚本所在目录,执行优先级比/run/systemd/system/高;
[root@fyh init.d]# ls /etc/systemd/system/
basic.target.wants                          default.target.wants     network-online.target.wants
dbus-org.fedoraproject.FirewallD1.service   getty.target.wants       sysinit.target.wants
dbus-org.freedesktop.nm-dispatcher.service  local-fs.target.wants    system-update.target.wants
default.target                              multi-user.target.wants

从上面的功能及优先级次序,我们可以知道,/etc/systemd/system/目录下的相关配置,决定系统了会不会执行某些服务,所以该目录下面一般放着一大堆链接文件。而/usr/lib/systemd/system/下,则放着实际执行的systemd启动脚本配置文件。因此如果你想要修改某个服务启动的设置,应该去/usr/lib/systemd/system/下面修改。/etc/systemd/system/仅是链接到正确的执行脚本配置文件而已。所以想要看执行脚本设置,应该就得要到/usr/lib/systemd/system/去查阅。

常用命令

任务 旧指令 新指令
开启自启动 chkconfig --level 3 httpd on systemctl enable httpd.service
关闭自启动 chkconfig --level 3 httpd off systemctl disable httpd.service
查看服务状态 service httpd status systemctl status httpd.service
查看所有已启动服务 chkconfig --list systemctl list-units --type=service
启动服务 service httpd start systemctl start httpd.service
停止服务 service httpd stop systemctl stop httpd.service
重启服务 service httpd restart systemctl restart httpd.service

查看服务状态

[root@fyh init.d]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)  # 启动状态
   Active: inactive (dead)  # 运行状态

# 相关日志
Oct 20 12:19:59 fyh systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' ...vice'
Hint: Some lines were ellipsized, use -l to show in full.

运行状态:

  • active(running):正有一只或多只程序正在系统中执行的意思;
  • active(exited):仅执行一次就正常结束的服务,目前并没有任何程序在系统中执行;
  • active(waiting):正在执行当中,不过还需要等待其他的事件才能继续处理;
  • inactive:这个服务目前没有运行;
  • dead:程序已经清除;

启动状态:

  • enabled:这个daemon将在开机时被执行;
  • disabled:这个daemon在开机时不会被执行;
  • static:这个daemon不可以自己启动(enable不可),不过可能会被其他的enabled 的服务来唤醒(关联属性的服务);
  • mask:这个daemon无论如何都无法被启动,因为已经被强制注销(非删除),可通过systemctlunmask方式改回原本状态;

其他命令

systemctl mask httpd.service   #禁用某个服务
systemctl unmask httpd.service   #解除禁用某个服务

systemctl isolate multi-user.target  #将目前的操作环境改为纯文本模式,关掉图形界面
systemctl isolate graphical.target  #将目前的操作环境改为图形界面

systemctl poweroff # 系统关机
systemctl reboot  # 重新开机
systemctl suspend  # 进入暂停模式
systemctl hibernate # 进入休眠模式
systemctl rescue  # 强制进入救援模式
systemctl emergency # 强制进入紧急救援模式

标签:systemd,off,target,service,管理,list,systemctl,linux,服务
来源: https://www.cnblogs.com/bkhb/p/16541406.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有