Detailed CentOS 7 service control and optimization of the startup process

CentOS 7 system services control

In the CentOSsystem, various system control script on the default services /usr/lib/systemddirectory. By systemctlmay achieve the specified control command system service tools.

  • systemctlcommand

    • Common formats

    systemctl [控制类型] [服务名称]

    • Control Type

    start(启动):运行指定的系统服务程序,实现服务功能。

    stop(停止):终止指定的系统服务程序,关闭相应的功能。

    restart(重启):先退出,再重新运行指定的系统服务程序。(慎用)

    reload(重载):不退出服务程序,只是刷新配置。

    status(查看状态):查看指定的系统服务的运行状态及相关信息。

[root@localhost ~]# systemctl start httpd     //启动httpd服务
[root@localhost ~]# systemctl status httpd    //查看httpd服务运行状态及相关信息
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2019-08-27 14:18:52 CST; 32s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1896 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─1896 /usr/sbin/httpd -DFOREGROUND
           ├─1899 /usr/sbin/httpd -DFOREGROUND
           ├─1900 /usr/sbin/httpd -DFOREGROUND
           ├─1901 /usr/sbin/httpd -DFOREGROUND
           ├─1903 /usr/sbin/httpd -DFOREGROUND
           └─1905 /usr/sbin/httpd -DFOREGROUND

8月 27 14:18:52 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
8月 27 14:18:52 localhost.localdomain httpd[1896]: AH00558: httpd: Could not ...
8月 27 14:18:52 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl stop httpd      //关闭httpd服务
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)                    //服务处于关闭状态
     Docs: man:httpd(8)
           man:apachectl(8)

8月 27 14:18:52 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
8月 27 14:18:52 localhost.localdomain httpd[1896]: AH00558: httpd: Could not ...
8月 27 14:18:52 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
8月 27 14:20:06 localhost.localdomain systemd[1]: Stopping The Apache HTTP Se...
8月 27 14:20:07 localhost.localdomain systemd[1]: Stopped The Apache HTTP Ser...
Hint: Some lines were ellipsized, use -l to show in full.
  • Note: For servers running in a real production environment, Do not perform stopor restartoperate, so as to avoid interrupting client access, unnecessary loss.

Switching the operating level

Run-level operating system is currently running a functional level. It allows some programs start at one level, and the other level is not the time to start.

LinuxValid login mode of the system has 0~9a total of ten, but follow UNIXup system of 6restrictions species, generally only 1to 6be effective. From 1to 6, they have different functions.

Run Level Systemdoftarget Explanation
0 target Off, when using this level will host closed
1 rescue.target Single-user mode, no password is required to log verification system, used for system maintenance
2 multi-user.target User-defined / domain-specific run level. The default is equivalent to3
3 multi-user.target Full multi-user mode interface characters, most of the server host is running at this level
4 multi-user.target User-defined / domain-specific run level. The default is equivalent to3
5 graphical.target Multi-user mode graphical interface provides a graphical desktop environment
6 reboot.target Reboot, reboot the host will use this level

1, view the current system run level

  • runlevel命令
[root@localhost ~]# runlevel
N 5

2, run-level switching system

When a user needs to switch to another operating system level, it is possible by conventional initprocedural, and just use a number corresponding to run-level (0~6)as a parameter to the command, or use the systemctlcommand target switching.

  • initProgram shift
[root@localhost ~]# init 3

Detailed CentOS 7 service control and optimization of the startup process

  • systemctlTools switch
    • systemctl isolate graphical.targetSwitch to a graphical interface inin 5( )
    • systemctl isolate multi-user.targetSwitch to the character interface inin 3( )

Detailed CentOS 7 service control and optimization of the startup process

Detailed CentOS 7 service control and optimization of the startup process

Detailed CentOS 7 service control and optimization of the startup processDetailed CentOS 7 service control and optimization of the startup process

  • Set boot automatically into the character interface.

    ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

    (Set boot character interface)

    systemctl get-default: View the current boot level

[root@localhost ~]# systemctl get-default     //查看当前启动级别状态
graphical.target                               //图形化界面
[root@localhost ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target                           //设置开启启动为字符界面
[root@localhost ~]# systemctl get-default                    //查看当前启动级别状态
multi-user.target                           //字符化界面

Optimization of the startup process

Linux操作系统中包含了大量的服务程序,这些服务程序在切换运行级别时根据预设的状态进行启动或终止。其中有不少系统服务可能并不是用户需要的,但是默认也运行了。

那么,在Linux操作系统中默认包括哪些系统服务?各自的作用是什么?如何控制开机后自动运行的系统服务,以减少资源占用、提高系统运行效率呢?

1.常见的系统服务

CentOS 系统中,默认安装的系统服务多达100余种,这些系统服务为用户提供了丰富的应用服务。只有了解各个系统服务的用途,才能有选择地进行优化操作,实现按需启用Linux 服务。

了 CentOS 中常见的一些系统服务:

服务名称 用途简介 备注
atd 延期、定时执行任务 建议关闭
bluetooth 发现、认证蓝牙相关设备 建议关闭
crond 按预定周期执行计划任务 建议开启
irqbalance 多核心 CPU 处理器的调度支持 建议开启
kdump 记录内核崩溃时的内存信息 建议关闭
lvm2-monitor LVM 管理及监控 建议开启
netfs 访问共享文件夹等网络文件系统 建议开启
network 配置及使用网卡、网络地址 建议开启
restorecond SELinux 安全机制的文件监控和恢复功能 建议关闭
rhnsd 访问 Red Hat Network,获取通知、提交订阅等 建议关闭
rpcgssd 管理 NFS访问中的客户程序语境 建议关闭
saslauthd 基于文本的身份认证 建议关闭
smartd 监控本地硬盘的状态并发送故障报告 建议开启
smb 文件共享服务 建议关闭
sshd 提供远程登录和管理 Linux主机的功能 建议开启
rsyslog 记录内核、系统的日志消息 建议开启
vsftpd 通过FTP提供文件上传、下载功能 建议关闭
  • 注意:必须强调的是,这些服务到底是选择开启还是关闭,应根据主机的实际功能需求来定,不要生搬硬套。

2.优化开机自动加载的服务

Linux 操作系统在每次开机后会进入默认的systemd运行目标(如字符模式或图形模式), 并运行该目标中默认设为启动的各种系统服务。若要禁止某些系统服务自动运行,可以使用ntsysv或者systemctl工具进行优化。

  • ntsysv工具

    ntsysvTool can be run in a character mode, to provide users with an interactive graphical user interface a copy, specifically for centralized configuration starting state of various systems and services. When the need to set multiple startup status of the service, using the ntsysvtool very convenient.

[root@localhost ~]# ntsysv      // 输入命令进入仿图形交互式操作界面

Detailed CentOS 7 service control and optimization of the startup process

Press operation , arrow keys to select a different system services, press the Space(空格)key services provided default startup state ( [*]indicating that the startup, [ ]disabled). If you want to view a description of the selected information services, press the F1button to get help.

  • systemctltool

    systemctlTools with ntsysvsimilar functionality, but systemctldoes not provide an interactive user interface, which is used to query or set the system service default startup state. When you need to set a default to start a service in the current running state of the target when using the systemctltool more efficient.

    • Command Format

    • systemctl 控制选项 服务名称
    • Common options
      • enable: at startup.
      • disable: turn off automatically.
      • is-enable: Check boot status.
[root@localhost ~]# systemctl enable httpd.service    //设置开机自动启动httpd服务
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl is-enabled httpd.service   //查看httpd开机启动状态
enabled
[root@localhost ~]# systemctl disable httpd.service     //设置开机不启动httpd服务
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@localhost ~]# systemctl is-enabled httpd.service   ////查看httpd开机启动状态
disabled

to add on

Under the current system to execute systemctl list-units --type=servicecommand to see all currently active system service system.

[root@localhost ~]# systemctl list-units --type=service //查看当前系统中所有已激活的系统服务
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
abrt-ccpp.service                  loaded active exited  Install ABRT coredump hook
abrt-oops.service                  loaded active running ABRT kernel log watcher
abrt-xorg.service                  loaded active running ABRT Xorg log watcher
abrtd.service                      loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service            loaded active running Accounts Service
alsa-state.service                 loaded active running Manage Sound Card State (restore and store)
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service
avahi-daemon.service               loaded active running Avahi mDNS/DNS-SD Stack
blk-availability.service           loaded active exited  Availability of block devices
...//省略部分内容...

Thank you for watching, I hope you can help us! ! !

Guess you like

Origin blog.51cto.com/14473285/2433005