From: Shannon Sterz <s.sterz@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH cluster v2 2/2] cluster: setup: match PermitRootLogin option more precisely
Date: Thu, 19 Nov 2026 12:23:18 +0100 [thread overview]
Message-ID: <20261119112318.118471-3-s.sterz@proxmox.com> (raw)
In-Reply-To: <20261119112318.118471-1-s.sterz@proxmox.com>
`setup_sshd_config()` tries to find a line relating to the setting
`PermitRootLogin` in the sshd config. the previous regex did not match
certain cases (e.g., " PermitRootLogin yes"; " # PermitRootLogin no").
if nothing is matched the function added a "PermitRootLogin yes" line
to the end of the file.
this would allow for cases where " PermitRootLogin no" and
"PermitRootLogin yes" are present in the same file. sshd applies the
first option it finds, so root login would be disabled. without
"PermitRootLogin yes" set any cluster join and certain functionality,
such as zfs replication, would break.
adjust the regex so that the "PermitRootLogin" option is picked up in
those cases too. also match case insensitively and allow statements
such as `permitrootlogin=yes` as OpenSSH also accepts them.
if a `Match` statement is present, split off the global section first.
check it for a global `PermitRootLogin` option and operate on that
section only. so a missing `PermitRootLogin` option is added at the
end of the global section before the first `Match` statement. this way
it cannot unintentionally end up as part of a `Match` block.
concatenate the global section with the rest of the file again before
writing it back out.
further, the early return is extended to only care about the first
occurrence of the `PermitRootLogin` option as that's the one OpenSSH
cares about.
not checking included files is intentional. an included file can
override the global `PermitRootLogin` option as well as adding a
`Match` block that restricts root access. this gives admins a
convenient way to restrict root logins to, for example, specific
networks or addresses.
Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
Notes:
note that by default on our installs the setting is "PermitRootLogin
yes". so this scenario could only happen if a user manually adjusts
it. however, setting "PermitRootLogin no" is often recommended so
someone accidentally adding a space in front of that line is not
impossible.
Changelog
---------
* also handle `Match` statements
src/PVE/Cluster/Setup.pm | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/PVE/Cluster/Setup.pm b/src/PVE/Cluster/Setup.pm
index 2550f70..f0ba401 100644
--- a/src/PVE/Cluster/Setup.pm
+++ b/src/PVE/Cluster/Setup.pm
@@ -150,13 +150,23 @@ sub setup_sshd_config {
my $conf = PVE::Tools::file_get_contents($ssh_system_server_config);
- return if $conf =~ m/^PermitRootLogin\s+yes\s*$/m;
+ # split off global section from the rest of config. the global section is everything before the
+ # first `Match` (`man sshd_config(5)`):
+ #
+ # > Introduces a conditional block. If all of the criteria on the Match line are satisfied, the
+ # > keywords on the following lines override those set in the global section of the config file,
+ # > until either another Match line or the end of the file.
+ my ($global, $separator, $rest) = split(/^(\h*Match(?:\h|=))/mi, $conf, 2);
+ $rest = defined($separator) ? $separator . $rest : '';
- if ($conf !~ s/^#?PermitRootLogin.*$/PermitRootLogin yes/m) {
- chomp $conf;
- $conf .= "\nPermitRootLogin yes\n";
+ return if $global =~ m/^\h*PermitRootLogin(?:\h*=\h*|\h+)(\S+)/mi && lc($1) eq 'yes';
+
+ if ($global !~ s/^(?:\h*#)*\h*PermitRootLogin.*$/PermitRootLogin yes/mi) {
+ chomp $global;
+ $global .= "\nPermitRootLogin yes\n";
}
+ $conf = $global . $rest;
my $mode = (stat($ssh_system_server_config))[2] & 07777;
PVE::Tools::file_set_contents($ssh_system_server_config, $conf, $mode);
--
2.47.3
prev parent reply other threads:[~2026-11-19 11:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 11:23 [PATCH cluster v2 0/2] Refine setup_sshd_config() Shannon Sterz
2026-11-19 11:23 ` [PATCH cluster v2 1/2] cluster: setup: keep file permissions for sshd config Shannon Sterz
2026-11-19 11:23 ` Shannon Sterz [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119112318.118471-3-s.sterz@proxmox.com \
--to=s.sterz@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox