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/<target名>.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 CommandSystemd CommandNotes
service frobozz startsystemctl start frobozz.serviceUsed to start a service (not reboot persistent)
service frobozz stopsystemctl stop frobozz.serviceUsed to stop a service (not reboot persistent)
service frobozz restartsystemctl restart frobozz.serviceUsed to stop and then start a service
service frobozz reloadsystemctl reload frobozz.serviceWhen supported, reloads the config file without interrupting pending operations.
service frobozz condrestartsystemctl condrestart frobozz.serviceRestarts if the service is already running.
service frobozz statussystemctl status frobozz.serviceTells 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 onsystemctl enable frobozz.serviceTurn the service on, for start at next boot, or other trigger.
chkconfig frobozz offsystemctl disable frobozz.serviceTurn the service off for the next reboot, or any other trigger.
chkconfig frobozzsystemctl is-enabled frobozz.serviceUsed to check whether a service is configured to start or not in the current environment.
chkconfig --listsystemctl 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 --listls /etc/systemd/system/*.wants/frobozz.serviceUsed to list what levels this service is configured on or off
chkconfig frobozz --addsystemctl daemon-reloadUsed 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 RunlevelSystemd TargetNotes
0runlevel0.target, poweroff.targetHalt the system.
1, s, singlerunlevel1.target, rescue.targetSingle user mode.
2, 4runlevel2.target, runlevel4.target, multi-user.targetUser-defined/Site-specific runlevels. By default, identical to 3.
3runlevel3.target, multi-user.targetMulti-user, non-graphical. Users can usually login via multiple consoles or via the network.
5runlevel5.target, graphical.targetMulti-user, graphical. Usually has all the services of runlevel 3 plus a graphical login.
6runlevel6.target, reboot.targetReboot
emergencyemergency.targetEmergency shell

Changing runlevels:

Sysvinit CommandSystemd CommandNotes
telinit 3systemctl 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.targetSet to use multi-user runlevel on next reboot.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值