This is not a direct answer to your main question, but rather to your comment:
Thanks @Raffa, I do consider that your comment complete the answer to
the question I had in mind! I should have asked why vim without sudo
was destroyed instead of why sudo vim was not destroyed. I'm still a
little surprised that using KillUserProcesses=yes and
KillExcludeUsers=root introduce a different behavior for a sudo
process compared to KillUserProcesses=no and KillExcludeUsers=root. If
it's a sudo process, then it's root and thus, process should be
excluded from being killed, no matter KillUserProcesses.... That's how
I read man logind.conf....
In as limited scope as possible and as simple terminology as possible and with demonstration.
Lets look behind the scenes of when you SSH to your server.
test@localhost
is my test demonstration user ... So, lets connect:
ubuntu@Lenovo:~$ ssh test@localhost
test@localhost's password:
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.19.0-43-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Expanded Security Maintenance for Applications is not enabled.
9 updates can be applied immediately.
To see these additional updates run: apt list --upgradable
21 additional security updates can be applied with ESM Apps.
Learn more about enabling ESM Apps service at https://ubuntu.com/esm
Last login: Tue Jun 6 17:22:18 2023 from 127.0.0.1
test@Lenovo:~$
Now connected and logged-in ... Lets see what login looks like behind the scenes(remember our scope is limited to and focused on the behavior in question):
test@Lenovo:~$ loginctl user-status --no-pager test
test (1004)
Since: Tue 2023-06-06 18:22:53 +03; 4s ago
State: active
Sessions: *455
Linger: no
Unit: user-1004.slice
├─session-455.scope
│ ├─60990 "sshd: test [priv]" "" "" ""
│ ├─61052 "sshd: test@pts/2" "" "" "" ""
│ ├─61055 -bash
│ └─61114 loginctl user-status --no-pager test
└─[email protected]
├─app.slice
│ ├─dbus.service
│ │ ├─61024 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
│ │ ├─61097 /usr/libexec/goa-daemon
│ │ └─61104 /usr/libexec/goa-identity-service
│ ├─gvfs-afc-volume-monitor.service
│ │ └─61084 /usr/libexec/gvfs-afc-volume-monitor
│ ├─gvfs-daemon.service
│ │ └─61037 /usr/libexec/gvfsd
│ ├─gvfs-goa-volume-monitor.service
│ │ └─61093 /usr/libexec/gvfs-goa-volume-monitor
│ ├─gvfs-gphoto2-volume-monitor.service
│ │ └─61080 /usr/libexec/gvfs-gphoto2-volume-monitor
│ ├─gvfs-mtp-volume-monitor.service
│ │ └─61089 /usr/libexec/gvfs-mtp-volume-monitor
│ └─gvfs-udisks2-volume-monitor.service
│ └─61075 /usr/libexec/gvfs-udisks2-volume-monitor
├─background.slice
│ ├─tracker-extract-3.service
│ │ └─61007 /usr/libexec/tracker-extract-3
│ └─tracker-miner-fs-3.service
│ └─61061 /usr/libexec/tracker-miner-fs-3
├─init.scope
│ ├─60995 /lib/systemd/systemd --user
│ └─60996 "(sd-pam)"
└─session.slice
├─pipewire-media-session.service
│ └─61005 /usr/bin/pipewire-media-session
├─pipewire-pulse.service
│ └─61006 /usr/bin/pipewire-pulse
└─pipewire.service
└─61004 /usr/bin/pipewire
يونيو 06 18:22:53 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Activating service name='org.gnome.Identity' requested by ':1.12' (uid=1004 pid=61097 comm="/usr/libexec/goa-daemon " label="unconfined")
يونيو 06 18:22:53 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.gnome.OnlineAccounts'
يونيو 06 18:22:53 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.gtk.vfs.GoaVolumeMonitor'
يونيو 06 18:22:53 Lenovo systemd[60995]: Started Virtual filesystem service - GNOME Online Accounts monitor.
يونيو 06 18:22:53 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.gnome.Identity'
يونيو 06 18:22:54 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.freedesktop.Tracker3.Miner.Files'
يونيو 06 18:22:54 Lenovo systemd[60995]: Started Tracker file system data miner.
يونيو 06 18:22:54 Lenovo systemd[60995]: Started Tracker metadata extractor.
يونيو 06 18:22:54 Lenovo systemd[60995]: Reached target Main User Target.
يونيو 06 18:22:54 Lenovo systemd[60995]: Startup finished in 726ms.
test@Lenovo:~$
From all that you need to know three things:
Linger: no
means your login session(s) will be terminated on logout(or ending your session by other means) and as a result all the above listed services, slices, scopes and other units will be stopped, killed, destroyed or "abandoned".
session-455.scope
is where user run services will live(not really but rather grouped by reference and governed ... in other words that "scope" is actually(among other things) a definition of what/how those services can "see" or can "be seen").
[email protected]
is what creates your user run-time environment that is essential for userspace applications/programs to run.
Let's zoom-in on session-455.scope
:
test@Lenovo:~$ systemctl --no-pager status session-455.scope
● session-455.scope - Session 455 of User test
Loaded: loaded (/run/systemd/transient/session-455.scope; transient)
Transient: yes
Active: active (running) since Tue 2023-06-06 18:22:53 +03; 23min ago
Tasks: 4
Memory: 3.5M
CPU: 166ms
CGroup: /user.slice/user-1004.slice/session-455.scope
├─60990 "sshd: test [priv]" "" "" ""
├─61052 "sshd: test@pts/2" "" "" "" ""
├─61055 -bash
└─61470 systemctl --no-pager status session-455.scope
test@Lenovo:~$
Notice how it reports as active (running)
... These are your current running user processes ... So, let's add one more then suspend it with Ctrl + z:
test@Lenovo:~$ vim
[1]+ Stopped vim
test@Lenovo:~$
And check the user scope unit again:
test@Lenovo:~$ systemctl --no-pager status session-455.scope
● session-455.scope - Session 455 of User test
Loaded: loaded (/run/systemd/transient/session-455.scope; transient)
Transient: yes
Active: active (running) since Tue 2023-06-06 18:22:53 +03; 28min ago
Tasks: 6
Memory: 10.3M
CPU: 275ms
CGroup: /user.slice/user-1004.slice/session-455.scope
├─60990 "sshd: test [priv]" "" "" ""
├─61052 "sshd: test@pts/2" "" "" "" ""
├─61055 -bash
├─61515 vim
└─61537 systemctl --no-pager status session-455.scope
test@Lenovo:~$
Nothing dramatic ... Just the new user process is now added to the scope.
Let's now run one more process, but with sudo
then suspend it:
test@Lenovo:~$ sudo vim
[sudo] password for test:
[2]+ Stopped sudo vim
test@Lenovo:~$
And check again:
test@Lenovo:~$ systemctl --no-pager status session-455.scope
● session-455.scope - Session 455 of User test
Loaded: loaded (/run/systemd/transient/session-455.scope; transient)
Transient: yes
Active: active (running) since Tue 2023-06-06 18:22:53 +03; 34min ago
Tasks: 10
Memory: 18.7M
CPU: 432ms
CGroup: /user.slice/user-1004.slice/session-455.scope
├─60990 "sshd: test [priv]" "" "" ""
├─61052 "sshd: test@pts/2" "" "" "" ""
├─61055 -bash
├─61515 vim
├─61610 sudo vim
├─61611 sudo vim
├─61612 vim
└─61637 systemctl --no-pager status session-455.scope
يونيو 06 18:56:27 Lenovo sudo[61610]: test : TTY=pts/2 ; PWD=/home/test ; USER=root ; COMMAND=/usr/bin/vim
يونيو 06 18:56:27 Lenovo sudo[61610]: pam_unix(sudo:session): session opened for user root(uid=0) by test(uid=1004)
test@Lenovo:~$
Now, something dramatic happened ... A new user session is opened for root
(technically a sudo
session) ... This session is opened from within your user scope, but in reality it's somebody else's(i.e. the user root
) session ... Now your user's scope is no more entirely yours alone and therefore tied with that user's session as well (waiting for it to close to be all yours again) ... In optimal situations, that root
session should close after your user application i.e. vim
exits ... But, what if situations are not optimal ... Let's see how our processes look:
test@Lenovo:~$ ps -ejf | { head -n 1; grep vim; }
UID PID PPID PGID SID C STIME TTY TIME CMD
test 61515 61055 61515 61055 0 18:49 pts/2 00:00:00 vim
root 61610 61055 61610 61055 0 18:56 pts/2 00:00:00 sudo vim
root 61611 61610 61611 61611 0 18:56 pts/3 00:00:00 sudo vim
root 61612 61611 61612 61611 0 18:56 pts/3 00:00:00 vim
test@Lenovo:~$
Then just kill
the ssh
session and give it the SIGHUP
:
test@Lenovo:~$ pkill --signal SIGHUP "ssh"
pkill: killing pid 1782 failedConnection to localhost closed by remote host.
Connection to localhost closed.
ubuntu@Lenovo:~$
Then, connect and login back again to see what happened:
ubuntu@Lenovo:~$ ssh test@localhost
test@localhost's password:
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.19.0-43-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Expanded Security Maintenance for Applications is not enabled.
9 updates can be applied immediately.
To see these additional updates run: apt list --upgradable
21 additional security updates can be applied with ESM Apps.
Learn more about enabling ESM Apps service at https://ubuntu.com/esm
Last login: Tue Jun 6 18:22:53 2023 from 127.0.0.1
test@Lenovo:~$ loginctl user-status --no-pager test
test (1004)
Since: Tue 2023-06-06 18:22:53 +03; 1h 2min ago
State: active
Sessions: *479 455
Linger: no
Unit: user-1004.slice
├─session-455.scope
│ ├─61610 sudo vim
│ ├─61611 sudo vim
│ └─61612 vim
├─session-479.scope
│ ├─62030 "sshd: test [priv]" "" "" ""
│ ├─62107 "sshd: test@pts/4" "" "" "" ""
│ ├─62108 -bash
│ └─62137 loginctl user-status --no-pager test
└─[email protected]
├─app.slice
│ ├─dbus.service
│ │ ├─61024 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
│ │ ├─61097 /usr/libexec/goa-daemon
│ │ └─61104 /usr/libexec/goa-identity-service
│ ├─gvfs-afc-volume-monitor.service
│ │ └─61084 /usr/libexec/gvfs-afc-volume-monitor
│ ├─gvfs-daemon.service
│ │ └─61037 /usr/libexec/gvfsd
│ ├─gvfs-goa-volume-monitor.service
│ │ └─61093 /usr/libexec/gvfs-goa-volume-monitor
│ ├─gvfs-gphoto2-volume-monitor.service
│ │ └─61080 /usr/libexec/gvfs-gphoto2-volume-monitor
│ ├─gvfs-mtp-volume-monitor.service
│ │ └─61089 /usr/libexec/gvfs-mtp-volume-monitor
│ └─gvfs-udisks2-volume-monitor.service
│ └─61075 /usr/libexec/gvfs-udisks2-volume-monitor
├─background.slice
│ └─tracker-miner-fs-3.service
│ └─61061 /usr/libexec/tracker-miner-fs-3
├─init.scope
│ ├─60995 /lib/systemd/systemd --user
│ └─60996 "(sd-pam)"
└─session.slice
├─pipewire-media-session.service
│ └─61005 /usr/bin/pipewire-media-session
├─pipewire-pulse.service
│ └─61006 /usr/bin/pipewire-pulse
└─pipewire.service
└─61004 /usr/bin/pipewire
يونيو 06 18:22:53 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.gtk.vfs.GoaVolumeMonitor'
يونيو 06 18:22:53 Lenovo systemd[60995]: Started Virtual filesystem service - GNOME Online Accounts monitor.
يونيو 06 18:22:53 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.gnome.Identity'
يونيو 06 18:22:54 Lenovo dbus-daemon[61024]: [session uid=1004 pid=61024] Successfully activated service 'org.freedesktop.Tracker3.Miner.Files'
يونيو 06 18:22:54 Lenovo systemd[60995]: Started Tracker file system data miner.
يونيو 06 18:22:54 Lenovo systemd[60995]: Started Tracker metadata extractor.
يونيو 06 18:22:54 Lenovo systemd[60995]: Reached target Main User Target.
يونيو 06 18:22:54 Lenovo systemd[60995]: Startup finished in 726ms.
يونيو 06 18:56:27 Lenovo sudo[61610]: test : TTY=pts/2 ; PWD=/home/test ; USER=root ; COMMAND=/usr/bin/vim
يونيو 06 18:56:27 Lenovo sudo[61610]: pam_unix(sudo:session): session opened for user root(uid=0) by test(uid=1004)
test@Lenovo:~$
The user session is still alive(see the login time) and we still have the user scope session-455.scope
:
test@Lenovo:~$ systemctl --no-pager status session-455.scope
● session-455.scope - Session 455 of User test
Loaded: loaded (/run/systemd/transient/session-455.scope; transient)
Transient: yes
Active: active (abandoned) since Tue 2023-06-06 18:22:53 +03; 1h 5min ago
Tasks: 4
Memory: 8.5M
CPU: 1.385s
CGroup: /user.slice/user-1004.slice/session-455.scope
├─61610 sudo vim
├─61611 sudo vim
└─61612 vim
يونيو 06 18:56:27 Lenovo sudo[61610]: test : TTY=pts/2 ; PWD=/home/test ; USER=root ; COMMAND=/usr/bin/vim
يونيو 06 18:56:27 Lenovo sudo[61610]: pam_unix(sudo:session): session opened for user root(uid=0) by test(uid=1004)
test@Lenovo:~$
With processes that we recognize and still reports as Active
, but now with (abandoned)
instead of (running)
... That's when your user session lost ultimate control on that scope ... KillUserProcesses=true
is for that type of situation i.e. "abandoned" processes or entire scopes as clearly stated in man logind.conf
:
KillUserProcesses=
Takes a boolean argument. Configures whether the processes of a user should be killed
when the user logs out. If true, the scope unit corresponding to the session and all
processes inside that scope will be terminated. If false, the scope is "abandoned",
see systemd.scope(5), and processes are not killed. Defaults to "no", but see the
options KillOnlyUsers= and KillExcludeUsers= below ...
KillExcludeUsers=root
would save an "abandoned" .scope
unit under root
login session and not yours … It works on whole user scope units and not on single user processes unless those processes are running under the user main session without a scope unit.
Let's have a closer look at our processes again:
test@Lenovo:~$ ps -ejf | { head -n 1; grep vim; }
UID PID PPID PGID SID C STIME TTY TIME CMD
root 61610 1 61610 61055 0 18:56 ? 00:00:00 sudo vim
root 61611 61610 61611 61611 0 18:56 ? 00:00:00 sudo vim
root 61612 61611 61612 61611 0 18:56 ? 00:00:00 vim
test@Lenovo:~$
voila.