How to Access Virtual Private Server (VPS) via SSH key?
To access a Virtual Private Server (VPS) via SSH key, you need to follow these steps:
1. Generate SSH Key Pair: If you haven't already, generate an SSH key pair on your local machine. You can do this using the
`ssh-keygen`
command. It will create a public key (`id_rsa.pub`)
and a private key (`id_rsa`)
in the `~/.ssh/`
directory by default.
Bash:
ssh-keygen -t rsa -b 4096
2. Copy Public Key to the VPS: Once you have your SSH key pair generated, you need to copy the public key to the VPS. You can do this manually by copying the contents of the `id_rsa.pub` file and appending it to the
`~/.ssh/authorized_keys`
file on the server. Or, you can use the `ssh-copy-id`
command:
Bash:
ssh-copy-id username@your_vps_ip
Replace
`username`
with your username on the VPS and `your_vps_ip`
with the IP address of your VPS.3. SSH Login with Key Pair: After copying the public key, you should be able to SSH into your VPS without being prompted for a password:
Bash:
ssh username@your_vps_ip
If you set a passphrase for your SSH key pair during generation, you'll be prompted to enter it. Otherwise, you'll be logged in directly.
4. Secure SSH Configuration (Optional): For added security, you might want to configure your SSH server to disallow password authentication and only allow SSH key authentication. You can do this by editing the SSH configuration file
(`/etc/ssh/sshd_config`)
on your VPS and setting:
Bash:
PasswordAuthentication no
After changing this setting, remember to restart the SSH service:
Bash:
sudo systemctl restart ssh
By following these steps, you should be able to securely access your VPS using SSH key authentication.
Last edited: