Secure Shell (SSH) keys provide a more secure and convenient way to access remote systems compared to traditional password-based authentication. In this guide, we will walk you through generating and setting up SSH keys on Ubuntu 24.04.
Step 1: Check for Existing SSH Keys
Before generating a new SSH key pair, check if you already have one by running:
ls -la ~/.ssh
If the directory contains files like id_ed25519
and id_ed25519.pub
, you already have a key pair. You can either use the existing keys or generate a new pair.
Step 2: Generate a New SSH Key Pair
To generate a new SSH key pair, use the following command:
ssh-keygen -t ed25519 -C "[email protected]"
-t ed25519
: Specifies the Ed25519 key type, which is more secure and faster than RSA.-C "[email protected]"
: Adds a comment to the key (optional but useful for identification).
Press Enter to accept the default location (~/.ssh/id_ed25519
) or specify a different location if needed.
If prompted, enter a passphrase for additional security (or leave it empty for no passphrase).
Step 3: Copy the Public Key to the Remote Server
You can use the ssh-copy-id
command to copy your public key to the remote server:
ssh-copy-id user@remote_host
Replace user
with your remote username and remote_host
with the server’s IP address or hostname.
If ssh-copy-id
is not available, manually copy the key using:
cat ~/.ssh/id_ed25519.pub | ssh user@remote_host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"
Ensure the correct permissions are set on the remote server:
ssh user@remote_host
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
Step 4: Test SSH Key Authentication
Once the key is copied, try logging into the remote server:
ssh user@remote_host
If the setup was successful, you should be able to log in without entering a password.
Step 5: Disable Password Authentication (Optional, for Extra Security)
To enhance security, disable password authentication by modifying the SSH configuration file on the remote server:
sudo nano /etc/ssh/sshd_config
Find and update the following lines:
PasswordAuthentication no
PubkeyAuthentication yes
Save the file and restart the SSH service:
sudo systemctl restart ssh
Conclusion
You have successfully set up SSH key authentication on Ubuntu 24.04. This method enhances security and streamlines access to remote servers. For even stronger security, consider using a passphrase with your key or setting up two-factor authentication (2FA) with SSH.
FAQ
1. What are SSH keys, and why should I use them?
SSH keys are cryptographic keys used for secure authentication when connecting to remote servers via SSH (Secure Shell). They are more secure than passwords and allow passwordless login.
2. How do I check if I already have SSH keys?
Run the following command in your terminal:
ls -l ~/.ssh/id_*
If you see id_rsa
(private key) and id_rsa.pub
(public key), you already have SSH keys. If not, you need to generate them.
3. How do I generate a new SSH key pair?
Use this command to create a new SSH key pair:
ssh-keygen -t ed25519 -C "[email protected]"
- Press Enter to save the key in the default location (
~/.ssh/id_ed25519
). - You can optionally set a passphrase for additional security.
4. How do I add my SSH key to the SSH agent?
Start the SSH agent and add your private key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
5. How do I copy my public SSH key to a remote server?
Use the following command, replacing user
and server_ip
:
ssh-copy-id user@server_ip
If ssh-copy-id
is unavailable, you can manually copy your key:
cat ~/.ssh/id_ed25519.pub
Then, add the output to ~/.ssh/authorized_keys
on the remote server.
6. How do I test SSH key authentication?
Try logging in to the remote server:
ssh user@server_ip
If it logs in without asking for a password, SSH keys are set up correctly.
7. How do I disable password authentication for SSH?
To enhance security, edit the SSH configuration file on the remote server:
sudo nano /etc/ssh/sshd_config<br>
Find and set:
PasswordAuthentication no
Then restart SSH:
sudo systemctl restart ssh
8. What if I get a “Permission denied (publickey)” error?
Check the following:
- Ensure your public key is correctly added to
~/.ssh/authorized_keys
on the remote server. - Set correct permissions:bashCopyEdit
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
- Restart the SSH service:bashCopyEdit
sudo systemctl restart ssh
9. How do I change my SSH key passphrase?
Run:
ssh-keygen -p -f ~/.ssh/id_ed25519
10. How do I delete my old SSH keys?
If you need to remove SSH keys, delete them with:
rm -f ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
- Roblox Force Trello - February 25, 2025
- 20 Best Unblocked Games in 2025 - February 25, 2025
- How to Use Java Records to Model Immutable Data - February 20, 2025