📒 

Secure Shell (SSH) is a cryptographic network protocol that allows secure remote access to a server. It is widely used for managing Virtual Private Servers (VPS). This guide will walk you through the steps to connect to a VPS using SSH and configure it for secure access.

1. Prerequisites

Before you start, ensure you have the following:

  • VPS Hosting: A VPS with an operating system installed (Linux distributions are common).
  • SSH Client: An SSH client installed on your local machine (Linux and macOS have built-in SSH clients; Windows users can use PuTTY or Windows Terminal).
  • Server IP Address: The IP address of your VPS.
  • Username: Typically, the default username is root or another user with sudo privileges.

2. Connecting to Your VPS via SSH

Step 1: Open the Terminal or SSH Client

On Linux or macOS:

  1. Open the terminal.
  2. Use the following command to connect to your VPS:
ssh username@your_server_ip

Replace username with your actual username (e.g., root) and your_server_ip with the IP address of your VPS.

On Windows using PuTTY:

  1. Open PuTTY.
  2. Enter the IP address of your VPS in the “Host Name (or IP address)” field.
  3. Ensure the port is set to 22 and the connection type is set to SSH.
  4. Click Open to connect.

Step 2: Enter Password

When prompted, enter your password. If this is your first time connecting, you may see a message about the server’s authenticity. Type yes to continue.

3. Configuring SSH for Secure Access

To enhance the security of your SSH connection, consider the following configurations:

Step 1: Change the Default SSH Port

Changing the default SSH port (22) to a non-standard port can reduce the risk of automated attacks.

  1. Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
  1. Find the line that says #Port 22 and change it to a different port, e.g., Port 2222. Remove the # to uncomment it.
  2. Save and exit (CTRL + X, then Y, then Enter).

Step 2: Disable Root Login

For better security, it is advisable to disable root login via SSH.

  1. In the same sshd_config file, find the line PermitRootLogin yes and change it to:
PermitRootLogin no
  1. Save and exit the file.

Step 3: Set Up SSH Key Authentication

Using SSH keys for authentication is more secure than using passwords.

Step 1: Generate SSH Key Pair

On your local machine, run the following command to generate an SSH key pair:

ssh-keygen -t rsa -b 4096 -C “your_email@example.com”

Follow the prompts to save the key (default is ~/.ssh/id_rsa) and set a passphrase for added security.

Step 2: Copy the Public Key to Your VPS

Use the following command to copy your public key to your VPS:

ssh-copy-id username@your_server_ip

This command will prompt you for your password and copy the public key to the ~/.ssh/authorized_keys file on your server.

4. Restarting SSH Service

After making changes to the SSH configuration, restart the SSH service for the changes to take effect:

sudo systemctl restart sshd

5. Testing Your Configuration

Step 1: Test SSH Key Login

  1. Disconnect from the server by typing exit.
  2. Reconnect using your SSH key:
ssh username@your_server_ip -p 2222 # Use the new port if you changed it

You should be able to log in without entering your password if you set up SSH key authentication correctly.

Step 2: Ensure Root Login is Disabled

Try to log in as root:

ssh root@your_server_ip

You should receive a message indicating that root login is not permitted.

6. Conclusion

Connecting and configuring SSH on your VPS is an essential step in managing your server securely. By following this guide, you can establish a secure SSH connection and enhance its security through various configurations. Regularly monitor your server’s security and keep your software up to date to maintain a safe and efficient environment.