Rocky Linux 10 Tutorial / SSH keys, effective login policy and lockout recovery

Secure Shell (SSH) public-key authentication on Rocky Linux 10 allows login using the client's private key and the public key registered to the server account. First, confirm that a new connection works with the key, and while keeping the recovery console and existing sessions, change the password login and root access policies to reduce the risk of losing access.

Prepare the server address, account, and recovery path.

SSH (Secure Shell) is a protocol that connects to another computer's shell over an encrypted connection. This article uses a Rocky Linux 10.2 VM for which the general account and sudo practice has been completed and a Windows OpenSSH client. The connection account is lfstudent and the VirtualBox console must also be accessible.

In this NAT practice VM, the host's localhost port was connected to the VM's SSH port. Check the NAT adapter's advanced settings and port forwarding in the network settings of the relevant VM in VirtualBox. Do not leave the host IP blank and specify it as 127.0.0.1, and do not reuse a host port that is already in use.

Location Practice values in this article
Host IP·Port 127.0.0.1, TCP 22222
Guest port TCP 22
Connection target to be used in Windows lfstudent@127.0.0.1, port 22222
Connections seen from the guest This VM is observed as client 10.0.2.2 → server 10.0.2.15:22

Port 22 inside the VM and port 22222 on the host are ports in different locations. When connecting to an actual LAN or cloud server, you need to change it to the server address, SSH port, and network access policy. This connection is a practice restricted to coming from the Windows host only and is not an example of configuring the full access control of an internet-facing server.

Check packages and services on the server console.

sudo dnf install openssh-server
sudo systemctl enable --now sshd
systemctl is-active sshd
sudo ss -lntp
sudo firewall-cmd --list-services

In the source lab’s minimal installation, OpenSSH server was already installed and sshd was active. Its records confirmed a listener on port 22 and the ssh service allowed in the active zone. If connection is blocked, investigate by distinguishing NAT, cloud firewall, and the guest's actual active zone. We do not disable firewalld or SELinux to quickly eliminate problems.

Generate a key on the client and store your private key.

Register the public key on the server that allows access, and keep the private key on the client. The file ending with .pub is the public key, and the matching file without an extension is the private key. We do not upload the private key to WordPress, mail, or the server's authorized_keys.

The following is an example of generating a separate practice key in Windows PowerShell. Name it separately so as not to overwrite the existing default key.

Get-Command ssh.exe, ssh-keygen.exe
$Key = Join-Path $env:USERPROFILE '.ssh/rocky10-lab'
if ((Test-Path -LiteralPath $Key) -or (Test-Path -LiteralPath "$Key.pub")) { throw 'A key file already exists. Inspect it or choose a different name.' }
New-Item -ItemType Directory -Force -Path (Split-Path $Key) | Out-Null
ssh-keygen -t ed25519 -f $Key -C 'rocky10-lab'
ssh-keygen -lf "$Key.pub"

The passphrase requested during key generation protects the private key through encryption; it is different from the server account password. For operational keys, follow your organization’s passphrase, key storage and SSH agent policies. The source lesson’s November 12, 2026 automated connection check used a temporary key dedicated to an isolated VM without a passphrase, so this is not interpreted as a test of entering a private key passphrase or operating an agent.

This VM had FIPS mode disabled and could use Ed25519 keys. In organizational encryption policies or FIPS environments, always check the allowed algorithms first and do not relax system security policies to match examples. You can check key types and file/passphrase options in OpenSSH ssh-keygen description.

Add the public key to the server account and check permissions.

Read only the public key in PowerShell.

Get-Content -LiteralPath "$Key.pub"

Copy a single line of the public key starting with ssh-ed25519. After logging in with lfstudent through existing management access or VirtualBox console, execute the following. If the same public key is already registered, do not add it again.

whoami
umask 077
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
printf '\n' >> ~/.ssh/authorized_keys
cat >> ~/.ssh/authorized_keys

The last command waits for input. Paste the entire copied public key on one line, press Enter, and then finish the input with Ctrl+D. The additional empty line at the end is to prevent the last key in the existing file from being concatenated with the new key, and it is not a command to erase the existing content.

sudo restorecon -Rv ~/.ssh
ls -ld ~/.ssh
ls -l ~/.ssh/authorized_keys
ssh-keygen -lf ~/.ssh/authorized_keys

The directory should be 700, authorized_keys 600, and ensure the owner is the account that will log in. The user public key fingerprint displayed on the server should match the client’s "$Key.pub" fingerprint. Instead of setting the SELinux context to an arbitrary value, apply a context that matches the policy with restorecon. In this VM, the two paths were ssh_home_t.

Rocky Linux SSH Key Guide also covers key registration and directory permissions. This article proceeds under the condition of using a regular account’s key login and sudo instead of root remote login.

Compare the host key fingerprint and test the first key login.

Separate from the user public key, the server has a host key that identifies itself. Do not automatically approve the server fingerprint displayed on first login; compare it with the fingerprint read from a trusted console.

ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

Execute the above command in the server console. Then, in a new Windows PowerShell window, attempt only public key authentication. If it is not the same window, re-specify the $Key path set earlier.

$Key = Join-Path $env:USERPROFILE '.ssh/rocky10-lab'
ssh -p 22222 -i $Key -o IdentitiesOnly=yes -o PreferredAuthentications=publickey lfstudent@127.0.0.1

If asked to trust for the first time, verify that the host, port, key type, and the full SHA256 fingerprint match the console values before approving. If they do not match, investigate the connection target. Do not skip the verification step by attaching StrictHostKeyChecking=no or deleting the existing known_hosts entirely.

Run the following in the new logged-in connection.

whoami
id -nG
sudo -v
sudo whoami

Sequentially verify regular user lfstudent, wheel group, sudo authentication, and root command execution. Just because key login is successful does not automatically fulfill sudo authentication and permission policies. Do not block password login to the server before completing this new connection verification.

After backup, write a public key-only policy in a separate fragment.

Now maintain the existing SSH session and VirtualBox console. This step is an example of a policy that allows only public key authentication on a newly installed practice VM. If your organization has multi-factor authentication or centralized authentication rules, do not replace them with the settings below.

The main configuration file in the source lab included /etc/ssh/sshd_config.d/*.conf at line 15, and the encryption policy file and the default distribution file used the 40·50 prefixes, respectively. According to OpenSSH server configuration explanation, generally, the first value read is used, and Include files are processed in lexical order. It is not determined by repeating the same option at the end of the file.

sudo grep -n Include /etc/ssh/sshd_config
sudo ls -l /etc/ssh/sshd_config.d
sudo mkdir -m 700 /root/lf-course-ssh-before-20261112
sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.d /root/lf-course-ssh-before-20261112/

If a message appears that the backup directory already exists, stop and check the existing work. Do not overwrite the backup with the same name. First, also check whether the next configuration file already exists.

sudo test ! -e /etc/ssh/sshd_config.d/00-linux-factory-lab.conf
echo $?

Write the new file below only if 0 appears and the corresponding file does not exist. If it is not 0 or an error occurred, do not overwrite it.

sudo tee /etc/ssh/sshd_config.d/00-linux-factory-lab.conf > /dev/null <<'EOF'
PubkeyAuthentication yes
AuthenticationMethods publickey
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
EOF
sudo chmod 600 /etc/ssh/sshd_config.d/00-linux-factory-lab.conf
sudo restorecon /etc/ssh/sshd_config.d/00-linux-factory-lab.conf

AuthenticationMethods publickey is a condition that requires public key authentication in this practice. Password authentication and keyboard-interactive authentication are turned off, and root remote login is blocked. Since sudo authentication for regular accounts is governed by a separate policy, setting this does not remove the need to enter the sudo password. The existing encryption policy file of the distribution is not modified.

After checking the syntax and effective settings, reload.

sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T -C user=lfstudent,host=10.0.2.2,addr=10.0.2.2 | grep -E '^(pubkeyauthentication|authenticationmethods|passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '

-t checks the configuration syntax and key-related aspects, while -T outputs the final value to be applied. The user, host, and address in -C are used for evaluating Match conditions, so they are compared with your own connection conditions. Here, the host refers to the connecting client's hostname, not the server name, and in this NAT exercise, a client address was used. Options can also be confirmed in OpenSSH sshd official documentation.

In this exercise, all five values must match expectations. If there are syntax errors or the values differ, resolve the cause before reloading.

pubkeyauthentication yes
authenticationmethods publickey
passwordauthentication no
kbdinteractiveauthentication no
permitrootlogin no
sudo systemctl reload sshd
systemctl is-active sshd

Without closing the existing session, execute the previous key connection command from another Windows window. You must verify that the normal account and sudo work in the new connection as well. In the source lab using Rocky Linux 10.2 and OpenSSH server 9.9p1, the existing session remained open during reload, and the new key connection from Windows OpenSSH 9.5p2 was also successful.

Connections with key usage disabled were rejected by Permission denied (publickey), and root login was also denied. Simply being aware that an existing connection is still active does not mean the new authentication configuration is functioning properly.

If the new connection fails, check the console for the cause and recovery path.

Symptoms Checks in order
Timeout · Connection Refused First, check the host port and NAT rules, whether sshd is running and listening, and the network · firewall path. Distinguish from key file issues.
Host Key Fingerprint Mismatch Compare whether the VM · address · port has changed with the console fingerprint. Do not hide just the warning.
Permission denied (publickey) Compare the connecting user and specified private key with the server's public key in one line · owner · permissions · SELinux context.
The value written in the file differs from the actual value Check the order of Include and other configuration fragments, Match conditions, and read the sshd -T -C result for the respective connection conditions.
Key login works but sudo fails Instead of SSH settings, check the group · sudo policy · authentication of the respective regular user.

Check sudo journalctl -u sshd -b --no-pager in an existing admin session or console that you can connect to. For more detailed client diagnostics, you can add -v to the same connection command, but before sharing the logs, hide unnecessary information such as username, host, and path.

If the connection failed due to the newly created fragment in this exercise, you can move only that fragment out of the Include target to restore the original policy. If a backup file for the target already exists below, use a different name, and do not delete the default files of the distribution entirely.

sudo mv -- /etc/ssh/sshd_config.d/00-linux-factory-lab.conf /root/lf-course-ssh-before-20261112/00-disabled.conf
sudo /usr/sbin/sshd -t
sudo systemctl reload sshd

If the syntax check fails, do not reload. In this VM, after confirming that the original effective policy returned using this restoration procedure, the fragment was returned to its original location, checked again, and reloaded, applying the public key-only policy finally. Only after confirming the new connection is normal should you close the previous session.