Hi everyone, in this post I’ll show you how to configure the SSH server on Debian 12 Server. SSH is the most common way to manage a Linux server remotely. With SSH, you can type commands on the server from a Windows laptop or PC without sitting in front of the server.
The SSH server that comes with Debian works right after installation, but its settings are still the defaults. It still listens on port 22, which is the port bots attack most often, password login is still enabled, and every user is allowed to try logging in. In this article we will secure SSH by logging in with an SSH key from Windows 11, changing the port, disabling root login, and rejecting password login.
This article continues from configuring repositories and sudo on Debian 12 Server. My lab uses a Debian 12 VM with IP 192.168.56.20 and a Windows 11 client VM with IP 192.168.56.30 on a VirtualBox Host-only network.
Check the SSH Server on Debian 12
Check the SSH service status

If you ticked SSH server during installation, the SSH service is already running. Check it with the following command.
systemctl status ssh --no-pagerYou can see that ssh.service is enabled and active (running), which means the SSH server is up and starts automatically at boot (number 1). On Debian the service is called ssh, while sshd is just an alias. If the service is not found, install it first with the following command. If you are asked for a password, enter your Debian user password.
sudo apt install -y openssh-serverCheck the port and default settings

systemctl is-enabled ssh.socket
sudo ss -tlnp | grep ssh
sudo sshd -T | grep -Ei '^(port|permitrootlogin|passwordauthentication|pubkeyauthentication) 'There are three important things in the output above. First, ssh.socket is disabled (number 1), which means Debian 12 does not use socket activation like newer Ubuntu releases, so the port can be set from the configuration file alone. Second, SSH listens on port 22 (number 2). Third, the defaults are that root may only log in with a key (without-password), key login is enabled, and password login is enabled too (number 3). The sshd -T command shows the values that are actually in effect, so it is more accurate than reading the configuration file.
Understand the sshd_config.d folder

grep -n 'Include' /etc/ssh/sshd_config
ls /etc/ssh/sshd_config.d/Line 12 of sshd_config contains Include /etc/ssh/sshd_config.d/*.conf (number 1). This means every file ending in .conf in that folder is read before the rest of sshd_config. In OpenSSH the first value read is the one that is used, so settings in the sshd_config.d folder override settings in sshd_config. That is why I do not edit the main file and create my own file in this folder instead. This approach is cleaner and safer when the SSH package gets updated.
Log In to SSH with a Key from Windows 11
Logging in with an SSH key is safer than a password, because the private key only exists on your computer and cannot be guessed. Windows 11 already includes the OpenSSH Client, so there is no need to install PuTTY.
Create an SSH key on Windows

Open PowerShell or Terminal on Windows (not on the server), then run the following command.
ssh-keygen -t ed25519 -C windows11-klienPress Enter to save it in the default location. When asked for a passphrase, you can fill one in so the key stays protected if your laptop is stolen, or press Enter twice to leave it empty. In my lab the passphrase is left empty. The result is two files (number 2): id_ed25519, the private key that must never be shared, and id_ed25519.pub, the public key that goes on the server. The ed25519 algorithm is shorter and faster than RSA.
Send the public key to the server

Windows does not have the ssh-copy-id command, so use the following command in Windows PowerShell. Replace the username and server IP with your own.
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh mamang@192.168.56.20 'mkdir -p ~/.ssh; chmod 700 ~/.ssh; cat >> ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys'Since this is the first connection to the server, you will be asked about the fingerprint. Type yes and press Enter (number 2). After that you will be asked for a password (number 3). Enter your Debian user password, and the public key will be added to the authorized_keys file on the server. This is the last time you need a password for SSH.
Note: The single quotes at the end of the command are intentional. If you use double quotes and && like in Linux tutorials, PowerShell 5.1 on Windows will reject it with the message The token && is not a valid statement separator.
Log in without a password

ssh mamang@192.168.56.20Now SSH logs you in right away without asking for a password (number 1). The authorized_keys file on the server also contains the public key named windows11-klien (number 2). Make sure this step works before moving on, because in the next section we will disable password login.
Secure the SSH Server Configuration
Create your own configuration file

Create a new file in the sshd_config.d folder with nano. The file name is up to you, as long as it ends in .conf.
sudo nano /etc/ssh/sshd_config.d/10-mamang.confFill it with the following configuration (number 1).
# Konfigurasi SSH Mamang Lab
Port 2222
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers mamang budiHere is what each line does.
Port 2222 : SSH moves from port 22 to 2222
PermitRootLogin no : the root user cannot log in over SSH
PasswordAuthentication no : password login is rejected, a key is required
PubkeyAuthentication yes : login with an SSH key is allowed
MaxAuthTries 3 : at most 3 attempts per connection
AllowUsers mamang budi : only these users may log in over SSHAdjust AllowUsers to match the usernames on your own server. If your user is not listed on this line, that user will not be able to log in even with the correct key. Save with Ctrl+O and Enter, then exit with Ctrl+X.
Test the configuration before restarting

To catch typos that would stop SSH from starting, test the configuration first.
sudo sshd -t && echo KONFIGURASI-OK
sudo sshd -T | grep -Ei '^(port|permitrootlogin|passwordauthentication|maxauthtries|allowusers) 'The text KONFIGURASI-OK appears (number 1), which means there are no syntax errors. The sshd -T output also shows that the new values have been picked up: port 2222, permitrootlogin no, and passwordauthentication no (number 2). If there is a mistake, sshd -t will show the line number that is wrong.
Restart SSH and check the new port

sudo systemctl restart ssh
sudo ss -tlnp | grep sshSSH now listens on port 2222 for both IPv4 and IPv6 (number 1). Port 22 is no longer used.
Warning: Do not close your active SSH session until you have logged in successfully with the new settings from another terminal window. Sessions that are already open are not dropped when ssh restarts, so you can use one to fix things if something goes wrong.
Testing from the Windows 11 Client
Port 22 is now refused

From Windows, try logging in as usual without specifying a port.
ssh mamang@192.168.56.20The result is Connection refused on port 22 (number 1). This proves SSH no longer accepts connections on the old port.
Password and root login are rejected

Also test whether password login and root login are really rejected.
ssh -o PubkeyAuthentication=no -p 2222 mamang@192.168.56.20
ssh -p 2222 root@192.168.56.20The first command forces SSH not to use a key so it can only use a password, and the result is Permission denied (publickey) (number 1). The server only offers the publickey method, so a password cannot be used at all. Logging in as root is also rejected (number 2).
Log in with a key on the new port

ssh -p 2222 mamang@192.168.56.20With the -p 2222 option, login succeeds without a password using the SSH key (number 1). The ss command inside the server also confirms that sshd is running on port 2222 (number 2).
My experience: So I don’t have to type -p 2222 every time, I usually create a config file on Windows at C:\Users\username\.ssh\config containing Host debian, HostName 192.168.56.20, User mamang, and Port 2222. After that, just type ssh debian.
Common Problems and How to Fix Them
Connection refused
This message appears when the SSH service is stopped or the port is wrong. Check with sudo systemctl status ssh and sudo ss -tlnp | grep ssh on the server console. If you changed the port, remember to add -p and the port number when logging in. If the server uses a firewall, open the new port before restarting SSH.
Permission denied (publickey)
This means the key on Windows does not match the contents of authorized_keys on the server, or the folder permissions are wrong. The ~/.ssh folder must be 700 and the authorized_keys file must be 600. Fix it from the server console with chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys. Also make sure your username is listed in AllowUsers.
Locked out after PasswordAuthentication no
If key login was not working yet but password login is already disabled, you cannot get in over SSH. Open the server console or the VirtualBox window, delete the file /etc/ssh/sshd_config.d/10-mamang.conf or change PasswordAuthentication to yes, then restart ssh. This is why key login must be tested before password login is disabled.
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
This message appears when the server has been reinstalled or the IP is now used by another server, so the fingerprint saved on Windows is different. If you are sure the server is the right one, remove the old fingerprint with the following command in Windows PowerShell, then log in again.
ssh-keygen -R 192.168.56.20Port changed but still 22
Check whether another file in /etc/ssh/sshd_config.d also contains a Port line, because the first value read is the one that is used. Run sudo sshd -T | grep port to see the port that is actually active.
At this point the SSH server on Debian Server 12 is secure: login only works with a key from Windows 11 on port 2222, and root can no longer log in. If you also use Ubuntu, similar steps are covered in the article on configuring the SSH server on Ubuntu. In the next article we will build a DNS server with BIND9 on Debian Server 12. If you run into an error while following the steps above, post the error message in the comments.












