If you’re a software developer, you’ll likely need to access a remote server using SSH (Secure Shell) at some point in your career. SSH is a network protocol that allows you to securely access and manage a remote computer over an unsecured network. You’ll need to add your SSH public key to the remote server to do so. In this article, we’ll show you how to add your SSH public key to a server step by step.
What is an SSH Public Key?
An SSH public key is a cryptographic key used to authenticate you as a user when you try to access a remote server via SSH. It’s created using a pair of keys: a public key and a private key. The private key is kept on your local computer and should never be shared, while the public key can be shared with others, such as a remote server you need to access.
Generating an SSH Key Pair
Before we can add our SSH public key to a server, we need to generate our key pair. To do so, we can use the ssh-keygen
command, which is included with most modern operating systems.
1. Open your terminal or command prompt and enter the following command:
ssh-keygen
2. You’ll be prompted to enter a file name to save the key pair. The default is id_rsa
in the ~/.ssh
directory, which is fine for most purposes. If you want to use a different file name or directory, you can specify it here.
3. You’ll then be prompted to enter a passphrase. This optional security feature adds an extra layer of protection to your private key. If you choose to set a passphrase, you must enter it every time you use your private key. If you don’t want to set a passphrase, press enter.
4. Your key pair will be generated and saved in the specified file location. You’ll see two files: id_rsa
(your private key) and id_rsa.pub
(your public key).
Adding Your SSH Public Key to a Server
Now that we have our SSH public key, we can add it to the server we want to access. The exact steps will vary depending on the server’s operating system and configuration, but the general process is the same.
1. Log in to the remote server via SSH using your username and password.
2. Once you’re logged in, create a .ssh
directory in your home directory if it doesn’t already exist:
mkdir -p ~/.ssh
3. Create a new file called authorized_keys
in the .ssh
directory:
touch ~/.ssh/authorized_keys
4. Open the authorized_keys
file in a text editor and paste into your SSH public key. You can do this manually by copying the contents of the id_rsa.pub
file on your local computer and pasting it into the authorized_keys
file on the server. Alternatively, you can use the ssh-copy-id
command to copy your public key to the server automatically:
ssh-copy-id username@server
5. Once your public key is in the authorized_keys
file on the server, you can test it by logging out of the server and then trying to log back in again using SSH. This time, you should be able to log in without being prompted for a password, since your public key will be used to authenticate you.
Troubleshooting SSH Key Issues
If you run into issues when trying to add your SSH public key to a server, there are a few things you can try to troubleshoot the problem.
Ensure Correct File Permissions
First, make sure that the permissions on your SSH files are set correctly. Your private key (id_rsa
) should have permission 600
, meaning only you can read and write to it. Your public key (id_rsa.pub
) and the authorized_keys
file on the server should have permission 644
, meaning anyone can read them but only you can write to them. You can check and set permissions using the chmod
command:
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
Check SSH Configuration
If you’re still having trouble, check your SSH configuration on both your local computer and the server. On your local computer, check the ~/.ssh/config
file and make sure it’s configured correctly for the server you’re trying to access. On the server, check the /etc/ssh/sshd_config
file and make sure it allows public key authentication. You may need to restart the SSH service on the server after making changes to the configuration file:
sudo service ssh restart
Check Firewall Settings
Finally, check the firewall settings on both your local computer and the server. If the server is behind a firewall, make sure that port 22 (the default SSH port) is open and accessible. If you’re still having trouble, try temporarily disabling the firewall on both your local computer and the server to see if that resolves the issue.
Conclusion
Adding your SSH public key to a server is a necessary step if you want to access the server securely using SSH. By following the steps outlined in this article, you should be able to add your public key to a server and start accessing it without being prompted for a password. Remember to keep your private key safe and secure, and to troubleshoot any issues that arise by checking file permissions, SSH configuration, and firewall settings. With these steps, you’ll be able to use SSH to access and manage remote servers with ease.
📕 Related articles about Linux
- How to Connect to SSH Server: A Comprehensive Guide for Software Developers
- How to Create an SSH SOCKS Proxy for Secure and Private Browsing
- How to Allow Public Access to SSH Tunnel
- How to open ISO image without burning to disc in Linux
- Most Common Utilities to Create Archives in Linux
- How to show hidden files and folders in Linux