AWS Builder Center
How SSH Keys Work in Linux šŸ”

How SSH Keys Work in Linux šŸ”

Learn how SSH public and private keys work in Linux, generate key pairs using ssh-keygen, and securely connect to remote servers and AWS EC2 instances using SSH key-based authentication.

How SSH Keys Work in Linux

1. What Are SSH Keys?

  • SSH (Secure Shell) allows secure access to remote Linux servers.
  • SSH key authentication uses a public and private key pair.
  • It is commonly used to access AWS EC2 instances and manage servers.

2. Private Key

  • Stored securely on your local machine.
  • Never share it with anyone.
  • Example: ~/.ssh/id_ed25519

3. Public Key

  • Shared with the remote server.
  • Stored in ~/.ssh/authorized_keys on the server.
  • Example: ~/.ssh/id_ed25519.pub

4. Generate SSH Keys

Run the following command:
1
ssh-keygen -t ed25519
  • Generates a public and private key pair.
  • You can set a passphrase for additional protection.

5. Copy the Public Key to the Server

1
ssh-copy-id username@server-ip
  • Installs your public key on the remote server.
  • Requires an existing authentication method and suitable permissions.

6. Connect to the Server

1
ssh username@server-ip
For AWS EC2:
1
ssh -i my-key.pem ubuntu@<EC2-PUBLIC-IP>

7. How SSH Authentication Works

  • The client connects to the SSH server.
  • The server checks whether the public key is authorized.
  • The client proves possession of the matching private key.
  • If verification succeeds, the server authenticates the user.
  • SSH establishes an encrypted session.

8. Security Best Practices

  • Never share your private key.
  • Use a passphrase to protect your private key.
  • Set appropriate file permissions.
  • Allow SSH access only from trusted IP addresses.
  • Verify server host keys before trusting a connection.

Conclusion

SSH keys provide secure authentication for Linux servers without sending your private key over the network. They are essential for Linux administration, AWS EC2 access, and DevOps workflows.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article