ssh Command in Linux

Introduction

This lab provides a practical guide on using the Secure Shell (SSH) command for connecting to remote Linux servers and securely transferring files. You'll explore the fundamental aspects of the SSH protocol, encompassing establishing secure connections, executing commands remotely, and utilizing the SCP (Secure Copy) command for file transfers. This content is ideal for individuals seeking to enhance their networking and communication skills within a Linux environment, particularly system administrators and developers.

Introduction to SSH (Secure Shell)

In this section, we'll delve into the Secure Shell (SSH) protocol, a cornerstone technology for secure remote access to Linux servers. SSH employs encryption to establish a secure communication channel between your local machine and the remote server, safeguarding the confidentiality and integrity of your sensitive data during transmission.

Let's begin by checking the SSH server status on the Ubuntu 22.04 Docker container:

sudo systemctl status ssh

Example output:

● ssh.service - OpenSSH server daemon
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-21 06:53:22 UTC; 1min 30s ago
   Main PID: 1026 (sshd)
      Tasks: 1 (limit: 1071)
     Memory: 3.0M
        CPU: 17ms
     CGroup: /system.slice/ssh.service
             └─1026 /usr/sbin/sshd -D

The above output confirms that the SSH server is actively running on the Ubuntu container, ready to accept secure connections.

Next, let's attempt to connect to the remote server using the SSH command:

ssh labex@localhost

When prompted, enter the password associated with the labex user. A successful connection will present you with the remote server's command prompt, indicating a secure SSH session.

To terminate the SSH session and return to your local machine, simply type exit and press Enter.

Connecting to a Remote Linux Server via SSH

This section focuses on the practical steps of connecting to a remote Linux server using the SSH command, a fundamental skill for any systemadmin or Linux user.

First, let's ensure that you can establish a connection to the remote server using the ssh command:

ssh labex@localhost

Upon being prompted, provide the password for the labex user. Upon successful authentication, you'll gain access to the remote server's command prompt.

Now, let's explore some of the valuable SSH connection options that extend its functionality:

## Connect to the remote server and execute a command
ssh labex@localhost ls -l

## Connect to the remote server and open an interactive shell
ssh -t labex@localhost bash

## Connect to the remote server using a specific SSH port (default is 22)
ssh -p 2222 labex@localhost

Example output:

total 12
drwxr-xr-x 2 labex labex 4096 Apr 21 07:00 project
-rw-r--r-- 1 labex labex    0 Apr 21 07:00 test.txt

To disconnect from the SSH session, type exit and press Enter.

Transferring Files Between Local and Remote Hosts Using SCP

This section illustrates how to leverage the Secure Copy (SCP) command to securely transfer files between your local machine and the remote Linux server, a common task for system administrators managing remote systems.

First, let's create a sample file on your local machine:

touch ~/project/test_file.txt
echo "This is a test file." > ~/project/test_file.txt

Now, let's securely copy this file from your local machine to the remote server using SCP:

scp ~/project/test_file.txt labex@localhost:~/project/

Enter the password for the labex user when prompted. This action will securely transfer the file to the specified directory on the remote server.

To transfer a file from the remote server back to your local machine, use the following command:

scp labex@localhost:~/project/test_file.txt ~/project/

Again, you'll be prompted for the labex user's password. After authentication, the file will be copied to the designated location on your local machine.

SCP also allows for the copying of entire directories between local and remote systems:

## Copy a directory from local to remote
scp -r ~/project labex@localhost:~/

## Copy a directory from remote to local
scp -r labex@localhost:~/project ~/

The -r option instructs SCP to recursively copy the directory and all its contained files and subdirectories, ensuring a complete transfer.

Summary

In this lab, you have gained a comprehensive understanding of the Secure Shell (SSH) protocol, a fundamental technology for establishing secure connections to remote Linux servers. You have learned how to verify the SSH server status on an Ubuntu 22.04 Docker container and successfully connect to a remote server using the SSH command. Furthermore, you have explored a variety of SSH connection options, including remote command execution, interactive shell access, and connecting to a specific port.

Additionally, you have mastered the Secure Copy (SCP) command for securely transferring files between local and remote hosts. This enables you to confidently copy files and directories between your local machine and remote servers, maintaining the confidentiality and integrity of your data throughout the process. These are essential skills for any systemadmin working with Linux systems.

400+ Linux Commands