How to Generate SSH Key on CentOS
In this article
Generating an SSH key pair on CentOS gives you a secure, passwordless way to log in to remote servers and manage Git repositories. This guide walks you through creating an Ed25519 or RSA key, copying the public key to a server, and setting up key-based authentication. You'll need terminal access to your CentOS machine and, for the final steps, the password for the remote account you want to connect to.
What You'll Need
- A CentOS machine (7, 8, 9, or Stream) with a terminal window open.
- For copying the key: the IP address or hostname of the remote server, and the username and password for that server.
- Optional but recommended: a passphrase for your private key (explained below).
Step-by-Step
- Open a terminal. If you're on the desktop, press
Ctrl+Alt+Tor search for "Terminal" in the applications menu. If you're on a headless server, you're already in the shell. - Check if you already have an SSH key. Run
ls -la ~/.ssh. If you see files likeid_ed25519andid_ed25519.pub(orid_rsaandid_rsa.pub), you already have a key. You can skip to step 5. If the directory doesn't exist or is empty, continue. - Generate the key. Run the following command to create an Ed25519 key (recommended, faster and more secure than RSA):
ssh-keygen -t ed25519 -C "your_email@example.com"
If your system or the remote server doesn't support Ed25519, use RSA instead:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
The-Cflag adds a comment, usually your email, to help identify the key. - Choose a location and passphrase. When prompted "Enter file in which to save the key", press
Enterto accept the default (/home/yourname/.ssh/id_ed25519). Then you'll be asked for a passphrase. This is an extra password that protects your private key. You can pressEnterto set no passphrase, but a passphrase is strongly recommended. If you set one, you'll need to enter it every time you use the key (unless you add it to an SSH agent). - Copy the public key to the remote server. The easiest way is with
ssh-copy-id. Run:ssh-copy-id username@remote_host
Replaceusernamewith your remote account andremote_hostwith the server's IP or hostname. You'll be prompted for the remote user's password. Once done, the public key is added to the server's~/.ssh/authorized_keysfile. - Test the login. Try connecting:
ssh username@remote_host. If you set a passphrase, you'll be asked for it, but not the remote password. If you didn't set a passphrase, you should be logged in directly. If it works, you're done. - If ssh-copy-id isn't available (some minimal installs don't have it), manually copy the key. First, view your public key with
cat ~/.ssh/id_ed25519.pub(orid_rsa.pub). Copy the entire output. Then, on the remote server, runmkdir -p ~/.ssh && chmod 700 ~/.ssh, thenecho "paste_your_key_here" >> ~/.ssh/authorized_keys, and finallychmod 600 ~/.ssh/authorized_keys. Log out and test as in step 6.
Common Mistakes
- Using the wrong key type: Some older servers don't support Ed25519. If you get an error like "no mutual signature algorithm", generate an RSA key with
ssh-keygen -t rsa -b 4096. - Copying the private key instead of the public key: The
.pubfile is the public key. Never share or copy the private key (the one without extension) to a server. - Incorrect file permissions: The remote server will reject keys if
~/.sshorauthorized_keyshave loose permissions. Ensure~/.sshis 700 andauthorized_keysis 600. - Forgetting the passphrase: If you set a passphrase and forget it, you can't recover it. You'd have to generate a new key and replace the old one on all servers.
- Using sudo with ssh-keygen: Running
sudo ssh-keygencreates a key for root, not your user. Always run it as your normal user.
Tips
- Use a passphrase and an SSH agent. If you find entering a passphrase annoying, start an SSH agent with
eval "$(ssh-agent)"and add your key withssh-add ~/.ssh/id_ed25519. You'll enter the passphrase once, and the agent will handle it for the session. - Keep your private key safe. Never share it, and consider backing it up (encrypted) if you rely on it for many servers.
- Use different keys for different purposes. For example, one key for work, another for personal servers. This makes it easier to revoke access if one key is compromised.
- Check your SSH config. If you have multiple keys, you can define which key to use for which host in
~/.ssh/configwith theIdentityFiledirective.
Troubleshooting
"Permission denied (publickey)" when you try to connect. This usually means the server doesn't have your public key. Check that you copied the correct key and that the remote authorized_keys file contains it. Also verify permissions on the remote: ls -la ~/.ssh should show drwx------ for the directory and -rw------- for the file.
"No supported authentication methods available" — the server might not accept the key type. Try generating an RSA key as described above. If you're using an older server, it might also require you to enable public key authentication in /etc/ssh/sshd_config (set PubkeyAuthentication yes) and restart sshd.
ssh-copy-id fails with "No such file or directory" — this usually means the ~/.ssh directory doesn't exist on the remote. Create it manually as shown in step 7, then try again.
You get prompted for a password even after copying the key — check that the remote server's SSH configuration allows public key authentication. Also, ensure you're connecting as the correct user; the key is added to a specific user's authorized_keys.
Passphrase prompt appears every time — this is normal unless you use an SSH agent. Start the agent and add your key as described in the Tips section.
Frequently Asked Questions
What's the difference between Ed25519 and RSA?
Ed25519 is a modern elliptic-curve algorithm that's faster and more secure than RSA for the same key size. RSA is more widely supported, especially on older systems. For new keys, Ed25519 is recommended unless you need compatibility.
Do I need a passphrase?
Not strictly, but it's strongly recommended. A passphrase protects your private key if it's stolen. Without one, anyone who gets the file can use it to log in to your servers.
How do I change my passphrase later?
Run ssh-keygen -p -f ~/.ssh/id_ed25519. You'll be prompted for the old passphrase and then the new one.
Can I use the same key on multiple servers?
Yes. You can copy the same public key to as many servers as you want. Just be mindful that if the private key is compromised, all those servers are at risk.
How do I remove a key from a server?
Edit the ~/.ssh/authorized_keys file on the remote server and delete the line containing the public key. You can also use ssh-keygen -R hostname to remove a key from your known_hosts file, but that's different — it's for host keys, not user keys.
What is the known_hosts file?
It's a file on your local machine that stores the fingerprints of remote servers you've connected to. It helps prevent man-in-the-middle attacks. If a server's key changes, SSH will warn you.