Generating and using SSH keys on Linux
Step 1
Before generating a new SSH key pair, check if
ls ~/.ssh
If you see files like id_rsa
and id_rsa.pub
, you already have SSH keys. You can use these
Step
If you don’t have an SSH key pair or want to generate a new one, use the following command:
ssh-keygen -t rsa -b 2048
-t rsa
:-b 2048
: Specifies the number of bits in the key, with 2048 being a common size.
You will be prompted to provide a location to save the key pair~/.ssh/id_rsa
) or specify a different
You can also set a passphrase for added security
Step 3: Copy the Public Key to Remote Server
To use yourssh-copy-id
command can help with this:
ssh-copy-id username@remote_server
Replace username
with your username on the remote serverremote_server
with the server’s address.
Step 4:
After copying the public key, test the SSH connection:
ssh username@remote_server
If you set a passphrase, you’ll be prompted to enter it.
Additional Tips:
- Use SSH Agent:
- To avoid entering your passphrase every time, you can use the SSH agent. Start the agent:
eval "$(ssh-agent -s)"
Add your key:
ssh-add ~/.ssh/id_rsa
- To avoid entering your passphrase every time, you can use the SSH agent. Start the agent:
- Configuring SSH:
- You can configure SSH options globally or per-host in the
~/.ssh/config
file.
- You can configure SSH options globally or per-host in the
- Revoke Access:
- If your private key is
- Use Different Keys for Different Services:
- Consider using different SSH keys for different services or servers.
By following these steps, you