Creating an SSH Key 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, follow this guide to generate a new SSH key.

Step-by-Step Guide

Prerequisites

  • Ensure youโ€™re logged in as root via password.

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

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

Add the Public Key to authorized_keys ๐Ÿ“‚

Append the new public key to the authorized_keys file:

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

Set Correct Permissions ๐Ÿ”’

Ensure the correct permissions on your SSH directory and files:

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

Test Your SSH Connection ๐Ÿงช

If your SSH service is running on a non-standard port, use:

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

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