linux运行模式systemd target unit管理


通过systemctl管理不同的操作环境(target unit)

回到顶部
最新的linux发行版已经取消了linux运行级别的概念,取而代之的是target unit。
target可以理解为系统的运行模式,我们在linux启动运行模式中说过linux分为文本模式和图形模式,这两种模式就是用target unit来表示。
target可以用来模拟运行级别。
当然了除了文本模式和图形模式外,linux下可不止只有这两种运行模式。
通过list-units观察系统中有哪些target:
[root@initroot ~]# systemctl list-units --type=target --all
UNIT                   LOAD      ACTIVE   SUB    DESCRIPTION                  
● all.target             not-found inactive dead   all.target                   
basic.target           loaded    active   active Basic System                 
cryptsetup.target      loaded    active   active Local Encrypted Volumes      
emergency.target       loaded    inactive dead   Emergency Mode               
getty-pre.target       loaded    inactive dead   Login Prompts (Pre)          
getty.target           loaded    active   active Login Prompts                
graphical.target       loaded    active   active Graphical Interface          
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    inactive dead   Network is Online            
network-pre.target     loaded    inactive dead   Network (Pre)                
network.target         loaded    active   active Network                      
nss-lookup.target      loaded    active   active Host and Network Name Lookups
nss-user-lookup.target loaded    active   active User and Group Name Lookups  
paths.target           loaded    active   active Paths                        
remote-fs-pre.target   loaded    inactive dead   Remote File Systems (Pre)    
remote-fs.target       loaded    active   active Remote File Systems          
rescue.target          loaded    inactive dead   Rescue Mode                  
shutdown.target        loaded    inactive dead   Shutdown                     
slices.target          loaded    active   active Slices                       
sockets.target         loaded    active   active Sockets                      
sound.target           loaded    active   active Sound Card                   
swap.target            loaded    active   active Swap                         
sysinit.target         loaded    active   active System Initialization        
time-sync.target       loaded    active   active System Time Synchronized     
timers.target          loaded    active   active Timers                       
umount.target          loaded    inactive dead   Unmount All Filesystems      

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.

28 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
在linuxmint下就有28 个target unit!
和操作界面相关性较高的target主要关注下面几个:
graphical.target:就是图形界面模式,当然我们以前提到过图形界面模式其实就是在文本模式的基础上开启了x window以及一些窗口管理器, 所以graphical.target首先依赖的就是下面的multi-user.target!
multi-user.target:纯文本模式!
rescue.target:rescue救援模式。在无法使用root登入的情况下,systemd在开机时会多加一个额外的暂时系统,与你原本的系统无关。 这时你可以取得root的权限来维护你的系统。但这是额外系统,因此可能需要用到chroot来取得你原有的系统!
emergency.target:emergency紧急救援模式。紧急处理系统的错误,还是需要使用root登入的情况,在无法使用rescue.target时,可以尝试使用这种模式!
shutdown.target:就是关机的流程。
getty.target:可以设置你需要几个tty之类的,如果想要降低tty的数目,可以修改这个东西的配置文件!
正常的模式是multi-user.target以及graphical.target两个,也是最经常使用的两种模式了。救援方面的模式主要是rescue.target以及更严重的emergency.target。 如果要修改可提供登入的tty数量,则修改getty.target。
[root@initroot ~]# systemctl [command] [unit.target]
和target有关的选项与参数:
command:
get-default :获取当前target
set-default :设置后面接的target成为默认的操作模式
isolate :切换到后面接的模式
上面三个选项我们在讲解linux运行模式的时候就已经使用过了。这里再象征性的复习了解一下: 查看当前系统的默认的启动运行模式:
[root@initroot ~]# systemctl get-default
graphical.target  # 果然是图形界面喔!
            
可以看到当前系统默认启动运行模式为graphical.target,即图形模式。
也就是说每次linux系统在开机后默认就会进入图形模式。
我们将系统启动模式设置为纯文本模式:
[root@initroot ~]# systemctl set-default multi-user.target [root@initroot ~]# systemctl get-default multi-user.target 这样下次再系统重启开机的时候就会进入纯文本模式。
上面设置的是系统默认启动模式,如果不想重启就使设置生效,那就使用isolate直接切换当前系统的运行模式:
[root@initroot ~]# systemctl isolate multi-user.target
这样立刻就会变为纯文本模式,但是下次重启的时候还是会进入图形界面模式。 将运行模式切换回图形界面模式:
[root@initroot ~]# systemctl isolate graphical.target
注意这里的systemctl的start/stop/restart命令选项无法应用于target,只能使用这里介绍的set-default、get-default和isolate。
除了上面介绍的图形模式和文本模式的切换,systemd也提供了几个简单的命令用于切换操作模式:
[root@initroot ~]# systemctl poweroff   #系统关机
[root@initroot ~]# systemctl reboot     #重新启动
[root@initroot ~]# systemctl suspend    #进入暂停模式
[root@initroot ~]# systemctl hibernate  #进入休眠模式
[root@initroot ~]# systemctl rescue     #强制进入救援模式
[root@initroot ~]# systemctl emergency  #强制进入紧急救援模式
suspend:暂停模式会将系统的状态数据保存到内存中,然后关闭掉大部分的系统硬件,并没有实际关机! 当用户按下唤醒机器的按钮,系统数据会重内存中回复,然后重新驱动被大部分关闭的硬件,就 开始正常运作!唤醒的速度较快。
hibernate:休眠模式则是将系统状态保存到硬盘当中,保存完毕后,将计算机关机。当用户尝试唤醒系统时,系统会开始正常运作, 然后将保存在硬盘中的系统状态恢复回来。因为数据是由硬盘读出,因此唤醒的效 能与你的硬盘速度有关。

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读