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:
- Open the terminal.
- Use the following command to connect to your VPS:
Replace username with your actual username (e.g., root) and your_server_ip with the IP address of your VPS.
On Windows using PuTTY:
- Open PuTTY.
- Enter the IP address of your VPS in the “Host Name (or IP address)” field.
- Ensure the port is set to 22 and the connection type is set to SSH.
- 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.
- Open the SSH configuration file:
- Find the line that says #Port 22 and change it to a different port, e.g., Port 2222. Remove the # to uncomment it.
- 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.
- In the same sshd_config file, find the line PermitRootLogin yes and change it to:
- 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:
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:
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:
5. Testing Your Configuration
Step 1: Test SSH Key Login
- Disconnect from the server by typing exit.
- Reconnect using your SSH key:
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:
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.