- 了解服务
- 启动和关闭服务
- 设置服务开机自动启动
刚装好Windows系统时,需要进行一些优化

右击某个服务,可以看到一些选项,包括启动、停止、重新启动等。这些选项管理的是这
个服务的当前状态。
在Windows中管理一个服务,有以下两种管理方式。
(1)管理服务当前状态。
(2)管理服务开机是否自动启动。
在RHEL8中,通过输入systemctl list-unit-file命令可以列出系统中所有的服务,其中后
缀为service的那些服务类似于Windows中的服务。查看后缀为service的服务可以使用
systemctl list-unit-files --type service命令。
[root@op ~]# systemctl list-unit-files --type service
UNIT FILE STATE
accounts-daemon.service enabled
alsa-restore.service static
alsa-state.service static
anaconda-direct.service static
anaconda-fips.service static
anaconda-nm-config.service static
最后按【q】退出
一般情况下,我们启动、停止、重新启动服务,指的就是这些后缀为service的服务,后
缀.service一般可以不用写。
7.1 管理服务当前状态
查看sshd这个服务是否允许,命令如下
[root@op ~]# systemctl is-active sshd
active
[root@op ~]#
关闭sshd,命令如下。
[root@op ~]# systemctl stop sshd
开启sshd,命令如下
systemctl start sshd
重启sshd,命令如下
systemctl restart sshd
查看sshd运行状态,命令如下
[root@op ~]# systemctl status sshd
7.2 管理服务开机是否自动启动
查看某服务开机是否自动启动,可以使用“svstemctl is-enabled 服务名”命令来判断,
结果如果是enabled则开机会自动运行,不管当前是否启动,系统启动时此服冬会白动启
动,如下所示。
[root@op ~]# systemctl is-enabled sshd
这里显示结果为enabled,说明sshd 服务开机时会自动启动。如果不希望开机自动启动,
则使用“systemctl disable服务名”即可,如下所示。
[root@op ~]# systemctl disable sshd
现在显示为disabled,说明sshd 服务开机时不会自动启动,即使sshd现在是运行的,但
是重启系统之后sshd也是不会自动运行的,只有手动start之后才能运行。
enable和disable操作影响的是开机是否会自动启动,并不影响当前状态。如果希望设置
开机自动启动,同时设置现在也启动起来,那么加上--now选项
[root@op ~]# systemctl enable sshd --now
[root@op ~]#
7.3 判断服务名是什么
很多时候我们安装了某个软件之后,想知道服务名是什么,可以通过“systemctl list
unit-files--type service | grep关键字”来判断。
[root@op ~]# systemctl list-unit-files --type service | grep ssh
anaconda-sshd.service static
sshd-keygen@.service disabled
sshd.service enabled
sshd@.service static
sssd-ssh.service indirect
[root@op ~]#