Linux ssh Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Secure Shell (SSH) command to connect to remote Linux servers and transfer files between local and remote hosts. The lab covers the basics of the SSH protocol, including how to establish a secure connection, execute commands on the remote server, and transfer files using the SCP (Secure Copy) command. The content is suitable for those interested in networking and communication skills in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") linux/RemoteAccessandNetworkingGroup -.-> linux/scp("`Secure Copying`") subgraph Lab Skills linux/exit -.-> lab-422931{{"`Linux ssh Command with Practical Examples`"}} linux/ls -.-> lab-422931{{"`Linux ssh Command with Practical Examples`"}} linux/cp -.-> lab-422931{{"`Linux ssh Command with Practical Examples`"}} linux/ssh -.-> lab-422931{{"`Linux ssh Command with Practical Examples`"}} linux/scp -.-> lab-422931{{"`Linux ssh Command with Practical Examples`"}} end

Introduction to SSH (Secure Shell)

In this step, you will learn about the Secure Shell (SSH) protocol, which is a widely used method for securely connecting to remote Linux servers. SSH provides encrypted communication between your local machine and the remote server, ensuring the privacy and integrity of your data.

First, let's check 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 output shows that the SSH server is running and active on the Ubuntu container.

Next, let's try connecting to the remote server using the SSH command:

ssh labex@localhost

When prompted, enter the password for the labex user. If the connection is successful, you should see the remote server's command prompt.

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

Connecting to a Remote Linux Server via SSH

In this step, you will learn how to connect to a remote Linux server using the SSH command.

First, let's verify that you can connect to the remote server using the ssh command:

ssh labex@localhost

When prompted, enter the password for the labex user. If the connection is successful, you should see the remote server's command prompt.

Now, let's explore some additional SSH connection options:

## 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 exit the SSH session, type exit and press Enter.

Transferring Files Between Local and Remote Hosts Using SCP

In this step, you will learn how to use the Secure Copy (SCP) command to transfer files between your local machine and the remote Linux server.

First, let's create a test file on the local machine:

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

Now, let's copy the file from the local machine to the remote server:

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

When prompted, enter the password for the labex user. The file should now be transferred to the remote server.

To copy a file from the remote server to the local machine, use the following command:

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

Again, enter the password for the labex user when prompted. The file should now be copied to the local machine.

You can also use SCP to copy entire directories between the local and remote hosts:

## 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 is used to recursively copy the directory and its contents.

Summary

In this lab, you learned about the Secure Shell (SSH) protocol, which is a widely used method for securely connecting to remote Linux servers. You verified the SSH server status on the Ubuntu 22.04 Docker container and successfully connected to the remote server using the SSH command. Additionally, you explored various SSH connection options, such as executing commands remotely, opening an interactive shell, and connecting to the server using a specific port.

Furthermore, you learned how to transfer files between the local and remote hosts using the Secure Copy (SCP) command. This allows you to securely copy files and directories between your local machine and the remote server, ensuring the privacy and integrity of your data.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like