Linux: Setting Up SSH for Remote Access

Secure Shell (SSH) is a fundamental protocol for securely accessing and managing remote servers and services. For Linux engineers, configuring SSH correctly is crucial for maintaining system security and enabling efficient remote administration. This guide walks you through setting up SSH for remote access, focusing on key-based authentication, particularly for interacting with services like GitHub.

Why Use SSH Keys?

While password authentication is common, SSH key pairs offer a more secure and convenient method. A key pair consists of a private key (kept secret on your local machine) and a public key (shared with the remote server or service). This method eliminates the risk of brute-force password attacks and often allows for password-less logins.

Prerequisites

  • A Linux operating system.
  • Basic command-line knowledge.
  • A GitHub account (for the specific examples).

Step-by-Step Guide to Setting Up SSH for GitHub

This section demonstrates how to configure SSH key authentication specifically for GitHub, a common use case for developers and engineers.

1. Check for Existing SSH Keys

First, check if you already have SSH keys generated. By default, they are stored in ~/.ssh/.

ls -al ~/.ssh

Look for files named id_rsa, id_ecdsa, id_ed25519 (private keys) and id_rsa.pub, id_ecdsa.pub, id_ed25519.pub (public keys). If you don’t see any, or want to create a new key, proceed to the next step.

2. Generate a New SSH Key

We’ll generate a new ED25519 key, which is recommended for its security and performance.

ssh-keygen -t ed25519 -C "your_email@example.com"
  • -t ed25519: Specifies the key type.
  • -C "your_email@example.com": Adds a comment (usually your email) to the key.

Output Example:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/username/.ssh/id_ed25519):

Press Enter to accept the default location (/home/username/.ssh/id_ed25519).

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Enter a secure passphrase (or leave empty for no passphrase, though less secure) . This passphrase protects your private key.

Example Output:

Your identification has been saved in /home/username/.ssh/id_ed25519
Your public key has been saved in /home/username/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
|    ...          |
|   . . .         |
|  . . o          |
| . o o .         |
|. o + o S        |
|o. = o o .       |
|o.+ + . o        |
|E= + . o         |
|B+o .            |
+----[SHA256]-----+

3. Add Your SSH Key to the SSH Agent

The SSH agent helps manage your keys and can remember your passphrase, so you don’t have to enter it every time .

Start the SSH agent:

eval "$(ssh-agent -s)"

Output Example:

Agent pid 12345

Add your generated private key to the agent:

ssh-add ~/.ssh/id_ed25519

If your key uses a different name or location, adjust the path accordingly. If you set a passphrase, you’ll be prompted to enter it now.

4. Add the Public Key to Your GitHub Account

To authenticate with GitHub using your SSH key, you need to add the public key (id_ed25519.pub) to your GitHub account .

Display the contents of your public key:

cat ~/.ssh/id_ed25519.pub

Output Example:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOnAbCdefGhIjKlMnOpQrStUvWxYz1234567890 your_email@example.com

Copy the entire output, including the ssh-ed25519 part and the comment at the end.

  • Go to your GitHub account settings.
  • Navigate to the “SSH and GPG keys” section .
  • Click the “New SSH key” or “Add SSH key” button .
  • Provide a descriptive title (e.g., “My Laptop”).
  • Paste the copied public key into the “Key” field .
  • Click “Add SSH key”.

5. Test Your SSH Connection

Test the connection to GitHub to ensure everything is set up correctly .

ssh -T git@github.com

The first time you connect, you might see a message like:

Output Example:

The authenticity of host 'github.com (140.82.112.4)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Type yes and press Enter.

If you set a passphrase for your key, you might be prompted to enter it . After successful authentication, you should see:

Output Example:

Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.

Congratulations! Your SSH key is now configured for GitHub.

Advanced Configuration: Using SSH Config File

For managing multiple SSH connections or simplifying commands, you can use the SSH config file located at ~/.ssh/config.

Example entry for GitHub:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

This configuration allows you to simply run ssh github.com or ensures Git operations use the correct key.

Conclusion

Setting up SSH key authentication is a critical step for any Linux engineer working with remote systems or services like GitHub. It enhances security by replacing potentially weak passwords with strong cryptographic keys and improves convenience through agent-based authentication. By following the steps outlined above, you can securely configure SSH access and streamline your workflow.


SSH Linux remote access SSH keys GitHub authentication security ssh-agent ed25519 public key private key Git CLI terminal passphrase