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! ๐๐