Debian 13 Tutorial / Configure SSH Key Authentication Without Losing Access

To configure Secure Shell (SSH) key authentication on Debian 13, verify the server’s host key fingerprint, register your public key and test a separate key-authenticated connection. Restrict password and direct root logins only after that test succeeds. Keep a working session and VM console available while checking sshd syntax, effective policy and new connections.

Prepare the Connection Path and Recovery Console

Use the minimal Debian VM installed with the SSH server task. The example assumes that lfstudent is your existing sudo administrator. If your username differs, replace it consistently in every command. Review the users, groups and permissions exercise if you need to check the account’s sudo access.

For VirtualBox NAT, forward host address 127.0.0.1:22213 to guest TCP port 22. With the VM powered off, select it in VirtualBox Manager, open Settings → Network → Adapter 1, confirm NAT, then open Advanced → Port Forwarding. Add a TCP rule with Host IP 127.0.0.1, Host Port 22213, a blank Guest IP and Guest Port 22. Reuse an existing matching rule and check for host port conflicts. This loopback-bound rule allows access from the current host; 127.0.0.1 on another computer refers to that computer, not this VM.

First, check on the Debian console. If you have not installed openssh-server, run sudo apt update and sudo apt install openssh-server on the console and prepare the service.

systemctl is-active ssh
systemctl is-enabled ssh
ss -lnt | grep ':22 '
sudo /usr/sbin/sshd -t
sudo ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

Make sure the service is active and enabled at startup. sshd -t usually outputs nothing if successful. If there is an error, do not start changing the connection policy. The actual verification in this article was conducted between a Debian 13.7 VM and a Windows OpenSSH client.

Distinguish Server Host Keys from User Keys

Distinction Target to check Storage location and role
Server host key Whether it is the server you connected to Identity is verified using the server's /etc/ssh/ssh_host_* and the client's known_hosts.
User private key Check if the connecting user owns the key Stored on the client. Do not copy to the server or share with others.
User public key The public half matching the permitted private key Registered in .ssh/authorized_keys in the server user's home directory.

Initially connect from Windows PowerShell using the following. Since the key has not been registered yet, the server's user password may be required.

ssh -o HostKeyAlgorithms=ssh-ed25519 -p 22213 lfstudent@127.0.0.1

Compare the initially displayed SHA256 fingerprint with the ED25519 host key fingerprint checked earlier in the VM console before approving it. Do not type 'yes' just because a fingerprint appears on the screen. ssh-keyscan is a tool to fetch keys from the network, not a means to independently verify the server's identity.

If you reinstall the server or change the NAT target, the host key may differ. If you see a REMOTE HOST IDENTIFICATION HAS CHANGED warning, first check the console and the connection target. Only remove the entry in PowerShell using ssh-keygen -R "[127.0.0.1]:22213" and re-verify the fingerprint if you have confirmed that the change is legitimate. Do not delete the entire known_hosts without verification.

Create an Ed25519 User Key on Windows

Run the following command on Windows. Make sure there is no file with the same name before creating it. If you already have a private key in use, do not overwrite it and choose an appropriate new file name. If you cannot find the OpenSSH command, check whether the OpenSSH client is installed in Windows Optional Features.

New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.ssh" | Out-Null
Test-Path "$env:USERPROFILE\.ssh\debian13_lab"
Test-Path "$env:USERPROFILE\.ssh\debian13_lab.pub"

Create it if both results are False. The passphrase is a phrase that protects the use of the private key file and is different from the server account password. For keys used by humans, set a unique phrase that is difficult to guess and establish a recovery and storage policy.

ssh-keygen -t ed25519 -f "$env:USERPROFILE\.ssh\debian13_lab" -C "debian13-lab-admin"
Get-Content "$env:USERPROFILE\.ssh\debian13_lab.pub"

The generated debian13_lab file without an extension is the private key; debian13_lab.pub is the public key. Copy only the single public-key line beginning with ssh-ed25519 to the server. The ssh-keygen manual documents key creation and fingerprint options. Debian 13 no longer supports Digital Signature Algorithm (DSA) keys, as explained in the OpenSSH release-note changes; do not try to restore access by re-enabling obsolete ssh-dss keys.

Register the Public Key and Test a New Connection

Run the following inside Debian as lfstudent from the existing SSH session or console. Check that other users cannot write to your home directory. Before running the backup command, verify that ~/.ssh/authorized_keys.before-course07 does not already exist; if it does, choose a fresh backup filename and use it consistently. Preserve existing keys while preparing .ssh.

whoami
ls -ld ~
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
cp -a ~/.ssh/authorized_keys ~/.ssh/authorized_keys.before-course07
nano ~/.ssh/authorized_keys

If a backup file already exists, preserve it using a new name. In the editor, keep the existing normal key and append the new public key on a new line. The fact that the contents of the public key appear wrapped on the screen due to the width is different from actual line breaks in the file. One key should be on a single line. After saving, check the owner and permissions.

stat -c '%a %U %G %n' ~ ~/.ssh ~/.ssh/authorized_keys

Check that the owner of .ssh and authorized_keys is lfstudent, and the modes are 700 and 600 respectively. If an administrator created them on behalf, make sure no incorrect root-owned files remain. Do not make the server home writable by everyone or put private keys in authorized_keys.

Execute a connection in a new PowerShell window allowing only key authentication, leaving the existing session as is. Here, it is limited to prevent mistaking a success that automatically switches to password authentication as a key authentication success.

ssh -i "$env:USERPROFILE\.ssh\debian13_lab" -o IdentitiesOnly=yes -o PreferredAuthentications=publickey -o PasswordAuthentication=no -p 22213 lfstudent@127.0.0.1

A prompt for the private key's passphrase may appear, but it will not switch to server account password authentication. After connecting, check id -un and sudo -v on Debian. Since key authentication connections and sudo authentication are separate, even if SSH is key-only, sudo may require the user password. Do not disable password login until the new session's connection and administrative operations are verified.

Restrict Password and Root Logins and Check Effective Policy

Keep the console and working SSH session available. Run the first backup-directory command separately and continue only if it succeeds. Stop if either target-path check fails: do not overwrite an earlier backup or an existing /etc/ssh/sshd_config.d/00-lf-course.conf. This example adds a new configuration fragment only.

sudo test ! -e /var/backups/lf-debian13-07 && sudo install -d -m 700 /var/backups/lf-debian13-07
sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.d /var/backups/lf-debian13-07/
sudo test ! -e /etc/ssh/sshd_config.d/00-lf-course.conf
sudo nano /etc/ssh/sshd_config.d/00-lf-course.conf

Put the following four lines in the new file. These are sshd_config directives, shown as plain text rather than shell commands.

PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

PasswordAuthentication controls password authentication, and KbdInteractiveAuthentication controls the interactive authentication path. Do not apply these four lines as they are to an operational server using interactive authentication in multi-factor authentication; first check your organization's policy. PermitRootLogin no prevents direct SSH login as root and is separate from a regular administrator using sudo.

sudo chmod 600 /etc/ssh/sshd_config.d/00-lf-course.conf
sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T -C user=lfstudent,host=localhost,addr=10.0.2.2 | grep -E '^(pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '

If -t fails, fix the error before reloading. The -T option prints effective settings, while -C supplies connection conditions. In this VirtualBox NAT lab, the guest sees 10.0.2.2 as the client-side address. Replace the example address and Match conditions with those for your actual session, using the output of printf '%s\n' "$SSH_CONNECTION". If you use hostname matching, supply the actual client hostname condition too.

permitrootlogin no
pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication no

sshd_config manual explains that most settings use the value obtained first. A filename that sorts later, or a directive placed last, does not automatically take precedence. The result can vary depending on the Include location in the default Debian file, other fragment files, and Match conditions, so check that the -T result matches your intention.

Reload and Verify Another New Connection

If the syntax and valid settings pass, reload the configuration without stopping the service.

sudo systemctl reload ssh
systemctl is-active ssh

Once the service reports active, open another new connection with the earlier PowerShell key-only command. An existing live session does not prove that new authentication attempts work. The sshd manual documents configuration tests; a successful real connection verifies a separate part of the setup.

On a dedicated VM, we created and registered a new Ed25519 test key and verified each new connection before and after policy changes. We confirmed that an incorrect separate configuration file is rejected with -t, but did not apply it to the service. Automatic verification keys are managed differently from passphrase-protected keys kept for interactive human use, and after the exercise, we removed the registration and also verified that reconnection with that key was denied.

If connection fails, check sudo journalctl -u ssh -b --no-pager -n 50 on the server. Permission denied (publickey) indicates checking the account, key, file permissions, and allowed policy; Connection refused is for the service and port forwarding target; timeouts are primarily checked for path and firewall. The client's ssh -v detailed output is helpful for diagnostics, but review the username, path, and server information before sharing it with others.

Recover Access and Revoke Unneeded Keys

If only the newly added files in this exercise are the cause, remove them from a maintained normal session or the VM console. Accurately verify the deletion target and do not apply it to files created by other administrators.

sudo rm -- /etc/ssh/sshd_config.d/00-lf-course.conf
sudo /usr/sbin/sshd -t && sudo systemctl reload ssh
sudo /usr/sbin/sshd -T | grep -E '^(pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '

If you have also modified /etc/ssh/sshd_config or other configuration fragments, restore the changes by comparing them with the backup and pass them through -t again. If the SSH connection is completely broken, do not try repeatedly to fix it through the network; log in through the VM console. You should also check before starting the work whether you can use console authentication and sudo.

When revoking a test key, only remove that one public key line from the authorized_keys. Do not delete existing keys that multiple people are using together. You can only restore the original authorized_keys backup if no other valid keys have been added since the backup. If you lose a private key, simply changing the password does not remove access from that key, so it must also be revoked from the server's list of allowed keys.