Linux SSH Key Generation
Prerequisites​
Install OpenSSH if not already present:
sudo apt update
sudo apt install openssh-client
Creating SSH Directory​
- Create .ssh directory if it doesn't exist:
mkdir -p /home/$USER/.ssh
- Set correct directory permissions:
chmod 700 /home/$USER/.ssh
Generating SSH Keys​
- Generate the key pair:
# Using default location
ssh-keygen -t rsa -b 4096 -C "[email protected]"
# using custom name in .ssh directory
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/my-key
- Press Enter to save in default location (~/.ssh/id_ed25519)
- Enter a secure passphrase (recommended) or press Enter twice for no passphrase
- Start SSH agent:
eval "$(ssh-agent -s)"
- Add key to SSH agent:
ssh-add ~/.ssh/id_ed25519
- View your public key:
cat ~/.ssh/id_ed25519.pub
Key Location​
- Private key:
~/.ssh/id_ed25519
- Public key:
~/.ssh/id_ed25519.pub
Alternative Key Types​
For legacy systems, use RSA:
ssh-keygen -t ed25519 -C "[email protected]"
Key Permissions​
Set correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Direct Path Creation​
If you need to specify the exact path:
# Create directory
mkdir -p /home/$USER/.ssh
# Generate key with specific path
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f /home/$USER/.ssh/id_rsa
# Set permissions
chmod 700 /home/$USER/.ssh
chmod 600 /home/$USER/.ssh/id_rsa
chmod 644 /home/$USER/.ssh/id_rsa.pub