美文网首页
systemctl命令的使用

systemctl命令的使用

作者: 小尛酒窝 | 来源:发表于2018-04-07 16:16 被阅读0次

    一、简介

    systemctl命令是RHEL 7上新的系统服务管理指令,其将此前service和chkconfig两个命令组合在一起。其功能可大致分为:查询或发送相应的控制命令给systemd、管理unit、启动或禁止相应的服务等等。

    二、systemctl命令的使用

    1、命令格式

    systemctl [OPTIONS...] COMMAND [NAME...]
    

    systemctl命令选项一般用的不多,更多时候是使用其子命令进行操作。

    2、systemctl命令的子命令COMMAND

    下面为常用的systemctl子命令的使用格式以及在旧版本中对应的实现方式:

    功能 旧指令 新指令
    启动服务 service NAME start systemctl start NAME[.service]
    停止服务 service NAME stop systemctl stop NANME[.service]
    重启服务 service NAME restart systemctl restart NAME[.service]
    查看对应服务的状态 service NAME status systemctl status NAME[.service]
    条件式重启 service NAME condrestart systemctl try-restart NAME[.service]
    重载或重启服务 - systemctl [reload|restart] NAME[.service]
    重载或条件式重启服务 - systemctl [reload|try-restart] NAME[.service]
    查看相应的服务的激活状态 - systemctl is-active NAME[.service]
    查看所有已激活的服务 - systemctl list-units --type service
    查看所有服务(包括已激活及未激活的) chkconfig --list NAME systemctl is-enabled NAME[.service]
    设置服务开机自启 chkconfig NAME on systemctl enable NAME[.service]
    禁止服务开机自启 chkconfig NAME off systemctl disable NAME[.service]
    查看某服务是否能够开机自启 chkconfig --list NAME systemctl is-enabled NAME[.service]
    查看服务的依赖关系 - systemctl list-dependencies NAME[.service]
    级别切换 init N systemctl isolate NAME.target
    查看运行级别 runlevel systemctl list-units --type target
    查看所有级别 - systemctl list-units -t target -a
    获取默认运行级别 - systemctl get-default
    修改默认运行级别 - systemctl set-default NAME[.service]
    切换至救援模式 - systemctl rescue
    切换至emergency模式 - systemctl emergency
    关机 - systemctl <halt|poweroff>
    重启 - systemctl reboot
    挂起 - systemctl suspend
    快照 - systemctl hibernate
    快照并挂起 - systemctl hybrid-sleep
    重新读取unit服务文件 - systemctl daemon-reload

    注意:在RHEL 7 上对应的运行级别文件分别为:

    级别0:runlevel0.target或poweroff.target
    级别1:runlevel1.target或rescue.target
    级别2:runlevel2.target或multi-user.target
    级别3:runlevel3.target或multi-user.target
    级别4:runlevel4.target或multi-user.target
    级别5:runlevel5.target或graphical.taget
    级别6:runlevel6.targe或reboot.target
    

    三、使用案例

    • 管理服务
      启动服务:
    [root@localhost ~]# systemctl start httpd
    

    重启服务:

    [root@localhost ~]# systemctl restart httpd
    

    关闭服务:

    [root@localhost ~]# systemctl stop httpd
    

    查看服务状态:

    [root@localhost ~]# systemctl status httpd
    ● httpd.service - apache
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; linked; vendor preset: disabled)
       Active: failed (Result: exit-code) since 六 2018-04-07 15:52:10 CST; 1min 23s ago
      Process: 3080 ExecStop=/bin/kill ${MAINPID} (code=exited, status=1/FAILURE)
      Process: 3077 ExecStart=/usr/local/apache/bin/apachectl -k start -DFOREGROUND (code=exited, status=0/SUCCESS)
     Main PID: 3077 (code=exited, status=0/SUCCESS)
    

    查看服务的依赖关系:

    [root@localhost ~]# systemctl list-dependencies apache
    apache.service
    ● ├─system.slice
    ● └─basic.target
    ●   ├─firewalld.service
    ●   ├─microcode.service
    ●   ├─rhel-autorelabel-mark.service
    ●   ├─rhel-autorelabel.service
    ●   ├─rhel-configure.service
    ●   ├─rhel-dmesg.service
    ●   ├─rhel-loadmodules.service
    ●   ├─paths.target
    ●   ├─slices.target
    ●   │ ├─-.slice
    ●   │ └─system.slice
    ●   ├─sockets.target
    ●   │ ├─dbus.socket
    ●   │ ├─dm-event.socket
    ●   │ ├─systemd-initctl.socket
    ●   │ ├─systemd-journald.socket
    ●   │ ├─systemd-shutdownd
    ....
    
    • 系统管理
      重启系统:
    [root@localhost ~]# systemctl reboot
    PolicyKit daemon disconnected from the bus.
    We are no longer a registered authentication agent.
    Connection closing...Socket close.
    
    Connection closed by foreign host.
    
    Disconnected from remote host(192.168.0.188:22) at 15:56:05.
    

    切换至运行级别1:

    [root@localhost ~]# systemctl isolate runlevel1.target
    PolicyKit daemon disconnected from the bus.
    We are no longer a registered authentication agent.
    [root@localhost ~]# runlevel
    3 1
    
    救援模式

    查看当前运行级别:

    [root@localhost ~]# systemctl list-units --type target
    UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
    basic.target          loaded active active Basic System
    cryptsetup.target     loaded active active Encrypted Volumes
    getty.target          loaded active active Login Prompts
    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
    network.target        loaded active active Network
    paths.target          loaded active active Paths
    remote-fs.target      loaded active active Remote File Systems
    slices.target         loaded active active Slices
    sockets.target        loaded active active Sockets
    swap.target           loaded active active Swap
    sysinit.target        loaded active active System Initialization
    timers.target         loaded active active Timers
    
    LOAD   = Reflects whether the unit definition was properly loaded.
    ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
    SUB    = The low-level unit activation state, values depend on unit type.
    
    15 loaded units listed. Pass --all to see loaded but inactive units, too.
    To show all installed unit files use 'systemctl list-unit-files'
    

    相关文章

      网友评论

          本文标题:systemctl命令的使用

          本文链接:https://www.haomeiwen.com/subject/opjyhftx.html