SSH Key Authentication: A Guide to Secure Server Access
Stop using passwords for server access. Learn how to generate SSH keys, secure your connections, and simplify remote management using SSH config files.

Previously in this course, we covered Ownership and Chown: Managing Linux File Access and File Permissions Fundamentals: Decoding Linux Access Controls to secure the files on our system. Now that we understand how Linux governs access, it’s time to secure the gateway to our server: SSH.
Why SSH Keys Matter
Password-based authentication is the weakest link in your Linux server security strategy. Passwords can be guessed, brute-forced, or intercepted.
SSH key authentication uses asymmetric cryptography. You hold a "private key" that never leaves your machine, and you place the corresponding "public key" on the server. When you connect, the server challenges your machine to prove it holds the private key. This process is mathematically impossible to replicate with a password and effectively renders brute-force attacks useless.
Step 1: Generating Your SSH Key Pair
On your local machine, use ssh-keygen to generate a pair. We'll use the Ed25519 algorithm, which is modern, secure, and fast.
Bashssh-keygen -t ed25519 -C "your_email@example.com"
The terminal will ask you where to save the file. Press Enter to accept the default location (~/.ssh/id_ed25519).
Next, it will prompt for a passphrase. Do not skip this. A passphrase adds a layer of encryption to your private key file itself. If someone steals your laptop, they still can't use your key without that passphrase.
Step 2: Copying the Public Key to the Server
Once generated, your public key is located at ~/.ssh/id_ed25519.pub. You need to authorize this key on your remote server. The easiest way to do this is using ssh-copy-id:
Bashssh-copy-id user@your-server-ip
This command logs into your server once (using your password), copies the public key into the ~/.ssh/authorized_keys file, and sets the correct permissions automatically.
If ssh-copy-id isn't available, you can manually append your key:
- Copy the contents of your local
~/.ssh/id_ed25519.pub. - On the server, run
mkdir -p ~/.ssh && nano ~/.ssh/authorized_keys. - Paste the key on a new line and save.
- Run
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keysto ensure the security of these files.
Step 3: Streamlining Access with SSH Config
Typing ssh user@192.168.1.50 every time is tedious. Create a local config file to simplify your life.
- Open (or create)
~/.ssh/configon your local machine. - Add the following entry:
TEXTHost web-server HostName 192.168.1.50 User your_username IdentityFile ~/.ssh/id_ed25519
Now, you can simply run:
Bashssh web-server
Hands-on Exercise
- Generate an Ed25519 key pair on your local machine.
- Copy your public key to your project server.
- Verify you can log in without entering a password.
- Create an entry in your local
~/.ssh/configfile to connect to your server using a custom alias (e.g.,web-server).
Common Pitfalls
- Permissions: The SSH daemon is extremely strict. If the permissions on your
.sshdirectory orauthorized_keysfile are too "loose" (e.g., group-writable), the server will ignore your keys for security reasons. Always ensure they are700and600respectively. - Wrong Key: If you have multiple keys, SSH might try the wrong one. You can force a specific key using
ssh -i ~/.ssh/my-key user@host. - Lost Passphrase: If you lose the passphrase to your private key, there is no "forgot password" button. You will have to delete the key and generate a new one.
FAQ
| Question | Answer |
|---|---|
| Can I use the same key for every server? | Technically yes, but it's better to use unique keys for different environments. |
| Is it safe to share my public key? | Yes, the public key is designed to be shared. Never share the private key. |
What is the ~/.ssh/authorized_keys file? | It's a list of public keys that are permitted to log in as that specific user. |
Recap
We've successfully moved from vulnerable passwords to robust, cryptographic authentication. By generating keys, deploying them to our remote server, and simplifying the connection process with an SSH config file, we've hardened our infrastructure access. This is a foundational step for any DevOps workflow.
Up next: We'll move into system monitoring by learning about processes and how the Linux kernel manages what's running on your server.
Work with me

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

