systemd & systemctl 的一些使用说明

原文链接: http://blog.sina.com.cn/s/blog_7f2122c501016kyf.html

概要:

从fedora15开始,系统对于daemon的启动管理方法不再采用SystemV形式,而是使用了sytemd的架构来管理daemon的启动。

 

runlevel 到 target的改变:

    在systemd的管理体系里面,以前的运行级别(runlevel)的概念被新的运行目标(target)所取代。tartget的命名类似于multi-user.target等这种形式,比如原来的运行级别3(runlevel3)就对应新的多用户目标(multi-user.target),run level 5就相当于graphical.target。

   由于不再使用runlevle概念,所以/etc/inittab也不再被系统使用。

而在systemd的管理体系里面,默认的target(相当于以前的默认运行级别)是通过软链来实现。

例如

ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

 

在/lib/systemd/system/ 下面定义runlevelX.target文件目的主要是为了能够兼容以前的运行级别level的管理方法。 事实上/lib/systemd/system/runlevel3.target,同样是被软连接到multi-user.target。

 

单元控制(unit)

在systemd管理体系里,称呼需要管理的daemon为单元(unit)。对于单元(unit)的管理是通过命令systemctl来进行控制的。

 

例如显示当前的处于运行状态的unit(即daemon)

#systemctl
UNIT                      LOAD   ACTIVE SUB       JOB DESCRIPTION
<略>
fedora-l...odules.service loaded active exited        Load legacy module configu
fedora-readonly.service   loaded active exited        Configure read-only root s
fedora-s...t-late.service loaded active exited        Initialize storage subsist   
fedora-s...e-init.service loaded active exited        Initialize storage subsyst
fedora-w...torage.service loaded active exited        Wait for storage scan
ip6tables.service         loaded active exited        IPv6 firewall with ip6tabl
iptables.service          loaded active exited        IPv4 firewall with iptable

 

如果要查看没有启动的daemon 只要在上面命令加上参数 –all

#systemctl --all

 

用systemctl status  daemon  显示该daemon的当前状态

# systemctl status httpd.service
httpd.service - The Apache HTTP Server (prefork MPM)
        Loaded: loaded (/lib/systemd/system/httpd.service; disabled)
        Active: inactive (dead)  <-- 表示未启动
        CGroup: name=systemd:/system/httpd.service
 
等同于 /etc/init.d/httpd status

 

从上面的输出可以很容易知道,原本在/etc/init.d/目录下的启动文件,被/lib/systemd/system/下相应的文件所取代。例如实例中的/lib/systemd/system/httpd.service,http的启动等相关的配置都在这个文件里修改。

 

unit的启动停止

启动,关闭unit

# systemctl start httpd.service

 

等同于 /etc/init.d/httpd start

 

 

# systemctl stop httpd.service

 

等同于 /etc/init.d/httpd stop

 

 

配置成系统启动时默认启动

# systemctl enable httpd.service
 
等同于 /sbin/checkconfig httpd

 

通过在启动文件/lib/systemd/system/httpd.service里的[Install]单元里指定启动的目标(target)级别。

比如

[Install]
WantedBy=multi-user.target

则表明在多用户目标(multi-user.target,相当于level3)时自动启动。

另外一旦设定了自动启动(enbale),就在/etc/systemd/system/.wants/下面建了一个httpd.service的软连接,连接到/lib/systemd/system/下的相应服务那里

 关闭自动启动

# systemctl disable httpd.service
 
相当于  /sbin/checkconfig httpd off

 

添加新的unit

对于新的unit(daemon)的添加,采用load命令

把新生成的foo.service 放到/lib/systemd/system/下面,然后采用load命令导入
 
#systemctl load foo.service
 
/sbin/chkconfig --add foo相当

删除一个unit没有相应的命令,通常的做法是停掉daemon,然后删除相应的配置文件。

显示自动启动状态的unit

如何能像/sbin/chkconfig –list那样显示自动启动的状态呢?在systemd里面没有相应的可操作的命令,只能用以下命令显示

#ls /etc/systemd/system/multi-user.target.wants/

 

 

https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet

 

SysVinit to Systemd Cheatsheet

 

This is a document to help system administrators who need to understand what commands in systemd replace their old workflow in sysvinit. If you want general information on systemd, refer to systemd.

Idea.png

Note on 'service' and 'chkconfig' commands 
The 'service' and 'chkconfig' commands will mostly continue to work as expected in the systemd world, this guide is how to use the native systemctl replacements.

 

 

Services

Sysvinit Command Systemd Command Notes
service frobozz start systemctl start frobozz.service Used to start a service (not reboot persistent)
service frobozz stop systemctl stop frobozz.service Used to stop a service (not reboot persistent)
service frobozz restart systemctl restart frobozz.service Used to stop and then start a service
service frobozz reload systemctl reload frobozz.service When supported, reloads the config file without interrupting pending operations.
service frobozz condrestart systemctl condrestart frobozz.service Restarts if the service is already running.
service frobozz status systemctl status frobozz.service Tells whether a service is currently running.
ls /etc/rc.d/init.d/ systemctl list-unit-files --type=service (preferred)
ls /lib/systemd/system/*.service /etc/systemd/system/*.service
Used to list the services that can be started or stopped 
Used to list all the services and other units
chkconfig frobozz on systemctl enable frobozz.service Turn the service on, for start at next boot, or other trigger.
chkconfig frobozz off systemctl disable frobozz.service Turn the service off for the next reboot, or any other trigger.
chkconfig frobozz systemctl is-enabled frobozz.service Used to check whether a service is configured to start or not in the current environment.
chkconfig --list systemctl list-unit-files --type=service(preferred)
ls /etc/systemd/system/*.wants/
Print a table of services that lists which runlevels each is configured on or off
chkconfig frobozz --list ls /etc/systemd/system/*.wants/frobozz.service Used to list what levels this service is configured on or off
chkconfig frobozz --add systemctl daemon-reload Used when you create a new service file or modify any configuration

 

Note that all /sbin/service and /sbin/chkconfig lines listed above continue to work on systemd, and will be translated to native equivalents as necessary. The only exception is chkconfig --list.

Warning (medium size).png

Additional commands
In SysVinit, services can define arbitrary commands. Examples would be service iptables panic, or service httpd graceful. Native systemd services do not have this ability.

Any service that defines an additional command in this way would need to define some other, service-specific, way to accomplish this task when writing a native systemd service definition.

Check the package-specific release notes for any services that may have done this.

Runlevels/targets

Systemd has a concept of targets which serve a similar purpose as runlevels but act a little different. Each target is named instead of numbered and is intended to serve a specific purpose. Some targets are implemented by inheriting all of the services of another target and adding additional services to it. There are systemd targets that mimic the common sysvinit runlevels so you can still switch targets using the familiar telinit RUNLEVEL command. The runlevels that are assigned a specific purpose on vanilla Fedora installs; 0, 1, 3, 5, and 6; have a 1:1 mapping with a specific systemd target. Unfortunately, there's no good way to do the same for the user-defined runlevels like 2 and 4. If you make use of those it is suggested that you make a new named systemd target as /etc/systemd/system/$YOURTARGET that takes one of the existing runlevels as a base (you can look at/lib/systemd/system/graphical.target as an example), make a directory /etc/systemd/system/$YOURTARGET.wants, and then symlink the additional services that you want to enable into that directory. (The service unit files that you symlink live in /lib/systemd/system).

Sysvinit Runlevel Systemd Target Notes
0 runlevel0.target, poweroff.target Halt the system.
1, s, single runlevel1.target, rescue.target Single user mode.
2, 4 runlevel2.target, runlevel4.target, multi-user.target User-defined/Site-specific runlevels. By default, identical to 3.
3 runlevel3.target, multi-user.target Multi-user, non-graphical. Users can usually login via multiple consoles or via the network.
5 runlevel5.target, graphical.target Multi-user, graphical. Usually has all the services of runlevel 3 plus a graphical login.
6 runlevel6.target, reboot.target Reboot
emergency emergency.target Emergency shell

Changing runlevels:

Sysvinit Command Systemd Command Notes
telinit 3 systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3) Change to multi-user run level.
sed s/^id:.*:initdefault:/id:3:initdefault:/ ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target Set to use multi-user runlevel on next reboot.

你可能感兴趣的:(笔记)