Linux的运行级别(从0~6总共七个运行级别):
0:关机
1:单用户模式
2:多用户模式(没有NFS)
3:完全的多用户模式
4:系统未使用
5:图形界面模式
6:重启
初始化系统
初始化系统通常由内核引导程序和一系列初始化脚本组成。内核引导程序负责启动内核并加载必要的驱动程序,而初始化系统负责启动和管理各种服务和应用程序。
Linux的三个初始化系统 System V(Sys V)、Upstart、systemd。
最初Linux系统用的都是System V(Sys V),后来出现Upstart,在Ubuntu系统中替代System V(Sys V),目前Linux发行版都用systemd来替代System V(Sys V)和Upstart。
SystemV:是传统的初始化系统,也是最早的Linux初始化系统之一。它是一个基于文本的系统,使用SysVinit或Systemd作为初始化脚本的运行程序。System V主要用于较旧的Linux发行版和某些商业Linux发行版。
Upstart:是一个基于事件的初始化系统,旨在替代System V。它能够在系统启动时自动启动服务,并监视和控制服务的运行状态。Upstart通过事件来触发任务的执行,使得系统更加灵活和可靠。
systemd:是目前最流行的Linux初始化系统之一,被广泛应用于大多数现代Linux发行版中。systemd是一个基于守护进程的系统和服务管理器,提供了强大的功能和灵活性,包括自动启动服务、监控服务状态、管理用户会话等。
系统服务
System V(Sys V)
System V(Sys V)系统用到两个命令管理服务 service和chkconfig,服务脚本在/etc/init.d/目录下
systemd
systemd则使用systemctl命令来管理服务,服务脚本在/usr/lib/systemd/system/目录下,系统执行过程中所产生的服务脚本在/run/systemd/system/目录下,这些脚本的优先级要比/usr/lib/systemd/system/高,管理员根据主机系统的需求所创建的执行脚本在/etc/systemd/system/目录下,执行优先级比/run/systemd/system/高。
/etc/systemd/system/目录下的相关配置,决定系统了会不会执行某些服务,所以该目录下面一般放着一大堆链接文件。而/usr/lib/systemd/system/下,则放着实际执行的systemd启动脚本配置文件。因此如果你想要修改某个服务启动的设置,应该去/usr/lib/systemd/system/下面修改。/etc/systemd/system/仅是链接到正确的执行脚本配置文件而已。所以想要看执行脚本设置,应该就得要到/usr/lib/systemd/system/去查阅。
常用命令对比
命令描述 | System V(Sys V)系统命令 | systemd系统命令 |
---|---|---|
使某服务自动启动 | chkconfig --level 3 服务名 on | systemctl enable 服务名.service |
使某服务不自动启动 | chkconfig --level 3 服务名 off | systemctl disable 服务名.service |
检查服务状态 | service 服务名 status | systemctl status 服务名.service (服务详细信息) systemctl is-active 服务名.service (仅显示是否 Active) |
显示所有已启动的服务 | chkconfig --list | systemctl list-units --type=service |
启动某服务 | service 服务名 start | systemctl start 服务名.service |
停止某服务 | service 服务名 stop | systemctl stop 服务名.service |
重启某服务 | service 服务名 restart | systemctl restart 服务名.service |
systemctl常用命令
查看服务状态
systemctl status 服务名
检查服务的所有配置详细信息
systemctl show 服务名
查看各服务开机自启情况
systemctl list-unit-files --type=service
获取服务的依赖项列表
systemctl list-dependencies 服务名.service
按层次列出控制组
systemd-cgls
根据CPU,内存,输入和输出列出控制组
systemd-cgtop
分析每个进程在引导时花费的时间
systemd-analyze blame
列出所有可用的系统套接字
systemctl list-unit-files --type=socket
套接字的操作
systemctl start cups.socket #启动套接字
systemctl restart cups.socket #重启套接字
systemctl stop cups.socket #停止套接字
systemctl reload cups.socket #重新加载套接字
systemctl status cups.socket #查看套接字状态
systemctl is-active cups.socket
systemctl enable cups.socket
systemctl disable cups.socket
其他命令
systemctl mask 服务名.service #禁用某个服务
systemctl unmask 服务名.service #解除禁用某个服务
systemctl isolate multi-user.target #将目前的操作环境改为纯文本模式,关掉图形界面
systemctl isolate graphical.target #将目前的操作环境改为图形界面
systemctl poweroff # 系统关机
systemctl reboot # 重新开机
systemctl suspend # 进入暂停模式
systemctl hibernate # 进入休眠模式
systemctl rescue # 强制进入救援模式
systemctl emergency # 强制进入紧急救援模式
systemctl命令与service命令的区别在于,systemctl命令支持systemd服务,而service命令只支持sysvinit服务。
在使用服务管理命令时,需要注意以下几点:
确定要管理的服务的名称,例如apache2、mysql等。
确定服务的启动脚本存储的位置,通常是在/etc/init.d目录下。
确定服务的配置文件存储的位置,通常是在/etc目录下。
在修改服务的配置文件时,建议先备份原始配置文件,以防止修改后引起的问题
rsyslog是Linux中一个常用的日志记录服务,它可以记录系统运行时的各种事件。看下他的配置文件在哪,日志文件在哪???
系统服务的配置文件
在Linux中,服务的配置文件通常存储在/etc目录下,不同的服务可能有不同的配置文件。例如,Apache Web服务器的主要配置文件是/etc/apache2/apache2.conf,而MySQL数据库服务器的主要配置文件是/etc/mysql/mysql.conf.d/mysqld.cnf,rsyslog日志记录服务的主要配置文件是/etc/rsyslog.conf,而firewalld防火墙服务的主要配置文件是/etc/firewalld/firewalld.conf。
自定义服务
System V(Sys V)
1、在/etc/init.d目录下创建脚本,脚本中写入如下内容
#!/bin/bash
# chkconfig: 345 99 10
# description: My Custom Service
case "$1" in
start)
/usr/bin/my_service
;;
stop)
killall my_service
;;
restart)
killall my_service
/usr/bin/my_service
;;
status)
ps aux | grep my_service | grep -v grep
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
2、设置启动脚本的权限,使其可以执行 sudo chmod +x /etc/init.d/my_service
3、设置服务自启动 sudo chkconfig --add my_service sudo chkconfig my_service on
systemd
1、在/etc/systemd/system目录下创建houzhuiwei.service的配置文件,里面内容如下
[Unit]
Description=My Custom Service #服务的名称
[Service]
ExecStart=/usr/bin/my_service #服务的启动命令
Restart=always #服务的重启方式常用的值包括always、on-failure、never
User=myuser #服务运行的用户
WorkingDirectory=/home/myuser #服务的工作目录
[Install]
WantedBy=multi-user.target
2、重载systemd配置文件 sudo systemctl daemon-reload
3、启动自定义服务 sudo systemctl start my_service
参考:https://zhuanlan.zhihu.com/p/398053266?utm_id=0
https://zhuanlan.zhihu.com/p/666362979