Connecting to a server via SSH using keys is a secure and convenient way to access your server without having to remember a password. Here’s a step-by-step guide on how to connect to a server via SSH using keys:
Prerequisites:
- You have a Unix-based operating system (such as Linux or macOS) or a tool that allows you to use SSH, such as PuTTY on Windows.
- You have an SSH client and permission to add a public key to the server account. Generate the key pair with OpenSSH’s
ssh-keygenor PuTTYgen. - You keep the private key securely on your local device and install the matching public key on the server. Never upload or share your private key.
Step 1: Generate a pair of SSH keys
If you haven’t already, generate a key pair with OpenSSH’s ssh-keygen or PuTTYgen. On macOS, Linux, or Windows with OpenSSH, a modern default is:
- Open Terminal or Git Bash.
- Run
ssh-keygen -t ed25519 -C "your_email@example.com". Use an RSA key if your server or provider requires it for compatibility. - Follow the prompts to create a new key pair.
- Save the private key at
~/.ssh/id_ed25519(or the location you choose). If that file already exists, choose a different filename rather than overwriting an existing key. - Save the matching public key as
~/.ssh/id_ed25519.pubor use the public-key text displayed by your key-generation tool.
Step 2: Copy the public key to your server
Copy the contents of the public key file (id_rsa.pub or id_ed25519.pub) and add it to the authorized_keys file on your server. You can do this using SFTP, SCP, or by pasting it directly into the file using SSH.
Step 3: Configure SSH client
To connect to your server using SSH, you’ll need to configure your SSH client. The process is as follows:
- On Linux/macOS, if
ssh-agentis running, add the key withssh-add ~/.ssh/id_ed25519. If you are not using an agent, connect directly withssh -i ~/.ssh/id_ed25519 username@hostname. Replace the example filename with your actual private-key path. - On Windows with PuTTY, generate or import the key using PuTTYgen, then copy the OpenSSH-format public key text into the server’s
authorized_keysfile. In PuTTY, open Connection > SSH > Auth > Credentials and select the private key file in PuTTY’s.ppkformat. Alternatively, load the.ppkinto Pageant for reuse during the session. Menu labels can differ slightly by PuTTY version.
Step 4: Connect to your server
Use your configured SSH client to connect to your server. The command would be:
ssh user@your-server-ip-or-hostname
Replace user with your username and your-server-ip-or-hostname with your server’s IP address or hostname.
Verify the server’s host key
On the first connection, SSH displays the server’s host-key fingerprint and asks whether to trust it. Compare that fingerprint with one supplied by your hosting provider or server administrator through a separate trusted channel before accepting it. SSH stores accepted host keys in known_hosts and warns if a previously recorded key changes. If you see a changed-key warning, stop and verify the new fingerprint; do not disable host-key checking or blindly remove the old entry.
Tips and Variations:
- On Linux/macOS, keep your private key readable only by your user (for example,
chmod 600 ~/.ssh/id_ed25519). On the server, use restrictive permissions such aschmod 700 ~/.sshandchmod 600 ~/.ssh/authorized_keys. SSH servers may reject keys if the home directory or.sshpath is writable by other users. - Use the hostname, username, and port supplied by your provider or administrator. For a non-default port, include
-p <port>, for examplessh -i ~/.ssh/id_ed25519 -p 2222 user@hostname; 2222 is only an example, not a universal SSH port. - If you’re connecting from a corporate network or behind a firewall, you may need to configure your firewall rules or ask your network administrator for assistance.
By following these steps, you can configure SSH key-based authentication, but the exact setup depends on the server, account permissions, and provider policy.
For provider-specific cPanel access, see our Manage Shell in cPanel guide. The feature, SSH port, and available commands depend on your hosting provider and plan.
For command and option details, consult the OpenSSH manual pages and the PuTTY public-key authentication guide.
Leave a Reply