How to Add Ssh Rsa Key in Linux

How to Add Ssh Rsa Key in Linux

If you want to know how to add ssh rsa key in linux, the process is straightforward: generate an RSA key pair with ssh-keygen, copy the public key to the remote Linux server, make sure the .ssh permissions are correct, and test the connection. Once it is set up properly, you can log in with SSH key authentication instead of typing a password every time.

What an SSH RSA key does

What an SSH RSA key does

An SSH RSA key is part of public key authentication in OpenSSH. You keep the private key on your Linux machine and place the public key on the server you want to access. When you connect, SSH checks whether those two keys match. If they do, the server lets you in without a password.

This is useful for:

  • Logging into remote Linux servers more securely
  • Automating admin tasks with SSH
  • Avoiding repeated password entry
  • Using tools that rely on key-based access, such as scripts and deployment jobs

RSA is still supported on many systems, but some environments now prefer Ed25519 keys for new setups. If you specifically need RSA, the steps below will show you how to use it correctly.

Generate the RSA key pair

The first step is to create the SSH key pair on the Linux machine you will use to connect.

  1. Open a terminal.

  2. Run this command:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

  3. When prompted for a file location, press Enter to accept the default path, usually ~/.ssh/id_rsa.

  4. Choose a passphrase if you want extra protection for the private key. You can leave it empty, but a passphrase is safer.

The command creates two files:

  • ~/.ssh/id_rsa, the private key
  • ~/.ssh/id_rsa.pub, the public key

Keep the private key secret. Never copy it to a server.

Copy the public key to the remote Linux server

Copy the public key to the remote Linux server

After the key pair is created, the public key must be added to the remote user account on the server you want to access.

Option 1: Use ssh-copy-id

If your system has ssh-copy-id, this is the easiest method:

ssh-copy-id user@server_ip

If your key is not stored in the default location, specify it explicitly:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@server_ip

This command copies your public key into the server’s ~/.ssh/authorized_keys file for that user.

Option 2: Add the key manually

Option 2 Add the key manually

If ssh-copy-id is unavailable, you can install the key by hand.

  1. Display your public key on the local machine:

    cat ~/.ssh/id_rsa.pub

  2. Log in to the remote server with your password:

    ssh user@server_ip

  3. On the server, create the SSH directory if needed:

    mkdir -p ~/.ssh

  4. Open or create the authorized keys file:

    nano ~/.ssh/authorized_keys

  5. Paste the entire public key on a single line, save the file, and exit.

Make sure the pasted key is not wrapped across multiple lines. SSH expects each public key to stay on one line in authorized_keys.

Fix SSH permissions

Incorrect permissions are one of the most common reasons SSH key login fails. The server is strict about ownership and file modes.

PathRecommended permissionPurpose
~/.ssh700Only the user can read, write, or enter the directory
~/.ssh/authorized_keys600Only the user can read and write the key file
Home directoryNot world-writableSSH may reject keys if the home path is too open

To set the recommended permissions on the remote server, run:

chmod 700 ~/.ssh

chmod 600 ~/.ssh/authorized_keys

chown -R $USER:$USER ~/.ssh

If you are setting up keys for another account, run the commands as that user or adjust ownership carefully with sudo.

Test the SSH connection

Once the public key is installed and permissions are correct, test the login from your local Linux machine:

ssh user@server_ip

If everything is configured correctly, you should connect without entering the account password. If you set a passphrase for the private key, you may be asked for that passphrase instead.

If you want to confirm which key SSH is offering, you can use verbose mode:

ssh -v user@server_ip

This is helpful when troubleshooting because it shows whether the client is using the correct private key.

Common SSH key problems

Even when the commands are correct, a few common issues can prevent login.

Permission denied publickey

This usually means the server did not accept the key. Check that the public key is in the right user account’s ~/.ssh/authorized_keys file and that the permissions are correct. Also confirm that you are connecting as the same user whose key was installed.

Wrong file permissions

If .ssh or authorized_keys is too open, SSH may ignore the key. Use chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys on the server.

Key file not found

If your key was saved with a custom name or location, specify it when connecting:

ssh -i ~/.ssh/custom_key user@server_ip

Also check that the public key you copied matches the private key you are using locally.

SSH server settings

On some servers, key authentication may be disabled in the SSH daemon configuration. If you administer the server, check /etc/ssh/sshd_config for settings related to public key authentication, then reload or restart the SSH service if changes are made.

RSA key size and compatibility notes

RSA key size and compatibility notes

For RSA keys, -b 4096 is a common choice because it provides stronger security than older, smaller RSA keys. Some older systems may only support certain key types or older SSH behavior, so RSA can still be useful for compatibility. If you are starting fresh and your environment supports it, Ed25519 is often preferred, but RSA remains widely used and supported.

Quick checklist

  • Create the key pair with ssh-keygen
  • Keep the private key on your local machine only
  • Copy the public key to the server’s authorized_keys file
  • Set 700 on ~/.ssh
  • Set 600 on authorized_keys
  • Test SSH login with the target user account

Frequently Asked Questions About how to add ssh rsa key in linux

How do I add SSH RSA key in Linux?

Generate an RSA key pair with ssh-keygen, then copy the public key to the server’s ~/.ssh/authorized_keys file. After that, verify the file permissions and test the SSH connection.

What is the difference between the public key and private key?

The public key goes on the server, while the private key stays on your local Linux machine. Anyone who has the private key can authenticate as you, so it must be protected.

Where do I put my SSH public key on the server?

Place it in the target user’s ~/.ssh/authorized_keys file, with one key per line.

What permissions should ~/.ssh and authorized_keys have?

Use 700 for ~/.ssh and 600 for ~/.ssh/authorized_keys. Incorrect permissions are a common reason key login fails.

Why does SSH still ask for a password after I add the key?

Usually the key is in the wrong account, the permissions are too open, the wrong private key is being used, or the SSH server is not accepting public key authentication.

Can I use a passphrase with an SSH RSA key?

Yes. A passphrase adds an extra layer of protection to your private key. You will enter the passphrase when the key is used, unless you load it into an SSH agent.

Conclusion

To add an SSH RSA key in Linux, generate the key pair, install the public key on the remote server, correct the file permissions, and test the connection. The key point is to keep the private key local and make sure the server recognizes the matching public key. Once set up correctly, SSH login becomes faster and more secure than password-based access.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *