PUB:20140901 Awesome systemd Commands to Manage Linux System

@szrlee
This commit is contained in:
wxy 2014-09-11 23:35:54 +08:00
parent d660407992
commit c97d39f9bf

View File

@ -1,13 +1,11 @@
(translated by szrlee)
真的超赞用systemd命令来管理linux系统 真的超赞用systemd命令来管理linux系统
================================================================================ ================================================================================
Systemd是一种新的linux系统服务管理器。 Systemd是一种新的linux系统服务管理器。
它替换了init系统能够管理系统的启动过程和一些系统服务一旦启动起来就将监管整个系统。在本文中我们用的是[centos 7.0 installed with systemd 216 version][1]和最新版本[available for download from freedesktop.org][2]。 它替换了init系统能够管理系统的启动过程和一些系统服务一旦启动起来就将监管整个系统。在本文中我们用的是[安装有 systemd 216 版本的centos 7.0][1],其最新版本[可以从 freedesktop.org 下载得到][2]。
因为linux系统里这一新的玩家PID 1被“systemd”占据了这能通过**pstree**命令看到。 因为linux操作系统里出现的这一个新人PID 1被“systemd”占据了这能通过**pstree**命令看到。
[root@linoxide ~]# pstree [root@linoxide ~]# pstree
@ -15,25 +13,25 @@ Systemd是一种新的linux系统服务管理器。
那么现在让我们来探索systemd擅长什么它又有多大的可能性成为sysVinit的新的替代品。 那么现在让我们来探索systemd擅长什么它又有多大的可能性成为sysVinit的新的替代品。
### 1. Faster startup ### ### 1. 更快启动 ###
sysvinit一次一个串行地启动进程。 sysvinit一次一个串行地启动进程。
而Systemd则并行地启动系统服务进程并且最初仅启动确实被依赖的那些服务极大地减少了系统引导的时间。 而Systemd则并行地启动系统服务进程并且最初仅启动确实被依赖的那些服务极大地减少了系统引导的时间。
你可以用下面的命令看到系统引导的过程 你可以用下面的命令看到系统引导用时
[root@linoxide ~]# systemd-analyze [root@linoxide ~]# systemd-analyze
![](http://linoxide.com/wp-content/uploads/2014/08/02.systemd_analyze.png) ![](http://linoxide.com/wp-content/uploads/2014/08/02.systemd_analyze.png)
**systemd-analyze time**也能够显示同样的内容。 使用 **time** 参数也能够显示同样的内容。
[root@linoxide ~]# systemd-analyze time [root@linoxide ~]# systemd-analyze time
![](http://linoxide.com/wp-content/uploads/2014/08/03.systemd_analyze2.png) ![](http://linoxide.com/wp-content/uploads/2014/08/03.systemd_analyze2.png)
如果你想以初始化时间排序打印出所有正在运行unit的列表,那么**systemd-analyze**命令可以帮助你完成这个任务。 如果你想以进程初始化所占用时间排序打印出所有正在运行的单元列表,那么**systemd-analyze**命令可以帮助你完成这个任务。
[root@linoxide ~]# systemd-analyze blame [root@linoxide ~]# systemd-analyze blame
@ -41,25 +39,25 @@ sysvinit一次一个串行地启动进程。
上面的截屏只显示了小部分进程你可以就像less分页器那样用箭头滚动列表。 上面的截屏只显示了小部分进程你可以就像less分页器那样用箭头滚动列表。
### 2. The systemctl command ### ### 2. systemctl 命令 ###
systemctl命令是自systemd出现以来被广泛讨论的命令。你可以通过这个命令管理你的整个系统让我们通过探究这个命令来更进一步。 systemctl命令是自systemd出现以来被广泛讨论的命令。你可以通过这个命令管理你的整个系统让我们通过探究这个命令来更进一步。
#### 2.1 List Units #### #### 2.1 列出单元 ####
**systemctl**命令可以带上list-units也可以什么选项都不带来列出所有正在运行的unit **systemctl**命令可以带上list-units也可以什么选项都不带来列出所有正在运行的单元
[root@linoxide ~]# systemctl [root@linoxide ~]# systemctl
or
[root@linoxide ~]# systemctl list-units [root@linoxide ~]# systemctl list-units
![](http://linoxide.com/wp-content/uploads/2014/08/05.systemd_list_units.png) ![](http://linoxide.com/wp-content/uploads/2014/08/05.systemd_list_units.png)
#### 2.2 Listing failed units #### #### 2.2 列出失败的单元 ####
运行失败的unit可以用带--failed选项的命令显示出来。 运行失败的单元可以用带--failed选项的命令显示出来。
[root@linoxide ~]# systemctl --failed [root@linoxide ~]# systemctl --failed
@ -67,11 +65,11 @@ or
你可以在这篇文章很多地方看到systemctl的用法。 你可以在这篇文章很多地方看到systemctl的用法。
### 3. Managing services ### ### 3. 管理服务 ###
让我们来看看systemd是怎么管理系统服务的。 让我们来看看systemd是怎么管理系统服务的。
#### 3.1 Active services #### #### 3.1 激活的服务 ####
所有被激活的服务可以同下面这条命令来查看。 所有被激活的服务可以同下面这条命令来查看。
@ -79,7 +77,7 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/07.systemd_active_services.png) ![](http://linoxide.com/wp-content/uploads/2014/08/07.systemd_active_services.png)
#### 3.2 Service status #### #### 3.2 服务状态 ####
在sysvinit中我们可以用“**service**”命令来管理服务但在systemd中我们用systemctl这个命令。 在sysvinit中我们可以用“**service**”命令来管理服务但在systemd中我们用systemctl这个命令。
我们可以用下面这个命令来查看服务是否在运行。 我们可以用下面这个命令来查看服务是否在运行。
@ -88,17 +86,17 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/08.systemd_status.png) ![](http://linoxide.com/wp-content/uploads/2014/08/08.systemd_status.png)
#### 3.3 Start a service #### #### 3.3 启动一个服务 ####
用下面这条命令来启动服务。 用下面这条命令来启动服务。
[root@linoxide ~]# systemctl start dnsmasq [root@linoxide ~]# systemctl start dnsmasq
相对于**service**服务这个命令不进行输出。但是毋庸置疑我们可以通过再次查看这个刚刚被启动的服务的status状态来确认他是否被成功地启动了。 对应于**service**命令这个命令不进行输出。但是毋庸置疑我们可以通过再次查看这个刚刚被启动的服务的status状态来确认他是否被成功地启动了。
![](http://linoxide.com/wp-content/uploads/2014/08/09.systemd_start.png) ![](http://linoxide.com/wp-content/uploads/2014/08/09.systemd_start.png)
#### 3.4 Stopping a service #### #### 3.4 停止一个服务 ####
现在聪明的你一定知道怎么在systemd下用命令来关闭服务了吧。 现在聪明的你一定知道怎么在systemd下用命令来关闭服务了吧。
@ -106,7 +104,7 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/10.systemd_stop.png) ![](http://linoxide.com/wp-content/uploads/2014/08/10.systemd_stop.png)
#### 3.5 Restart a service #### #### 3.5 重启一个服务 ####
类似的,重启系统服务是用‘**systemctl restart**’来管理的。 类似的,重启系统服务是用‘**systemctl restart**’来管理的。
@ -114,7 +112,7 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/11.systemd_restart.png) ![](http://linoxide.com/wp-content/uploads/2014/08/11.systemd_restart.png)
#### 3.6 Reload a service #### #### 3.6 重新加载一个服务 ####
在我们需要重新加载服务的配置文件又不想重启这个服务例如ssh我们可以用这个命令。 在我们需要重新加载服务的配置文件又不想重启这个服务例如ssh我们可以用这个命令。
@ -122,17 +120,17 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/12.systemd_reload.png) ![](http://linoxide.com/wp-content/uploads/2014/08/12.systemd_reload.png)
虽然上述命令的语法是可以工作的,但是官方文档建议我们用下面这种语法形式来运行命令: 虽然上述几个命令的语法是可以工作的,但是官方文档建议我们用下面这种语法形式来运行命令LCTT 译注,即使用在服务名后面跟上“.service”的完整名称
[root@linoxide ~]# systemctl status dnsmasq.service [root@linoxide ~]# systemctl status dnsmasq.service
![](http://linoxide.com/wp-content/uploads/2014/08/13.systemd_alternate_syntax.png) ![](http://linoxide.com/wp-content/uploads/2014/08/13.systemd_alternate_syntax.png)
### 4. Managing services at boot ### ### 4. 管理引导时的服务 ###
**chkconfig**命令被用来管理系统引导时的服务。同样用systemd也可以管理boot时的系统服务。 **chkconfig**命令被用来管理系统引导时的服务。同样用systemd也可以管理引导时的系统服务。
#### 4.1 Checking service status at boot #### #### 4.1 检查服务引导时是否运行 ####
这条命令用来确定服务是否是引导时启动的。 这条命令用来确定服务是否是引导时启动的。
@ -140,7 +138,7 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/14.systemd_is_enabled.png) ![](http://linoxide.com/wp-content/uploads/2014/08/14.systemd_is_enabled.png)
#### 4.2 Enable a service at boot #### #### 4.2 让服务在引导时运行 ####
**systemctl**命令是这样来enable使之在引导时启动一个服务的。这相当于sysvinit中的**chkconfig on** **systemctl**命令是这样来enable使之在引导时启动一个服务的。这相当于sysvinit中的**chkconfig on**
@ -148,7 +146,7 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/15.systemd_enable.png) ![](http://linoxide.com/wp-content/uploads/2014/08/15.systemd_enable.png)
#### 4.3 Disable a service at boot #### #### 4.3 取消服务在引导时运行 ####
类似的,使服务不在引导时启动用这个命令。 类似的,使服务不在引导时启动用这个命令。
@ -156,18 +154,18 @@ or
![](http://linoxide.com/wp-content/uploads/2014/08/16.systemd_disable.png) ![](http://linoxide.com/wp-content/uploads/2014/08/16.systemd_disable.png)
### 5. Managing Remote systems ### ### 5. 管理远程系统 ###
所有刚才提到的systemctl命令通常都能被用来管理远程主机完成这个任务将用到**ssh**来进行通讯。你只需要像这样将远程主机和用户名 所有刚才提到的systemctl命令通常都能被用来管理远程主机完成这个任务将用到**ssh**来进行通讯。你只需要像这样将远程主机和用户名添加到systemctl命令后。
添加到systemctl命令后。
[root@linoxide ~]# systemctl status sshd -H root@1.2.3.4 [root@linoxide ~]# systemctl status sshd -H root@1.2.3.4
![](http://linoxide.com/wp-content/uploads/2014/08/17.systemd_remote.png) ![](http://linoxide.com/wp-content/uploads/2014/08/17.systemd_remote.png)
### 6. Managing targets: ### ### 6. 管理目标 ###
Systemd有一个完成与sysVinit的runlevels相似任务的构想。 Systemd有一个完成与sysVinit的runlevels相似任务的构想。
sysVinit的runlevels大多是以数字分级的。这里是runlevers在systemd中的对应元素。 sysVinit的runlevels大多是以数字分级的。这里是runlevers在systemd中的对应元素。
> 0 runlevel0.target, poweroff.target > 0 runlevel0.target, poweroff.target
@ -184,7 +182,7 @@ sysVinit的runlevels大多是以数字分级的。这里是runlevers在systemd
> >
> emergency emergency.target > emergency emergency.target
#### 6.1 Changing current target #### #### 6.1 改变当前目标 ####
当前target可以用这个命令切换。 当前target可以用这个命令切换。
@ -192,39 +190,38 @@ sysVinit的runlevels大多是以数字分级的。这里是runlevers在systemd
![](http://linoxide.com/wp-content/uploads/2014/08/18.systemd_isolate.png) ![](http://linoxide.com/wp-content/uploads/2014/08/18.systemd_isolate.png)
#### 6.2 List current target #### #### 6.2 列出当前目标 ####
如果你想查看你正处于哪个target中你需要列出相应的units。虽然这样操作可能让你不太爽但是这就是systemd工作的方式。 如果你想查看你正处于哪个target中你需要列出相应的单元。虽然这样操作可能让你不太爽但是这就是systemd工作的方式。
[root@linoxide ~]# systemctl list-units --type=target [root@linoxide ~]# systemctl list-units --type=target
![](http://linoxide.com/wp-content/uploads/2014/08/19.systemd_targets.png) ![](http://linoxide.com/wp-content/uploads/2014/08/19.systemd_targets.png)
你可以看到“graphical.target”列在此处这就是我们刚才切换到的target。现在让我们切换runlever到multi-user.target然后分析下列命令的输出。 你可以看到“graphical.target”列在此处这就是我们刚才切换到的目标。现在让我们切换runlevel到multi-user.target然后分析下列命令的输出。
[root@linoxide ~]# systemctl isolate multi-user.target [root@linoxide ~]# systemctl isolate multi-user.target
[root@linoxide ~]# systemctl list-units --type=target [root@linoxide ~]# systemctl list-units --type=target
![](http://linoxide.com/wp-content/uploads/2014/08/20.systemd_multi-user.png) ![](http://linoxide.com/wp-content/uploads/2014/08/20.systemd_multi-user.png)
#### 6.3 List default target #### #### 6.3 列出默认目标 ####
用这个systemctl命令来查看默认target 用这个systemctl命令来查看默认目标
[root@linoxide ~]# systemctl get-default [root@linoxide ~]# systemctl get-default
![](http://linoxide.com/wp-content/uploads/2014/08/21.systemd_get_default.png) ![](http://linoxide.com/wp-content/uploads/2014/08/21.systemd_get_default.png)
#### 6.4 Change default target #### #### 6.4 改变默认目标 ####
通过systemctl的set-default命令可以将某个target设置成默认target 通过systemctl的set-default命令可以将某个目标设置成默认目标
[root@linoxide ~]# systemctl set-default graphical.target [root@linoxide ~]# systemctl set-default graphical.target
![](http://linoxide.com/wp-content/uploads/2014/08/22.systemd_set_default.png) ![](http://linoxide.com/wp-content/uploads/2014/08/22.systemd_set_default.png)
### 7. Logging in systemd ### ### 7. 记录 systemd 的日志 ###
journald是systemd独有的日志系统替换了sysVinit中的syslog守护进程。命令**journalctl**用来读取日志。 journald是systemd独有的日志系统替换了sysVinit中的syslog守护进程。命令**journalctl**用来读取日志。
@ -232,7 +229,7 @@ journald是systemd独有的日志系统替换了sysVinit中的syslog守护进
![](http://linoxide.com/wp-content/uploads/2014/08/23.systemd_logs.png) ![](http://linoxide.com/wp-content/uploads/2014/08/23.systemd_logs.png)
#### 7.1 Boot messages #### #### 7.1 查看引导信息 ####
运行**journalctl -b**命令来查看所有引导日志。 运行**journalctl -b**命令来查看所有引导日志。
@ -240,16 +237,15 @@ journald是systemd独有的日志系统替换了sysVinit中的syslog守护进
![](http://linoxide.com/wp-content/uploads/2014/08/24.systemd_boot.png) ![](http://linoxide.com/wp-content/uploads/2014/08/24.systemd_boot.png)
#### 7.2 Follow logs #### #### 7.2 即时显示引导日志 ####
下面这个命令可以实时显示系统日志(类似**tail -f**)。
下面这个命令可以即时显示系统日志(类似**tail -f**)。
[root@linoxide ~]# journalctl -f [root@linoxide ~]# journalctl -f
![](http://linoxide.com/wp-content/uploads/2014/08/25.systemd_follow_logs.png) ![](http://linoxide.com/wp-content/uploads/2014/08/25.systemd_follow_logs.png)
#### 7.3 Service specific logs #### #### 7.3 查看特定服务的日志 ####
你可以像这样运用**journalctl**来查看你只想看到的服务或可执行程序的日志。 你可以像这样运用**journalctl**来查看你只想看到的服务或可执行程序的日志。
@ -257,11 +253,11 @@ journald是systemd独有的日志系统替换了sysVinit中的syslog守护进
![](http://linoxide.com/wp-content/uploads/2014/08/26.systemd_specific.png) ![](http://linoxide.com/wp-content/uploads/2014/08/26.systemd_specific.png)
### 8. Power management ### ### 8. 电源管理 ###
systemctl命令也可以用来关机重启或者休眠。 systemctl命令也可以用来关机重启或者休眠。
To poweroff, reboot, suspend and hibernate, use the following commands respectively: 要关机、重启、挂起和休眠,分别使用如下命令:
[root@linoxide ~]# systemctl poweroff [root@linoxide ~]# systemctl poweroff
@ -271,7 +267,7 @@ To poweroff, reboot, suspend and hibernate, use the following commands respectiv
[root@linoxide ~]# systemctl hibernate [root@linoxide ~]# systemctl hibernate
### 9. Bonus ### ### 9. 又及 ###
**systemd**带来了一整套与操作系统交互的新途径并且极具特色。举个栗子你可以用hostnamectl命令来获得你的linux机器的hostname和其它有用的独特信息。 **systemd**带来了一整套与操作系统交互的新途径并且极具特色。举个栗子你可以用hostnamectl命令来获得你的linux机器的hostname和其它有用的独特信息。
@ -285,7 +281,7 @@ via: http://linoxide.com/linux-command/linux-systemd-commands/
作者:[Raghu][a] 作者:[Raghu][a]
译者:[szrlee](https://github.com/szrlee) 译者:[szrlee](https://github.com/szrlee)
校对:[校对者ID](https://github.com/校对者ID) 校对:[ wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出