How to Generate a New SSH Key for Root User on CentOS 7.9

Managing SSH keys is crucial for maintaining secure access to your Linux server. If you’re logged in as the root user on CentOS 7.9 and need to generate a new SSH key, follow this straightforward guide.

Step-by-Step Guide

Prerequisites: Ensure you’re logged in as root via password.

1. Generate a New SSH Key ๐Ÿ”‘

To create a new SSH key pair, open your terminal and run:

ssh-keygen -t rsa -b 2048 -f /root/.ssh/id_rsa_2

This command generates a 2048-bit RSA key pair named id_rsa_2.

2. Add the Public Key to authorized_keys ๐Ÿ“‚

Append the new public key to the authorized_keys file:

cat /root/.ssh/id_rsa_2.pub >> /root/.ssh/authorized_keys

3. Set Correct Permissions ๐Ÿ”’

Ensure the correct permissions on your SSH directory and files:

chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys

4. Test Your SSH Connection ๐Ÿงช

If your SSH service is running on a non-standard port (e.g., 22022), use:

ssh -i /root/.ssh/id_rsa_2 -p 22 root@localhost

5. Restart the SSH Service ๐Ÿ”„

To apply changes, restart the SSH service:

systemctl restart sshd

Conclusion

With these steps, youโ€™ve securely set up a new SSH key for the root user on your CentOS system. Always ensure your keys are backed up and your permissions are correct. Happy secure computing! ๐ŸŒ๐Ÿ”

Scroll to Top