Log in and switch users in multiuser targets
Log in and switch users in multiuser targets
I prefer to power off or reboot my machine with init <number>
command rather than any command or clicking the menus. In this section we learn indepth about the <number>
initiated with, it called runlevel
and this is the explanation of each level that related to systemd
initial mode.
Name | Runlevel | Description | Old Command | New Command |
---|---|---|---|---|
/lib/systemd/system/poweroff.target | 0 | Power off the machine | init 0 | systemctl isolate poweroff.target |
/lib/systemd/system/rescue.target | 1 | Boot into rescue mode (single user) | init 1 | systemctl isolate rescue.target |
/lib/systemd/system/runlevel2.target | 2 | Boot into single use with no network setup | init 2 | systemctl isolate runlevel2.target |
/lib/systemd/system/runlevel3.target | 3 | Boot text based multi-user | init 3 | systemctl isolate runlevel3.target |
/lib/systemd/system/runlevel4.target | 4 | reserved for special purposed text mode | init 4 | systemctl isolate runlevel4.target |
/lib/systemd/system/graphical.target | 5 | Boot into multi user graphical mode | init 5 | systemctl isolate graphical.target |
/lib/systemd/system/reboot.target | 6 | Reboot the machine | init 6 | systemctl isolate reboot.target |
Get the current run-level target
To get our currend run-level mode, we could use these commands
[rhcsa@localhost ~]$ runlevel
N 5
[rhcsa@localhost ~]$ systemctl get-default
graphical.target
[rhcsa@localhost ~]$ systemctl --type target|grep "multi\|rescue\|graph\|runlevel[0-6]"
graphical.target loaded active active Graphical Interface
multi-user.target loaded active active Multi-User System
We could also check the current run-level by looking at the default.target is content
[rhcsa@localhost ~]$ cat /etc/systemd/system/default.target
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes
If we take a closer look at each run-level target files available in /lib/systemd/system
, they are basically comprises of the file itself and its soft link
, technically shows in column 3 with value 1
which mean the file has only one link to itself. A specific topic related soft link
and hard link
will be covers in the next article about Symbolic Link
.
[root@localhost ~]# ls -li /lib/systemd/system/*.target|grep 'runlevel\|reboot\|rescue\|graphic\|multi\|poweroff'|sort -k2 -r
34787643 -rw-r--r--. 1 root root 607 Jan 18 2022 /lib/systemd/system/poweroff.target
34787616 -rw-r--r--. 1 root root 606 Jan 18 2022 /lib/systemd/system/graphical.target
34809600 -rw-r--r--. 1 root root 598 Jan 18 2022 /lib/systemd/system/reboot.target
34787636 -rw-r--r--. 1 root root 540 Jan 18 2022 /lib/systemd/system/multi-user.target
34809605 -rw-r--r--. 1 root root 500 Jan 18 2022 /lib/systemd/system/rescue.target
34809613 lrwxrwxrwx. 1 root root 17 Sep 23 2022 /lib/systemd/system/runlevel4.target -> multi-user.target
34809612 lrwxrwxrwx. 1 root root 17 Sep 23 2022 /lib/systemd/system/runlevel3.target -> multi-user.target
34809611 lrwxrwxrwx. 1 root root 17 Sep 23 2022 /lib/systemd/system/runlevel2.target -> multi-user.target
34809615 lrwxrwxrwx. 1 root root 16 Sep 23 2022 /lib/systemd/system/runlevel5.target -> graphical.target
34787604 lrwxrwxrwx. 1 root root 16 Sep 23 2022 /lib/systemd/system/default.target -> graphical.target
34809609 lrwxrwxrwx. 1 root root 15 Sep 23 2022 /lib/systemd/system/runlevel0.target -> poweroff.target
34809616 lrwxrwxrwx. 1 root root 13 Sep 23 2022 /lib/systemd/system/runlevel6.target -> reboot.target
34809610 lrwxrwxrwx. 1 root root 13 Sep 23 2022 /lib/systemd/system/runlevel1.target -> rescue.target
34787598 lrwxrwxrwx. 1 root root 13 Sep 23 2022 /lib/systemd/system/ctrl-alt-del.target -> reboot.target
Switch the run-level
To switch between run-level we can use the old command or new command as listed on the table above we could also use this command, for instance we wanted to switch to rescue.target
[root@localhost ~]# systemctl set-default rescue.target
Or we could also replace the symlink of default.target
with run-level target that we wanted
[root@localhost ~]# ls -li /etc/systemd/system/default.target
34950082 lrwxrwxrwx. 1 root root 40 Feb 15 18:16 /etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target
[root@localhost ~]# ln -s -f -v /lib/systemd/system/rescue.target /etc/systemd/system/default.target
Then reboot the machine
Log in and switch users
To switch between users, we use su
command whether with option -
or -l
or not. I we use -
option we will load the target user’s shell profile listed on .bash_profile
file
[root@localhost ~]# su -V;su -h
su from util-linux 2.37.4
Usage:
su [options] [-] [<user> [<argument>...]]
Change the effective user ID and group ID to that of <user>.
A mere - implies -l. If <user> is not given, root is assumed.
Options:
-m, -p, --preserve-environment do not reset environment variables
-w, --whitelist-environment <list> don't reset specified variables
-g, --group <group> specify the primary group
-G, --supp-group <group> specify a supplemental group
-, -l, --login make the shell a login shell
-c, --command <command> pass a single command to the shell with -c
--session-command <command> pass a single command to the shell with -c
and do not create a new session
-f, --fast pass -f to the shell (for csh or tcsh)
-s, --shell <shell> run <shell> if /etc/shells allows it
-P, --pty create a new pseudo-terminal
-h, --help display this help
-V, --version display version
For more details see su(1).
[user1@localhost ~]$ echo "echo \"hi, this is from $USER .bash_profile\"" >> ~/.bash_profile
[user1@localhost ~]$ exit
logout
[root@localhost ~]# su user1
[user1@localhost root]$ exit
exit
[root@localhost ~]# su - user1
hi, this is from user1 .bash_profile
[user1@localhost ~]$
This material is refers to the rhca objectives