How to Use SSH Login to Connect to a Remote Server?

Secure Shell (SSH) is a protocol to access and manage remote systems securely. It encrypts the communication between a client and a server, protecting data from unauthorized access and enhancing system security.

SSH provides a secure and efficient way for developers, system administrators, and IT technicians to remotely access computers without being physically present. This improves productivity and system management. Additionally, SSH is simple to use and supports various tools and clients. Beyond remote access, it can also be used for file transfers, executing commands, and even tunneling connections.

This guide will walk you through how to use ssh login command to connect remote servers using SSH on Windows, Linux, and macOS.

How SSH Works? (How to SSH into Server)

SSH operates by establishing a secure connection between a client and an SSH server (sshd).

In the previous section, the ssh command acted as the client, while the remote system ran the SSH server (sshd).

Most Linux systems automatically start the SSH server upon boot. However, if it is not running, you may need to access your server through a web-based or local console to start it manually.

Start the SSH Server on Ubuntu

If the SSH server is not running, you can start it with the following command:

$ sudo systemctl start ssh

Once the server is running, you can remotely log in using SSH.

$ sudo systemctl status ssh

How to Use SSH in Windows?

If you're using Windows 10 or later, SSH is already built into the system, so setting it up is simple.

Enabling SSH on Windows 10

  1. Open Settings and navigate to Apps > Optional Features.
  2. Check if OpenSSH Client is installed. If not, click Add a feature, search for OpenSSH Client, and install it.

Connecting to a Remote Server

Once SSH is enabled, you can connect to a remote system using PowerShell or Command Prompt:

ssh username@server_ip

Replace username with your actual username and server_ip with the remote server’s IP address.

Using PuTTY for Older Windows Versions

If you're using Windows 8 or an earlier version that doesn’t include SSH by default, you can use PuTTY, a free SSH client with a graphical interface:

  1. Download and install PuTTY from the official Putty page.
  2. Open the program and enter the server’s IP address.
  3. Set the port to 22 (default SSH port).
  4. how to use ssh login to connect to a remote server?
  5. Click Open to establish the connection.

PuTTY is also a great option if you prefer a graphical interface over command-line tools.

How to Use SSH Login in Linux?

Most Linux distributions come with SSH pre-installed. To verify if SSH is available on your system, open the terminal and run the following command:

$ssh

Installing SSH on Linux

If SSH is not installed, you can add it using the package manager. For Debian-based distributions like Ubuntu, use:

$sudo apt-get install openssh-client

Connecting to a Remote Server

Once SSH is installed, you can establish a secure connection to a remote server by running:

$ssh username@server_ip

In the above command, you need to replace the username with your actual username and server_ip with the remote server’s IP address.

How to Use SSH on Mac?

Follow these steps to establish an SSH connection on macOS:

Click on the Terminal icon in the Dock to launch it.

Initiate an SSH Connection: Use the SSH command in the following format:

ssh username@hostname

For example, if the username test and the device is Centos.local, the command would be:

ssh test@Centos.local

Confirm the Connection: If connecting for the first time, type Yes when prompted to verify the connection.

Enter Your Password: Input the password when prompted to authenticate the session.

Exit the SSH Session: To end the session, either press CTRL+D or type:

exit

Closing the session prevents unintentional commands from being sent and enhances security.

Common SSH Commands and Features

After establishing an SSH connection to a remote server, you can perform various tasks such as tunneling, file transfers, and executing remote commands.

1. Secure Port Forwarding with SSH

SSH tunneling allows the secure routing of traffic between your local and remote systems. To set up local port forwarding, use:

ssh -L 8080:localhost:3306 user@192.168.1.110

In this example, port 8080 on your local machine forwards traffic to port 3306 on the remote server at 192.168.1.110.

2. Secure File Transfer and Management

SSH also supports encrypted file transfers using SCP and SFTP.

  • Transferring a file from local to remote using SCP:

scp report.pdf user@192.168.1.110:/home/user/documents/

This command securely copies report.pdf from the local system to the /home/user/documents/ directory on the remote server.

  • Using SFTP to manage remote files:

sftp user@192.168.1.110

Once connected, you can navigate inside directories to upload and download files interactively.

3. Simplify SSH Remote Access with a Config File

To streamline connections, you can configure an SSH file (~/.ssh/config) to store frequently used connection settings.

Example configuration:

Host devserver  

    HostName 192.168.1.110  

    User user  

    Port 2222  

Now, instead of typing the full command, you can connect with:

$ ssh devserver

4. Running Commands on a Remote Server

To execute a single command remotely without logging in interactively, use:

$ ssh user@192.168.1.110 "df -h"

This command retrieves disk usage information (df -h) from the remote server.

How to Login into SSH Using Keys?

Although password authentication allows access to remote systems, using SSH keys is a more efficient and secure method.

How Does Key-Based Authentication Work?

SSH key authentication operates using a pair of cryptographic keys:

  • Private Key: This is Stored securely on the client’s device and should never be shared.
  • Public Key: This can be freely shared or placed on any server you wish to access.

When a connection attempt is made, the remote server uses the public key to send an encrypted challenge message to the client. The client’s private key decrypts this message and sends a response, verifying its identity. This exchange happens automatically once SSH keys are configured.

Generating SSH Keys

To set up SSH key authentication, generate the key pair on your local machine by running:

$ ssh-keygen -t rsa

During the setup, you may be asked to set a passphrase for the key files, but it’s optional. Press Enter to accept the default settings. The keys will be saved in the ~/.ssh/ directory with these filenames:

Public key: ~/.ssh/id_rsa.pub

Private key: ~/.ssh/id_rsa

Verifying SSH Key Files

Navigate to the .ssh directory:

cd ~/.ssh

Check the permissions and contents of the generated keys using:

ls -l

This command displays details about the files, ensuring they are correctly created.

How to Disable Password Authentication for SSH?

Once SSH key authentication is set up, you can further enhance security by disabling password-based login. This ensures that only users with the correct private key can access the server.

Important Note: Before proceeding, make sure your public key is properly installed on the server. Otherwise, you may lose access.

Change SSH Configuration to Disable Password Login

Open the SSH configuration file as a user with root or sudo privileges:

$ sudo nano /etc/ssh/sshd_config

1. Find the line that says PasswordAuthentication, remove the # at the beginning (if present), and change its value to "no":

PasswordAuthentication no

2. Ensure that public key authentication is enabled and challenge-response authentication is disabled. These settings should be present in the file:
PubkeyAuthentication yes

ChallengeResponseAuthentication no

Save and exit the file.

To activate the new settings, restart the SSH service with:

$ sudo systemctl reload ssh


In this way, password authentication will be disabled, and access will only be possible through SSH key authentication.

Best Practices to Improve Security for SSH Connections 

To secure your SSH connections, follow these best practices:

1. Use SSH Keys or Strong Passwords: SSH keys provide better security than passwords. If using a password, ensure it is long, unique, and complex with a mix of letters, numbers, and symbols.

2. Disable Root Login: Prevent unauthorized access by disabling direct root login in the SSH configuration file (/etc/ssh/sshd_config) by setting:

PermitRootLogin no

3. Change the Default SSH Port: Modify the default port (22) to a custom one to reduce the risk of automated attacks. Update the "Port 22" line in sshd_config and restart SSH for the changes to take effect.

4. Use Security Tools: Install tools like Fail2Ban to detect and block repeated failed login attempts, preventing brute-force attacks:
$ sudo apt-get install fail2ban

Implementing these measures will strengthen your SSH security and protect against unauthorized access.

Conclusion

Mastering SSH is a valuable skill for anyone working with cloud computing. You learned from this guide how to connect remote server using SSH. As you explore its features, you'll uncover advanced capabilities that simplify tasks. Its strong security, efficiency, and versatility have made SSH a widely used tool across various applications.

Deploy your KVM VPS instantly across 20 global locations with BlueVPS! Enjoy seamless remote access, full control, and 100% dedicated resources with KVM virtualization. Choose from Linux or Windows-based VPS with all major control panels, backed by 24/7 expert support. Get started now for a powerful and reliable hosting experience!

 

Blog