RHEL8---服务管理

本章主要介绍如何管理Linux中的服务

  • 了解服务
  • 启动和关闭服务
  • 设置服务开机自动启动

我们平时刚装好Windows系统时,需要进行一些优化,如下图所示。

  右击某个服务,可以看到一些选项,包括启动、停止、重新启动等。这些选项管理的是这 个服务的当前状态。 

  双击服务名,在启动类型中设置的是系统启动时,这个服务要不要跟着一起运行,如下图所示。

  在Windows中管理一个服务,有以下两种管理方式。 

(1)管理服务当前状态。

(2)管理服务开机是否自动启动。

  在RHEL8中,通过输入systemctl list-unit-file命令可以列出系统中所有的服务,其中后缀为service的那些服务类似于Windows中的服务。查看后缀为service的服务可以使用 systemctl list-unit-files --type service命令。

[root@rhel03-23 ~]# 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  
anaconda-noshell.service                   static  
anaconda-pre.service                       static  
anaconda-shell@.service                    static  
anaconda-sshd.service                      static  

最后按【q】退出

一  般情况下,我们启动、停止、重新启动服务,指的就是这些后缀为service的服务,后 缀.service一般可以不用写。 

1、管理服务当前状态

查看sshd这个服务是否允许,命令如下

[root@rhel03-23 ~]# systemctl is-active sshd
active

  只有状态为active才说明此服务是正常运行的,其他状态都表示这个服务没有运行或没有 正常运行。 

关闭sshd,命令如下。

[root@rhel03-23 ~]# systemctl stop sshd
[root@rhel03-23 ~]# systemctl is-active sshd
inactive

开启sshd,命令如下

[root@rhel03-23 ~]# systemctl start sshd
[root@rhel03-23 ~]# systemctl is-active sshd
active

重启sshd,命令如下

[root@rhel03-23 ~]# systemctl restart sshd

查看sshd运行状态,命令如下 

[root@rhel03-23 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2023-12-01 06:06:17 +03; 51s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 2794 (sshd)
    Tasks: 1 (limit: 12128)
   Memory: 1.1M
   CGroup: /system.slice/sshd.service
           └─2794 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@o>

12月 01 06:06:17 rhel03-23.33 systemd[1]: sshd.service: Succeeded.
12月 01 06:06:17 rhel03-23.33 systemd[1]: Stopped OpenSSH server daemon.
12月 01 06:06:17 rhel03-23.33 systemd[1]: Starting OpenSSH server daemon...
12月 01 06:06:17 rhel03-23.33 sshd[2794]: Server listening on 0.0.0.0 port 22.
12月 01 06:06:17 rhel03-23.33 sshd[2794]: Server listening on :: port 22.
12月 01 06:06:17 rhel03-23.33 systemd[1]: Started OpenSSH server daemon.

上面enabled说明此服务开机时自动启动,active表示当前是正在运行的。

最下行并没有显示终端提示符,按【q】键退出。

禁用服务,命令如下。

[root@rhel03-23 ~]# systemctl mask sshd
Created symlink /etc/systemd/system/sshd.service → /dev/null.
[root@rhel03-23 ~]# systemctl stop sshd
[root@rhel03-23 ~]# systemctl start sshd
Failed to start sshd.service: Unit sshd.service is masked.

  将sshd设置为mask,则此服务关闭之后就不能再启动了,这个类似于Windows中把某服 务禁用了。通过systemctl status sshd来查看是否设置为了mask。

[root@rhel03-23 ~]# systemctl status sshd
● sshd.service
   Loaded: masked (Reason: Unit sshd.service is masked.)
   Active: inactive (dead) since Fri 2023-12-01 06:09:52 +03; 40s ago
 Main PID: 2794 (code=exited, status=0/SUCCESS)

12月 01 06:06:17 rhel03-23.33 systemd[1]: sshd.service: Succeeded.
12月 01 06:06:17 rhel03-23.33 systemd[1]: Stopped OpenSSH server daemon.
12月 01 06:06:17 rhel03-23.33 systemd[1]: Starting OpenSSH server daemon...

要取消mask,使用unmask命令即可,如下所示

[root@rhel03-23 ~]# systemctl unmask sshd
Removed /etc/systemd/system/sshd.service.
[root@rhel03-23 ~]# systemctl start sshd

以上这些除mask外的操作只是影响当前状态,并不会影响系统重启后此服务是否会自动启动。

2、管理服务开机是否自动启动 

  查看某服务开机是否自动启动,可以使用“svstemctl is-enabled 服务名”命令来判断, 结果如果是enabled则开机会自动运行,不管当前是否启动,系统启动时此服务会自动启动,如下所示。 

[root@rhel03-23 ~]# systemctl is-enabled sshd
enabled

  这里显示结果为enabled,说明sshd 服务开机时会自动启动。如果不希望开机自动启动, 则使用“systemctl disable服务名”即可,如下所示。 

[root@rhel03-23 ~]# systemctl disable sshd
Removed /etc/systemd/system/multi-user.target.wants/sshd.service.
[root@rhel03-23 ~]# systemctl is-enabled sshd
disabled

  现在显示为disabled,说明sshd 服务开机时不会自动启动,即使sshd现在是运行的,但 是重启系统之后sshd也是不会自动运行的,只有手动start之后才能运行。 

  enable和disable操作影响的是开机是否会自动启动,并不影响当前状态。如果希望设置开机自动启动,同时设置现在也启动起来,那么加上--now选项,如下所示。

[root@rhel03-23 ~]# systemctl stop sshd
[root@rhel03-23 ~]# systemctl enable sshd --now
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.

现在查看sshd的状态,如下所示。 

[root@rhel03-23 ~]# systemctl is-active sshd
active
[root@rhel03-23 ~]# systemctl is-enabled sshd
enabled

3、判断服务名是什么 

  很多时候我们安装了某个软件之后,想知道服务名是什么,可以通过“systemctl list-unit-files --type service | grep关键字”来判断。例如,ssh这个服务到底是ssh还是 sshd ?   

[root@rhel03-23 ~]# 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值