Linux Connect Remote Server using SSH


Using SSH to connect to remote servers in Linux is straightforward and involves running the ssh command with the server’s IP address or hostname. Let’s go through the process with example commands and expected outputs.

Step 1: Basic SSH Connection

To connect to a remote server, you’ll need the server’s IP address or hostname and a user account with SSH access. The general syntax for connecting to a server is:

ssh username@server_ip

Example:

ssh john@192.168.1.100

Example Output:

The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established. ECDSA key fingerprint is SHA256:Vp+Kd8jrzyPhq2dV8B4i0vNlRTYplQJ4Sod5gRePv1U. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.1.100' (ECDSA) to the list of known hosts. john@192.168.1.100's password: Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-42-generic x86_64) Last login: Sun Nov 9 08:34:07 2024 from 192.168.1.10 john@remote-server:~$
  • If it’s the first time connecting to the server, you’ll see a security prompt asking if you trust the host. Type yes to continue.
  • You’ll then be prompted to enter the password for the specified user (john in this case). After entering the password, you’ll have access to the server’s command line.

Step 2: Connecting to a Server on a Different Port

By default, SSH runs on port 22. If the server uses a different port, specify it with the -p flag.

Command:

ssh -p 2222 john@192.168.1.100

Example Output:

The authenticity of host '[192.168.1.100]:2222' can't be established. RSA key fingerprint is SHA256:2I4L7F+7yAn/mr6JUb9gskQ/c4Uoei0X7F0xDl5qSDg. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.1.100]:2222' (RSA) to the list of known hosts. john@192.168.1.100's password: Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-42-generic x86_64) Last login: Sun Nov 9 08:40:15 2024 from 192.168.1.10 john@remote-server:~$

Here, the SSH connection is made on port 2222.

Step 3: Using SSH Key Authentication

If SSH key-based authentication is set up, you won’t need to enter a password each time. The SSH command will use your key by default if it’s located in ~/.ssh/id_rsa.

Command:

ssh john@192.168.1.100

Example Output:

Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-42-generic x86_64) Last login: Sun Nov 9 08:34:07 2024 from 192.168.1.10 john@remote-server:~$

Since SSH key authentication is used, the server grants access without asking for a password.

Step 4: SSH with a Different Key File

If you’re using a non-default key file, specify it with the -i option:

Command:

ssh -i ~/.ssh/other_key john@192.168.1.100

Example Output:

Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-42-generic x86_64) Last login: Sun Nov 9 08:45:13 2024 from 192.168.1.10 john@remote-server:~$

Here, ~/.ssh/other_key is used for authentication.

Step 5: Running a Command on the Remote Server

You can execute a single command on the remote server without starting an interactive session. This is useful for automation or quick checks.

Command:

ssh john@192.168.1.100 'uptime'

Example Output:

08:50:01 up 15 days, 3:21, 1 user, load average: 0.15, 0.10, 0.05

The uptime command runs on the remote server, and the output is displayed directly on your local machine.

Step 6: Transferring Files with SCP

You can use scp (Secure Copy Protocol) to copy files between your local machine and the remote server. Here’s an example of copying a file from the local machine to the server:

Command:

scp /path/to/local/file john@192.168.1.100:/path/to/remote/destination

Example Output:

file 100% 1234 1.2KB/s 00:01

This output shows the transfer progress and confirms the file was successfully copied.


These examples demonstrate basic SSH usage in Linux for remote connections and commands. SSH offers flexible, secure access to remote servers, making it essential for system administration and file management across networked devices.