SSH: Disable Password Login and Secure Your Server
Turning off SSH password login is one of the most dangerous settings on a VPS if you do it in the wrong order. There is only one safe sequence: first set up key login and check that it works, and only then disable the password. Break that order and you can lose access to the server for good, unless your hosting provider gives you console access that bypasses SSH.
This article follows the safe order: keys and testing first, and only then switching the password off.
Why turn off SSH password login
Any VPS with an open SSH port is scanned by bots around the clock, trying logins and passwords. Even a strong password can in theory be guessed with enough attempts, and a weak one falls quickly. An SSH key works differently: it is a pair made of a private and a public part. The private part is never sent over the network, so it cannot be intercepted or brute-forced in any reasonable time.
Once you switch to keys and turn the password off, password-guessing attacks against your server become impossible: there is simply nothing left to guess.
Step 1: Generate an SSH key
The key is created on your local computer, not on the server. If you already have one, skip this step. You can check with ls ~/.ssh/.
Create a new key pair:
ssh-keygen -t ed25519 -C "my-work-laptop"
The ed25519 algorithm is the modern standard: it is faster than the older RSA and considered at least as secure. The -C flag adds a comment to the key, which helps you tell keys apart if you have several.
The tool asks where to save the key. Just press Enter to accept the default path. Then it asks for a passphrase for the key itself. This is an extra layer of protection: if the key file is stolen, it is useless without the phrase. A passphrase makes sense on a workstation; for unattended automation, keys are sometimes created without one.
When the command finishes, two files appear in ~/.ssh/: id_ed25519 is the private key, which stays on your computer and is never given to anyone, and id_ed25519.pub is the public key, which you can freely copy to any server.
Step 2: Copy the public key to the server
The easiest way is a utility that creates the needed files and permissions on the server for you:
ssh-copy-id user@server_address
It asks for the server password one last time, copies the public key and sets all the required permissions automatically.
If ssh-copy-id is not available, you can get the same result by hand:
cat ~/.ssh/id_ed25519.pub | ssh user@server_address "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Step 3: Check the permissions on the server
This step is critical and often forgotten. SSH on the server checks the permissions of the authorized_keys file and the .ssh directory. If they are too open, SSH ignores the file completely for security reasons, even though your key is in it. This is the most common cause of the puzzling situation “the key is added, but the server still asks for a password”.
Log in to the server with the password one last time and run:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Mode 700 on the directory means only the owner can access it. Mode 600 on the file means only the owner can read and change it, and nobody else.
Step 4: Check that key login works
Before changing anything in the SSH configuration on the server, make sure key login really works. Open a new terminal window without closing the current session and try to connect:
ssh user@server_address
If you connect without a password prompt, the key is set up correctly and you can move on. If the system still asks for a password, do not go further: go back to the permissions step and check everything again.
Step 5: Back up the configuration before changing it
Before you edit the SSH daemon configuration file, save a copy. That lets you roll back quickly if something goes wrong:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
Step 6: Turn off the password in the configuration
Only now, when key login is tested and working, can you edit the SSH daemon configuration:
sudo nano /etc/ssh/sshd_config
Find or add these parameters:
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PasswordAuthentication no disables password login completely. PubkeyAuthentication yes explicitly allows key login. It is usually the default already, but it is useful to state it in the configuration so you do not depend on defaults that differ between SSH versions.
Root access over SSH has its own parameter:
PermitRootLogin prohibit-password
The value prohibit-password means root can connect only with a key, not a password. It is a compromise between convenience and security. If you do not need root over SSH at all, use the stricter PermitRootLogin no.
Need a VPS to set up secure SSH access? Promo code WIKI15 takes 15% off a VPS order at UFO.Hosting.
Step 7: Check the configuration before applying it
Before you restart the SSH daemon, always check the syntax of the changed file. If the configuration has an error, the daemon may fail to start after the restart, which is the same as losing access to the server:
sudo sshd -t
If the command prints nothing, the syntax is correct and you can continue. If it prints an error, the message points at the problem line. Fix it and run the check again.
Step 8: Apply the changes
Restart the SSH daemon to apply the new configuration:
sudo systemctl restart sshd
On some Ubuntu systems the service has a different name:
sudo systemctl restart ssh
Step 9: Final check in a new window
This is the last and most important safety check. After restarting the SSH daemon, do not close your current session. Open a new, separate terminal window and try to connect again:
ssh user@server_address
If the connection succeeds, the configuration was applied correctly and only now can you close the original session. If the new connection fails, you still have the original session open, and you can use it to roll back from the backup:
sudo cp /etc/ssh/sshd_config.backup /etc/ssh/sshd_config
sudo systemctl restart sshd
Extra hardening
With the password off, you can tighten the SSH configuration further with a few more parameters in the same file, /etc/ssh/sshd_config.
Limiting the number of authentication attempts per connection makes automated attacks less effective:
MaxAuthTries 3
Limiting the time allowed to authenticate after the connection is made:
LoginGraceTime 30
An allow-list of users who may log in over SSH. All other system users will not be able to connect at all, even if they learn a password or get hold of someone else’s key:
AllowUsers your_login
After adding any of these, repeat the steps of checking the configuration, restarting and connecting in a new window. The procedure is the same whatever you change.
Changing the default SSH port
Moving SSH from port 22 to a non-standard one reduces the number of automated attacks from bots that scan the internet for the default port. On its own it is not real protection, and it should be used only in addition to turning off the password, never instead of it.
Port 2222
Important: before you restart the SSH daemon with the new port, open that port in the server firewall. Otherwise, after the restart there will be nowhere to connect:
sudo ufw allow 2222/tcp
After changing the port, connect with the number given explicitly:
ssh -p 2222 user@server_address
Frequently asked questions
What happens if I turn off the SSH password before testing the key?
If the key is set up wrongly or the permissions on authorized_keys are incorrect, and the password is already off, you cannot get into the server any way at all. In that case you can restore access only through your hosting provider’s console, which bypasses SSH, if one is offered, or by reinstalling the server completely. That is why testing key login in a new terminal window must happen strictly before you disable the password, not after.
Why does SSH still ask for a password when the key is already added?
The most common reason is wrong permissions on ~/.ssh/authorized_keys or on the ~/.ssh directory on the server. For security reasons SSH ignores the keys in the file completely if the permissions are too open. Fix it with chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
What is the difference between PermitRootLogin no and prohibit-password?
PermitRootLogin no forbids root login over SSH in any way. PermitRootLogin prohibit-password allows root login only with a key and forbids password login for that user. The second option is more convenient if you need administrative access as root while still being protected from password guessing.
Should I change the SSH port for security?
Changing the port reduces the number of automated scans from bots that only check the default port 22, but it does not protect against a targeted attack: an attacker can scan all the ports of a server in seconds. Real protection comes from turning off password login. Changing the port is an extra, optional measure on top.
How do I get back in if I have locked myself out?
If the original SSH session is still open, use it to roll the configuration back from the backup. If the session is already closed and you cannot connect any way, the only option is console access through your hosting provider’s control panel, if it offers that outside SSH.
Official documentation: man.openbsd.org/sshd_config
Before you experiment with SSH settings on a production server, make sure you have a spare way to recover access. At UFO.Hosting every VPS comes with web console access in the client area. It works independently of SSH and lets you recover access even after a configuration mistake.
Related
All articles
Nextcloud on a VPS: your own cloud with Docker Compose
Nextcloud is a cloud you own: files synced to your phone and computer, a calendar, contacts, and share-by-link. On a VPS you run it with Docker Compose as five containers: Nextcloud itself, a PostgreSQL database, a Redis cache, a container…
RDP for two users on Windows Server 2016-2025: setup guide
By default, Windows Server 2016, 2019, 2022 and 2025 let two administrators connect over RDP at the same time, and you don’t need to buy Remote Desktop licenses for it. Below: how this works, how resources are shared, and a…