Linux学习笔记_8-系统服务

Ginc
Ginc
管理员
66
文章
0
粉丝
教程干货评论104阅读模式
前言 linux系统中,有一些特殊程序,启动后会在后台持续运行,常驻在内存中等待用户或者其他软件调用,这种程序我们称为服务。

1、介绍

系统服务就是为用户或程序提供服务,可以提供一些系统或网络功能。

例如:apache的web服务,ftp提供的下载服务,ssh提供的远程连接服务,防火墙提供的安全防护服务。文章源自今夕何夕兮-https://www.ginc.site/share1296.html

2、管理工具

1、systemV与init

1、介绍文章源自今夕何夕兮-https://www.ginc.site/share1296.html

systemV中有一个叫init的程序,可以让系统去调用/etc/init.d目录下的脚本,我们可以通过命令去控制服务的启动与关闭。init会按照优先级的高低,先后唤醒其他服务。文章源自今夕何夕兮-https://www.ginc.site/share1296.html

2、运行级别文章源自今夕何夕兮-https://www.ginc.site/share1296.html

init会根据用户指定的运行级别来启动不同服务,在Linux系统中包含了7个运行级别:文章源自今夕何夕兮-https://www.ginc.site/share1296.html

0  ---  关机     1  ---  单用户   2  ---  无网络的多用户  3  ---  字符模式  4  ---  保留  5  ---  图形模式  6  ---  重启文章源自今夕何夕兮-https://www.ginc.site/share1296.html

3、分类文章源自今夕何夕兮-https://www.ginc.site/share1296.html

按照功能分为文章源自今夕何夕兮-https://www.ginc.site/share1296.html

系统服务:服务对象是Linux系统本身,或Linux系统的用户文章源自今夕何夕兮-https://www.ginc.site/share1296.html

网络服务:服务对象是网络中的其它客户端文章源自今夕何夕兮-https://www.ginc.site/share1296.html

按照启动方式分为

独立服务:这类服务启动后除非关闭系统或管理员手动关闭,否则会一直在系统内存中运行,响应速度快,但占用内存多。

临时服务:服务本身独立存在,管理一些服务,用户可以选择何时启动,临时服务响应速度慢,占用内存少。

2、systemd与unit

从redhat7开始init服务被效率更高的systemd取代。

1、优势

服务并行启动,响应速度更快,会自动解决服务的依赖关系,类似yum,向下兼容init服务脚本。

2、管理命令:

命令:

systemctl

命令语法:

systemctl [command] [unit]
 
command选项:
             start  立刻启动unit
	     stop   立刻关闭unit
	     restart  立刻关闭后再启动unit 
	     reload   不关闭unit的情况下,重新载入配置文件,让设置生效
	     enable  设置下次开机时 unit启动
	     disable  设置下次开机时unit不启动
	     status  查看unit状态,会列出是否执行,开机是否启动等信息
	     is-active  目前没有运行
	     is-enabled  开机时默认不启动
	     kill   关闭进程
	     show  列出unit配置
	     mask  注销nuit 注销后无法启动   
	     unmask  取消注销

例:

[root@Ginc ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)  #是否开机启动
   Active: active (running) since Wed 2025-01-15 07:06:26 EST; 5 days ago  #状态
  Process: 150388 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 150387 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 150385 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 150390 (nginx)   #进程PID
    Tasks: 3 (limit: 11252)
   Memory: 5.1M
   CGroup: /system.slice/nginx.service
           ├─150390 nginx: master process /usr/sbin/nginx
           ├─150391 nginx: worker process
           └─150392 nginx: worker process

1月 15 07:06:26 Ginc systemd[1]: Starting The nginx HTTP and reverse proxy server...
1月 15 07:06:26 Ginc nginx[150387]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
1月 15 07:06:26 Ginc nginx[150387]: nginx: configuration file /etc/nginx/nginx.conf test is successful
1月 15 07:06:26 Ginc systemd[1]: Started The nginx HTTP and reverse proxy server.

3、查看服务

1、查看系统中所有服务的状态

命令:

systemctl list-unit-files

例:

[root@Ginc ~]# systemctl list-unit-files
UNIT FILE                                  STATE    
proc-sys-fs-binfmt_misc.automount          static   
-.mount                                    generated
boot.mount                                 generated
dev-hugepages.mount                        static   
dev-mqueue.mount                           static   
proc-fs-nfsd.mount                         static   
proc-sys-fs-binfmt_misc.mount              static   
run-vmblock\x2dfuse.mount                  disabled 
chrony-dnssrv@.timer        disabled 
dnf-makecache.timer        enabled 
---中间省略---
fstrim.timer           disabled 
fwupd-refresh.timer             disabled 
mdadm-last-resort@.timer             static 
mdcheck_continue.timer                    disabled 
mdcheck_start.timer                 disabled 
mdmonitor-oneshot.timer               disabled 
mlocate-updatedb.timer              enabled 
podman-auto-update.timer          disabled 
systemd-tmpfiles-clean.timer            static 
unbound-anchor.timer               enabled 
---其余省略---

2、查看指定类型服务

命令:

systemctl --type target --all

例:

[root@Ginc ~]# systemctl --type target --all
  UNIT                      LOAD      ACTIVE   SUB    DESCRIPTION                  
  basic.target              loaded    active   active Basic System                 
  cryptsetup.target         loaded    active   active Local Encrypted Volumes      
  emergency.target          loaded    inactive dead   Emergency Mode               
  getty-pre.target          loaded    inactive dead   Login Prompts (Pre)          
  getty.target              loaded    active   active Login Prompts                
  graphical.target          loaded    active   active Graphical Interface          
  initrd-fs.target          loaded    inactive dead   Initrd File Systems          
  initrd-root-device.target loaded    inactive dead   Initrd Root Device           
  initrd-root-fs.target     loaded    inactive dead   Initrd Root File System      
  initrd-switch-root.target loaded    inactive dead   Switch Root                  
  initrd.target             loaded    inactive dead   Initrd Default Target        
  local-fs-pre.target       loaded    active   active Local File Systems (Pre)     
  local-fs.target           loaded    active   active Local File Systems           
  multi-user.target         loaded    active   active Multi-User System            
  network-online.target     loaded    active   active Network is Online    
---省略---

3、查看服务依赖关系

命令:

systemctl list-dependencies multi-user.target

例:

[root@Ginc ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─atd.service
● ├─auditd.service
● ├─avahi-daemon.service
● ├─chronyd.service
● ├─crond.service
● ├─cups.path
● ├─cups.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─ksm.service
● ├─ksmtuned.service
● ├─libstoragemgmt.service
● ├─libvirtd.service
● ├─mcelog.service
● ├─mdmonitor.service
● ├─ModemManager.service、
---省略----

 

 

转载请注明来自:今夕何夕兮
本站原创内容版权遵循 CC-BY-NC-SA4.0 协议授权,本站部分资源收集于网络,只做学习和交流使用,版权归原作者所有。

weinxin
提示:
若文章中图片、链接等信息出错,请及时反馈,博主将在第一时间更新。谢谢大家!
 
Ginc
  • 本文由 Ginc 发表于2025年1月21日
  • 转载请保留本文链接:Linux学习笔记_8-系统服务:https://www.ginc.site/share1296.html
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证