当前位置: > Linux教程 > 系统运维 >

systemd & systemctl 的一些使用说明

时间:2014-12-15 21:17来源:linux.it.net.cn 作者:IT
概要:
从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/



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容