Marcio Cunha

How to Configure SSH Keys for Passwordless Access: Cryptography and Practical Security

Learn how to replace vulnerable passwords with cryptographic key pairs for remote access. A practical guide to configuring secure SSH authentication on Linux servers.

Marcio Cunha12 min
Also available in:EspañolPortuguês
Summary
  • Cryptographic key authentication eliminates the risk of brute-force attacks against textual passwords on exposed servers.
  • The key pair consists of a private key kept in absolute secrecy locally and a public key hosted on the remote server.
  • Modern algorithms like Ed25519 offer stronger mathematical security and superior performance compared to traditional RSA.
  • Disabling password login in the server configuration file closes the primary entry point for automated intrusions.
  • Using an SSH agent temporarily stores the private key in RAM, removing the need to repeatedly type master passphrases.

The Fundamental Problem of Passwords in Remote Servers

When managing remote cloud servers or local network computers, the traditional access method relies on manually typed usernames and passwords. In practice, this means any attacker on the internet can attempt to guess your password billions of times per second using automated scripts. This vulnerability, known as a brute-force attack, compromises thousands of infrastructures worldwide every day. Modern systems engineering solves this problem by replacing human passwords with mathematical cryptographic keys that are impossible to guess through trial and error.

The technology enabling this secure communication is SSH, which stands for Secure Shell, acting as an armored digital tunnel between your computer and the target server. Inside this tunnel, all data traffic, commands, and passwords flow completely encrypted, preventing anyone intercepting the network from reading the contents. However, while encryption secures the path, password authentication remains the weakest link. This is precisely where cryptographic key pairs come into play, radically changing the security paradigm by requiring mathematical proof of identity instead of a memorized secret word.

How Cryptographic Keys Work in Practice

The concept of SSH keys relies on asymmetric cryptography, a method using two different yet logically interconnected mathematical keys. To understand it simply, think of them as a padlock and its corresponding physical key. The padlock, known as the public key, can be distributed openly and installed on as many servers as you wish. Meanwhile, the physical key, known as the private key, is kept with maximum security strictly on your personal computer, far from any curious eyes.

When you attempt to connect to the remote server, an invisible mathematical challenge takes place behind the scenes. The server uses your installed padlock to lock a random secret message and sends it back to your computer. Only your computer, holding the correct physical key, can unlock and read this message to prove its identity. In practice, this means the server never gets to know or store your private key, eliminating leak risks if the server gets breached. If someone steals the public key from the server, they cannot do anything, as the padlock alone opens no doors.

Generating Your First Key Pair with Ed25519

To put theory into practice, the first step is generating your own cryptographic key pair using your operating system terminal. Historically, the standard algorithm used was RSA, but modern standards recommend using Ed25519, which offers greater mathematical security using much smaller keys and extremely fast processing. In practice, opening the terminal and typing the generation command creates the necessary files inside the hidden directory of your user profile.

Run the following command in your terminal:

ssh-keygen -t ed25519 -C '[email protected]'

Running this line prompts the system to ask where to save the file and whether you want to protect the private key with an additional password called a passphrase. Adding a passphrase to your private key is an excellent defense-in-depth practice, ensuring that even if someone physically steals your unlocked laptop, they still need the password to use the key. After confirming, the terminal generates two files in the .ssh folder: the extensionless file containing your private key and the .pub file containing your public key.

Copying the Public Key to the Remote Server

With the key pair generated locally, the next operational step involves transferring the public key (the padlock) to the remote server where you want to connect without a password. There is an official tool called ssh-copy-id that automates this process cleanly and securely, avoiding manual errors of copying and pasting long text blocks through the terminal. In practice, this tool connects to the server using your current password one last time and injects your public key into the remote system's authorization file.

The standard command to perform this copy is:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_address

In practice, what this command does behind the scenes is access the remote user's hidden .ssh folder and append your public key content into a file named authorized_keys. Each line in this file represents a padlock authorized to enter the server. If you manage multiple servers, simply repeat this same command for each target machine. Once completed, you can test access by typing the usual SSH connection command, observing that the server opens the session instantly without requesting any textual password.

Comparative table of the main SSH key algorithms available in the current ecosystem:

AlgorithmKey SizePerformanceCurrent Recommendation
RSA2048 to 4096 bitsSlowerLegacy (compatibility)
ECDSA256 to 521 bitsFastStandard alternative
Ed25519256 bits fixedExcellentHighly recommended

Hardening the Server by Disabling Password Login

Configuring SSH keys is only half the job; true security is only achieved when you explicitly forbid the server from accepting traditional passwords. As long as password login remains enabled, the server remains vulnerable to brute-force attacks whether you use keys or not. In practice, hardening server configuration means locking the front door and leaving only the encrypted hatch open for those possessing the correct credential.

To make this change, you must edit the main SSH service configuration file on the remote server using a text editor with administrative privileges. The file is typically located at /etc/ssh/sshd_config. Look for the following directives and change their values according to the secure standard below:

PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin prohibit-password

After saving the changes to the configuration file, it is crucial to restart the server's SSH service (using commands like systemctl restart ssh) before closing your current terminal session. This precaution ensures that if you made any typing error in the configuration, your active session remains open so you can fix the problem without losing definitive access to the remote machine.

Final Considerations on Best Practices and Access Management

The transition to SSH key-based authentication represents a watershed in the operational security of any technological infrastructure. By completely eliminating textual passwords, we remove the most exploited attack vector by cybercriminals in corporate and personal environments. However, continuous security demands discipline in managing private keys. Never share your private key with third parties, always store them with restricted permissions in the file system, and perform periodic audits on your servers' authorized_keys file to remove keys of former team members.

In short, mastering SSH key configuration and remote service hardening empowers developers and system administrators to build resilient and reliable environments. Automating daily tasks and cloud deployment scripts becomes infinitely smoother and safer when integrated into this cryptographic architecture. Adopting these practices from the start of any project guarantees a solid foundation to scale operations with peace of mind and non-negotiable technical robustness.