Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 7. SSH Remote Connection

SCP (Secure Copy Protocol)

SCP (Secure Copy Protocol)

Secure File Transfer with SCP Protocol

SSH can be used not only for remote login but also for file transfer in a secure way. There are two protocols for transferring files with SSH – SCP (Secure Copy Protocol) and SFTP (Secure File Transfer Protocol). In this section, we'll explain SCP.

Transferring from local to remote

To transfer a file from the local computer to the remote server, the following five sets of information are needed for the SCP command.

Key file
  • Private key file path on your local computer (usually, under the .ssh directory)
Source file
  • Source file path on your local computer
Destination
  • User name of the remote server
  • IP address of the remote server
  • Destination directory path on the remote server. If you want to transfer the file to the user's home directory of the remote server, you can skip this argument.

For a better understanding, please go through the following practice section.

Practice 1

Objective:
Transfer a file from the local computer to the remote server

The following are the paths of the file to be transferred and the destination directory.

  • File to be transferred: ~/dir_ch7_local/sample.txt (on the local computer)
  • Destination directory: ~/dir_ch7_remote (on the remote server)

Note: For this practice, open two terminals (command lines). One for a local operation and another one for a remote operation.

1. Create a directory on the remote server

Create a directory for receiving a file.

For this operation, use the terminal connected to the remote server.

Command Line - INPUT (Remote)
cd ~
mkdir dir_ch7_remote

2. Create a sample file on the local computer

Create a sample file for transferring to the remote server.

For this operation, use the local terminal that is not connected to the remote server.

Command Line - INPUT (Local)
cd ~
mkdir dir_ch7_local
cd dir_ch7_local
touch sample.txt

3. Transfer the file

Run the scp command with the private key path, source file path, and destination directory path.

Command Line - INPUT (Local)
scp -i ~/.ssh/id_rsa sample.txt ubuntu@18.143.143.190:dir_ch7_remote
Command Line - RESPONSE
sample.txt                                    100%    0     0.0KB/s   00:00 

If you set a passphrase for the private key, you need to enter the passphrase to move forward.

4. Check if the file is transferred properly.

Using the terminal connected to the remote server, check if the file is successfully transferred.

Command Line - INPUT (Remote)
ls dir_ch7_remote

You can see that the file is successfully transferred.

Command Line - RESPONSE
sample.txt

IdeaTips: Transfer a directory

When you want to transfer a directory with files underneath, use the -r option. The following is the example command.

Command Line - INPUT

scp -i ~/.ssh/id_rsa -r sample_dir ubuntu@18.143.143.190:dir_ch7_remote

Transferring from remote to local

To transfer a file from the remote server to the local computer, the following five sets of information are needed for the SCP command.

Key file
  • Private key file path on your local computer (usually under the .ssh directory)
Source file
  • User name of the remote server
  • IP address of the remote server
  • Source file path on the remote server
Destination
  • Destination directory path on your local computer. If you want to transfer the file to your home directory on your computer, you can skip this argument.

For a better understanding, please go through the following practice section.

Practice 2

Objective:
Transfer a directory from the remote server to the local computer

The following are the paths of the directory to be transferred and the destination directory.

  • Directory to be transferred: ~/dir_ch7_remote (on the remote server)
  • Destination directory: ~/dir_ch7_local (on the local computer)

Note: This practice can be done only with the local terminal.

1. Transfer the directory

Run the scp command with the private key path, source directory path, and destination directory path.

Command Line - INPUT (Local)
cd ~
scp -i ~/.ssh/id_rsa -r ubuntu@18.143.143.190:dir_ch7_remote dir_ch7_local
Command Line - RESPONSE
sample.txt                                    100%    0     0.0KB/s   00:00 

If you set a passphrase for the private key, you need to enter the passphrase to move forward.

2. Check if the directory is transferred properly.

Check if the directory is successfully transferred from the remote server to the local computer.

Command Line - INPUT (Local)
ls dir_ch7_local

You can see that the file is successfully transferred.

Command Line - RESPONSE
dir_ch7_remote		sample.txt

Typical Mistakes

There are two typical mistakes you might make when you run the scp command.

1. Forgetting to add : (colon)

When you forget to add : (colon), the command will have a different meaning. The command will create a file copy on your local computer. For example, if you run the following command (without a colon at the end), you'll create a file named ubuntu@18.143.143.190 in the same directory.

Command Line - INPUT
scp -i ~/.ssh/id_rsa sample.txt ubuntu@18.143.143.190
Command Line - RESPONSE
ubuntu@18.143.143.190

2. Misusing spacing

Omitting or adding an extra space is also a common mistake. Check spaces when you encounter an error message in your command line.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Installing Django: A Simple Guide

Install Django

Resetting Changes with git reset

Undo Changes – git reset

Undoing and Redoing Actions in Vim Editor

Normal Mode (4) – Undo and Redo

How to Manage Conflicts in Git

Managing Conflict

Configuring Git User Settings

Git User Settings – git config

Installing Django: A Simple Guide

Install Django

Resetting Changes with git reset

Undo Changes – git reset

Undoing and Redoing Actions in Vim Editor

Normal Mode (4) – Undo and Redo

How to Manage Conflicts in Git

Managing Conflict

Configuring Git User Settings

Git User Settings – git config

Tags:

SSH

Remote Connection

SCP

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server